0

I have this code:

final RelativeLayout headerRl=new RelativeLayout(SpeakersActivity.this);
            headerRl.setBackgroundColor(Color.parseColor(almacen.getColor()));
            final TextView initial=new TextView(SpeakersActivity.this);
            final RelativeLayout.LayoutParams headerParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80);
            headerParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
            initial.setText(arrayInitials.get(i));
            initial.setTextColor(getColor(R.color.white));
            initial.setTextSize(16);
            initial.setPadding(10,0,0,0);
            runOnUiThread(new Runnable() {
                @Override
                public void run() {
                    headerRl.addView(initial,headerParams);
                    ll.addView(headerRl);
                }
            });

"ll" is a LinearLayout. I am adding the rule on a RelativeLayout.LayoutParams, I am adding the view to a RelativeLayout, which is added to a LinearLayout. But my view is not centered vertically. I tried to center it horizontally to see if it was working, but it isn't. Why is my view not centered in the RL? How to center it?

Thank you.

Fustigador
  • 6,339
  • 12
  • 59
  • 115

3 Answers3

1

Depending on what result you are expecting There are 2 ways to deal with your problem.

First one: The issue is in the headerRl which is set by default as wrap_content, wrap_content So, what you need is to define it's layout params with match_parent:

headerRlLayoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT) 
headerRl.setLayoutParams(headerRlLayoutParams)

The first parameter could be wrap_content, depends on what you want, but the second one should be match_parent, otherwise, it will be wrap_content by default which will position your view at the top

Create headerParams as headerParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT... And that's it. Match parent here causes your view to take the whole parent's width. If that's what you want, then see the second option below

Second one: If you want your view as match_parent and take the whole view, then your issue is: new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT Which cause your text view to be match_parent in the layout. And because you didn't set view's gravity - text has default one - top/start.

And the solution is simple - Set TextView Gravity (which same as setting gravity param in the XML) - the view will stay as match_parent - taking the whole width of its parent's layout but the text will be centered vertically

initial.setGravity(Gravity.CENTER_VERTICAL);

And a small hint - You also can set in developer options "show layout bounds" to see how much space your layout takes and how views are positioned them self in the layout. This could help to debug and understand what exactly is going wrong

Vovchik
  • 244
  • 1
  • 6
0

Try with

 final RelativeLayout.LayoutParams headerParams=new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 80);

/**
 * Rule that centers the child vertically with respect to the
 * bounds of its RelativeLayout parent.
 */           
 headerParams.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);

 // your textview
 initial.setGravity(Gravity.CENTER_VERTICAL);
Rakhi Dhavale
  • 1,196
  • 12
  • 19
  • If I do that, it will be centered vertically and horizontally. I only want it centered vertically. – Fustigador Apr 03 '19 at 07:17
  • Then you should probably set gravity as `Gravity.CENTER_VERTICAL` for your textView since `Gravity.CENTER` centers it both horizontally & vertically – Rakhi Dhavale Apr 03 '19 at 13:00
0
initial.setGravity(Gravity.CENTER_VERTICAL);
fancyyou
  • 965
  • 6
  • 7