Creating Navigation tabs Example

NavigationTabExample 

MainActivity.java

package com.madhu;

import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.FragmentActivity;
import android.widget.TabHost;

public class MainActivity extends FragmentActivity {
TabHost tHost;



    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
     
        tHost = (TabHost) findViewById(android.R.id.tabhost);
        tHost.setup();
     
        /** Defining Tab Change Listener event. This is invoked when tab is changed */
        TabHost.OnTabChangeListener tabChangeListener = new TabHost.OnTabChangeListener() {

@Override
public void onTabChanged(String tabId) {
android.support.v4.app.FragmentManager fm =   getSupportFragmentManager();
AndroidFragment androidFragment = (AndroidFragment) fm.findFragmentByTag("android");
AppleFragment appleFragment = (AppleFragment) fm.findFragmentByTag("apple");
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();

/** Detaches the androidfragment if exists */
if(androidFragment!=null)
ft.detach(androidFragment);

/** Detaches the applefragment if exists */
if(appleFragment!=null)
ft.detach(appleFragment);

/** If current tab is android */
if(tabId.equalsIgnoreCase("android")){

if(androidFragment==null){
/** Create AndroidFragment and adding to fragmenttransaction */
ft.add(R.id.realtabcontent,new AndroidFragment(), "android");
}else{
/** Bring to the front, if already exists in the fragmenttransaction */
ft.attach(androidFragment);
}

}else{ /** If current tab is apple */
if(appleFragment==null){
/** Create AppleFragment and adding to fragmenttransaction */
ft.add(R.id.realtabcontent,new AppleFragment(), "apple");
}else{
/** Bring to the front, if already exists in the fragmenttransaction */
ft.attach(appleFragment);
}
}
ft.commit();
}
};


/** Setting tabchangelistener for the tab */
tHost.setOnTabChangedListener(tabChangeListener);
             
/** Defining tab builder for Andriod tab */
        TabHost.TabSpec tSpecAndroid = tHost.newTabSpec("android");
        tSpecAndroid.setIndicator("Android",getResources().getDrawable(R.drawable.android));      
        tSpecAndroid.setContent(new DummyTabContent(getBaseContext()));
        tHost.addTab(tSpecAndroid);
     
     
        /** Defining tab builder for Apple tab */
        TabHost.TabSpec tSpecApple = tHost.newTabSpec("apple");
        tSpecApple.setIndicator("Apple",getResources().getDrawable(R.drawable.apple));      
        tSpecApple.setContent(new DummyTabContent(getBaseContext()));
        tHost.addTab(tSpecApple);
     
     }
 
}

DummyTabContent.java


package com.madhu;

import android.content.Context;
import android.view.View;
import android.widget.TabHost.TabContentFactory;

public class DummyTabContent implements TabContentFactory{
private Context mContext;

public DummyTabContent(Context context){
mContext = context;
}


@Override
public View createTabContent(String tag) {
View v = new View(mContext);
return v;
}


}

AppleFragment.java

packagecom.madhu;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AppleFragment extends ListFragment{
    /** An array of items to display in ArrayList */
String apple_versions[] = new String[]{
             "Mountain Lion",
             "Lion",
             "Snow Leopard",
             "Leopard",
             "Tiger",
             "Panther",
             "Jaguar",
             "Puma"
     };


   
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/** Creating array adapter to set data in listview */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, apple_versions);

        /** Setting the array adapter to the listview */
        setListAdapter(adapter);

return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
    public void onStart() {
            super.onStart();

            /** Setting the multiselect choice mode for the listview */
            getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
}

AndroidFragment .java

packagecom.madhu;

import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class AndroidFragment extends ListFragment{
    /** An array of items to display in ArrayList */
    String android_versions[] = new String[]{
            "Jelly Bean",
            "IceCream Sandwich",
            "HoneyComb",
            "Ginger Bread",
            "Froyo"
    };

   
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/** Creating array adapter to set data in listview */
        ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity().getBaseContext(), android.R.layout.simple_list_item_multiple_choice, android_versions);

        /** Setting the array adapter to the listview */
        setListAdapter(adapter);

return super.onCreateView(inflater, container, savedInstanceState);
}
@Override
    public void onStart() {
            super.onStart();

            /** Setting the multiselect choice mode for the listview */
            getListView().setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
    }
}

AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.madhu"
    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="@android:style/Theme.Black" >
        <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>

res->drawable->android.xml


<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item
        android:state_selected="true" 
        android:state_pressed="false"        
        android:drawable="@drawable/android_selected" />     
        
    <item 
        android:drawable="@drawable/android_unselected" /> 
</selector>

res->drawable->apple.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">    
    <item
        android:state_selected="true" 
        android:state_pressed="false"        
        android:drawable="@drawable/apple_selected" />     
        
    <item 
        android:drawable="@drawable/apple_unselected" /> 
</selector>

drawable-hdpi







drawable_ldpi






drawable_mdpi







drawable_xhdpi:



ic_launcher-web.png


res-->layout
activity_main.xml

<TabHost
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
          android:layout_width="match_parent"
        android:layout_height="match_parent">

        
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="0"/>
        

        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="0dp"
            android:layout_height="0dp"
            android:layout_weight="0"/>
        

        <FrameLayout
            android:id="@+id/realtabcontent"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"/>            
       

    </LinearLayout>
</TabHost>

menu
activity_main.xml

<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/menu_settings"
        android:title="@string/menu_settings"
        android:orderInCategory="100"
        android:showAsAction="never" />
</menu>



Values

strings.xml

<resources>

    <string name="app_name">NavigationTabDemo</string>
    <string name="hello_world">Hello world!</string>
    <string name="menu_settings">Settings</string>
    <string name="title_activity_main">MainActivity</string>
    
    <string name="str_tv_android_tab">Android Tab Content</string>
    <string name="str_tv_apple_tab">Apple Tab Content</string>

</resources>

styles.xml

<resources>

    <style name="AppTheme" parent="android:Theme.Light" />

</resources>

values-v11

styles.xml

<resources>

    <style name="AppTheme" parent="android:Theme.Holo.Light" />

</resources>

values-v15

styles.xml

<resources>

    <style name="AppTheme" parent="android:Theme.Holo.Light.DarkActionBar" />

</resources>

Download  android-support-v4.jar .Keep it in libraries.




Comments

Popular posts from this blog

Android Objective type Question and Answers

Android Questions and Answers for written exams