I want to figure out how to position views programmatically in android. Lets say for example we have this XML code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLayout">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="121dp"
android:layout_marginTop="140dp"
android:text="Results" /></RelativeLayout>
How can I achieve this layout programmatically in android? because I want to have a random position of my textview.
use the RelativeLayout.LayoutParams with the setLayoutParams Method of the TextBox
RelativeLayout.LayoutParams p = (RelativeLayout.LayoutParams)textView1.getLayoutParams();
p.leftMargin = xxx; // in PX
p.topMargin = xxx; // in PX
textView1.setLayoutParams(p)
look up dp to px conversion if u want to use dp values
check this..
RelativeLayout main = new RelativeLayout(this);
main.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
TextView textV = new TextView(this);
textV.setGravity(Gravity.LEFT);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(121, 140, 0, 0);
textV.setLayoutParams(layoutParams);
text.setText("Result ");
main .addView(text);
Related
if I use padding within a XML layout, my background image is a little bit smaller than if I do the layout programatically an use .setPadding() on my imageView.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
>
<ImageView
android:id="#+id/ivBackgroundStartUp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#000000"
android:alpha="0.30"
android:src="#drawable/start"
android:paddingLeft="15dp"
android:paddingRight="15dp"
/>
</RelativeLayout>
and programatically...
this.mContext = activity;
// RELATIVE LAYOUT
RelativeLayout rlMain = new RelativeLayout(mContext);
rlMain.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
rlMain.setBackgroundColor(Color.parseColor("#000000"));
// HINTERGRUNDBILD
ImageView ivBG = new ImageView(mContext);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
rlp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
ivBG.setImageResource(R.drawable.start);
ivBG.setAlpha(0.30f);
ivBG.setPadding(15,0,15,0);
rlMain.addView(ivBG);
both times i´d set padding to left, right / 15, 15 but the result is different.
Maybe anybody knows why...
It is because, when you give the padding programatically, you are giving it in int which is taken as pixels. And, when you are giving in XML you are giving it as dp which is Density-independent pixel. So the values are changed.
If you want it to be the same. Try something like this in the XML.
android:paddingRight="15px"
If you want to specify programatically, without changing the XML and want the output to be the same. You must convert, px to dp and then use.
Shaik MD Ashiq is right. To solve this problem programatically I have to use this code...
// BACKGROUND
ImageView ivBG = new ImageView(mContext);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
rlp.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
ivBG.setImageResource(R.drawable.start);
ivBG.setAlpha(0.30f);
float scale = mContext.getResources().getDisplayMetrics().density;
int densityPix = (int) (15 * scale + 0.5f);
ivBG.setPadding(densityPix, 0, densityPix, 0);
rlMain.addView(ivBG);
I have the following code within my layout xml file
<RelativeLayout
android:id="#+id/contentTextViewLayout"
android:layout_weight="9"
android:layout_width="fill_parent"
android:layout_height="0dip">
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
How do I fetch the value of layout_weight attribute of the RelativeLayout in my code?
Why not instead just get the width of the relative layout, then set the width of the edittext to a percentage of the layout width.
int i = relativelayout.getWidth();
int x = //any number, depending on how wide you what your text view
textview.setWidth(i/x);
You have to use RelativeLayout.LayoutParams
Try the following :
RelativeLayout r = ((RelativeLayout)(findViewById(R.id.contentTextViewLayout)));
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT);
params.weight = 1.0f;
r.setLayoutParams(params);
params.weight = 1.0f can be changed as per your desire.
Here is my code:
//define tablerows. each table row has max 3 buttons
LinearLayout llRow1 = (LinearLayout)findViewById(R.id.llRow1); llRow1.removeAllViews();
float scale = getResources().getDisplayMetrics().density;
int padding_5dp = (int) (5 * scale + 0.5f);
//Define FrameLayout
FrameLayout flTmp = new FrameLayout(this);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(FrameLayout.LayoutParams.FILL_PARENT, FrameLayout.LayoutParams.FILL_PARENT, 1f);
lp.setMargins(0, 0, padding_5dp, padding_5dp);
flTmp.setLayoutParams(lp);
//Add dynamically TextView
TextView tvTmp = new TextView(this);
tvTmp.setText("Test");
LinearLayout.LayoutParams tvPara=new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT );
tvPara.gravity = Gravity.BOTTOM | Gravity.LEFT;
tvPara.setMargins(padding_5dp, 0, 0, 0);
tvTmp.setLayoutParams(tvPara);
//Add dynamically Buttons for juice
ImageButton btnTmp = new ImageButton(this);
[...]
//Add Button and TextView to FrameLayout
flTmp.addView(btnTmp);
flTmp.addView(tvTmp);
llRow1.addView(flTmp);
What i try to do is create an imagebutton dynamically with a textview description like following xml code:
<FrameLayout>
<ImageButton android:background="#null" android:id="#+id/button_x" android:layout_width="fill_parent" android:layout_height="fill_parent" android:scaleType="fitXY" android:src="#drawable/button_graphic"></ImageButton>
<TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:clickable="false" android:text="TEST TEST"></TextView>
</FrameLayout>
works fine with my code, but the textview ignores margin and gravity parameters.
its located on upper left corner without margins.
Any hint?
The LayoutParams you use need to be of the type of parent that the child is contained in. You're FrameLayout contains the TextView, but you're assigning a LinearLayout.LayoutParams reference to it. Make it a FrameLayout.LayoutParams or change to a LinearLayout. If I had to guess, it's silently catching this error and just ignoring it.
I am trying to set the width of my LinearLayout code side. I am trying to find a way that I can size my selectedNavigationItem of my IcsSpinner to the selected item size...when I set the linearLayout in xml to a certain width, this is achieved...but I need dynamic sizing.
<LinearLayout
android:id="#+id/spinner_ll"
android:layout_width="75dp"
android:layout_height="wrap_content"
android:layout_gravity="right" >
<com.actionbarsherlock.internal.widget.IcsSpinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Is there any way to set the width of the LinearLayout, not the child?
UPDATE...ANSWER:
LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams)ll.getLayoutParams();
lp.width = 85; // set this to size...this is arbitrary number for proof of concept
ll.requestLayout();
You can set the layout parameters from code like this:
LinearLayout layout = (LinearLayout) findViewById(R.id.spinner_11);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100 /* in pixels */, LayoutParams.WRAP_CONTENT);
layout.setLayoutParams(layoutParams);
You can convert dps to pixels in code like this:
int pixelValue = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10 /* in dps */, getResources().getDisplayMetrics());
Take a look at the documentation here: https://developer.android.com/reference/android/util/TypedValue.html#applyDimension(int, float, android.util.DisplayMetrics)
I am a bit confused about this. I have an app where user draws rectangular text objects. I added some textviews in a relative layout in my xml (lets say that I have a maximum of 3 text objects). The objects can be moved and resized. I added this code for each textView
TextView tv=(TextView) findViewById(R.id.text1);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
System.out.println(texts.get(i).Sx+ " , "+texts.get(i).Sy+ " , "+ texts.get(i).Lx+ " , "+texts.get(i).Ly);
if(texts.get(i).Sx<=texts.get(i).Lx){
if(texts.get(i).Sy<=texts.get(i).Ly){
lp.setMargins((int)texts.get(i).Sx, (int)texts.get(i).Sy, (int)texts.get(i).Lx, (int)texts.get(i).Ly);
} else{
lp.setMargins((int)texts.get(i).Sx, (int)texts.get(i).Ly, (int)texts.get(i).Lx, (int)texts.get(i).Sy);
}
} else{
if(texts.get(i).Sy<=texts.get(i).Ly){
lp.setMargins((int)texts.get(i).Lx, (int)texts.get(i).Sy, (int)texts.get(i).Sx, (int)texts.get(i).Ly);
} else{
lp.setMargins((int)texts.get(i).Lx, (int)texts.get(i).Ly, (int)texts.get(i).Sx, (int)texts.get(i).Sy);
}
}
tv.setLayoutParams(lp);
tv.setWidth((int)(texts.get(i).width));
tv.setHeight((int)(texts.get(i).height));
tv.setTextSize(texts.get(i).textSize);
tv.setText(text.text);
tv.setTextColor(Color.WHITE);
tv.requestLayout();
Sx, Sy are the starting coos of the object in pixels and Lx, Ly the ending coos.
in the xml I added this:
<RelativeLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/texts" >
<TextView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/text1" />
<TextView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/text2" />
<TextView
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:id="#+id/text3" />
</RelativeLayout>
This seems to work as for in placing the text in the right place. But the width and height of the textview does not seem to be right. Is this a problem of setting the margins in pixels? Some enlightenment would be very useful right now
LayoutParams are used to align your view dynamically. You can add rule to your params like below, above, bottom, top, align, margin and all(same like you do through xml) then finally you can set this layout params to your view. E.g :
// Set the params eg. for a button.
RelativeLayout.LayoutParams button_params = new RelativeLayout.LayoutParams(RelativeLayout
.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
button_params.addRule(RelativeLayout.LEFT_OF,
any_view.getId());
button_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,
RelativeLayout.TRUE);
button_params.bottomMargin = screen_height / 15;
button_params.rightMargin = 20;
button.setLayoutParams(button_params);
You first set width/height to FILL_PARENT, then you overwrite it with px ?
try something like this
View temp = this.findViewById(recent);
RelativeLayout.LayoutParams relativeParams = (LayoutParams) temp.getLayoutParams();
relativeParams.setMargins(0, 0, 0, 50); // llp.setMargins(left, top, right, bottom);
relativeParams.width = 123;
relativeParams.height = 123;
temp.setLayoutParams(relativeParams);