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)
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);
<LinearLayout
android:layout_width="match_parent"
android:layout_height=" "
android:orientation="vertical" >
Here's the thing, lets say i have this LinearLayout that has a width of match_parent, so the width of the LinearLayout depends on the width of the device. What i dont know is how could i set the android:layout_height=" " value to correspond on the value of the android:layout_width="match_parent" so i could have a Square LinearLayout i know i can do this using LayoutParams but is there a way to do it on xml? Thank you guys..
First get screen width
Display mDisplay = activity.getWindowManager().getDefaultDisplay();
int width = mDisplay.getWidth();
int height = mDisplay.getHeight();
Set Size to Layout
LinearLayout layout = (LinearLayout)findViewById(R.id.LayoutID);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = width;
params.width = width;
layout.setLayoutParams(params);
In XML:
<LinearLayout
android:layout_width="0dp"
android:layout_height="0dp"
android:id="#+id/LayoutID"
android:orientation="vertical" >
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.
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);
I have two ImageViews inside an AbsoluteLayout.
<AbsoluteLayout android:id="#+id/AbsoluteLayout01"
android:layout_width="fill_parent" android:background="#drawable/whitebackground"
android:layout_height="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView android:id="#+id/floorPlanBackgroundImage"
android:src="#drawable/ic_tab_lights_gray"
android:scaleType="center"
android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<ImageView android:id="#+id/fpLight"
android:src="#drawable/ic_tab_lights_gray"
android:scaleType="center" android:layout_x="50px" android:layout_y="50px"
android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
</AbsoluteLayout>
the floorPlanBackgroundImage is dynamically set from an image that is 800x600 in size. The can scroll around it.
My second image, fpLight represents a light in a room. It's a small 20x20 image. What I need to do is change the layout_x & layout_y properties in code, but I don't see a way to set that in the ImageView.
I'd expected something like this...
fpLight.setLayoutX("55px");
Is there a way?
You should use:
AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams(int width, int height, int x, int y)
layout Parameter object with the preferred x and y and set it to your fpLight image by
fpLight.setLayoutParams(param);
AbsoluteLayout is Deprecated
You should use RelativeLayout instead
EXAMPLE:
Suppose you want a ImageView of size 50x60 on your screen at postion (70x80)
// RelativeLayout. though you can use xml RelativeLayout here too by `findViewById()`
RelativeLayout relativeLayout = new RelativeLayout(this);
// ImageView
ImageView imageView = new ImageView(this);
// Setting layout params to our RelativeLayout
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(50, 60);
// Setting position of our ImageView
layoutParams.leftMargin = 70;
layoutParams.topMargin = 80;
// Finally Adding the imageView to RelativeLayout and its position
relativeLayout.addView(imageView, layoutParams);
yourImageView.setX(floatXcoord);
yourImageView.setY(floatYcoord);