Merha
Merha

Reputation: 91

toolbar item click in DialogFragment

I have a fullscreen DialogFragment with a toolbar. On the right side of the toolbar I have an item. I want to do an action when the item is clicked. The problem is, when I click on it nothing happens. But if I click next to it on the edge of the screen the action is called.

I print "Item clicked!" when it is called. You can see the toolbar in below.

enter image description here

The onMenuItemClick method is called only when I click on the red circled part of the screen.

This is the class that extends DialogFragment :

@Override
public void onCreate(Bundle bundle) {
    super.onCreate(bundle);
    setStyle(DialogFragment.STYLE_NORMAL, R.style.Preference_DialogPreference_Material);
    fish = getArguments().getParcelable("species");
}

public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    View v = inflater.inflate(R.layout.fragment_fish_details, container, false);

    Toolbar toolbar = (Toolbar) v.findViewById(R.id.toolbar);
    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
            // Handle the menu item
            System.out.println("clicked");
            return true;
        }
    });
    toolbar.inflateMenu(R.menu.details);
    toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
    toolbarTitle.setText(R.string.learn);

The menu I'm using :

<menu 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"
    tools:context="ch.boas.aquatis.android.activity.MainActivity" >
    <item android:id="@+id/action_share"
        android:title="Share"
        android:icon="@drawable/ic_share_species"
        app:showAsAction="always"/>
</menu>

The layout of this fragment :

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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="@color/colorPrimaryDark"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    android:weightSum="1">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.design.widget.AppBarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:theme="@style/AppTheme.AppBarOverlay">

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

                <TextView
                    android:id="@+id/toolbar_title"
                    style="@style/pages_titles"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content" />

            </android.support.v7.widget.Toolbar>

        </android.support.design.widget.AppBarLayout>

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="150dp"
            android:background="@color/mr_white"
            android:gravity="center">

            <ImageView
                android:id="@+id/fish_photo"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginBottom="7dp" />

            <ProgressBar
                android:id="@+id/progress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_centerHorizontal="true"
                android:layout_centerVertical="true"
                android:indeterminate="true"
                android:indeterminateTint="@color/colorPrimary"
                android:indeterminateTintMode="src_atop" />
        </RelativeLayout>
    </LinearLayout>

    <ScrollView style="@style/scrollview_style">

        <RelativeLayout
            android:id="@+id/abc"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:paddingBottom="10dp"
            android:paddingTop="190dp">

            <RelativeLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:background="@drawable/button"
                android:paddingBottom="20dp">

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginBottom="20dp"
                    android:orientation="vertical">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="horizontal"
                        android:paddingBottom="20dp">

                        <ImageView
                            android:id="@+id/logo_iucn"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:src="@drawable/ic_cr" />

                        <TextView
                            android:id="@+id/code_text"
                            style="@style/ParagraphCenter"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:gravity="left"
                            android:text="" />

                        <View
                            android:layout_width="0dp"
                            android:layout_height="match_parent"
                            android:layout_weight="1" />

                        <ImageButton
                            android:id="@+id/picto_info"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:background="@android:color/transparent"
                            android:clickable="true"
                            android:src="@drawable/ic_infobulle" />

                    </LinearLayout>

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content">

                        <ImageView
                            android:id="@+id/share_button"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:src="@drawable/ic_share_species" />

                        <!--<Button
                            android:id="@+id/shar_fb"
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_alignParentRight="true"
                            android:layout_below="@+id/share_button"
                            android:text="share" />-->
                    </RelativeLayout>


                    <TextView
                        android:id="@+id/name"
                        style="@style/TitleLeft"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="-20dp"
                        android:text="" />

                    <TextView
                        android:id="@+id/latin_name"
                        style="@style/TitleLeft"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:paddingTop="5dp"
                        android:text=""
                        android:textStyle="italic" />

                    <TextView
                        android:id="@+id/type"
                        style="@style/details"
                        android:text="" />

                    <TextView
                        android:id="@+id/continent"
                        style="@style/details"
                        android:layout_width="wrap_content"
                        android:text="" />

                    <TextView
                        android:id="@+id/family"
                        style="@style/details"
                        android:text=" " />


                    <TextView
                        android:id="@+id/features"
                        style="@style/details"
                        android:text="" />

                    <TextView
                        android:id="@+id/size"
                        style="@style/details"
                        android:text="" />

                    <LinearLayout
                        android:id="@+id/section_desc"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <View
                            style="@style/view_style"
                            android:layout_marginBottom="10dp"
                            android:layout_marginTop="10dp" />

                        <!--<TextView
                            style="@style/TitleLeft"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:text="@string/description" />-->

                        <TextView
                            android:id="@+id/desc"
                            style="@style/details"
                            android:text="" />


                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/section_alert"
                        android:layout_width="match_parent"
                        android:layout_height="match_parent"
                        android:orientation="vertical">

                        <View
                            style="@style/view_style"
                            android:layout_marginBottom="10dp"
                            android:layout_marginTop="10dp" />

                        <TextView
                            style="@style/TitleLeft"
                            android:layout_width="wrap_content"
                            android:layout_height="match_parent"
                            android:text="@string/alert" />

                        <TextView
                            android:id="@+id/alert"
                            style="@style/details"
                            android:text="" />
                    </LinearLayout>

                </LinearLayout>
            </RelativeLayout>
        </RelativeLayout>
    </ScrollView>
</RelativeLayout>

Upvotes: 1

Views: 1415

Answers (3)

Merha
Merha

Reputation: 91

The problem was in the LinearLayout. I've excluded the AppBarLayout from it and then placed it above. When it was in the LinearLayout, the Scrollview was placed above the item so most part of it was hidden except its right side (where the click was working). I couldn't see it without scrolling.. Now the Scrollview passes under the toolbar when scrolling and the item click works fine.

My layout looks like this now :

<android.support.design.widget.AppBarLayout
    android:id="@+id/toolbar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

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

        <TextView
            android:id="@+id/toolbar_title"
            style="@style/pages_titles"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar_layout"
    android:orientation="vertical">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="150dp"
        android:background="@color/mr_white"
        android:gravity="center">

        <ImageView
            android:id="@+id/fish_photo"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginBottom="7dp" />

        <ProgressBar
            android:id="@+id/progress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:indeterminate="true"
            android:indeterminateTint="@color/colorPrimary"
            android:indeterminateTintMode="src_atop" />
    </RelativeLayout>
</LinearLayout>

Upvotes: 0

Felhi Abdelhafidh
Felhi Abdelhafidh

Reputation: 478

Be sure in your layout (design mode) that you place the item in the right place.Sometimes you make two element near to each other but in real they are superimposed.

Upvotes: 0

Hemant Parmar
Hemant Parmar

Reputation: 3976

First inflate your menu in toolbar after apply listener like this

 Toolbar toolbar = (Toolbar) v.findViewById(R.id.toolbar);
 toolbar.inflateMenu(R.menu.details);
    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(MenuItem item) {
          switch (item.getItemId()) {
        case R.id.action_share:
           //do some here
                Log.d("debug","action share has clicked");
            return true;
    }
    return false;

        }
    });

    toolbarTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
    toolbarTitle.setText(R.string.learn);

Upvotes: 1

Related Questions