Faisal Shaikh
Faisal Shaikh

Reputation: 4147

Unable to access TextView of the the Custom ActionBar

I am using different layout and showing into action bar. I want to change value of TextView programatically which I used in layout.

Here is my xml file:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="@drawable/splashscreen_background">


                 <TextView 
                    android:id="@android:id/text1"
                    android:layout_width="?attr/actionBarSize"
                    android:layout_height="match_parent"
                    android:textColor="#ff0000"
                    android:textSize="10sp"
                    android:textStyle="bold"
                    android:background="#034552"
                    android:text="M:101"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:gravity="center"
                    android:minHeight="?android:attr/listPreferredItemHeight"
                    android:layout_alignParentLeft="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginLeft="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"/>


                  <TextView 
                    android:id="@android:id/text2"
                    android:layout_width="?attr/actionBarSize"
                    android:layout_height="match_parent"
                    android:textColor="#ff0000"
                    android:textSize="10sp"
                    android:textStyle="bold"
                    android:background="#034552"
                    android:text="N:101"
                    android:textAppearance="?android:attr/textAppearanceLarge"
                    android:gravity="center"
                    android:minHeight="?android:attr/listPreferredItemHeight"
                    android:layout_alignParentRight="true"
                    android:layout_alignParentTop="true"
                    android:layout_marginRight="5dp"
                    android:layout_marginTop="5dp"
                    android:layout_marginBottom="5dp"/>
</RelativeLayout>

And here is java file code:

        ActionBar ab = getSupportActionBar();
        ab.setDisplayShowHomeEnabled(false);
        ab.setDisplayShowTitleEnabled(false);
        LayoutInflater li = LayoutInflater.from(this);
        View customView = li.inflate(R.layout.test, null);
        TextView tv = (TextView)customView.findViewById(R.id.text1);
        ab.setCustomView(customView);
        ab.setDisplayShowCustomEnabled(true);

Can any one suggest me how can I change the value of TextView programmatically.

Upvotes: 0

Views: 86

Answers (1)

Mohib Irshad
Mohib Irshad

Reputation: 1990

In xml, replace android:id="@android:id/text1" with android:id="@+id/text1". Same goes for TextView with id text2 ..

and in java, you will be able to programmatically set it like this:

TextView tv = (TextView)customView.findViewById(R.id.text1);
tv.setText("Your Text");

To findout what's the difference between @android:id and @+id, Please see this: https://stackoverflow.com/a/9530028/2534007

Upvotes: 1

Related Questions