I'm having trouble finding exactly the syntax I need to use to set the paramters on child views of a relative layout. I have a root relative layout that I want to set 2 child textviews next to each other like this
---------- ---------
| Second | | First |
---------- ---------
So I have
public class RL extends RelativeLayout{
public RL(context){
TextView first = new TextView(this);
TextView second = new TextView(this);
first.setText('First');
first.setId(1);
second.setText('Second');
second.setId(2);
addView(first, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.ALLIGN_PARENT_RIGHT ???);
addView(first, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,
LayoutParams.ALLIGN_RIGHT_OF(first.getId()) ???);
}
}
How do I set the relative alignments?
public class RL extends RelativeLayout {
public RL(Context context) {
super(context);
TextView first = new TextView(context);
TextView second = new TextView(context);
first.setText("First");
first.setId(1);
second.setText("Second");
second.setId(2);
RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(second, lpSecond);
RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpFirst.addRule(RelativeLayout.RIGHT_OF, second.getId());
addView(first, lpFirst);
}
}
You only need ALIGN_PARENT_RIGHT if you want the right edge of the view to line up with the right edge of its parent. In this case, it would push the 'first' view off the side of the visible area!
Falmarri, you'll need to use the 'addRule(int)' method.
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lp.addRule(RIGHT_OF, first.getId());
The full list of constants that can be used for addRule can be found here:
http://developer.android.com/reference/android/widget/RelativeLayout.html
And here is the addRule method reference:
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int,%20int)
Related
In the code snippet below there's one commented line. When I uncomment that line, then the contents of LinearLayout are not displayed in a TableRow. Without setting the LayoutParams, the row displays both texts. I don't understand this behavior. I know that I can include complex views via xml files, but I'd rather understand what's wrong with this code:
TableLayout tableLayout = (TableLayout) findViewById(R.id.table);
TableRow tableRow = new TableRow(this );
tableRow.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(TableRow.LayoutParams.MATCH_PARENT, TableRow.LayoutParams.MATCH_PARENT);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.HORIZONTAL);
// when I comment out this line, the row only shows the second text.
// linearLayout.setLayoutParams(layoutParams);
TextView textLabel = new TextView(this);
textLabel.setText("inside linear layout");
linearLayout.addView(textLabel);
TextView message = new TextView(this);
message.setText( "inside tablerow");
tableRow.addView(linearLayout);
tableRow.addView(message);
tableLayout.addView(tableRow);
Assuming the question is something like "What's the issue of this? How to resolve this?", here's my answer:
When you are setting LayoutParams to a View, those params would be used by the parent of this View to layout that View appropriately. So in your case what you have done is following:
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(...);
linearLayout.setLayoutParams(layoutParams);
tableRow.addView(linearLayout);
Now, the tableRow is confused, because it expects TableRow.LayoutParams in order to layout the view appropriately, but it suddenly finds out some other layout params. Whereas, had you not explicitly specified params (i.e. when linearLayout.setLayoutParams() is commented out), the default layout params would be generated.
#Override
protected LinearLayout.LayoutParams generateDefaultLayoutParams() {
return new LayoutParams(); // this is TableRow.LayoutParams
}
So, instead of creating LinearLayout.LayoutParams, create TableRow.LayoutParams:
TableRow.LayoutParams layoutParams = new TableRow.LayoutParams(...);
linearLayout.setLayoutParams(layoutParams);
tableRow.addView(linearLayout);
Trying to dynamically add a textView and a button into a LinearLayout, then add this layout into a main layout that setContentView use
So its something like this
T - text view
B - button
Ltb - layout that contains T and B
Lm - Main layout that contains Ltb
Then use this.setContentView(Lm) to show to result
Roles:
T must be on the left.
B must be on right of the screen within the layout
All element above are declared dynamically, without using layout xml
Actual result:
Display fine. but when I type in text that is longer than the screen width, the Button got pushed outside of the screen and gone.
Problem, is it something my dynamic layout doing wrong ?
Code here:
public SearchBar(Context c){
et=new EditText(c);
bt=new Button(c);
et.setHint("added et");
bt.setText("added btn");
ll=new LinearLayout(c);
setLinearLayout();
et.setLayoutParams(flowLeft());
bt.setLayoutParams(flowRight());
ll.addView(et);
ll.addView(bt);
}
private RelativeLayout.LayoutParams flowRight(){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
//params.weight = 1.0f;
//params.gravity=Gravity.RIGHT;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
return params;
}
private RelativeLayout.LayoutParams flowLeft(){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT);
//params.weight = 1.0f;
//params.gravity=Gravity.RIGHT;
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
return params;
}
private void setLinearLayout(){
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ll.setOrientation(LinearLayout.HORIZONTAL);
ll.setLayoutParams(params);
}
// try this way, hope this will help you...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ltb = new LinearLayout(this);
TextView T = new TextView(this);
Button B =new Button(this);
LinearLayout.LayoutParams ltbParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
LinearLayout.LayoutParams TParms = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT,1f);
LinearLayout.LayoutParams BParms = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
T.setLayoutParams(TParms);
B.setLayoutParams(BParms);
T.setText("Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text Demo Text");
B.setText("Button");
ltb.addView(T);
ltb.addView(B);
ltb.setLayoutParams(ltbParams);
setContentView(ltb);
}
Do like this. This may help you.
Ltb.setWeightSum(100);
Ltb.setOrientation(LinearLayout.HORIZONTAL);
T.setLayoutParams(new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,50));
B.setLayoutParams(new LinearLayout.LayoutParams(0,LayoutParams.MATCH_PARENT,50));
I have done some research, but the answer i found does not work for me. Here is some part of my code. the R.id.relative is the id of the relativelayout in the xml file
RelativeLayout RL = (RelativeLayout) findViewById(R.id.relative);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView title = new TextView(this);
title.setText(" History ");
title.setId(99099);
title.setTextSize(30);
title.setGravity(Gravity.CENTER);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
title.setLayoutParams(params);
RL.addView(title);
RelativeLayout.LayoutParams test_params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test = new Button(this);
test.setText(" Back ");
test_params.addRule(RelativeLayout.BELOW,99099);
test.setId(199291);
test.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params);
RL.addView(test);
RelativeLayout.LayoutParams test_params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test2 = new Button(this);
test2.setText(" Clear ");
test_params2.addRule(RelativeLayout.BELOW,test.getId());
test2.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params2);
RL.addView(test2);
all 3 items did show up, but they stack together. I can't get them below another.
Could anyone help ?
From what I've been able to find out, you have to add the view using LayoutParams. Here's an example:
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
parentView.addView(linearLayout, relativeParams);
And to relatively position your items programmatically you have to assign ids to them, this stops them from 'overlapping'.
TextView tv1 = new TextView(this);
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setId(2);
Then addRule(RelativeLayout.RIGHT_OF, tv1.getId());
Change test.setLayoutParams(test_params2); to test2.setLayoutParams(test_params2);
Explanation: You have inadvertently set the layout params of test a second time, with params that instruct it to be below itself, which I guess just puts it top-left (default placement in a RelativeLayout). Since you never give test2 any layout params, it also gets default placement. So everything is at the top and thus appear atop each other.
Incidentally, if you just want them arranged linearly, why not use a vertical LinearLayout?
The rule you have added is causing your view to be stacked together.If you need to add it should be used by the following rule.
test_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
Next, add the view to your RelativeLayout with your LayoutParams:
RL.addView(yourAdView, rLParams);
same goes for the each cases.try to run.It will solve your problem
I'd like to insert a pause button and a Highscore text for my game with the pause button at the upper left of the gameview and the highscore text at the upper right. I'd like to code it programmaticaly, but the highscore and button are not showing up. Here's the code :
gameView = new SFGameView(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
TextView textBox = new TextView(SFEngine.context);
textBox.setText("HIGH SCORE");
textBox.setId(1);
Button pauseButton = new Button(SFEngine.context);
pauseButton.setText("PAUSE");
pauseButton.setGravity(Gravity.RIGHT);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.RIGHT_OF, textBox.getId());
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(textBox);
layout.addView(pauseButton,lp);
layout.addView(gameView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
setContentView(layout,rlp);
here's the button listener, the button won't respond to the touch event
pauseButton.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e) {
try{
Thread.sleep(1000);
}
catch(InterruptedException ex){
}
return true;
}
});
You need to add the LayoutParams correctly. Each View needs LayoutParams of the type of its parent. E.g. your TextView and Button need RelativeLayout.LayoutParams, and your RelativeLayout needs FrameLayout.LayoutParams (as this is the built-in container for anything you add in setContentView).
For the pauseButton set the RelativeLayout.LayoutParams to WRAP_CONTENT & WRAP_CONTENT, with rules ALIGN_PARENT_TOP & ALIGN_PARENT_RIGHT.
For the textBox add another RelativeLayout.LayoutParams with WRAP_CONTENT & WRAP_CONTENT and rules ALIGN_PARENT_TOP & ALIGN_PARENT_LEFT.
Then the RelativeLayout named layout needs FrameLayout.LayoutParams with MATCH_PARENT & MATCH_PARENT.
Example code:
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
SFGameView game = new SFGameView(this);
game.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Button btn = new Button(this);
btn.setText("||");
RelativeLayout.LayoutParams btnLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnLP.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
btnLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
btn.setLayoutParams(btnLP);
TextView tv = new TextView(this);
tv.setText("Some Text");
RelativeLayout.LayoutParams textLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textLP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
textLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
tv.setLayoutParams(textLP);
layout.addView(game);
layout.addView(btn);
layout.addView(tv);
setContentView(layout);
As for the listener. Use setOnClickListener (not onTouch).
I am working to create fields at run time, like in a relative layout am adding one text field at right corner and one Check-box at the left corner.
For this am getting problem, currently i am using the following code:
ViewGroup hori_layout=new RelativeLayout(getParent());
hori_layout.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
TextView tv1=new TextView(getParent());
tv1.setText(_medContactNames[i]);
tv1.setTextColor(Color.BLACK);
CheckBox cb = new CheckBox(getApplicationContext());
hori_layout.addView(tv1);
hori_layout.addView(cb);
layout.addView(hori_layout);
*
/**
* GENERATING RELATIVE LAYOUT AT RUNTIME
* */
public class RL extends RelativeLayout {
public RL(Context context,int i,String flag) {
super(context);
//FIRST FIELD OF THE LAYOUT
TextView firstField = new TextView(context);
firstField.setTextColor(Color.BLACK);
if(flag.equalsIgnoreCase("LAW")){
firstField.setText(_lawContactNames[i]);
}else{
firstField.setText(_medContactNames[i]);
}
firstField.setId(1);
//SECOND FIELD OF THE LAYOUT
CheckBox secondField = new CheckBox(context);
secondField.setId(2);
//FIRST LAYOUT WHICH MUST BE PRESENT AT LEFT END == TEXT FIELD
RelativeLayout.LayoutParams lpSecond = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
addView(firstField, lpSecond);
//SECOND LAYOUT AT RIGHT END == CHECK BOX
RelativeLayout.LayoutParams lpFirst = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
lpFirst.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, secondField.getId());
addView(secondField, lpFirst);
}
}
*