I have made a custom title bar for my floating activity. Now I want to change text of TextView in my custom title programmatically, but unable to do so. I can change text via xml, but i want it to do in code.
here is code of label.java(floating activity) which is not updating textView in titlebar
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.my_title);
TextView label = (TextView)findViewById(R.id.myTitle);
label.setText("Label here code");//not working
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
setContentView(R.layout.label);// as i need this layout for rest of activity
//rest of code
myTitle.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/myTitle"
android:text="Label here"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@android:color/white"
/>
Your'e doing it wrong. You have to do it like this:
public class CustomTitleActivity extends Activity {
private TextView title;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.label);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.my_title);
title = (TextView) findViewById(R.id.title);
title.setText("My custom title");
}
}
You were trying to inflate your custom title layout into the content area of your Activity with setContentView(R.layout.my_title)
, which of course lets you grab the TextView
because you inflated it into the container, but then you told it to inflate your custom title into the Window, which inflated an entirely different TextView which is actually the one you want. Then you overwrote the content of the Activity with setContentView(R.layout.label)
.