AdityaSetyadi
AdityaSetyadi

Reputation: 161

How to create Android custom titlebar

I try to build an application on Android. And I'm new in Android. But I don't know how to build a Title Bar like this. So we can give the application name like Seesmic and Komutta with the tab button. Can anyone help me to give me the answer or just a link for that tutorial?

Thank you.

https://lh6.ggpht.com/Hf6XKfa9K0B-CvlV6tD6qj2Yt8wJcyJ7wa8vE9BVkBbUDm0Y2pqOxgxVf7auQgXrh0gR

https://lh4.ggpht.com/rwceS5ZK1IZkHHCVixbaXlsHXwstpmIO888aMC4U0uD2oa54NiGvphcp_penGK9Q9WE

I'm sorry I can't upload the image, so I just can give the link for that image.

Upvotes: 0

Views: 1499

Answers (3)

Amit Gajera
Amit Gajera

Reputation: 143

  1. Create a new project and name your main activity "MyActivity"
  2. 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)

  1. 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"/>
    

  2. 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

  3. Now go to the AndroidManifest.xml file and put the new theme in the application tag.

? 1

  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");
    }
    

    }

Upvotes: 0

idiottiger
idiottiger

Reputation: 5177

android site has a demo you can check CustomTitle, and how-to-create-custom-window-title-in-android

Upvotes: 0

kenota
kenota

Reputation: 5662

This is called "Action Bar" you can get it nativly starting from Android 3.0 or grab code to do it on earlier versions of android here.

Upvotes: 1

Related Questions