vimal mishra
vimal mishra

Reputation: 1167

How to position element in action bar?

I am not able to align element in action bar.Can someone please help me out with the issue.I want to align image on the left corner and title in the middle and one setting option in the right corner.There is one arrow to go back on other activity .I want to remove that option.Please refer the screen shot for more info.

ActionBar.xml

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

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@drawable/pic13"/>

    <TextView
        android:id="@+id/actionbar_textview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:maxLines="1"
        android:clickable="false"
        android:focusable="false"
        android:longClickable="false"
        android:textStyle="bold"
        android:textSize="18sp"
        android:textColor="#FFFFFF" />

    </LinearLayout>

Activity :

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_student_profile);
    final ActionBar abar = getSupportActionBar();
    abar.setBackgroundDrawable(getResources().getDrawable(R.color.title));//line under the action bar
    View viewActionBar = getLayoutInflater().inflate(R.layout.abs_layout, null);
    ActionBar.LayoutParams params = new ActionBar.LayoutParams(//Center the textview in the ActionBar !
            ActionBar.LayoutParams.WRAP_CONTENT,
            ActionBar.LayoutParams.MATCH_PARENT,
            Gravity.CENTER);
    TextView textviewTitle = (TextView) viewActionBar.findViewById(R.id.actionbar_textview);
    textviewTitle.setText("Test");
    abar.setCustomView(viewActionBar, params);
    abar.setDisplayShowCustomEnabled(true);
    abar.setDisplayShowTitleEnabled(false);
    abar.setDisplayHomeAsUpEnabled(true);
    abar.setIcon(R.drawable.pic13);
    abar.setHomeButtonEnabled(true);
}

enter image description here

Upvotes: 0

Views: 617

Answers (1)

Irfan
Irfan

Reputation: 956

add this code to your xml

Toolbar is a ViewGroup, you can customize is as much as you want.

<android.support.v7.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary"
    app:popupTheme="@style/AppTheme.PopupOverlay">


    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:src="@mipmap/ic_launcher" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Title"
        android:textColor="@android:color/white" />
</android.support.v7.widget.Toolbar>

Upvotes: 2

Related Questions