Home > Net >  custom actionbar in android
custom actionbar in android

Time:02-18

I created custom toolbar and i got this result: enter image description here

i want to that the toolbar match the width of screen.

this is xml code:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
    android:background="#009688">

    <LinearLayout
        android:id="@ id/linearLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

        <TextView
            style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="@string/app_name"
            android:textColor="@android:color/white"
            android:textSize="18sp"
            android:textStyle="bold" />
    </LinearLayout>


</androidx.constraintlayout.widget.ConstraintLayout>

java code:

private void intView() {
        if (getSupportActionBar() != null) {

            getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
            getSupportActionBar().setCustomView(R.layout.toolbar_custum_title);

        }

    }

CodePudding user response:

for that fist you have to hide default toolbar.. in your activity onCreate method write this code to hide default action bar

    this.getSupportActionBar().hide();

then go to you activity layout xml

    <androidx.appcompat.widget.Toolbar
            android:id="@ id/MainMenutoolbar_id"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="@color/mycustom"
        app:titleTextColor="@color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintHorizontal_bias="0.0"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintVertical_bias="0.0">
   
      //here you can implement textview edittext etc it can be custom  
        
      </androidx.appcompat.widget.Toolbar>
  • Related