Add An Actionbar For Each Fragment
Suppose I have 4 fragments that I put in mainActivity, I want to create different actionbar different in each frgament. When I press fragment 1 then the actionBar appears fragment
Solution 1:
Yes, you can add the custom toolbar to your fragment and make your activity theme with no action bar in the Manifest and do something like this. this layout may be your fragment layout and inflate in your activity.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:map="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?actionBarSize"
android:layout_alignParentTop="true"
android:background="@color/colorPrimary"
android:gravity="top"
app:titleTextColor="@color/white">
<ImageView
android:id="@+id/iv_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:contentDescription="TODO"
android:visibility="visible"
app:srcCompat="@drawable/ic_back" />
<TextView
android:id="@+id/toolbar_title"
style="@style/TextAppearance.AppCompat.Widget.ActionBar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="List Entry"
android:textColor="@color/white"
android:textSize="@dimen/large_text_size" />
<ImageView
android:id="@+id/iv_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_margin="@dimen/middium_padding"
android:contentDescription="TODO"
android:src="@mipmap/add"
android:visibility="visible" />
</android.support.v7.widget.Toolbar>
</RelativeLayout>
and access you toolbar with framgment view.
Solution 2:
In this scenario you usually have:
- Activity and layout that owns and contains Fragments, let us call them FragmentOwnerActivity.kt and fragment_owner_layout.xml
- A class derived from Fragment and a layout associated with it, let us call them MyFragment.kt and my_fragment_layout.xml
To enable toolbars in your fragments you'll need to put the following to my_fragment_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/someid"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyFragment">
<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
android:layout_marginStart="0dp"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="0dp"
>
<android.support.v7.widget.Toolbar
android:id="@+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="@attr/actionBarSize"
android:background="@attr/colorPrimary"
android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp"
android:elevation="2dp"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
>
<TextView
android:id="@+id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="@string/toolbar_title"
android:textSize="22sp"
android:textStyle="bold" />
</android.support.v7.widget.Toolbar>
In MyFragment.kt you'll need to enable the toolbar:
override fun onActivityCreated(savedInstanceState: Bundle?) {
super.onActivityCreated(savedInstanceState)
(activity as AppCompatActivity).setSupportActionBar(view?.findViewById(R.id.my_toolbar))
In FragmentOwnerActivity.kt you'll need to inflate a fragment specific menu and process menu click events
// You'll need to create an xml with a menu for your fragment
// and then use it in inflate function below
override fun onCreateOptionsMenu(menu: Menu): Boolean {
menuInflater.inflate(R.menu.my_fragment_menu_id, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
// Handle item selection
when (item.getItemId()) {
R.id.menu_item1_id -> {
doMenuItem1()
return true
}
R.id.menu_item2_id -> {
doMenuItem2()
return true
}
else -> return super.onOptionsItemSelected(item)
}
}
Finally, your fragment_owner_layout.xml is just FrameLayout element that controls other frames
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MyFragmentOwner"/>
Post a Comment for "Add An Actionbar For Each Fragment"