Android why this button is not available

I dont know why the last two buttons are not clickable, I have used the background (which I have set for them) many times and it works with other layouts, I am sure something like foucable or clickable is wrong, but I dont know , where exactly.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#FFFFFF"
    android:fillViewport="true" >

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

        <LinearLayout
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:background="#FFFFFF"
            android:orientation="vertical" >

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dip"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                android:layout_marginTop="20dip"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/b_orderMeal_selectRestaurant"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginRight="30dip"
                    android:text="@string/b_selectRestaurant" />

                <ImageView
                    android:id="@+id/iv_orderMeal_isSelectRestaurant"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_alignParentRight="true"
                    android:contentDescription="@string/iv_isSelected"
                    android:src="@drawable/x" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dip"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                android:layout_marginTop="20dip"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/b_orderMeal_selectMealItems"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginRight="30dip"
                    android:text="@string/b_select_MealItems" />

                <ImageView
                    android:id="@+id/iv_orderMeal_isSelectMealItems"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_alignParentRight="true"
                    android:contentDescription="@string/iv_isSelected"
                    android:src="@drawable/x" />
            </RelativeLayout>

            <RelativeLayout
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginBottom="20dip"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="10dip"
                android:layout_marginTop="20dip"
                android:layout_weight="1" >

                <Button
                    android:id="@+id/b_orderMeal_selectPayMethod"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_alignParentLeft="true"
                    android:layout_marginRight="30dip"
                    android:text="@string/b_selectPayMethod" />

                <ImageView
                    android:id="@+id/iv_orderMeal_isSelectPayMethod"
                    android:layout_width="25dp"
                    android:layout_height="25dp"
                    android:layout_alignParentRight="true"
                    android:contentDescription="@string/iv_isSelected"
                    android:src="@drawable/x" />
            </RelativeLayout>

            <Button
                android:id="@+id/b_orderMeal_checkBasket"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="10dip"
                android:layout_marginRight="40dip"
                android:layout_marginTop="20dip"
                android:layout_weight="1"
                android:text="@string/b_check_basket" />

            <TextView
                android:id="@+id/tv_orderMeal_errorMessage"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content"
                android:layout_marginLeft="20dip"
                android:layout_marginRight="20dip"
                android:layout_marginTop="30dip"
                android:layout_weight="1"
                android:text="@string/tv_orderMeal_errorMessage"
                android:textColor="#FF0000"
                android:textSize="20dip"
                android:textStyle="bold"
                android:typeface="sans" />
        </LinearLayout>

        <LinearLayout
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:background="@drawable/bg_selector"
            android:orientation="horizontal" >

            <Button
                android:id="@+id/b_orderMeal_send"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginRight="4dip"
                android:layout_weight="1"
                android:background="@drawable/button_bg"
                android:text="@string/b_send"
                android:focusable="true"
                android:clickable="true"
                android:textColor="#FFFFFF" />

            <Button
                android:id="@+id/b_orderMeal_cancel"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="4dip"
                android:layout_weight="1"
                android:background="@drawable/button_bg"
                android:text="@string/b_cancel"
                android:clickable="true"
                android:textColor="#FFFFFF" />
        </LinearLayout>
    </RelativeLayout>

</ScrollView>

      

I'm talking about the last two buttons

java

b_send = (Button) findViewById(R.id.b_orderMeal_send);
        b_send.setOnClickListener(this);
        b_cancel = (Button) findViewById(R.id.b_orderMeal_cancel);
        b_cancel.setOnClickListener(this);

      

when you press

@Override
    public void onClick(View v) {
      case R.id.b_orderMeal_cancel:
        Toast.makeText(this, "roma", Toast.LENGTH_LONG);
        break;          
    }

      

note

toast is not displayed

+3


source to share


4 answers


I don't know why the last two buttons are not clickable, I have used the background (which I have set for them) many times and it works with different layouts, I am sure something like foucable is wrong or but I don't know where exactly.

I think your design XML

is fine, so the problem is most likely in Java

. Most likely you forgot to register OnClickListener

for your Button(s)

.

Button btn = (Button) findViewById(R.id.b_orderMeal_send);
btn.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {
       Toast.makeText(this, "Click event works.", Toast.LENGTH_SHORT).show();
     }
 }));

      



Or you can use an interface OnClickListener

and then

public void onClick(View v) {
   switch (v.getId()) {
      case R.id.b_orderMeal_send:
         Toast.makeText(this, "Click event works.", Toast.LENGTH_SHORT).show();
      break;
   }
}

      

+5


source


Replace the method onClick()

below and tell us about logcat. You seem to be trying to show toast

, but can't use the method .show()

to display the toast.
Try under code and let us know



 @Override
 public void onClick(View v) {
 if(v.equals(b_send)){
     Toast.makeText(this,"b_send clicked", Toast.LENGTH_LONG).show();
     Log.d("TEMP TAG","b_send clicked");
 } else if(v.equals(b_cancel)){ 
    Toast.makeText(this,"b_cancel clicked", Toast.LENGTH_LONG).show();
      Log.d("TEMP TAG","b_cancel clicked");
      }

  }

      

+1


source


set clickable = true in xml for your button and try that in your code.

findViewById(R.id.b_orderMeal_send).setOnClickListener(commonClickListener);
findViewById(R.id.b_orderMeal_cancel).setOnClickListener(commonClickListener);


private OnClickListener commonClickListener = new OnClickListener() {
@Override
public void onClick(View v) {
    int selectedItemId = v.getId();
    switch (selectedItemId) {
    case R.id.b_orderMeal_send:
        Toast.makeText(this, "roma", Toast.LENGTH_LONG).show();
        break;
    case R.id.b_orderMeal_cancel:
    Toast.makeText(this, "roma", Toast.LENGTH_LONG).show();
        break;


    }
}

      

+1


source


Sometimes the problem comes from the emulator , so I would like to tell you guys how to use a real Android device to test / run your application.

+1


source







All Articles