1

I have an app, which shows toast below the actionbar. I'm using Crouton library for displaying toast. Here I'm placing custom layout for toast, that layout contains one textview and one button (for close option). So Toast appears with button. If I click that it should close the toast but nothing happens.. Below Code for this example. Any Help Appreciated

MainActivity.java

public class MainActivity extends SherlockActivity implements OnClickListener {

private View customToastView;
private TextView customToastMessageView;
private Button customToastCloseButton;
private Button doneButton;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    doneButton = (Button) findViewById(R.id.done_button);
    customToastView = getLayoutInflater().inflate(R.layout.toast_custom_layout, null);
    customToastMessageView = (TextView) customToastView.findViewById(R.id.messages);
    customToastCloseButton = (Button) customToastView.findViewById(R.id.close_button);
    customToastCloseButton.setOnClickListener(this);
    doneButton.setOnClickListener(this);
}

@Override
public void onClick(View v) {

    switch (v.getId()) {
        case R.id.done_button:
            Crouton.cancelAllCroutons();
            customToastMessageView.setText("Message");
            Crouton.show(this, customToastView);
            break;
        case R.id.close_button:
            Crouton.cancelAllCroutons();
            break;
      }
  }
}

toast_custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent" >

    <TextView
        android:id="@+id/messages"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_marginLeft="26dp"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/close_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginRight="23dp"
        android:text="Button" />

</RelativeLayout>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >

<Button
    android:id="@+id/done_button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentTop="true"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="146dp"
    android:text="Button" />

</RelativeLayout>
Charles
  • 50,943
  • 13
  • 104
  • 142
RajeshVijayakumar
  • 10,281
  • 11
  • 57
  • 84
  • The "crouton" tag is about the Cruton library for Android. Please review the tag wiki. We do not need two new tags in its place. – Charles Mar 08 '13 at 21:14
  • Have you stepped into the onClick method and are sure, that your case `R.id.close_button` is being executed? – Ben Weiss Mar 25 '13 at 09:37
  • Which version of Crouton are you using? Also: The method `Crouton.hide(Crouton)` is a better way of hiding a single Crouton. – Ben Weiss Apr 25 '13 at 09:48
  • Crouton.hide(Crouton).. while using this statement its throws an error saying expression expected? – user2511882 Jan 06 '14 at 18:27

0 Answers0