Question

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

enter image description here

(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 :

enter image description here

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.

Was it helpful?

Solution

Try this:

RelativeLayout toastLayout = new RelativeLayout(this);
    RelativeLayout.LayoutParams llp = new RelativeLayout.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 !");

    RelativeLayout.LayoutParams toastTextLp = new RelativeLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    toastTextLp.addRule(RelativeLayout.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);

OTHER TIPS

Try this...

LinearLayout.LayoutParams paramsOfAnim = new LinearLayout.LayoutParams(
            LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
    paramsOfAnim.gravity = Gravity.center_horizontal;
    paramsOfAnim.topMargin = 85;
  • Let's take that A contains B and B contains C. You set B.layoutParams for describing the placement of B in the A, not for placement of items inside B, such as C.
  • The other thing is that layout parameters are complex enough, it is difficult not to forget something, and you don't need really to set all parameters by hand. So, it is better to read them, change and set back.

As here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top