Issue in Dynamic relative layout - android

Kindly check this one.
RelativeLayout layout = new RelativeLayout(this);
ImageView item = new ImageView(this);
item.setImageResource( R.drawable.invite );
item.setAdjustViewBounds(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_LEFT, RelativeLayout.TRUE);
item.setLayoutParams(params);
item.setId( mIconIdCounter );
layout.addView(item, params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, item.getId());
TextView tv1=new TextView(getApplicationContext());
tv1.setText("Invite");
tv1.setTextSize(15);
tv1.setId(2);
params.addRule(RelativeLayout.BELOW, item.getId());
layout.addView(tv1, params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, item.getId());
ImageView item2 = new ImageView(this);
item2.setImageResource( R.drawable.logout );
item2.setAdjustViewBounds(true);
item2.setLayoutParams(params);
item2.setId(3);
layout.addView(item2, params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, item2.getId());
params.addRule(RelativeLayout.RIGHT_OF, item.getId());
TextView tv2=new TextView(getApplicationContext());
tv2.setText("Logout");
tv2.setTextSize(15);
tv2.setId(4);
params.addRule(RelativeLayout.BELOW, item2.getId());
layout.addView(tv2, params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
ImageView item3 = new ImageView(this);
item3.setImageResource( R.drawable.fbplaceholder);
item3.setAdjustViewBounds(true);
item3.setLayoutParams(params);
item3.setId(5);
layout.addView(item3, params);
Here , i have to set item3 Imageview in the above of item imageview. But m unable to do it.
In this code item3 is overlapping item.
i have to set it in the top of item imageview.
Kindly suggest me where m going wrong.
Thanks in advance
Gaurav Gupta

as you adding the item in the last and it is relative layout where item overlaps as per params
try this
add item3 as first element
and
in item params add this params.addRule(RelativeLayout.BELOW, item3.getId());

Related

Android TextView Right Align in LinearLayout Programmatically

I am working on dynamic UI. I have Parent LinearLayout and I created another LinearLayout in it and add TextView in this LinearLayout. I want to Right Align of Screen that TextView
I tried like below
public MessageView(LinearLayout parent, Message msg) {
myParent = parent;
messageDetails = new LinearLayout(parent.getContext());
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
layoutParams.gravity = RelativeLayout.ALIGN_PARENT_RIGHT;
messageDetails.setLayoutParams(layoutParams);
senderTV = new TextView(parent.getContext());
senderTV.setTypeface(null, Typeface.ITALIC);
messageDetails.addView(senderTV);
}
How can I do it?
Try this:
messageDetails = new RelativeLayout(parent.getContext());
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
messageDetails.addView(senderTV);

Programmatically added Views not behaving

I've created many custom views and I am trying to add them to my fragment. They get added but I can't seem to get them to go where I want. There should be 2 columns and 3 rows but it ends up as 1 column with all of the custom views stacked on top of one another. Here is my code to add the views and set the layout params to the fragment layout:
RelativeLayout fm = (RelativeLayout) view.findViewById(R.id.fragmentLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
CustomImages cs = new CustomImages(getActivity());
cs.setId(R.id.one);
cs.setLayoutParams(params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.RIGHT_OF, cs.getId());
CustomImages2 cs2 = new CustomImages2(getActivity());
cs2.setId(R.id.two);
cs2.setLayoutParams(params);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params2.addRule(RelativeLayout.BELOW, cs2.getId());
CustomImages3 cs3 = new CustomImages3(getActivity());
cs3.setId(R.id.three);
cs3.setLayoutParams(params);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params3.addRule(RelativeLayout.RIGHT_OF, cs3.getId());
CustomImages4 cs4 = new CustomImages4(getActivity());
cs4.setId(R.id.four);
cs4.setLayoutParams(params);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params4.addRule(RelativeLayout.BELOW, cs4.getId());
CustomImages5 cs5 = new CustomImages5(getActivity());
cs5.setId(R.id.five);
cs5.setLayoutParams(params);
cs3.setLayoutParams(params);
cs4.setLayoutParams(params);
cs5.setLayoutParams(params);
I believe params there should be replaced with params2, params3 and params4 respectively.
UPDATE:
Also, you should specify LAYOUT_BELOW for all views which are not on top, and do it correctly:
params2.addRule(RelativeLayout.BELOW, cs.getId()); // not cs2
params3.addRule(RelativeLayout.BELOW, cs2.getId()); // add this
params4.addRule(RelativeLayout.BELOW, cs3.getId()); // not cs4

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

Adding more than one relative layout to the linear layout dynamically

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.

Programmatically add view one below other in relative layout

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

Categories

Resources