0

Hello I am new to android studio I have just learned how logs and toast work so I thought to test it out, but my button is not showing anything. Here's the code:

MainActivity.java

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity {
    public void me(View view) {
        EditText editText = (EditText) findViewById(R.id.editText);
        Log.i("Info","Button pressed");
        Toast.makeText(this, "Hello! " + editText.getText().toString(), 
Toast.LENGTH_LONG).show();
    }

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
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">

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="72dp"
    android:layout_marginLeft="72dp"
    android:layout_marginTop="35dp"
    android:layout_marginEnd="73dp"
    android:layout_marginRight="73dp"
    android:text="@string/what_is_your_name"
    android:textSize="30sp"
    android:textStyle="bold"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

<EditText
    android:id="@+id/editText"
    android:layout_width="371dp"
    android:layout_height="44dp"
    android:layout_marginStart="16dp"
    android:layout_marginLeft="16dp"
    android:layout_marginTop="75dp"
    android:layout_marginEnd="16dp"
    android:layout_marginRight="16dp"
    android:layout_marginBottom="536dp"
    android:ems="10"
    android:hint="@string/name_please"
    android:inputType="textPersonName"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/textView"
    android:autofillHints="" tools:targetApi="o" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="162dp"
    android:layout_marginLeft="162dp"
    android:layout_marginTop="67dp"
    android:layout_marginEnd="160dp"
    android:layout_marginRight="160dp"
    android:layout_marginBottom="421dp"
    android:text="@string/click_me"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText" />

</android.support.constraint.ConstraintLayout>
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

2 Answers2

0

You have to add the onClick attribute in your xml. Add:

android:onClick="me"

to your Button. So your Button should look like:

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginStart="162dp"
    android:layout_marginLeft="162dp"
    android:layout_marginTop="67dp"
    android:layout_marginEnd="160dp"
    android:layout_marginRight="160dp"
    android:layout_marginBottom="421dp"
    android:text="@string/click_me"
    android:onClick="me"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/editText" />
Nongthonbam Tonthoi
  • 12,667
  • 7
  • 37
  • 64
-1

You need to write your own codes in onCreate metod

 public class MainActivity extends AppCompatActivity {


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

    Button btn = findViewById(R.id.button);

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

            Log.i("Info","Button pressed");
            Toast.makeText(this, "Hello! " + editText.getText().toString(), 
            Toast.LENGTH_LONG).show();
        }
    }
}

}