user2847219
user2847219

Reputation: 555

Android multipane toolbar

I'm using with FrameLayout success for the multi pane. The only problem is that related to option menu. As you can see from the picture, an icon belongs to the left fragment instead are displayed here, in the fragment of the right. Where is my mistake? thank you

a_drawer

<android.support.v7.widget.Toolbar
    android:id="@+id/appbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?attr/colorPrimary"
    android:minHeight="?attr/actionBarSize"
    android:theme="?attr/toolbarTheme"
    app:popupTheme="?attr/toolbarPopupTheme"/>

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:baselineAligned="false"
    android:orientation="horizontal">


    <FrameLayout
        android:id="@+id/frame_container"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="2"/>

    <FrameLayout
        android:id="@+id/frame_container1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_weight="1"/>

</LinearLayout>

Main

 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
 setContentView(R.layout.a_drawer);


    toolbar = (Toolbar) findViewById(R.id.appbar);
    setSupportActionBar(toolbar);

and the fragment placed to the right

 @Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.vedi_, container, false);
    setHasOptionsMenu(true);
...
...
}
 @Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_modifica, menu);
    inflater.inflate(R.menu.menu_elimina, menu);
}
 @Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()) {
...
}

the fragment XML

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            xmlns:app="http://schemas.android.com/apk/res-auto"
            xmlns:fab="http://schemas.android.com/tools">


<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginLeft="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="16dp"
    android:layout_below="@+id/appbar">

    <RelativeLayout
        android:id="@+id/relativeLayout3"
        android:layout_width="fill_parent"
        android:layout_height="55dp"
        android:layout_alignParentTop="true"
        android:layout_marginBottom="10dp"
        android:layout_marginTop="10dp">

        <TextView
            android:id="@+id/textView4"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="60dp"
            android:ellipsize="end"
            android:gravity="center_vertical|left"
            android:singleLine="true"
            android:text="conto"
            android:textColor="#000000"
            android:textSize="21sp"/>

        <ImageView
            android:id="@+id/imageView3"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"/>
    </RelativeLayout>

    <ListView
        android:id="@+id/listView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_below="@+id/relativeLayout3"/>

    <com.github.clans.fab.FloatingActionButton
        android:id="@+id/FloatingActionButton02"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_centerHorizontal="true"
        android:baselineAlignBottom="false"
        android:src="@mipmap/ic_photo_white_24dp"
        fab:fab_colorNormal="#ff4806"
        fab:fab_size="normal"
        />

    <TextView
        android:id="@+id/textView5"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true"
        android:layout_centerVertical="true"
        android:gravity="center_vertical|center_horizontal"
        android:text="@string/nonContieneCampi"
        android:textAppearance="?android:attr/textAppearanceMedium"
        android:textColor="#000000"
        android:textSize="14sp"/>

</RelativeLayout>

enter image description here

Upvotes: 1

Views: 149

Answers (1)

Mikhail Spitsin
Mikhail Spitsin

Reputation: 2608

If you want to display something like that (there are some screenshots) then I consider you need to implement 2 toolbars instead of one. So you have to options:

1) Add to layout another Toolbar widget.

2) Remove toolbar widget from layout, and add toolbar widgets to each fragment layout

For me second option is more right, because it encapsulates each 'fragment' toolbar in their layouts.

Next you need to use Toolbar.inflateMenu method to provide custom menu resource to each toolbar. And don't use 'setSupportActionBar' (in case if you use it), because you don't need it.

If you want more info, please, comment. I will try to explain more details.

Edited:

You need to replace toolbars to your fragments's xml layout. Next you need to remove

toolbar = (Toolbar) findViewById(R.id.appbar);
setSupportActionBar(toolbar);

From onCreate in your Activity. Also you need to remove

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    super.onCreateOptionsMenu(menu, inflater);
    inflater.inflate(R.menu.menu_modifica, menu);
    inflater.inflate(R.menu.menu_elimina, menu);
}

And add something like that to each fragment. For first one:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.vedi_, container, false);
    Toolbar toolbar = rootView.findViewById(R.id.toolbar);
    toolbar.inflateMenu(R.id.my_menu_with_refresh_icon);
    toolbar.setOnMenuItemClickListener(this);
}

For second one:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.vedi_, container, false);
    Toolbar toolbar = rootView.findViewById(R.id.toolbar);
    toolbar.inflateMenu(R.id.my_menu_with_delete_and_edit_icons);
    toolbar.setOnMenuItemClickListener(this);
}

Also you need to connect first fragment to Activity through onAttach method. Read more here (this is for connecting toolbar from first fragment to drawer)

Upvotes: 2

Related Questions