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 - Adding ActionBar Navigation Tabs

Note: if you are develop App < 3.0 Android OS then use  ActionBarSherlock   support library. ActionBarSherlock is an extension of the  support library  designed to facilitate the use of the action bar design pattern across all versions of Android with a single API. Create new Android Project : in Main Activity package com.AlarmManager; import android.os.Bundle; import android.view.View; import com.actionbarsherlock.app.ActionBar; import com.actionbarsherlock.app.ActionBar.Tab; import com.actionbarsherlock.app.SherlockFragmentActivity; public class AlarmManagerActivity extends SherlockFragmentActivity { public static String ACTIVE_TAB = "activeTab"; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { setTheme(R.style.Theme_Sherlock_Light_DarkActionBar); super.onCreate(savedInstanceState); // setContentView(R.lay

Android Material Design (with Design support Library) - 4

Introducing  Design Support Library  , to use Material design component in older version API - android 2.1 and above.  You’ll find a navigation drawer view, floating labels for editing text, a floating action button, snackbar, tabs, and a motion and scroll framework to tie them together. I have used my previous example, so its easy for demonstrate.  Note: Update your Android SDK support  repositories, support library if not updated i  used compileSdkVersion 'android-MNC' for Android M but you can change it to build in older API add  dependencies in build.gradle file compile 'com.android.support:appcompat-v7:22.2.0' compile 'com.android.support:design:22.2.0' compile 'com.android.support:support-v4:22.2.0' start with  navigation drawer , its very easy to use lets, design for drawer <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://

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