Change Relative layout width and height dynamically - android

Hi i am using following layout structure inside LinearLayout
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
</TableRow>
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv1"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="10dp"
android:textColor="#000" />
<TextView
android:id="#+id/tv2"
android:layout_height="wrap_content"
android:layout_weight="1.0"
android:gravity="right"
android:paddingRight="10dp"
android:textColor="#000" />
</TableRow>
</TableLayout>
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="320px"
android:layout_height="320px"
android:gravity="center" >
</RelativeLayout>
and want to set relative layout width and height dynamically from java file instead of setting it to 320px in the xml file but not able to do that , Orientation change is not an issue for as i restricting it to only in Portrait mode. It is possible to set the relative layout on full screen by using match_parent but i have to put another views on the screen so is it possible or i have to achieve it another way...

Android does NOT refresh layout of views with wrap_content once it has been displayed.
Even with invalidate() or requestLayout().
So if you add a child view, or modify the content dynamically, you're screwed.
getLayoutParams().height = x plus requestLayout() are a good solution if you know the "x", and if you need to do it ONLY ONCE.
After that the wrap_content in LayoutParams is lost, since you have overridden it.
To solve that, I've written a static class that recomputes the sizes and forces the update of the layout for the views with wrap_content. The code and instructions to use are available here:
https://github.com/ea167/android-layout-wrap-content-updater
Enjoy!

try using this
getLayoutParams().height= x;
requestLayout(); or invalidate();

give an id to your relative layout in .xml :
android:id="#+id/relativelayout"
now in your java file write this:
RelativeLayout Rl = (RelativeLayout) findViewById(R.id.relativelayout);
RelativeLayout.LayoutParams layout_description = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedwidth,
RelativeLayout.LayoutParams.FILL_PARENT or YourUserDefinedHeight);
Rl.setLayoutParams(layout_description);

From below code u can get device height and width:-
Display display = getWindowManager().getDefaultDisplay();
int width = (display.getWidth() );
int height = (display.getHeight() );
paramsTop ur realative layout:-
Now u can set height or width what you want.
paramsTop = new RelativeLayout.LayoutParams(width, height);

You can set views dimensions dynamically using LayoutParams.
Something like this:
RelativeLayout layout = (RelativeLayout) findViewById(R.id.yourlayout_id);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
params.height = 320;
params.width = 320;

I faced this problem, the best solution for Change Relative layout width and height dynamically like this
RelativeLayout relMain = (RelativeLayout) convertView.findViewById(R.id.relMain);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.width =200;
params.height =200;
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
relMain.setLayoutParams(params);

If you intending to change Height, then this would make a trick.
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.yourId);
relativeLayout.getLayoutParams().height = 100;
relativeLayout.getLayoutParams().width = 100;

I encountered same issue when working with LinearLayout which has wrap_content and one child as TextView which had match_parent.
Remove the TextView programatically and then add it again.
linearLayout.removeView(textView)
linearLayout.addView(textView)
I know it sound stupid but it works.
In my case calling invalidate didn't work, only this worked.
Depending on your implementation you need to take care of view index inside its parent

Related

Android change gravity programmatically

I have this layout:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="left" />
<TextView
android:id="#+id/button_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:textSize="40dp" />
</LinearLayout>
Now, I want to modify gravity of the ImageView to left and gravity of the TextView to right. All other values present in xml (width, height, ...) must be preserved.
I tried:
1) setGravity method. Unfortunately, ImageView doesn't have this method. Why?
2)
LinearLayout.LayoutParams iconLayoutParams = new LinearLayout.LayoutParams(icon.getWidth(), icon.getHeight());
iconLayoutParams.gravity = Gravity.LEFT;
This somehow destroys View's dimensions
3)
android.view.ViewGroup.LayoutParams iconLayoutParams = icon.getLayoutParams();
This doesn't have gravity property.
(how is that even possible that icon.setLayoutParams() accepts layout parameters where gravity can be set and icon.getLayoutParams return some different kind of layout params without gravity property? That is a mess)
Can you please help me with that?
I think the easiest way to sort this out is using the 3rd approach but modify it slightly.
LinearLayout.LayoutParams iconLayoutParams = (LinearLayout.LayoutParams) icon.getLayoutParams();
And then do:
iconLayoutParams.gravity = Gravity.LEFT;
Reason LayoutParams itself does not have Gravity is that not all containers have gravity. LinearLayout does, so you need to cast to LinearLayout.LayoutParams since you surely know that your container is LinearLayout. And the method getLayoutParams is in the View class so it must return the parent of all other LayoutParams, which again, does not have to have Gravity.
I hope I explained properly :)

Edit position of an Android button created with a linearLayout

I am trying to dynamically create buttons, and they will be of varying size and in varying positions.
I have the code to create a button of varying size, but I am stuck on changing the position.
I am using linearlayout and am trying to use setMargins to move the button around, but it seems to be changing the margin within the button. My code is as follows:
public void button(int a, int b) {
newButton = new Button(this);
newButton.setText("HELLO");
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 0, 0, 0);
ViewGroup.LayoutParams params = layout.getLayoutParams();
params.width = a;
params.height = b;
layout.requestLayout();
layout.addView(newButton, layoutParams);
}
Here is my manifest for this bit:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="58dp"
android:layout_toRightOf="#+id/textView1"
android:text="Button" />
<LinearLayout
android:id="#+id/layout"
android:layout_width="50sp"
android:layout_height="40sp"
android:orientation="vertical" >
</LinearLayout>
Do you understand what a LinearLayout is and why you're using it? Every child of the layout snaps in place. If I have a LinearLayout that's vertical and it has 3 children they will be on top of each other. I can change their gravity so they are attracted to different margins but to "change" its position is impossible depending on what you mean by "change".
Check out the other layouts. You may want to use a RelativeLayout.
Why donĀ“t you use button.setX(float x) and button.setY(float y)? You'll need to use RelativeLayout instead of the LinearLayout. It's more easy but it's only available since api 11...

Avoid layout to be resized on margin changed

I'm programmatically changing the margin of a layout inside a framelayout. My goal is to make a sliding view like the Facebook app. Is it possible to avoid this layout to be resized? This is my layout moving:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#00a2ff"
android:gravity="center_vertical"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/left"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/ivGPSSearching"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="4dp"
android:src="#drawable/actionbar_gps_searching" />
</LinearLayout>
<ImageView
android:id="#+id/ivStarsFilter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:src="#drawable/actionbar_star" />
</LinearLayout>
I don't want the left LinearLayout to be resized. I hope you will understand what I want :)
Thanks
To achieve something similar we extended a HorizontalScrollView - just overriding onInterceptTouchEvent and onTouchEvent to always return false.
Then you just need to put your menu in the left side and the content on the right (the content must match the screen width).
Finally setup the initial HorizontalScrollView scroll and bind to a button click a event to change the scroll position(horizontalScrollView.scrollTo or horizontalScrollView.smoothScrollTo).
I hope this helps.
The below is an example code for using TranslateAnimation and set listener for the animation and in
#Override
public void onAnimationEnd(Animation animation)
{
//Just set the layout params for your view (layout) which is animated,
//to make your view shift to the new position
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutWidth, LayoutHeight);
params.leftMargin = 100; //for example you want to shift 100 px from left
params.rightMargin = -Layoutwidth; //this will avoid your view
//from shrinking and make it as wide as its width was, and - negative value will
//move it towards right
otherLayout.setLayoutParams(params);
}
I hope it make you people clear how to move your view after animation
I've been scratching my head around this for the last few hours as well.
Turns out that if you change both opposite margins, the views inside the ReleativeLayout will not be resized nor moved around:
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams( LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
lp.setMargins(0, - yOffset, Integer.MIN_VALUE, yOffset);
This is crazy but it works...

FrameLayout margin not working

My layout structure is like this
LinearLayout
FrameLayout
ImageView
ImageView
FrameLayout
TextView
LinearLayout
I have set margin's for the two ImageView which are inside FrameLayout. But FrameLayout margins are discarded and it always set's the Image to top left corner. If i change from FrameLayout to LinearLayout the margin's work properly. How to handle this ?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/inner1"
>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:layout_width="24px"
android:layout_height="24px"
android:id="#+id/favicon"
android:layout_marginLeft="50px"
android:layout_marginTop="50px"
android:layout_marginBottom="40px"
android:layout_marginRight="70px"
/>
<ImageView
android:layout_width="52px"
android:layout_height="52px"
android:id="#+id/applefavicon"
android:layout_marginLeft="100px"
android:layout_marginTop="100px"
android:layout_marginBottom="100px"
android:layout_marginRight="100px"
/>
</FrameLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title"
android:layout_marginLeft="10px"
android:layout_marginTop="20px"
android:textColor="#FFFFFF"
android:textSize = "15px"
android:singleLine = "true"
/>
</LinearLayout>
I had the same issue myself and noticed that setting the layout_ margins does not work until you also set your ImageView's layout gravity i.e. android:layout_gravity="top" in the XML resource file, or from code: FrameLayout.LayoutParams.gravity = Gravity.TOP;.
To make it more clear why. The FrameLayout.onLayout() call contains this (in api v2.3.4 at least):
// for each child
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
final int gravity = lp.gravity;
if (gravity != -1) {
// handle all margin related stuff
So if gravity is -1, there will be no margin calculation. And the thing is, gravity in FrameLayout.LayoutParams is defined by:
gravity = a.getInt(com.android.internal.R.styleable.FrameLayout_Layout_layout_gravity, -1);
So if gravity is not set, there will be no margin calculation.
add your xml this attribute and re run
android:layout_gravity="top"
everything is Ok!
and you dont set new layout params like this;
FrameLayout.LayoutParams llp = new FrameLayout.LayoutParams(WallpapersActivity.ScreenWidth/2, layH);
use like this:
FrameLayout.LayoutParams llp = (LayoutParams) myFrameLay.getLayoutParams();
llp.height = 100;
llp.width = 100;
myFrameLay.setLayoutParams(llp);
Taken from the FrameLayout docs (link)
The size of the frame layout is the size of its largest child (plus padding), visible or not (if the FrameLayout's parent permits).
This seems to describe the fact that it'll strip margins out. Like boulder mentioned, you could try switching to padding as it can be used to produce a similar effect if done properly.
Out of curiosity, you mentioned that it does work fine when using a LinearLayout container, why the choice of FrameLayout?
Have you tried android:layout_gravity ? Try using android:padding in you ImageViews instead of android:layout_margin. AFAIK margins doesn't work properly on Frame layout. I even had to write custom layout for that purpose once. BTW, how do you want allign you ImageViews?
try setCropToPadding(true) to ImageView ,this should be helped!
you have to set your ImageView's layout gravity top i.e. android:layout_gravity="top" in the XML resource file, or from code: FrameLayout.LayoutParams.gravity = Gravity.TOP

Android: How to set the absolute size of an Activity's Window?

Is there a way to set a GUI screen (Activity) to be 200dip wide and 150dip high, either using XML or programmatically?
I found getWindow().setLayout(), but that only take predefined constants for width and height.
You can use absolute numbers in Window.setLayout(), just like you can everywhere else you specify layout width and height. Sorry the doc isn't clear on this.
WindowManager.LayoutParams params = getWindow().getAttributes();
params.x = 0;
params.height = 650;
params.width = totalWidth;
params.y = totalHeght/10;
this.getWindow().setAttributes(params);
may be it works...but make sure about the values...
Just use a full screen layout, and inside define another layout that has the size you want. Kind of like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:layout_width="200dip"
android:layout_height="150dip">
<Button android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
I put a button in there just to show where the content goes.

Categories

Resources