<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="400dp"
android:layout_height="fill_parent"
android:orientation="horizontal">
<ImageView android:id="@+id/ImageView01"
android:layout_width="57dp"
android:layout_height="wrap_content"
android:background="@drawable/icon1"/>
<TextView
android:id="@+id/myTitle"
android:text="This is my new title"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textColor="@color/titletextcolor"
/>
</LinearLayout>
TitleBar.java
public class TitleBar extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported =
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if (customTitleSupported) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE,
R.layout.titlebar);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if (myTitleText != null) {
myTitleText.setText("NEW TITLE");
// user can also set color using "Color" and then
// "Color value constant"
// myTitleText.setBackgroundColor(Color.GREEN);
}
}
}
In code: in code, call the setTitle() method with a String or the id of String as the argument.
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
setTitle(R.string.TitleWhichYouWantToDisplay);
// OR You can also use the line below
// setTitle("MyTitle")
setContentView(R.layout.activity_main);
}
}
ActionBar ab = getActionBar();
TextView tv = new TextView(getApplicationContext());
LayoutParams lp = new RelativeLayout.LayoutParams(
LayoutParams.MATCH_PARENT, // Width of TextView
LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setTextColor(Color.RED);
ab.setCustomView(tv);