Vitali Vitali - 2 months ago 17
Android Question

Linear Layout not handle clicks

I have such fragment layout:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/addNewItem"
android:clickable="true">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/addCityCityNameEdit"
android:layout_gravity="center_horizontal"
android:hint="@string/cityNameEditHint"
android:gravity="center_vertical|center_horizontal"
android:layout_weight="4"
android:layout_margin="@dimen/defaultMargin"
android:clickable="false" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/addCityButton"
android:id="@+id/addCityButton"
android:layout_weight="1"
android:src="@android:drawable/ic_menu_add"
android:background="@null"
android:layout_margin="@dimen/defaultMargin" />
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/cityListRecycleView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>




And problem with my layout "addNewItem". I need to response to click the Layout space, but nothing happen when I click. Code in fragment:

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.city_list_fragment, container, false);
addNewView = (LinearLayout) view.findViewById(R.id.addNewItem);
addNewView.setAlpha(0.6f);
addNewView.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.d(TAG, "clicked");
}
});
.........


What's wrong? My layout have cliclable=true, but it's not respond to my clicks.
I want to implement this view in way that at first it's children disabled(setEnabled(false)). And when user long click on LinearLayout it "unlock" its childs and then user can add item in recycler view via this unlocked editText and add button.

Answer

How about you try something like this:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical" android:layout_width="match_parent"
          android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:id="@+id/addNewItem"
                android:clickable="true">
    <View
        android:id="@+id/dummyView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="invisible"/>
    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/addCityCityNameEdit"
        android:layout_gravity="center_horizontal"
        android:hint="@string/cityNameEditHint"
        android:gravity="center_vertical|center_horizontal"
        android:layout_weight="4"
        android:layout_margin="@dimen/defaultMargin"
        android:clickable="false" />
    <ImageButton
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/addCityButton"
        android:id="@+id/addCityButton"
        android:layout_weight="1"
        android:src="@android:drawable/ic_menu_add"
        android:background="@null"
        android:layout_margin="@dimen/defaultMargin" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
    android:id="@+id/cityListRecycleView"
    android:scrollbars="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
</android.support.v7.widget.RecyclerView>

Then in your activity:

    findViewById(R.id.dummyView).setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            v.setVisibility(View.GONE);
            return false;
        }
    });

Since you would now be in a relative layout, the first child lies above the others and would capture the click events until its visibility is set to gone (done in the onlongclicklistener). You still need to change the way the views are set in the relative layout, but should be easy now (you could also nest another linear layout in there to avoid that, not sure it is a very good practice though).

Original answer: The height is taken completely by the ImageButton and EditText, are the click event not captured even when you click on the "side" of these two views?

You could set the onClickListener to these two views and handle from there too.

public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.city_list_fragment, container, false);
addNewView = (LinearLayout) view.findViewById(R.id.addNewItem);
view.findViewById(R.id.addCityCityNameEdit).setOnClickListener(this);
view.findViewById(R.id.addCityButton).setOnClickListener(this);
addNewView.setAlpha(0.6f);
addNewView.setOnClickListener(this);

@Override
public void onClick(View v){
    Log.d(TAG, "clicked");
}
.........