Issue with programmatically trying to set a FrameLayout for Android

Viewed 364

I am able to successfully a achieve this doing so with the XML layout as seen in the image below:

enter image description here

Then when trying to do so within Java I am not able to get the same result as seen in the screen shot:

enter image description here

Here is the Java code I have that renders the result above:

FrameLayout frameLayout = new FrameLayout(this.context);
FrameLayout.LayoutParams layoutparams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL|Gravity.CENTER_VERTICAL);
frameLayout.setLayoutParams(layoutparams);


ImageView bodyImageView = new ImageView(this.context);
bodyImageView.setBackgroundResource(R.drawable.niners);


TextView bodyTextView = new TextView(this.context);
bodyTextView.setBackgroundColor(Color.TRANSPARENT);
bodyTextView.setText("SF");
bodyTextView.setTextColor(Color.YELLOW);
bodyTextView.setGravity(Gravity.BOTTOM);


frameLayout.addView(bodyImageView);
frameLayout.addView(bodyTextView);

Can anyone see what I am missing or doing wrong?

UPDATE:

Based on the response below the screen shot results in the following:

enter image description here

2 Answers

You need to add the layout gravity as a LayoutParams to the TextView:

FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;

And change the gravity of the the TextView to the center:

bodyTextView.setGravity(Gravity.CENTER);

Here's the entire layout:

FrameLayout frameLayout = new FrameLayout(this.context);
FrameLayout.LayoutParams layoutparams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
        ViewGroup.LayoutParams.WRAP_CONTENT, Gravity.CENTER_HORIZONTAL | Gravity.CENTER_VERTICAL);
frameLayout.setLayoutParams(layoutparams);


ImageView bodyImageView = new ImageView(this.context);
bodyImageView.setBackgroundResource(R.drawable.niners);

TextView bodyTextView = new TextView(this.context);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
        ViewGroup.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.BOTTOM;
bodyTextView.setLayoutParams(params);
bodyTextView.setBackgroundColor(Color.GRAY);
bodyTextView.setText("SF");
bodyTextView.setTextColor(Color.YELLOW);
bodyTextView.setGravity(Gravity.CENTER);
frameLayout.addView(bodyImageView);
frameLayout.addView(bodyTextView);

setContentView(frameLayout);

I change to kotlin sorry but, test this code

       val frameLayout = FrameLayout(this)
        val layoutparams = FrameLayout.LayoutParams(
            ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT,
            Gravity.CENTER_VERTICAL or Gravity.CENTER_HORIZONTAL
        )

        frameLayout.layoutParams = layoutparams
        
        val bodyImageView = ImageView(this)
        bodyImageView.setBackgroundResource(R.drawable.ic_launcher_background)

        val params =
            FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT)
        params.gravity = Gravity.BOTTOM

        val bodyTextView = TextView(this)

        bodyTextView.text = "SF"
        bodyTextView.gravity = Gravity.CENTER
        bodyTextView.setTextColor(Color.YELLOW)

        bodyTextView.setBackgroundColor(R.color.purple_700)

        frameLayout.addView(bodyImageView)
        frameLayout.addView(bodyTextView,params)

You only have to add parameters to your view

in Java

        // Add this code
           FrameLayout.LayoutParams layoutparams =
                new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        layoutparams.gravity = Gravity.BOTTOM;

        // Add in your code for textView
        frameLayout.addView(bodyTextView,layoutparams);
Related