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);
}
}
*
Related
When the user inputs a word, he creates a number of Buttons equal to the length of the word. For example: if user inputs "aaaa" he will create 4 Buttons, side by side, in the first row. Then if the user enters "bb" he will create 2 Buttons, side by side, in the second row. And "ccc" he creates 3 Buttons...
Image to demonstrate:
I dynamically create a RelativeLayout, then dynamically add Buttons to that layout. And finally I add the RelativeLayout to my existing LinearLayout. But the problem is, only one Button is added per row. And my program currently looks like this:
Can someone please me fix this problem?
CODE:
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
RelativeLayout relativeLayout = new RelativeLayout(view.getContext());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length(); //the user input number of buttons
int id = 1;
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
myButton.setId(id);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
relativeLayout.addView(myButton, rlp);
id++;
}
linearLayout.addView(relativeLayout, llp);
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId());
This line says that myButton should be added to right of myButton, which doesn't make any sense.
simple way to resolve this is to use the following line instead
rlp.addRule(RelativeLayout.RIGHT_OF, myButton.getId()-1);
But this isn't the best way to do this, you should use LinearLayout with horizontal orientation instead.
The structure should be simple
Just need to add your buttons in 3 different linear layout with orientation horizontal.
Like
<Relative layout>{
<LinearLayout global container with vertical orientation >{
<LinearLayout for 'a' type buttons container with horizontal orientation>
<LinearLayout for 'b' type buttons container with horizontal orientation>
<LinearLayout for 'c' type buttons container with horizontal orientation>
}
}
You guys are right. It is much easier using a LinearLayout. For those interested
final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_bttn_words);
final LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
button_test.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
LinearLayout linearLayout2 = new LinearLayout(view.getContext());
linearLayout2.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams rlp = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
int size = enter_txt.getText().toString().length();
for (int i=0; i<size; i++)
{
Button myButton = new Button(view.getContext());
myButton.setBackgroundResource(R.drawable.button);
linearLayout2.addView(myButton, rlp);
}
linearLayout.addView(linearLayout2, llp);
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 had to implement the expand/collapse(show/hide) a particular view on click of a some other view..Finally got it working but with a small problem.
I have a relative layout with two children: first is the textview and second is a linear layout.
On the click event of the textview i set the visibility(VISIBLE or GONE) of the following linear layout.
Now the problem is that after the linear layout is visible it somehow manages to hide the textview..
I tried textview.bringToFront() but it just makes the textview overlap the first row of the linearlayout ie the textview comes on top of the linear layout content.
I tried putting the textview in a linearlayout, but it makes no difference.
I tried setting the linear layout as BELOW. All in vain..
I know the textview exists because when i click the first row(which is overlapping the textview) the click event gets fired..
All i want is that no matter what my textview should be visible and the linear layout must take its position below the textview if it is visible..
EDIT
RelativeLayout wrapperlayout = new RelativeLayout(getActivity());
//wrapperlayout.setLayoutParams(new android.widget.RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT));
//-------------HANDLE---------------------------
TextView txtHeader = new TextView(getActivity());
txtHeader.setOnClickListener(new OnClickListener() {
#SuppressLint("NewApi")
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView tv = (TextView) v;
RelativeLayout rParent = (RelativeLayout) tv.getParent();
LinearLayout lInner = (LinearLayout) rParent.getChildAt(1);
if(lInner.isShown())
{
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
//tv.bringToFront();
lInner.setVisibility(View.GONE);
//lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_up));
}
else{
tv.setCompoundDrawablesWithIntrinsicBounds(R.drawable.collapse, 0, 0, 0);
//tv.bringToFront();
lInner.setVisibility(View.VISIBLE);
lInner.setTop(tv.getBottom());
//lInner.startAnimation(AnimationUtils.loadAnimation(getActivity(), R.anim.slide_down));
}
}
});
txtHeader.setText("Header");
txtHeader.setCompoundDrawablesWithIntrinsicBounds(R.drawable.expand, 0, 0, 0);
txtHeader.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
//--------------CONTENT-------------------------
LinearLayout lContent = new LinearLayout(getActivity());
lContent.setLayoutParams(new android.widget.LinearLayout.LayoutParams(android.widget.LinearLayout.LayoutParams.MATCH_PARENT, android.widget.LinearLayout.LayoutParams.WRAP_CONTENT));
lContent.setOrientation(LinearLayout.VERTICAL);
HashMap<String, String> MySet = new HashMap<String, String>();
MySet = getData();
Iterator<String>RowItr = MySet.keySet().iterator();
int rowcnt = 0;
while (RowItr.hasNext()) {
LinearLayout lRow = new LinearLayout(getActivity());
LinearLayout.LayoutParams lparams1 = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lRow.setLayoutParams(lparams1);
lRow.setOrientation(LinearLayout.HORIZONTAL);
TextView txtLbl = new TextView(getActivity());
txtLbl.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
TextView txtVal = new TextView(getActivity());
txtVal.setLayoutParams(new LinearLayout.LayoutParams(0, android.view.ViewGroup.LayoutParams.WRAP_CONTENT, 1));
String Lbl = RowItr.next();
txtLbl.setText(Lbl);
if(rowcnt % 2 != 0)
lRow.setBackgroundColor(Color.parseColor("#dbe4f0"));
else
lRow.setBackgroundColor(Color.parseColor("#ffffff"));
txtVal.setText(MySet.get(Lbl));
lRow.addView(txtLbl);
lRow.addView(txtVal);
lRow.setPadding(10, 10, 10, 10);
lContent.addView(lRow);
rowcnt++;
}
lContent.setVisibility(View.GONE);
wrapperlayout.addView(txtHeader);
wrapperlayout.addView(lContent);
RelativeLayout.LayoutParams rPARAMS = new RelativeLayout.LayoutParams(android.widget.RelativeLayout.LayoutParams.MATCH_PARENT, android.widget.RelativeLayout.LayoutParams.WRAP_CONTENT);
//rPARAMS.addRule(RelativeLayout.ABOVE, txtHeader.getId());
//rPARAMS.addRule(RelativeLayout.BELOW, lContent.getId());
wrapperlayout.setLayoutParams(rPARAMS);
well you can store your LinearLayout as an instance variable, and simply call layout.setvisibility(View.GONE); in your onClick method. Doubt theres any other solution unless you want to save the parent(the layout that both of your views are attached to) and perform findViewById inside onClick or call getChildAt(1) orso
Got a solution.. i tried setting margins..and it all worked out well.. te handle and content both are visible without hiding any other views..
Please help me to fix the code below.
I am making a mousepad view for my Android Remote Application.
public class MousePadView extends RelativeLayout {
private float scale;
// MOUSE BUTTONS LEFT CLICK, MIDDLE CLICK, RIGHT CLICK
private LinearLayout layoutMouseButtonBody;
private RelativeLayout.LayoutParams paramsMouseButtonBody;
private Button left;
private Button middle;
private Button right;
private LinearLayout.LayoutParams paramsButtons;
// BUTTONS FOR MOUSEWHEEL (UP AND DOWN)
private LinearLayout layoutWheelsBody;
private RelativeLayout.LayoutParams paramsWheelBody;
private Button up;
private Button down;
private LinearLayout.LayoutParams paramsWheelButton;
public MousePadView2(Context context, AttributeSet attrs, int defStyle) { // CONSTRUCTOR
super(context, attrs, defStyle);
scale = context.getResources().getDisplayMetrics().density; // GET SCALE FOR CONVERTING DPI TO PIXELS
// MOUSE BUTTON LAYOUT
paramsButtons = new LinearLayout.LayoutParams(DpiToPixels(0), LayoutParams.WRAP_CONTENT, 1);
left = new Button(context);
left.setText("L");
left.setLayoutParams(paramsButtons);
middle = new Button(context);
middle.setText("M");
middle.setLayoutParams(paramsButtons);
right = new Button(context);
right.setText("R");
right.setLayoutParams(paramsButtons);
paramsMouseButtonBody = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
paramsMouseButtonBody.addRule(ALIGN_PARENT_BOTTOM); // RELATIVE LAYOUT RULES
paramsMouseButtonBody.addRule(ALIGN_PARENT_LEFT); // RELATIVE LAYOUT RULES
paramsMouseButtonBody.addRule(ALIGN_PARENT_RIGHT); // RELATIVE LAYOUT RULES
layoutMouseButtonBody = new LinearLayout(context);
layoutMouseButtonBody.setBackgroundResource(android.R.drawable.bottom_bar);
// layoutMouseButtonBody.setPadding(0, DpiToPixels(4), 0, 0);
layoutMouseButtonBody.setOrientation(LinearLayout.HORIZONTAL);
layoutMouseButtonBody.setLayoutParams(paramsMouseButtonBody);
layoutMouseButtonBody.addView(left);
layoutMouseButtonBody.addView(middle);
layoutMouseButtonBody.addView(right);
// WHEELS
paramsWheelButton = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
up = new Button(context);
up.setText("U");
up.setLayoutParams(paramsWheelButton);
down = new Button(context);
down.setText("D");
down.setLayoutParams(paramsWheelButton);
paramsWheelBody = new RelativeLayout.LayoutParams(DpiToPixels(32), LayoutParams.WRAP_CONTENT);
paramsWheelBody.addRule(LEFT_OF, layoutMouseButtonBody.getId());
paramsWheelBody.addRule(ALIGN_PARENT_BOTTOM);
layoutWheelsBody = new LinearLayout(context);
layoutWheelsBody.setOrientation(LinearLayout.VERTICAL);
layoutWheelsBody.setBackgroundResource(android.R.drawable.bottom_bar);
layoutWheelsBody.setLayoutParams(paramsWheelBody);
layoutWheelsBody.addView(up);
layoutWheelsBody.addView(down);
// PARENT
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
addView(layoutMouseButtonBody); // add mousebutton layout in parent (relativelayout)
addView(layoutWheelsBody); // add mousewheel button layout in parent (relativelayout)
}
private int DpiToPixels(int dp) {
return (int)(dp * scale + 0.5f); // converting DPI to Pixels
}
}
The image on the Left is the output generated by Android SDK and The right one is the output that I want.
Please Help me.
I don't want to inflate layout from XML.
I think the only way to achieve what you're looking for is by grouping both your wheel and button layout into another layout. After that, simply align the result to the bottom of the parent.
Add something like this:
LayoutParams paramsTotal = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
paramsTotal.addRule(ALIGN_PARENT_BOTTOM);
LinearLayout layoutTotal = new LinearLayout(context);
layoutTotal.setOrientation(LinearLayout.VERTICAL);
layoutTotal.setLayoutParams(paramsTotal);
layoutTotal.addView(layoutWheelsBody);
layoutTotal.addView(layoutMouseButtonBody);
// PARENT
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
addView(layoutTotal); // add the combined layout
That should place the wheel buttons right above the mouse buttons (or vice versa: the mouse buttons directly below the wheel buttons) and the combination all the way at the bottom.
//Edit: alternatively, you could make the root layout a LinearLayout and add a dummy view with a weight of '1' at the top, which will push the other elements down to the bottom. I'd probably prefer the RelativeLayout option though.
Just out of curiousity: why not just inflate the layout? Personally I find that way more managable.
Your layout management is messed up. I suggest using this code:
// MOUSE BUTTON LAYOUT
paramsButtons = new LinearLayout.LayoutParams(DpiToPixels(0), LayoutParams.WRAP_CONTENT, 1);
left = new Button(context);
left.setText("L");
middle = new Button(context);
middle.setText("M");
right = new Button(context);
right.setText("R");
paramsMouseButtonBody = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT);
paramsMouseButtonBody.addRule(ALIGN_PARENT_BOTTOM); // RELATIVE LAYOUT RULES
layoutMouseButtonBody = new LinearLayout(context);
layoutMouseButtonBody.setBackgroundResource(android.R.drawable.bottom_bar);
// layoutMouseButtonBody.setPadding(0, DpiToPixels(4), 0, 0);
layoutMouseButtonBody.setOrientation(LinearLayout.HORIZONTAL);
layoutMouseButtonBody.addView(left, paramsButtons );
layoutMouseButtonBody.addView(middle, paramsButtons );
layoutMouseButtonBody.addView(right, paramsButtons );
// WHEELS
paramsWheelButton = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
up = new Button(context);
up.setText("U");
down = new Button(context);
down.setText("D");
paramsWheelBody = new RelativeLayout.LayoutParams(DpiToPixels(32), LayoutParams.WRAP_CONTENT);
paramsWheelBody.addRule(ABOVE, layoutMouseButtonBody.getId());
layoutWheelsBody = new LinearLayout(context);
layoutWheelsBody.setOrientation(LinearLayout.VERTICAL);
layoutWheelsBody.setBackgroundResource(android.R.drawable.bottom_bar);
layoutWheelsBody.addView(up, paramsWheelButton);
layoutWheelsBody.addView(down, paramsWheelButton);
// PARENT
setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
addView(layoutMouseButtonBody, paramsMouseButtonBody ); // add mousebutton layout in parent (relativelayout)
addView(layoutWheelsBody, paramsWheelBody); // add mousewheel button layout in parent (relativelayout)
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)