Thakur Karthik
Thakur Karthik

Reputation: 3518

Intent is not calling when button is pressed

I have the following piece of codes.I am calling a second activity from main activity.Whenever the send button is pressed i want a toast to show button is pressed and start the activity.But due to some context problems only toast is appearing.Please correct the context for intent and give some clear explaination about these contexts.

MainActivity.java

public class MainActivity extends AppCompatActivity {

public final static String EXTRA_MESSAGE="com.example.iamka.androiddevelop.MESSAGE";
public void Toast1(String s){
    Toast.makeText(this,s+" is called",Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Log.d("My app","onCreate is called");
    Toast1("onCreate");
    Button btn=(Button)findViewById(R.id.button);
    btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("My app","Button is pressed");
            Toast.makeText(MainActivity.this,"Button pressed",Toast.LENGTH_SHORT).show();
        }
    });
}
public void sendMessage(View view) {
    // Do something in response to button
    Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    Log.i("intent","intent is started");
    startActivity(intent);
}
}

DisplayMessageActivity.java

public class DisplayMessageActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_display_message);
    // Get the Intent that started this activity and extract the string
    Intent intent = getIntent();
    String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);

    // Capture the layout's TextView and set the string as its text
    TextView textView =(TextView) findViewById(R.id.textView);
    Log.i("intent","displaymessage");
    textView.setText(message);
}
}

activity_main.xml

<EditText
    android:id="@+id/editText"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:ems="10"
    android:hint="@string/edit_message"
    android:inputType="textPersonName"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    app:layout_constraintRight_toLeftOf="@+id/button"
    app:layout_constraintHorizontal_chainStyle="spread" />

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="16dp"
    android:onClick="sendMessage"
    android:text="@string/button_send"
    app:layout_constraintBaseline_toBaselineOf="@+id/editText"
    app:layout_constraintLeft_toRightOf="@+id/editText"
    app:layout_constraintRight_toRightOf="parent" />

activity_display_message.xml

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:text="TextView"
    app:layout_constraintLeft_toLeftOf="parent"
    app:layout_constraintRight_toRightOf="parent"
    app:layout_constraintTop_toTopOf="parent" />

When i remove the onClickListener() method then the intent is working.

Upvotes: 0

Views: 94

Answers (3)

Saurav Mir
Saurav Mir

Reputation: 144

Just copy paste the following MainActivity code and it will do the work. The xmls are loaded first and then on runtime you are setting the onclick listener to the button again. so the xmls onclick has been replaced by your onclick listener in java code. Hence you get toast but the sendMessage() is never called

   public class MainActivity extends AppCompatActivity {

   public final static String EXTRA_MESSAGE="com.example.iamka.androiddevelop.MESSAGE";
public void Toast1(String s){
Toast.makeText(this,s+" is called",Toast.LENGTH_SHORT).show();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Log.d("My app","onCreate is called");
Toast1("onCreate");
Button btn=(Button)findViewById(R.id.button);

}
public void sendMessage(View view) {
// Do something in response to button
Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
EditText editText = (EditText) findViewById(R.id.editText);
String message = editText.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
Log.i("intent","intent is started");
startActivity(intent);
}
} 

Upvotes: -1

AxelH
AxelH

Reputation: 14572

Since you are setting your own View.OnClickListener, you are removing the one from the XML definition. Button only support one View.OnClickListener. First, the XML will create one from the android:onclick attribute like :

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

Then you are setting yours with the Toast. The button will only keep the last one, so the Intent is never send.

Solutions :

  • call the sendMessage method in your listener

Like:

btn.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Log.d("My app","Button is pressed");
        Toast.makeText(MainActivity.this,"Button pressed",Toast.LENGTH_SHORT).show();
        sendMessage(v); //Or anywhere in that method, your call.
    }
});
  • remove the listener to keep the one create by the android:onclick.

FYI:

Usually, a set### methods means this is not supporting multiple values, add### methods do.

Also, you can check at android- multi onClick listener in one button to implement your own multi listener button if you like. But I didn't check if there were some more up to date...

Upvotes: 2

Naveen Kumar M
Naveen Kumar M

Reputation: 7557

Just remove android:onClick="sendMessage" for button and try. Either you have to set click listener in xml or in class file. Both it won't work

<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="16dp"
    android:text="@string/button_send"
    app:layout_constraintBaseline_toBaselineOf="@+id/editText"
    app:layout_constraintLeft_toRightOf="@+id/editText"
    app:layout_constraintRight_toRightOf="parent" />

Change this in your Activity :

btn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Log.d("My app","Button is pressed");
            Toast.makeText(MainActivity.this,"Button pressed",Toast.LENGTH_SHORT).show();
            sendMessage();
        }
    });

private void sendMessage() {
    // Do something in response to button
    Intent intent = new Intent(MainActivity.this, DisplayMessageActivity.class);
    EditText editText = (EditText) findViewById(R.id.editText);
    String message = editText.getText().toString();
    intent.putExtra(EXTRA_MESSAGE, message);
    Log.i("intent","intent is started");
    startActivity(intent);
}

Upvotes: 2

Related Questions