Programmatically add view one below other in relative layout - android

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);

Related

handling programatically created layout from xml: ids challenge

I need to be able to add a layout to my main layout from xml file many times programatically.
The problem is handling assigning ids of the new layout's views. See the code:
MainFragment.java
private int belowOfWhat = R.id.layout_header;
...
onActivityResult:
LayoutInflater myInflater = LayoutInflater.from(getContext());
RelativeLayout layout = (RelativeLayout) myInflater.inflate(R.layout.assignment_layout, null, false);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
//here problems start
//i want to position the new layout below the previously created layout
params.addRule(RelativeLayout.BELOW, belowOfWhat);
layout.setLayoutParams(params);
mRelativeLayout = rootView.findViewById(R.id.to_do_layout);
mRelativeLayout.addView(layout);
belowOfWhat = generateViewId();
idsOfToDos.add(belowOfWhat);
CheckBox assignmentCheckbox = (CheckBox)
//assignment_checkbox is an id of checkbox in the xml layout I add
rootView.findViewById(R.id.assignment_checkbox);
assignmentCheckbox.setId(belowOfWhat);
assignmentCheckbox.setText(mToDoInfo);
I don't know where the problem is, so here is how the app works now: I add a new layout, it gets positioned correctly below layout_header.
But when I add the second or more layouts they overlap each other on top of the app instead of being positioned one below the other.
I'd appreciate if you guided me to the solution of the problem.
If you don't need views'ids for other purposes you could solve more simple.
If I've understood what you are trying to do, what you need is a vertical LinearyLayout instead of a RelativeLayout, then subviews will be added one below each other
You have to use Relative Layout and use the following properties with that to align the views.
RelativeLayout.BELOW
RelativeLayout.RIGHT_OF
RelativeLayout.ALIGN_BOTTOM
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);
hope so it gives you an idea how to align views pragmatically.

set textview below textview in android programmatically

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);

Tablerow textview above edittext (Android)

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.

Trying to center buttons, TextView and Image View I’m creating Dynamically

I have a program that generates a rows of 4 buttons, in a horizontal container that gets added to a vertical container.
I cannot figure out how to have it so the buttons are centered, now they are aligns to the right hand side. Code:
for(int i=0; i<cGlobals.mNames.length; i+=2) {
iSoundIdList[i]=soundPool.load(this, cGlobals.mSounds[i], 1);
iSoundIdList[i+1]=soundPool.load(this, cGlobals.mSounds[i+1], 1);
// would like these views to all be centered
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.HORIZONTAL);
LinearLayout.LayoutParams llp = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(llp);
favBut[i]=new ImageView(this);
favBut[i].setImageResource(R.drawable.heartunselected);
favBut[i].setId(defStartFavId+i);
favBut[i].setOnClickListener(this);
layout.addView(favBut[i]);
Button but1=new Button(this);
but1.setText( cGlobals.mNames[i]);
but1.setWidth(120);
layout.addView(but1);
but1.setOnClickListener(this);
but1.setId(defStartButId+i);
TextView t=new TextView(this);
t.setText(" ");
layout.addView(t);
favBut[i+1]=new ImageView(this);
favBut[i+1].setImageResource(R.drawable.heartunselected);
favBut[i+1].setId(defStartFavId+i+1);
favBut[i+1].setOnClickListener(this);
layout.addView(favBut[i+1]);
t=new TextView(this);
t.setText(" ");
layout.addView(t);
Button but2=new Button(this);
but2.setText( cGlobals.mNames[i+1]);
but2.setWidth(120);
but2.setId(defStartButId+i+1);
but2.setOnClickListener(this);
layout.addView(but2);
Container.addView(layout);
}
Below snippet will help
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setLayoutParams(new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT));
ImageView imageView = new ImageView(this);
imageView.setImageResource(getResources().getIdentifier(
"sample_image", "drawable", getPackageName()));
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.CENTER_IN_PARENT);
imageView.setLayoutParams(lp);
relativeLayout.addView(imageView);

Programmatically adding items to a relative layout

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);};

Categories

Resources