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.

-------------Problems Reply------------

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: http://stackoverflow.com/a/9530028/2534007

Category:java Views:2 Time:2018-07-08

Related post

Copyright (C) dskims.com, All Rights Reserved.

processed in 0.137 (s). 11 q(s)