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

Popular posts from this blog

Android - Google MAP V2 PART 1

Download Full Code  of PART-1 , 2 , 3 , 4 Download and configure Google Play Services Library in Eclipse Google Map for Android is now integrated with Google Play Services. So we need to set up Google Play Service Library for developing Google Map application in Android. Get the API key for Google Maps v2: Linux  :  For any other OS use this :  keytool -list -v -alias androiddebugkey \ -keystore <path_to_debug_keystore>debug.keystore \ -storepass android -keypass android Register with the Google APIs Console :    Google APIs Console  Select here the  Services  entry: Activate the  Google Maps Android API v2 . Register your application via its package in this console together with the SHA-1 fingerprint of your signature key. Click on Create new Android Key.. Add you SHA-1 and your package name in to box like example given in alert box. Support library...

Share Text To Facebook & Twitter ANDROID

Shaadi.com Indian Matrimonials Before Create this app you must Have to create Application on FACEBOOK & TWITTER Download Code

Android - Google MAP V2 PART 2

Download Full Code  of  PART-1 , 2 , 3 , 4 Before do this check part 1 Note : I have to run this in API 8 to API 17 that why i have used  ActionBarSherlock  support library. if you don't want that then replace it.  In main Activity i have added following code. package com.djandroid.mapsv2; import android.os.Bundle; import android.support.v4.app.FragmentManager; import com.actionbarsherlock.app.SherlockFragmentActivity; import com.google.android.gms.maps.GoogleMap; import com.google.android.gms.maps.GoogleMap.OnMapClickListener; import com.google.android.gms.maps.SupportMapFragment; import com.google.android.gms.maps.model.LatLng; import com.google.android.gms.maps.model.MarkerOptions; public class MainActivity extends SherlockFragmentActivity { private GoogleMap MAP; @Override protected void onCreate(Bundle arg0) { // TODO Auto-generated method stub setTheme(R.style....