Search code examples
javaandroidcrouton

Crouton centering custom view and issues with disappearing


Today I changed my Toast to Crouton. But I had these problems :

1)Showing Crouton in center

2)Whenever I want to display new Crouton it is displaying previous Crouton again, though previous one hided long ago

I want to know if there is better way to solve above problems?? Cause I looked at sample and library but could not find"

Currently I'm solving these ways

activity layout:

<RelativeLayout 
    android:id="@+id/bigcontainer"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
      android:layout_width="match_parent"
    android:layout_height="match_parent"
    >





<FrameLayout 
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".DilmancSRActivity"
    tools:ignore="MergeRootFrame" />

     <RelativeLayout
    android:id="@+id/alternate_view_group"
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"   
    android:layout_centerInParent="true" 

    />
</RelativeLayout>

Creating my custom toast view and centering it horizontally:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/toast_layout_root"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    >
<LinearLayout 
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/kdrw"
    android:orientation="horizontal"
    android:padding="8dp" >

    <ImageView
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:layout_marginRight="8dp"
        android:contentDescription="@string/strnull"
        android:src="@drawable/logo" />

    <TextView
        android:id="@+id/toasttextView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:text="Large Text"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#000000" />

</LinearLayout>

</LinearLayout>

Showing Crouton:

    LayoutInflater inflater = localdsr.getLayoutInflater();
                        View layout = inflater.inflate(R.layout.toast, null); 
                        TextView text = (TextView) layout.findViewById(R.id.toasttextView1);
                        text.setText(info);  
                        if (toast != null)
                            toast.cancel();

                        Configuration croutonConfiguration=new Configuration.Builder().setDuration(1500).build();


                    toast=Crouton.make(localdsr, layout ,R.id.alternate_view_group ,croutonConfiguration); 

                    toast.show();

For solving the Second one I had to download library source and use it. .As I do not want to use animations firstly, I disabled animations

   //croutonView.startAnimation(crouton.getInAnimation());

issue on removing function that was making things appear again inside protected void removeCrouton(Crouton crouton)

  //sendMessageDelayed(crouton, Messages.DISPLAY_CROUTON, crouton.getOutAnimation().getDuration());
 //I changed it with below .plus removed crouton.getOutAnimation().getDuration() so it does not throw exception
sendMessage(crouton, Messages.DISPLAY_CROUTON);

I believe I am missing something. Hope there is already right way to do those .


Solution

  • Making changes within the library itself is not encouraged as it might create issues that only appear within your version of Crouton.

    You can change the Animation via Configuration.Builder.setInAnimation(...) and .setOutAnimation(...).

    The Configuration can be re-used and does not have to be created every time you want to show the Crouton. The same is valid for your View. It does not have to be inflated every time.

    Regarding the duplicate display of a Crouton: This behavior is not intended. The code above looks like it should display a Crouton with the info text set. If your info text has changed during the display of Croutons it might be a bug within the library. If that is the case, feel free to report it.