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);
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!
I hope you can help. So what i need to do is to have edittext below textview (Very well explained in the picture)
(Picture)
Code:
for(int x=0 ; x < loopCount; x++){
TableRow.LayoutParams params = new TableRow.LayoutParams( headerCellsWidth[x+1],LayoutParams.MATCH_PARENT);
params.setMargins(0, 0, 0, 0); //left? up? right? down?
TextView tv1=new TextView(getContext());
EditText et1=new EditText(getContext());
et1.setHeight(40);
et1.setWidth(40);
tv1.setText("Text");
TextView textViewB = this.bodyTextView(info[x]);
taleRowForTableD.addView(tv1,params);
taleRowForTableD.addView(et1);
}
I hope you can help me out :)
Here is full example http://www.codeofaninja.com/2013/08/android-scroll-table-fixed-header-column.html
Try keeping your EditText and TextView in a RelativeLayout, and then put that RelativeLayout in TableRow's cell.
RelativeLayout layout = new RelativeLayout(this);
TextView tv = new TextView(this);
tv.setText("My text");
EditText et = new EditText(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, tv.getId());
layout.addView(tv);
layout.addView(et, lp);
taleRowForTableD.addView(layout);
Hope it helps.
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.
I want something like this programmatically:
view1 | view2
view3 | view4
----------------
view1 | view2
view3 | view4
----------------
view1 | view2
view3 | view4
---------------
...........
......
which keeps repeating
--------------
I don't want to use ListView.
Important: Remember to set the ID for each view.
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
RelativeLayout.LayoutParams params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
tv1.setId(1);
tv1.setText("textView1");
TextView tv2 = new TextView(this);
params2.addRule(RelativeLayout.RIGHT_OF, tv1.getId());
tv2.setId(2);
tv2.setText("textView2");
TextView tv3 = new TextView(this);
params3.addRule(RelativeLayout.BELOW, tv1.getId());
tv3.setId(3);
tv3.setText("textView3");
TextView tv4 = new TextView(this);
params4.addRule(RelativeLayout.RIGHT_OF, tv3.getId());
params4.addRule(RelativeLayout.ALIGN_BOTTOM, tv3.getId());
tv4.setId(4);
tv4.setText("textView4");
layout.addView(tv1, params1);
layout.addView(tv2, params2);
layout.addView(tv3, params3);
layout.addView(tv4, params4);
Instead of using multiple layout params as suggested in #Askilondz answer, you can use addRule and removeRule as below:
RelativeLayout rl = new RelativeLayout(this.getContext());
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
TextView tv1 = new TextView(this);
tv1.setId(View.generateViewId());
tv1.setText("textView1");
TextView tv2 = new TextView(this);
tv2.setId(View.generateViewId());
tv2.setText("textView2");
lp.addRule(RelativeLayout.BELOW, tv1.getId());
rl.addView(tv2, lp);
lp.removeRule(RelativeLayout.BELOW);
.
.
.
If you are using SDK < 17, you have to create id's.xml file, as values -> ids.xml with structure like:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="tv1" type="id" />
<item name="tv2" type="id" />
</resources>
and set them in your code as:
tv1.setId(R.id.tv1);
tv2.setId(R.id.tv2);
UPDATE
It looks the Adding multiple rules for the same LayoutParams not working fine, anyhow, I'll keep my answer in case someone fine something usefull in it for him
ImageView imageView = new ImageView(this);
// set the Drawable on the ImageView
imageView.setImageDrawable(bmd);
// center the Image
imageView.setScaleType(ScaleType.CENTER);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
I have been searching everywhere for an answer to this question. I'm new to Android and attempting to add items to a relative layout programmatically through java instead of xml. I have created a test class to try it out but the items keep stacking instead of formatting correctly. I simply want one TextView under the other for now (eventually I will use the left of and right of parameters but I am starting simple. What am I missing?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
RelativeLayout ll = new RelativeLayout(this);
ll.setId(99);
sv.addView(ll);
TextView tv = new TextView(this);
tv.setText("txt1");
tv.setId(1);
TextView tv2 = new TextView(this);
tv2.setText("txt2");
tv2.setId(2);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
ll.addView(tv, lay);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
ll.addView(tv2, p); this.setContentView(sv);};
p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
This line means that the the bottom of tv2 is aligned with the bottom of tv- in other words, they will cover each other up. The property you want is presumably RelativeLayout.BELOW . However, I strongly recommend using xml for this instead.
Use:
p.addRule(RelativeLayout.BELOW, tv.getId());
you are missing various things, first the ScrollView has no measures, set it with LayoutParams.FILL_PARENT or WRAP_CONTENT, second; it TextView1 are misplaced so TextView2, set TextView1 position with lay.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ScrollView sv = new ScrollView(this);
RelativeLayout ll = new RelativeLayout(this);
ll.setId(99);
sv.addView(ll, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
TextView tv = new TextView(this);
tv.setText("txt1");
tv.setId(1);
TextView tv2 = new TextView(this);
tv2.setText("txt2");
tv2.setId(2);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
ll.addView(tv, lay);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.ALIGN_BOTTOM, tv.getId());
ll.addView(tv2, p);
this.setContentView(sv);};