Android custom actionbar

we can easily add custom action bar in our android project. If you want to add an image icon and title in actionbar you can easily do it with custom actionbar. You can develop custom action bar using custom layout. In first step we need to add custom layout .

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal">


    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:srcCompat="@drawable/telepostsmall"
        android:layout_gravity="left"
        android:gravity="left"
        android:layout_marginLeft="0dp"
        />

    <android.support.v7.widget.AppCompatTextView
        android:id="@+id/tvTitle"
        style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
         android:layout_marginLeft="6dp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:gravity="center"
        android:textColor="#FFFFFF" />

</LinearLayout>

in next step in activity we need to set this custom actionbar

getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
       getSupportActionBar().setCustomView(R.layout.custom_toplayout);
       ((AppCompatTextView)getSupportActionBar().getCustomView().findViewById(R.id.tvTitle)).setText(R.string.texttoshow);

I am a freelance developer you can catch me at https://www.upwork.com/freelancers/~017baf8a30dad8db1a

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *