1

When i run the app. The buttons refuse to respond. I set a Toast and a logcat to check for response. but non. please help resolve

this is my source code

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Hello World!"
    android:id="@+id/textView" />

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:id="@+id/button"
    tools:onClick="topClick" />

<Button
    android:text="Button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_below="@+id/button"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="14dp"
    android:id="@+id/button2"
    tools:onClick="bottomClick" />

and for my java methods;

public class MyActivity extends AppCompatActivity {

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

        //Testing the Toast and Log in action
        Toast.makeText(this, "Can you see me?", Toast.LENGTH_SHORT).show();

        Log.i("info", "Done creating the app");
    }


    //creating method topclick
    public void topClick(View v) {
        Toast.makeText(this, "Top button clicked", Toast.LENGTH_SHORT).show();

        Log.i("info", "The user clicked the top button");
    }

    //creating method bottomclick
    public void bottomClick(View v) {
        Toast.makeText(this, "Bottom button clicked", Toast.LENGTH_SHORT).show();

        Log.i("info", "the user clicked the bottom button");
    }

}
Shaishav Jogani
  • 2,111
  • 3
  • 23
  • 33
mitch
  • 67
  • 2
  • 11

2 Answers2

0

Replace

tools:onClick

with

android:onClick

for both buttons.

For more context that will help you understand why this change is required, read up on the tools namespace:

Android Studio supports a variety of XML attributes in the tools namespace that enable design-time features (such as which layout to show in a fragment) or compile-time behaviors (such as which shrinking mode to apply to your XML resources). When you build your app, the build tools remove these attributes so there is no effect on your APK size or runtime behavior.

stkent
  • 19,772
  • 14
  • 85
  • 111
0

I would highly not recommended using that implementation of your as you can hardly know which method for what button. Try this instead.

XML

<Button
    android:text="Button"
    android:id="@+id/button1" />

<Button
    android:text="Button"
    android:id="@+id/button2" />

ACTIVITY.JAVA

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

    Button button01 = (Button)findViewById(R.id.button01);
    Button button02 = (Button)findViewById(R.id.button02);

    button01 .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MyActivity.this, "This is button01", Toast.LENGTH_SHORT).show();
        }
    });
    button02 .setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Toast.makeText(MyActivity.this, "This is button02", Toast.LENGTH_SHORT).show();
        }
    });
}
UmarZaii
  • 1,355
  • 1
  • 17
  • 26