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.
Related
I'm creating a Layout programmatically consisting of a RelativeLayout and some TextViews. I want them to be placed like two columns like following example:
textview0 textview1
textview2 textview3
textview4 textview5
and the following LayoutParameters is applied to them:
width: WRAP_CONTENT
height: WRAP_CONTENT
and rules for example textview4:
RelativeLayout.BELOW, textview3.getId()
RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE
and rules for textview5:
RelativeLayout.RIGHT_OF, textview4.getId()
RelativeLayout.ALIGN_BASELINE, textview4.getId()
This ensures that the contents in column to the right can be wrapped but textviews below it is still showing fine, as it's placed below the textview that could be wrapped.
Well, when I try this out I get it to work for the first two rows of textviews, they are placed as they should, but then I add a third one and that one get placed on the same row as it should be placed below. And I cant figure out what's wrong.
RelativeLayout rl = new RelativeLayout(context);
rl.setPadding(5, 5, 5, 5);
rl.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
RelativeLayout.LayoutParams lp0 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp0.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView0.setLayoutParams(lp0);
rl.addView(textView0);
RelativeLayout.LayoutParams lp1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp1.addRule(RelativeLayout.RIGHT_OF, textView0.getId());
lp1.addRule(RelativeLayout.ALIGN_BASELINE, textView0.getId());
lp1.setMargins(5, 0, 0, 0);
textView1.setLayoutParams(lp1);
rl.addView(textView1);
RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp2.addRule(RelativeLayout.BELOW, textView1.getId());
lp2.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView2.setLayoutParams(lp2);
rl.addView(textView2);
RelativeLayout.LayoutParams lp3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp3.addRule(RelativeLayout.RIGHT_OF, textView2.getId());
lp3.addRule(RelativeLayout.ALIGN_BASELINE, textView2.getId());
lp3.setMargins(5, 0, 0, 0);
textView3.setLayoutParams(lp3);
rl.addView(textView3);
RelativeLayout.LayoutParams lp4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp4.addRule(RelativeLayout.BELOW, textView3.getId());
lp4.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
textView4.setLayoutParams(lp4);
rl.addView(textView4);
RelativeLayout.LayoutParams lp5 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp5.addRule(RelativeLayout.RIGHT_OF, textView4.getId());
lp5.addRule(RelativeLayout.ALIGN_BASELINE, textView4.getId());
lp5.setMargins(5, 0, 0, 0);
textView5.setLayoutParams(lp5);
rl.addView(textView5);
So the example code above results in textView4 being placed above textView2 even though it should be placed below it, ruled by LayoutParameters for textView3. I can't see anything wrong with this code, and I have debugged it to ensure that the ID's was unique among the textviews and they are.
Any suggestions, I'm kind of stuck!
first I created a relative layout and insert the first text view and its icon
RelativeLayout relativeLayout = new RelativeLayout(getActivity());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
TextView tv = new TextView(getActivity());
tv.setText("Test");
tv.setCompoundDrawablesWithIntrinsicBounds(0,
0,
R.drawable.rsz_page_pic,
0);
tv.setCompoundDrawablePadding(20);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setLayoutParams(rlp);
relativeLayout.setBackgroundColor(Color.WHITE);
relativeLayout.addView(tv);
It's work fine with me now I want to add textview below the textview that I created above so please can any one help me I'm really sorry for my bade English and I hope you understand what I want
you can addRule on LayoutParam for RelativeLayout
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
TextView tv = new TextView(getActivity());
tv.setText("Test");
tv.setCompoundDrawablesWithIntrinsicBounds(0,
0,
R.drawable.rsz_page_pic,
0);
tv.setCompoundDrawablePadding(20);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setLayoutParams(rlp);
relativeLayout.setBackgroundColor(Color.WHITE);
relativeLayout.addView(tv);
How to add more than one relative layout to the linear layout programmatically .
I tried but it gives me this exception "Specified child already has a parent. You must call removeview()"
Here is my code,
RelativeLayout addlangmid = new RelativeLayout(mActivity);
addlangmid.setBackgroundResource(R.drawable.language_bg_top);
TextView langname = new TextView(mActivity);
LayoutParams params = new LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params.setMargins(20, 0, 30, 30);
langname.setText(str[0]);
addlangmid.addView(langname, params);
fulllay.addView(addlangmid, 0);
RelativeLayout addlangmid1 = new RelativeLayout(mActivity);
addlangmid.setBackgroundResource(R.drawable.language_bg_middle);
TextView langname1 = new TextView(mActivity);
LayoutParams params1 = new LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params1.setMargins(20, 0, 30, 30);
langname1.setText(str[0]);
addlangmid1.addView(langname, params1);
fulllay.addView(addlangmid1, 1);
Change the below line
addlangmid1.addView(langname, params1);
to
addlangmid1.addView(langname1, params1);
You are adding langname to addlangmid and addlangmid1. Hence the error is coming.
I have created 2 buttons in a relative layout programmatically. I want no margin space between the buttons and tried to achieve this by using 'setmargin' but failed. Below is the code.
//creating the relative layout
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT);
//creating buttons
Button button1 = new Button(this);
button1.setId(R.id.button1);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(
SCR_W/2,
RelativeLayout.LayoutParams.WRAP_CONTENT);//SCR_W is the device screenwidth
params1.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params1.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params1.setMargins(0, 0, 0, 0);
button1.setLayoutParams(params1);
relativeLayout.addView(button1);
Button button2 = new Button(this);
button2.setId(R.id.button2);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(
SCR_W/2,
RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params2.addRule(RelativeLayout.BELOW, button1.getId());
params2.setMargins(0, 0, 0, 0);
button2.setLayoutParams(params2);
relativeLayout.addView(button2);
//setting the relative layout as the contentview
setContentView(relativeLayout, rlp);
Thanks in advance..
I tried your code, and works ok to me,
The two buttons are at distance 0. To check it, simply set different color to each button, and you will see that they are together.
button1.setBackgroundColor(Color.BLUE);
button2.setBackgroundColor(Color.RED);
Try setting the height of the relative layout to wrap_content..
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Just try something like this, it will work -:
params1.setMargins(0, 0, 0, -5);
params2.setMargins(0, -5, 0, 0);
I have a image placed in the relative layout. How can i place a small image over the background image and two text view side by side next to the image. Any help will be really appreciated. The only thing is I want to do this programmatically. I have tried doing it the following way but for some reason the textview appears on the top left of the screen. I want it appear on the imagebutton.
LinearLayout LinearLayout = new LinearLayout(this);
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams relativeLayoutParams;
Button[] btn = new Button[1];
btn[0] = new Button(this);
btn[0].setId(1);
btn[0].setGravity(Gravity.CENTER);
btn[0].setText("text");
btn[0].setBackgroundResource(R.drawable.button_grey);
btn[0].setTextColor(Color.parseColor("#FFFFFF"));
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT | RelativeLayout.ALIGN_PARENT_TOP);
relativeLayoutParams.setMargins(20, 20, 15, 15);
relativeLayout.addView(btn[0], relativeLayoutParams);
ImageButton[] Imgbtn = new ImageButton[10];
Imgbtn[0] = new ImageButton(this);
Imgbtn[0].setId(2);
Imgbtn[0].setBackgroundResource(R.drawable.box_round_corners);
Imgbtn[0].setMaxHeight(200);
Imgbtn[0].setClickable(true);
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayoutParams.addRule(RelativeLayout.BELOW,btn[0].getId());
relativeLayoutParams.height = 100;
relativeLayoutParams.setMargins(15, 0, 15, 10);
Imgbtn[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(Imgbtn[0], relativeLayoutParams);
TextView[] txtview = new TextView[10];
txtview[0] = new TextView(this);
txtview[0].setId(3);
txtview[0].setTextColor(Color.parseColor("#000000"));
txtview[0].setText("text");
relativeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
// relativeLayoutParams.addRule(RelativeLayout.ALIGN_LEFT | RelativeLayout.ALIGN_TOP);
relativeLayoutParams.addRule(RelativeLayout.BELOW,Imgbtn[0].getId());
relativeLayoutParams.height = 20;
relativeLayoutParams.setMargins(5, 5, 5, 5);
txtview[0].setLayoutParams(relativeLayoutParams);
relativeLayout.addView(txtview[0], relativeLayoutParams);
android.widget.ScrollView ScrollV = new ScrollView(this);
ScrollV.addView( relativeLayout, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addContentView(ScrollV, new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
try to set your views LayoutParams with setLayoutParams() method;
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(width,height);
lp.topMargin=//what is your desired y coordinate
lp.leftMargin=//what is your desired x coordinate
lp.gravity=Gravity.NO_GRAVITY;
view.setLayoutParams(lp);
FrameLayout is better choise for overlaying purposes in my opinion.