Frage

i am trying to use tab Widget in android for one of my app. there are two tab in this tab widget. i have two background image like follows. when user click on any tab the image should be change. how can it is possible in my situation? the following is my code in xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/relative_my_student_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#FFFFFF" 
    android:padding="-4dp"
    >

    <RelativeLayout
        android:id="@+id/relative_top"
        android:layout_width="fill_parent"
        android:layout_height="140dp"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:background="@drawable/header" >
    </RelativeLayout>

    <TabHost
        android:id="@android:id/tabhost"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_above="@+id/footer"
        android:layout_below="@+id/relative_top" >

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="match_parent"
            android:orientation="vertical" >

            <TabWidget
                android:id="@android:id/tabs"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" />

            <FrameLayout
                android:id="@android:id/tabcontent"
                android:layout_width="match_parent"
                android:layout_height="match_parent" >
            </FrameLayout>
        </LinearLayout>
    </TabHost>
</RelativeLayout>

The Following code is from java

    TabHost tabHost = getTabHost();

    // Tab for Photos
    TabSpec student_list = tabHost.newTabSpec("Photos");
    student_list.setIndicator("",        getResources().getDrawable(R.drawable.my_student_list_green_line));
    Intent photosIntent_intent = new Intent(this, Student_List_Activity.class);
    student_list.setContent(photosIntent_intent);

    // Tab for Songs
    TabSpec add_new_student = tabHost.newTabSpec("Songs");
    // setting Title and Icon for the Tab
    add_new_student.setIndicator("", getResources().getDrawable(R.drawable.my_student_add_new_student));
    Intent add_new_student_intent = new Intent(this, Add_New_Student.class);
    add_new_student.setContent(add_new_student_intent);

    // Tab for Videos

    // Adding all TabSpec to TabHost
    tabHost.addTab(student_list); // Adding photos tab
    tabHost.addTab(add_new_student); // Adding songs tab

I have two images as follows

enter image description here

enter image description here

when user click on any tab at that time these images should be interchange how can i do that

War es hilfreich?

Lösung

Try this solution

 //Tab Host 1
    getTabHost().addTab(getTabHost().newTabSpec("")
    //set the tab name
    .setIndicator("")
    //Add additional flags to the intent (or with existing flags value).
    .setContent(new Intent(this, StudentListActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP))); 


    //Tab Host 2
    getTabHost().addTab(getTabHost().newTabSpec("")
    //set the tab name
    .setIndicator("")
    //Add additional flags to the intent (or with existing flags value).
    .setContent(new Intent(this,AddNewStudent.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)) );
    getTabWidget().setStripEnabled(false);// this line is specifically used to remove the line in bottm at tabWidget 

    getTabHost().getTabWidget().getChildAt(0).setBackgroundResource(R.drawable.tabhost_student_images);
    getTabHost().getTabWidget().getChildAt(1).setBackgroundResource(R.drawable.tabhost_add_new_student_images);



    //On Drowable
    //tabhost_add_new_student_images.xml
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/tab_second" android:state_selected="true"/>
    <item android:drawable="@drawable/my_student_add_new_student" android:state_selected="false"/>

    </selector>

    //tabhost_student_images.xml
    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@drawable/my_student_list_green_line" android:state_selected="true"/>
    <item android:drawable="@drawable/tab_add_new_student" android:state_selected="false"/>

    </selector>
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top