access the weight attribute in code - android

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.

Related

android - setting LayoutParams to View elements programmatically

I want that the buttons had identical sizes and table was fully filled the buttons. But the buttons have a different size, what do I wrong in?
My xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background"
android:orientation="horizontal" >
<LinearLayout
</LinearLayout>
<TableLayout
android:id="#+id/mainTableL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="40dp" >
</TableLayout>
</LinearLayout>
My code:
layoutParams = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f);
mainTableLayout = (TableLayout) findViewById(R.id.mainTableL);
tableRow = new TableRow[VALUE_ROWS];
btn = new Button[VALUE_ROWS*VALUE_COLUMNS];
for (int indexRow = 0; indexRow < VALUE_ROWS; indexRow++){
tableRow[indexRow] = new TableRow(this);
tableRow[indexRow].setLayoutParams(new TableRow.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
for (int indexColumn = 0; indexColumn < VALUE_COLUMNS; indexColumn++){
btn[countBtn] = new Button(this);
btn[countBtn].setLayoutParams(layoutParams);
btn[countBtn].setId(countBtn);
btn[countBtn].setBackgroundResource(R.drawable.fon);
tableRow[indexRow].addView(btn[countBtn],layoutParams);
countBtn++;
}
mainTableLayout.addView(tableRow[indexRow], layoutParams);
}
I am sorry for my pure english.
Thank you
I'm not sure if changing this will help you, but this definitely should be fixed. For table rows you should set TableLayout.LayoutParams instead of TableRow.LayoutParams:
tableRow[indexRow].setLayoutParams(new TableLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT, 1.0f));
and later when you add a row, don't specify layoutParams:
mainTableLayout.addView(tableRow[indexRow]);
Also note that specifying size for rows doesn't have any effect, their width is set to MATCH_PARENT and the height - to WRAP_CONTENT (see the doc). And similar with buttons, TableRow sets their width to WRAP_CONTENT and the height - to MATCH_PARENT regardless of what you specify in the layout params.

Android: TextView ignores Gravity and Margin

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.

Android - positioning views programmatically

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

Set width of LinearLayout declared in XML Programatically

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)

android - how does LayoutParams work?

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

Categories

Resources