-1

i have this toolbar

enter image description here

i want to remove the title of the fragment so i can display more items

is it from the xml file or the activity?

because i tried to add this to my main activity:

getActionBar().setDisplayShowTitleEnabled(false);

getSupportActionBar().setDisplayShowTitleEnabled(false);

setSupportActionBar(toolbar).setTitle("");

toolbar.setTitle(null)

but none of them worked

any suggestion?

this is MainActiviy code:

class MainActivity : AppCompatActivity() {











override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)

    setContentView(R.layout.activity_main)
    setSupportActionBar(toolbar)
    









    val navController = Navigation.findNavController(this, R.id.nav_host_fragment)



    setupBottomNavMenu(navController)
    setupSideNavigationMenu(navController)
    setupActionBar(navController)
3
  • Are you using a Navigation component? Commented Apr 5, 2021 at 8:40
  • @GabrieleMariotti yes
    – Enigma
    Commented Apr 5, 2021 at 8:44
  • Post your relevant code of your setup Commented Apr 5, 2021 at 9:20

2 Answers 2

0

Since you are using a Navigation component you can use the addOnDestinationChangedListener to set your custom title after your setup method.

navController.addOnDestinationChangedListener { _, destination, _ ->
   if(destination.id == R.id.xxxx) {
       //If in your setup you are using a Toolbar
       toolbar.title = ""
       //supportActionBar?.title = "" //If you are using setSupportActionBar()
   } else {
       //
   }
}
1
  • i tired to add this, it didn't work as well
    – Enigma
    Commented Apr 5, 2021 at 9:20
0

You could create your own tool bar for each fragment and customize it as per your requirement. I have made a common toolbar xml file and reusing it in every fragment and customizing it. Look :

common_toolbar.xml

<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">

<androidx.constraintlayout.widget.ConstraintLayout
    android:id="@+id/const_item"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize">

    <TextView
        android:id="@+id/back_btn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:layout_gravity="center_vertical"
        android:layout_marginStart="@dimen/margin_normal"
        android:gravity="center"
        android:padding="@dimen/padding_five"
        android:text="Back"
        android:textColor="@android:color/white"
        android:textSize="@dimen/font_size_fifteen"
        app:drawableStartCompat="@drawable/ic_action_chevron_left"
        app:drawableTint="@android:color/white"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <TextView
        android:id="@+id/title_toolbar"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_centerHorizontal="true"
        android:gravity="center"
        android:text="@string/menu"
        android:textColor="@android:color/white"
        android:textSize="@dimen/font_size_fifteen"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />
    
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>

And then adding it to fragments's UI. Let's say, adding to fragment_user_details.xml

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">

<include layout="@layout/toolbar_common"
    android:id="@+id/toolbar_user_details"/>
<!-- 
 Other View widgets
 -->
</androidx.constraintlayout.widget.ConstraintLayout>

As each fragment has its own tool bar, so I am getting it by it's id and customize as per my requirement. Make sure Activity has Theme.MaterialComponents.Light.NoActionBar theme.

Menu Items can also be added like below:

@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
1
  • Previous code did not work because you are using Navigation Component. I am achieving this result by using just updated code. Commented Apr 5, 2021 at 9:14

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.