Skip to main content

ANDROID - Using Fragments

I have used android-support-v4.jar because i have to run this in my Android 2.2.1 Device






Main Activity:

 package com.example.freagmenttest;  
 import android.os.Bundle;  
 import android.support.v4.app.FragmentActivity;  
 import android.view.Menu;  
 public class MainActivity extends FragmentActivity {  
      @Override  
      public void onCreate(Bundle savedInstanceState) {  
           super.onCreate(savedInstanceState);  
           setContentView(R.layout.activity_main);  
      }  
      @Override  
      public boolean onCreateOptionsMenu(Menu menu) {  
           getMenuInflater().inflate(R.menu.activity_main, menu);  
           return true;  
      }  
 }  

activity_main.xml

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:orientation="horizontal"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent">  
   <fragment android:name="com.example.freagmenttest.ItemClass"  
        android:id="@+id/headlines_fragment"  
        android:layout_weight="1"  
        android:layout_width="0dp"  
        android:layout_height="match_parent" />  
   <fragment android:name="com.example.freagmenttest.DetailsClass"  
        android:id="@+id/article_fragment"  
        android:layout_weight="2"  
        android:layout_width="0dp"  
        android:layout_height="match_parent" />  
 </LinearLayout>  

ItemClass:

 package com.example.freagmenttest;  
 import java.util.ArrayList;  
 import android.os.Bundle;  
 import android.support.v4.app.ListFragment;  
 import android.view.View;  
 import android.widget.ArrayAdapter;  
 import android.widget.ListView;  
 public class ItemClass extends ListFragment {  
      ArrayList<String> items = new ArrayList<String>();  
      DetailsClass frag;  
      int position = 0;  
      @Override  
      public void onCreate(Bundle savedInstanceState) {  
           // TODO Auto-generated method stub  
           super.onCreate(savedInstanceState);  
           fillarray();  
           setListAdapter(new ArrayAdapter<String>(getActivity(),  
                     android.R.layout.simple_list_item_1, items));  
      }  
      @Override  
      public void onActivityCreated(Bundle savedInstanceState) {  
           // TODO Auto-generated method stub  
           getListView().requestFocusFromTouch();  
           getListView().setSelection(position);  
           getListView().performItemClick(  
                     getListView().getAdapter().getView(position, null, null),  
                     position, position);  
           super.onActivityCreated(savedInstanceState);  
      }  
      @Override  
      public void onListItemClick(ListView l, View v, int position, long id) {  
           // TODO Auto-generated method stub  
           frag = (DetailsClass) getFragmentManager().findFragmentById(  
                     R.id.article_fragment);  
           if (frag != null && frag.isInLayout()) {  
                frag.setText(items.get(position), position);  
           }  
           super.onListItemClick(l, v, position, id);  
      }  
      private void fillarray() {  
           // TODO Auto-generated method stub  
           items.clear();  
           items.add("Android alpha");  
           items.add("Android beta");  
           items.add("1.5 Cupcake (API level 3)");  
           items.add("1.6 Donut (API level 4)");  
           items.add("2.0 Eclair (API level 5)");  
           items.add("2.0.1 Eclair (API level 6)");  
           items.add("2.1 Eclair (API level 7)");  
           items.add("2.2–2.2.3 Froyo (API level 8)");  
           items.add("2.3–2.3.2 Gingerbread (API level 9)");  
           items.add("2.3.3–2.3.7 Gingerbread (API level 10)");  
           items.add("3.0 Honeycomb (API level 11)");  
           items.add("3.1 Honeycomb (API level 12)");  
           items.add("3.2 Honeycomb (API level 13)");  
           items.add("4.0–4.0.2 Ice Cream Sandwich (API level 14)");  
           items.add("4.0.3–4.0.4 Ice Cream Sandwich (API level 15)");  
           items.add("4.1 Jelly Bean (API level 16)");  
           items.add("4.2 Jelly Bean (API level 17)");  
           items.add("5.0 Key Lime Pie (API level 18)");  
      }  
      public interface OnTutSelectedListener {  
           public void onTutSelected(String str);  
      }  
 }  

DetailsClass:

 package com.example.freagmenttest;  
 import android.os.Bundle;  
 import android.support.v4.app.Fragment;  
 import android.view.LayoutInflater;  
 import android.view.View;  
 import android.view.ViewGroup;  
 import android.widget.ImageView;  
 import android.widget.TextView;  
 public class DetailsClass extends Fragment {  
      @Override  
      public View onCreateView(LayoutInflater inflater, ViewGroup container,  
                Bundle savedInstanceState) {  
           View view = inflater.inflate(R.layout.detail, container, false);  
           return view;  
      }  
      public void setText(String item, int pos) {  
           TextView view = (TextView) getView().findViewById(R.id.textView1);  
           view.setText(item);  
           ImageView image = (ImageView) getView().findViewById(R.id.imageView1);  
           switch (pos) {  
           case 0:  
                image.setImageResource(R.drawable.android_alpha);  
                break;  
           case 1:  
                image.setImageResource(R.drawable.android_beta);  
                break;  
           case 2:  
                image.setImageResource(R.drawable.cupcake);  
                break;  
           case 3:  
                image.setImageResource(R.drawable.android_donut);  
                break;  
           case 4 & 5 & 6:  
                image.setImageResource(R.drawable.android_eclair);  
                break;  
           case 5:  
                image.setImageResource(R.drawable.android_eclair);  
                break;  
           case 6:  
                image.setImageResource(R.drawable.android_eclair);  
                break;  
           case 7:  
                image.setImageResource(R.drawable.android_froyo);  
                break;  
           case 8:  
                image.setImageResource(R.drawable.android_gingerbread);  
                break;  
           case 9:  
                image.setImageResource(R.drawable.android_gingerbread);  
                break;  
           case 10:  
                image.setImageResource(R.drawable.android_honeycomb);  
                break;  
           case 11:  
                image.setImageResource(R.drawable.android_honeycomb);  
                break;  
           case 12:  
                image.setImageResource(R.drawable.android_honeycomb);  
                break;  
           case 13:  
                image.setImageResource(R.drawable.android_ice_cream_sandwich);  
                break;  
           case 14:  
                image.setImageResource(R.drawable.android_ice_cream_sandwich);  
                break;  
           case 15:  
                image.setImageResource(R.drawable.jb_new_logo);  
                break;  
           case 16:  
                image.setImageResource(R.drawable.jb_new_logo);  
                break;  
           case 17:  
                image.setImageResource(R.drawable.android_kai);  
                break;  
           default:  
                break;  
           }  
      }  
 }  

detail.xml:

 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
   android:layout_width="fill_parent"  
   android:layout_height="fill_parent"  
   android:orientation="vertical" >  
   <TextView  
     android:id="@+id/textView1"  
     android:layout_width="match_parent"  
     android:layout_height="wrap_content"  
     android:text="Large Text"  
     android:textAppearance="?android:attr/textAppearanceLarge" />  
   <ImageView  
     android:id="@+id/imageView1"  
     android:layout_width="match_parent"  
     android:layout_height="match_parent"  
     android:src="@drawable/ic_launcher" />  
 </LinearLayout>  

Manifst File:

 <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
   package="com.example.freagmenttest"  
   android:versionCode="1"  
   android:versionName="1.0" >  
   <uses-sdk  
     android:minSdkVersion="8"  
     android:targetSdkVersion="15" />  
   <application  
     android:icon="@drawable/ic_launcher"  
     android:label="@string/app_name"  
     android:theme="@style/AppTheme" >  
     <activity  
       android:name=".MainActivity"  
       android:label="@string/title_activity_main" >  
       <intent-filter>  
         <action android:name="android.intent.action.MAIN" />  
         <category android:name="android.intent.category.LAUNCHER" />  
       </intent-filter>  
     </activity>  
   </application>  
 </manifest>  

Comments

  1. i want to add another textview below imageview for that where i have to make changes in the code? plz help me

    ReplyDelete

Post a Comment

Popular posts from this blog

Android - Google MAP V2 PART 3 (add Polyline)

Download Full Code  of  PART-1 , 2 , 3 , 4 Before go ahead Please look in to PART-1 & PART-2 I have just modify Main Activity class: package com.djandroid.mapsv2; import android.graphics.Color; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.OnMapClickListener; import com.google.android.gms.maps.GoogleMap.OnMarkerClickListener; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.Marker; import com.google.android.gms.maps.model.MarkerOptions; import com.google.android.gms.maps.model.Polyline; import com.google.android.gms.maps.model.PolylineOptions; public class MainActivity extends FragmentActivity { private GoogleMap MAP; private boolean markClic...

Android show Data from Sqlite DB into Grid View

Shaadi.com Matrimonials Shaadi.com Indian Matrimonials Your Main Activity class package com . Sqlite_grid_view ; import java . util . ArrayList ; import java . util . List ; import android . app . Activity ; import android . os . Bundle ; import android . util . Log ; import android . view . View ; import android . widget . AdapterView ; import android . widget . AdapterView . OnItemClickListener ; import android . widget . ArrayAdapter ; import android . widget . GridView ; import android . widget . TextView ; import android . widget . Toast ; public class AndroidSQLiteTutorialActivity extends Activity { private GridView gridView ; public static ArrayList < String > ArrayofName = new ArrayList < String >(); /** Called when the activity is first created. */ @ Override public void onCreate ( Bundle savedInstanceState ) { super . onCreate ( savedInstanceState ); setContentView ( R . l...

Connecting mysql Database in ANDROID using PHP & JSON

 To implement this tutorial you should have basic knowledge of how to run PHP script and start server.  If we talk about client-server architecture, client is Android device and in server side there is a combination of PHP Script and MySQL. In short, PHP Script sits in middle as shown in image. Lets suppose that we have a MySQL database named Employee, and a table int created, with the following SQL: CREATE TABLE `employee` (   `emp_id` int(11) NOT NULL auto_increment,   `emp_name` varchar(100) NOT NULL,   PRIMARY KEY  (`emp_id`) ) ENGINE=InnoDB  DEFAULT CHARSET=latin1 AUTO_INCREMENT=5 ; The PHP code will be very simple: Now Create Android Project :  The Android part is only a bit more complicated: -use a HttpPost to get the data -convert response to string -parse JSON data in to List In Your First Activity : O/P :