- Create a new project and name your main activity "MyActivity"
Go to res - drawable and create a new xml file and call it "custom_title_background" and put the following code:
<item android:top="20dp">
<shape android:shape="rectangle">
<gradient android:angle="90" android:endcolor="#9eacbf" android:startcolor="#8296af">
</gradient></shape>
</item>
This drawable will be used to set the background from custom_title_bar (from step 3) and to set the windowTitleBackgroundStyle from custom_title_style (from step 4)
Go to res-layout and create a new xml and name it "custom_title_bar". Here you will create a layout with a text view like in the following code:
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="@android:color/white"
android:textStyle="bold"
android:id="@+id/custom_title_text"
android:layout_centerInParent="true"
android:shadowColor="@android:color/black"
android:shadowRadius="3"/>
Go to res - values and create a new xml file and call it custom_title_style. Here you will create a new theme by overriding the existing one. The name of the style "custom_title_theme" from below will be used into the manifest file to "activate" the new theme.
40dp
@drawable/custom_title_background
Now go to the AndroidManifest.xml file and put the new theme in the application tag.
?
1
And at this last step, you have to go to the MyActivity class and put the following code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
import android.widget.TextView;
public class MyActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//this must be called BEFORE setContentView
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
//this must bew called AFTER setContentView
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.custom_title_bar);
//set the title
TextView textView = (TextView)findViewById(R.id.custom_title_text);
textView.setText("Custom Title");
}
}