0

I have a bunch of buttons with( the texts inside are date like FRI 12 ) in the screen for user to select. I want to get the text from the selected button to pass it to another activity but still struggle.

I google it for hours and try the code like this

private String date_selected;

public void onClick(View button) {

    Button b = (Button)button;
    date_selected = b.getText().toString();
    Log.d("myTag", date_selected);
    Toast.makeText(this,date_selected,Toast.LENGTH_LONG);

}

But it doesn't work. Please help me to solve the problem, I have 3 hours left for my deadline (this is a small homework for my mobile course).

Thank you for your help.

Rostan
  • 809
  • 9
  • 25
Bad Son
  • 131
  • 2
  • 16
  • do you want to get text of the clicked button ? – Vivek Mishra Jun 11 '19 at 11:57
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) – Zoe Jun 11 '19 at 11:58
  • 1
    Are you getting any error? What does the logcat show? – Froyo Jun 11 '19 at 11:58
  • I can build and run the app but the logcat didn't show anything ... @VivekMishra yes I want to get the text of the selected button – Bad Son Jun 11 '19 at 12:01
  • The above code should crash saying **The log needs a message to print** .. but the log is null – Santanu Sur Jun 11 '19 at 12:01
  • @TrungLe Have you attached on click listener to the button? Otherwise, this method won't be called. – Froyo Jun 11 '19 at 12:01
  • @Froyo this method seems like defined in xml using `onClick` attribute – Vivek Mishra Jun 11 '19 at 12:02
  • b.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { date_selected = b.getText().toString(); Log.d("myTag", date_selected); Toast.makeText(this, dateSelected, Toast.LENGTH_LONG).show();//if not display try Toast.makeText(ACTIVITY_NAME.this, dateSelected, Toast.LENGTH_LONG).show(); } }); – Developer Jun 11 '19 at 12:21
  • Thank you everyone for helping me,my code works ok but I missed the .show() method of the toast so it did not show up. For the onClick I have defined in the xml :). Thank you all, I am very appreciated. – Bad Son Jun 11 '19 at 12:42

3 Answers3

2

You need to show your Toast by adding .show() at the end:

Toast.makeText(this, dateSelected, Toast.LENGTH_LONG).show();

Hope this helps.

Rostan
  • 809
  • 9
  • 25
  • 1
    Thank you, what a silly mistake I made, so the code in my post works well, but because I missed the .show method of Toast so it didn't show up in the screen – Bad Son Jun 11 '19 at 12:41
2

If you're not having android:onClick attribute in your XML, make sure you have following in your code :

buttonXYZ.setOnClickListener(this);

Else double check your android:onClick method name

Also, as suggested by others, call .show() after your toast

Shrey Garg
  • 1,317
  • 1
  • 7
  • 17
1

Replace your

 Toast.makeText(this,date_selected,Toast.LENGTH_LONG)

with

 Toast.makeText(this,date_selected,Toast.LENGTH_LONG).show()

, otherwise the Toast is created but never shown.

coroutineDispatcher
  • 7,718
  • 6
  • 30
  • 58