ANDROID - Using Fragments

12:59:00 Dhaval Sodha Parmar 2 Comments

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>  

2 comments:

  1. hi..Im college student, thanks for sharing :)

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

    ReplyDelete