简体   繁体   中英

Can't Click Button whilst in linear layout

I want my two buttons inline with one an other and taking equal space, so I thought it best to use a LinearLayout. However when I put them in the linear layout I can no longer click the buttons.

<LinearLayout
    android:id="@+id/linearLayoutStart"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="30dp"
    android:clickable="true"
    android:weightSum="2"
    android:orientation="horizontal"
    android:layout_alignParentStart="true">

    <Button
        android:id="@+id/btn_signup"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_weight="1"
        android:background="@drawable/input_frame_white"
        android:text="@string/skip"
        android:textColor="@android:color/white" />

    <Button
        android:id="@+id/btn_login"
        android:layout_width="0dp"
        android:layout_height="40dp"
        android:layout_marginLeft="15dp"
        android:layout_marginRight="15dp"
        android:layout_weight="1"
        android:background="@drawable/input_frame_white"
        android:text="@string/next"
        android:textColor="@android:color/white" />
</LinearLayout> 

This is the java call:

========================================================================

    btnSignUp = (Button) findViewById(R.id.btn_signup);
    btnLogIn = (Button) findViewById(R.id.btn_login);

    btnSignUp.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            launchSignUp();
        }
    });

    btnLogIn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            launchLogInScreen();
        }
    });

Thanks guys much appreciated.

If there is another layout element after the sample code, it may be rendered on top of the buttons. Put the Linear layout containing the buttons (as in the code sample above) as the last element. For example:

<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout 
    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:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

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

        <ListView
            android:id="@+id/listView"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/linearLayoutStart"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentEnd="true"
        android:layout_marginBottom="30dp"
        android:clickable="true"
        android:weightSum="2"
        android:orientation="horizontal"
        android:layout_alignParentStart="true">

        <Button
            android:id="@+id/btn_signup"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_weight="1"
            android:background="@drawable/input_frame_white"
            android:text="@string/skip"
            android:textColor="@android:color/white" />

        <Button
            android:id="@+id/btn_login"
            android:layout_width="0dp"
            android:layout_height="40dp"
            android:layout_marginLeft="15dp"
            android:layout_marginRight="15dp"
            android:layout_weight="1"
            android:background="@drawable/input_frame_white"
            android:text="@string/next"
            android:textColor="@android:color/white" />
    </LinearLayout> 

</androidx.coordinatorlayout.widget.CoordinatorLayout>

I think beacuse of this code:

android:clickable="true"

in your LinearLayout

remove this code and try again.

The code is correct. The only thing I can think of is that

android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"

reveales that your LinearLayout is inside a RelativeLayout, and some transparent element of the RelativeLayout might overlap the buttons.

This if - as you said - removing "Clickable" from the LinearLayout did not fix the issue (indeed: if the LinearLayout is clickable, the buttons might not receive the clicks)

Look before buttons code, there may be an exception (error) let the app jumps to catch directly, logcat can help you see those exemptions.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM