I need to programmatically create a Toast that use an ImageView and a TextView, and that appears in the middle of the screen, and I have done it.
THIS IS THE RENDER I WANT
(The black square with the heart is the ImageView image, and the "J'aime" is the TextView)
XML Code :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/toastImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:src="@drawable/like" />
<TextView
android:id="@+id/toastText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="85dp"
android:text="J'aime !"
android:textColor="#FFFFFF" />
</FrameLayout>
THIS IS THE RENDER I HAVE with JAVA code :
JAVA CODE :
FrameLayout toastLayout = new FrameLayout(this);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
toastLayout.setLayoutParams(llp);
ImageView toastImg = new ImageView(this);
Bitmap toastBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.like);
toastImg.setImageBitmap(toastBitmap);
TextView toastText = new TextView(this);
toastText.setText("J'aime !");
LinearLayout.LayoutParams toastTextLp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);
toastText.setLayoutParams(toastTextLp);
toastLayout.addView(toastImg);
toastLayout.addView(toastText);
Toast toast = new Toast(this);
toast.setView(toastLayout);
I have tried with a relativelayout or a linearlayout, but the framelayout works better for this.
So I have a question :
What is the JAVA equivalent of :
android:layout_gravity="center_horizontal"
android:layout_marginTop="85dp"
? Because this is visibly not :
LinearLayout.LayoutParams toastTextLp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL);
toastTextLp.setMargins(0, 85, 0, 0);
Need help.