2

I have an application with a few tabs which all belong to the same TabHost. Is there any way to change the TextView android:text value before the TabWidget in any activity other than the main activity which extends TabActivity?

This is my main.xml

<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@android:id/tabhost"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:background="@color/sigma_darkgray">
        <TextView
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:background="@color/title_color"
            android:text="Home page"
            android:textStyle="bold"
            android:paddingTop="6dip"
            android:paddingBottom="6dip"
            android:gravity="center_horizontal"
            android:textAppearance="?android:attr/textAppearance"/>
        <TabWidget
            android:id="@android:id/tabs"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:paddingTop="1dip"
            android:background="@color/title_color"/>
        <View android:id="@+id/separator" 
            android:background="#ffffff"
            android:layout_width = "fill_parent"
            android:layout_height="1dip"
            android:layout_centerVertical ="true"
            android:layout_alignParentTop="true"/>     
        <FrameLayout
            android:id="@android:id/tabcontent"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"/>
    </LinearLayout>
</TabHost>
iCantSeeSharp
  • 3,880
  • 4
  • 42
  • 65

2 Answers2

2

Yes You can. Just give the id to the TextView. And Declare reference of that textview in MainActivity where there ie TabActivity is Extended.

Now in another child activity You can call that textView with the MainActivity.textview object.

Now here you cna change the TextValue of the textView and it is done from child activity.

Enjoy.

Shreyash Mahajan
  • 23,386
  • 35
  • 116
  • 188
  • I think you mean make a method in the main activity `public void changeTextView(String newText)` and define the `TextView _myTitle` in the Main Activity class, right? – iCantSeeSharp Nov 15 '11 at 10:58
  • 1
    U can make method for it or if dont want to make mathod then also you can change the text directly. – Shreyash Mahajan Nov 15 '11 at 12:22
2

In the activity started from the TabHost

((MyParentActivity)getParent()).setYourText("whatever")

where you must create the "setYourText(String text)" method.

Maragues
  • 37,861
  • 14
  • 95
  • 96
  • I would have accepted your answer if you were a few minutes faster, both answers are the same, thanks anyway! – iCantSeeSharp Nov 15 '11 at 11:07
  • How do you fetch the parent activity following his advice? Also, I don't think it's a good practice to access another Activity's field directly through "MyActivity.textviewReference". It may confuse newcomers that follow his answer. Anyway, glad you got your problem fixed. – Maragues Nov 15 '11 at 11:21