I've created many custom views and I am trying to add them to my fragment. They get added but I can't seem to get them to go where I want. There should be 2 columns and 3 rows but it ends up as 1 column with all of the custom views stacked on top of one another. Here is my code to add the views and set the layout params to the fragment layout:
RelativeLayout fm = (RelativeLayout) view.findViewById(R.id.fragmentLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
CustomImages cs = new CustomImages(getActivity());
cs.setId(R.id.one);
cs.setLayoutParams(params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.RIGHT_OF, cs.getId());
CustomImages2 cs2 = new CustomImages2(getActivity());
cs2.setId(R.id.two);
cs2.setLayoutParams(params);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params2.addRule(RelativeLayout.BELOW, cs2.getId());
CustomImages3 cs3 = new CustomImages3(getActivity());
cs3.setId(R.id.three);
cs3.setLayoutParams(params);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params3.addRule(RelativeLayout.RIGHT_OF, cs3.getId());
CustomImages4 cs4 = new CustomImages4(getActivity());
cs4.setId(R.id.four);
cs4.setLayoutParams(params);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params4.addRule(RelativeLayout.BELOW, cs4.getId());
CustomImages5 cs5 = new CustomImages5(getActivity());
cs5.setId(R.id.five);
cs5.setLayoutParams(params);
cs3.setLayoutParams(params);
cs4.setLayoutParams(params);
cs5.setLayoutParams(params);
I believe params there should be replaced with params2, params3 and params4 respectively.
UPDATE:
Also, you should specify LAYOUT_BELOW for all views which are not on top, and do it correctly:
params2.addRule(RelativeLayout.BELOW, cs.getId()); // not cs2
params3.addRule(RelativeLayout.BELOW, cs2.getId()); // add this
params4.addRule(RelativeLayout.BELOW, cs3.getId()); // not cs4
Related
Try #1: I am trying to set layout gravity for RelativeLayout programmatically.
FrameLayout frameLayout = new FrameLayout(this);
FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
frameLayout.setLayoutParams(frameLayoutParams);
setViewPaddingInDp(frameLayout, 16, 16, 16, 16);
RelativeLayout relativeLayout = new RelativeLayout(this);
LinearLayout.LayoutParams relativeLayoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.gravity = Gravity.CENTER;
relativeLayout.setLayoutParams(relativeLayoutParams);
frameLayout.addView(relativeLayout);
ImageView imageView = new ImageView(this);
setViewWidthAndHeight(imageView, 128, 192);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(R.drawable.maxwell);
relativeLayout.addView(imageView);
setContentView(frameLayout);
This is what I am getting as result:
Try #2: I have tried set CENTER_IN_PARENT programmatically, but no result
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT);
relativeLayout.setLayoutParams(relativeLayoutParams);
frameLayout.addView(relativeLayout);
Try #3: Also tried to set CENTER_HORIZONTAL, but no effect:
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
// relativeLayoutParams.addRule(RelativeLayout.CENTER_VERTICAL, RelativeLayout.TRUE);
// relativeLayoutParams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
relativeLayout.setLayoutParams(relativeLayoutParams);
frameLayout.addView(relativeLayout);
How to set layout gravity CENTER for RelativeLayout? I need to set gravity for RelativeLayout without changing FrameLayout or ImageView layout params.
Here is the fix, just verified myself. Working as required. The important part we were missing is imageViewParam.addRule(RelativeLayout.CENTER_HORIZONTAL);
FrameLayout frameLayout = new FrameLayout(this);
FrameLayout.LayoutParams frameLayoutParams = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
frameLayout.setLayoutParams(frameLayoutParams);
//setViewPaddingInDp(frameLayout, 16, 16, 16, 16);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
relativeLayoutParams.addRule(CENTER_IN_PARENT, TRUE);
relativeLayout.setLayoutParams(relativeLayoutParams);
frameLayout.addView(relativeLayout);
ImageView imageView = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.CENTER_HORIZONTAL);
imageView.setLayoutParams(params);
// params.addRule(RelativeLayout.CENTER_IN_PARENT);
// params.addRule(RelativeLayout.CENTER_VERTICAL);
// setViewWidthAndHeight(imageView, 128, 192); // if you are using this method, then set the layout param accordingly
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setImageResource(R.drawable.maxwell);
relativeLayout.addView(imageView);
setContentView(frameLayout);
Set MATCH_PARENT instead of WRAP_CONTENT to RelativeLayout like
RelativeLayout.LayoutParams relativeLayoutParams = new
RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
And set a RelativeLayout.LayoutParams to your ImageView
RelativeLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) imageView.getLayoutParams();
layoutParams.addRule(RelativeLayout.CENTER_IN_PARENT,
RelativeLayout.TRUE);
imageView.setLayoutParams(layoutParams);
I want to draw a TV remote interface on an Android Activity screen. But I don't succeed in getting the buttons to the right places. Because I'm using a grid internally to init the buttons (programmatically by the way) I thought using a GridLayout and placing the buttons as subviews should be the best way. (I was trying it with TableLayout before, but I couldn't find out how to init a button that has a height going over two or more TableRows.)
But the GridLayout class automatically resets all buttons when I change the size of one button, as a result my view looks pretty bad. Is there a way to set the exact places where my buttons should be using a GridLayout? Or would you recommend another type of Layout?
This is what I expect the whole thing to look like at the end (except the "Endgeräte auswählen" button):
try relative layout :
RelativeLayout Main = new RelativeLayout(this);
RelativeLayout.LayoutParams viewParamsCenter = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT);
Main.setLayoutParams(viewParamsCenter);
Button but = new Button(this);
but.setText("BTN");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_TOP);
viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("BTN 2");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_TOP);
viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("Center");
but.setId(998900);
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("below");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.BELOW, 998900);
viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("Center above");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.ABOVE, 998900);
viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("Center left");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.LEFT_OF, 998900);
viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("Center right");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.RIGHT_OF, 998900);
viewParamsCenter.addRule(RelativeLayout.CENTER_IN_PARENT);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
but = new Button(this);
but.setText("Bottom");
but.setBackgroundResource(R.drawable.ic_launcher);
viewParamsCenter = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
viewParamsCenter.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
but.setLayoutParams(viewParamsCenter);
Main.addView(but);
RelativeLayout Main_SUPER = (RelativeLayout) findViewById(R.id.relativeLayout);
Main_SUPER.addView(Main);
I'm having some troubles creating a relativeLayout programmatically.For better understanding I attached a picture below. The things with plus and minus are buttons, which should also be dynamically created and added to the layout. The values of tv4 and tv5 should increase/decrease accordingly to button presses.
What I have done so far:
1) creating the layout:
RelativeLayout rl = new RelativeLayout(this);
rl.setId(i);
rl.setBackgroundResource(R.drawable.bg);
RelativeLayout.LayoutParams Lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.BELOW, R.id.RL_default);
Lparams.setMargins(3, 5, 3, 0);
rl.setLayoutParams(Lparams);
2) adding the tv1:
Lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Lparams.setMargins(10, 0, 0, 0);
TextView txt = new TextView(this);
txt.setTextColor(Color.parseColor("#FFFFFF"));
txt.setId(x);
txt.setTextSize(25);
txt.setLayoutParams(Lparams);
txt.setText(name);
rl.addView(txt);
3) adding the tv2:
Lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Lparams.addRule(RelativeLayout.BELOW, txt.getId());
Lparams.setMargins(10, 0, 0, 0);
TextView txtS = new TextView(this);
txtS.setId(y);
txtS.setText("Test: ");
txtS.setTextSize(22);
txtS.setLayoutParams(Lparams);
txtS.setGravity(Gravity.BOTTOM);
txtS.setPadding(0, 0, 0, 20);
rl.addView(txtS);
4) now I want to create the first button:
Button btnSminus = new Button(this);
btnSminus.setId(btn1);
btnSminus.setText("<");
btnSminus.setTextSize(20);
Lparams= new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.RIGHT_OF, txtS.getId());
btnSminus.setLayoutParams(Lparams);
rl.addView(btnSminus);
The problem is, the the button View seems to just disappear from the screen when the line Lparams.addRule(RelativeLayout.RIGHT_OF, txtS.getId()); is executed. What can be the reason?
My guess would be that it's because you set the width of txtS to match_parent, so the button gets pushed off the screen. You can fix that by changing that to wrap_content.
Kindly check this one.
RelativeLayout layout = new RelativeLayout(this);
ImageView item = new ImageView(this);
item.setImageResource( R.drawable.invite );
item.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_LEFT, RelativeLayout.TRUE);
item.setLayoutParams(params);
item.setId( mIconIdCounter );
layout.addView(item, params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, item.getId());
TextView tv1=new TextView(getApplicationContext());
tv1.setText("Invite");
tv1.setTextSize(15);
tv1.setId(2);
params.addRule(RelativeLayout.BELOW, item.getId());
layout.addView(tv1, params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, item.getId());
ImageView item2 = new ImageView(this);
item2.setImageResource( R.drawable.logout );
item2.setAdjustViewBounds(true);
item2.setLayoutParams(params);
item2.setId(3);
layout.addView(item2, params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, item2.getId());
params.addRule(RelativeLayout.RIGHT_OF, item.getId());
TextView tv2=new TextView(getApplicationContext());
tv2.setText("Logout");
tv2.setTextSize(15);
tv2.setId(4);
params.addRule(RelativeLayout.BELOW, item2.getId());
layout.addView(tv2, params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
ImageView item3 = new ImageView(this);
item3.setImageResource( R.drawable.fbplaceholder);
item3.setAdjustViewBounds(true);
item3.setLayoutParams(params);
item3.setId(5);
layout.addView(item3, params);
Here , i have to set item3 Imageview in the above of item imageview. But m unable to do it.
In this code item3 is overlapping item.
i have to set it in the top of item imageview.
Kindly suggest me where m going wrong.
Thanks in advance
Gaurav Gupta
as you adding the item in the last and it is relative layout where item overlaps as per params
try this
add item3 as first element
and
in item params add this params.addRule(RelativeLayout.BELOW, item3.getId());
How to set android:layout_alignParentBottom="true" for a videoview, by code and not from xml?
I assume you are trying to add your VideoView to a RelativeLayout ?
RelativeLayout relativeLayout = findViewById(R.id.your_relative_layout);
mVideo = new VideoView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); // or wrap_content
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayout.addView(mVideo , layoutParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
YourVideoView.setLayoutParams(params);
get layout params from your view and add rule
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Some References are Here1 and Here2
this is what you have to do:
Create a RelativeLayout.LayoutParams object.
Use addRule(int) or addRule(int, int) to set the rules. The first method is used to add rules that don’t require values.
Set the parameters to the view (in this case, to each button).
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
yourView.setLayoutParams(params);