Layout height decrease from the bottom - android

I' m trying to reduce a linear layout height in the code, it works but it decreases from the bottom not the top.
Here is my code :
<LinearLayout
android:id="#+id/pension_layout"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#drawable/linearborder"
android:gravity="center"
android:layout_gravity="top"
android:weightSum="100">
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 600);
pl.setLayoutParams(params);

I was also facing the same issue. In my case, what I did is -
just rotate the view in X direction
xView.setRotationX(180);
Then set height using layoutParam.
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams)
xView.getLayoutParams();
params.height = 300;
xView.setLayoutParams(params);
It will cut from the top.
Hope this will help.

Related

Interesting layout weight behavior

I've encountered some interesting behavior setting the weight of two views in a linear layout.
I have a horizontal Linear layout with weightSum = 1. It has two children views, leftView & rightView. I am making a horizontal bar graph by setting the weight of the views.
If I set the left view with a weight of 0.05 and the right view with a weight of 0.95 programmatically. The behavior presented in the UI will show the left view with taking up 95% of the width and the right view 5% of the width.
If I reverse it setting the leftView with 0.95 & the rightView with 0.05 the UI will present the leftView taking up 5% of the space (the way I wish it to be)
Does anyone have any insight to why this occurs?
float percentage= (float)(x * 100) / y; // (2000 * 100) / 40000 = 5
LinearLayout.LayoutParams leftViewParams =
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
percentage/100f); // (5 / 100) = .05
leftView.setLayoutParams(leftViewParams);
LinearLayout.LayoutParams rightViewParams=
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT,
(100 - percentage)/100f); //(100 - 5) / 100 = .95
rightView.setLayoutParams(rightViewParams);
However if I set the leftview to have a weight of .95 and the right view to have a weight of .05 the left view takes up 95% of the space while the right view only takes up 5%.
Can anyone explain what is happening here?
BONUS XML - if I set leftView layout_weight=0.05 and rightView layout_weight=0.95 it displays correctly.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/core_dimen_16dp"
android:orientation="horizontal">
<View
android:id="#+id/leftView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#000" />
<View
android:id="#+id/rightView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#AAA" />
</LinearLayout>
I tested your code and everything is correct. There is no problem. I think that probably you make a little mistake.
I paid attention to your code and I noticed that you should change remainingWeight to rightViewParams in the line: rightView.setLayoutParams(remainingWeight);
I hope this will help.

android TableLayout with LinearLayout as cells - layout_margin is ignored

I have TableLayout. As each cell in Table Layout I have LinearLayout with few elements inside a nice border.
LinearLayout definition:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginBottom="2dp"
android:orientation="vertical"
android:background="?border_empty"
android:layout_weight="1"
>
Unfortunately layout_marginLeft and layout_marginBottom seems to be ignored. :-( I've even tried to set very high values ex. 15 dp. How to set more space between cells (I would prefer to set it on left and on bottom if possible)?
I saw your question yesterday but I wanted to wait until today to answer, because now I'm facing exactly the same issue.
I've tried using this (being fila my TableRow):
final TableLayout.LayoutParams trParams = new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.WRAP_CONTENT);
trParams.setMargins(10, 5, 10, 5);
fila.setLayoutParams(trParams);
But as you comment, both left and right margins seem to be ignored. I finally managed it through the TextView itself, this way (being tv my TextView):
final LinearLayout.LayoutParams lp = (LinearLayout.LayoutParams) tv.getLayoutParams();
lp.rightMargin = 10;
lp.leftMargin = 10;
This seems to work perfectly.

How to create button at run time on specified location inside frame layout android?

I am trying to create button at runtime.I am getting the coordinate and height,width of button from backend and I have to create button at same location on run time.I have used following code.
Button btn=new Button(getApplicationContext());
btn.setText("CLICK ME");
#SuppressWarnings("deprecation")
AbsoluteLayout.LayoutParams param = new AbsoluteLayout.LayoutParams( 121, 58, 50, 50);
btn.setLayoutParams(param);
mParentView.addView(btn);
My xml is
<FrameLayout
android:id="#+id/frameView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ViewFlipper
android:id="#+id/viewFlipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</ViewFlipper>
<fragment
android:id="#+id/header_fragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
class="com.sdei.presentationapp.activity.PresentationModeTopBar"
tools:layout="#layout/presentation_top_bar" />
</FrameLayout>
here the parentview is framelayout.
I am able to create the button but the problem is it is created always at top left corner,no matter what coordinate we pass.Please help.
Thanks in advance.
You cannot set button at your desired position in framelayout only possible in absolute layout. but you can use margin with respect to your left and top which will work like your (x, y) coordinates.
// First create your button:
Button test_button = new Button(getApplicationContext());
test_button.setText("test");
// Then create layout params for you buttons.
FrameLayout.LayoutParams lp = new LayoutParams(100, 100); // Button width and button height.
lp.leftMargin = 200; // your X coordinate.
lp.topMargin = 300; // your Y coordinate.
// Then find layout and add button on it.
FrameLayout layout = (FrameLayout) findViewById(R.id.FrameLayout1);
layout.addView(test_button, lp);
Hope this can help you.
I have not compile this code, so change as per your layout.
LinearLayout ll = new LinearLayout(this);
fl .setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(30, 20, 30, 0);
Button okButton=new Button(this);
okButton.setId(ok_id)
okButton.setText("some text");
See one reference link here
The absolute layout class is deprecated, you are encouraged to use Frame Layout or Relative layout instead.
The reason of that is it won’t be compatible with all the android phones as they have different screen sizes and resolutions.
Absolute layout lays widgets by specifying their exact X and Y positions. In android the origin (0,0) coordinate is located at the top left of the screen. By default, if you define any control in absolute layout without defining it’s x,y coordinates, it will be placed in the origin point at (x,y)=(0,0);
Thus your defined position for AbsoluteLayout.LayoutParams is not working properly.
As you are using FrameLayout then you can try the following way-
// declare and initialize LayoutParams for the framelayout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
// decide upon the positioning of the button //
// you will likely need to use the screen size to position the
// button anywhere other than the four corners
params.setMargins(.., .., .., ..);
// use static constants from the Gravity class
params.gravity = Gravity.CENTER_HORIZONTAL;
btn.setLayoutParams(params);
mParentView.addView(btn);

Android Layout, view height is equal to screen size

in Android how to make a view have same height as its screen size, is it possible to achieve this with only the xml? or if it must use script, tell me how
Thanks.
Sorry for being not clear, and thanks for your reply
but i think, match_parent and fill_parent attribute is not reliable, because when i put the view inside one container or change the view container hierarchy, it won't work.
Here my complete xml layout.
The element i want to make the height sam with device screen is the last list view inside relative layout
No you cannot achieve this in XML only.
As Android supports multiple screen sizes, at runtime you need to check for each device size. The height for each device can be calculated like this:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;
With the above code, you will get the height of the screen and you need to set this height in dp to your view at runtime.
Do this in your activity:
// get view you want to resize
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main);
// get layout parameters for that view
ViewGroup.LayoutParams params = mainLayout.getLayoutParams();
// change height of the params e.g. 480dp
params.height = 480;
// initialize new parameters for my element
mainLayout.setLayoutParams(new LinearLayout.LayoutParams(params));
This can be possible from xml layout. To do this make the parent layout height and width fill_parent or match_parent and then set each child view width fill_parent or match_parent. Don't set any padding or margin to parent layout. Hope it will work. Here I am giving you an example:
<LinearLayout 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:orientation="vertical" >
<Button
android:id="#+id/btn_swap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Left or Right" />
<SurfaceView
android:id="#+id/my_surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Attention: If you use ScrollView, you have to set fillViewport="true" otherwise it will not work. A Google engineer said about it before. Check it from here
Display screenDisplay = getActivity().getWindowManager().getDefaultDisplay();
LayoutHeight = screenDisplay.getHeight();
LinearLayout.LayoutParams listLayoutParams = new LinearLayout.LayoutParams(
LayoutHeight, LinearLayout.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(listLayoutParams);

Android: Change absolute position of a view programmatically

If you use an AbsoluteLayout (I know that it is deprecated, but it was the only way to solve my problem) you can give the childViews the tag android:layout_x and android:layout_y to set their absolute position within the AbsoluteLayout.
However I don't want to set these information in the xml, because I only know them at runtime. So how can I set these parameters at runtime programmatically? I don't see any method on the View like view.setLayoutX(int x) or something.
Here is my XML, which works fine, when I set the layout_x and layout_y values:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/myImageView"
android:layout_width="1298px"
android:layout_height="945px"
android:layout_x="0px"
android:layout_y="0px" />
<Button
android:id="#+id/myButton1"
android:text="23"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50px"
android:layout_y="300px"
android:tag="23"/>
<Button
android:id="#+id/myButton2"
android:text="48"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="50px"
android:layout_y="300px"
android:tag="48"/>
</AbsoluteLayout>
In fact, I don't want to set any button within the xml anymore, but rather retrieve some information via remote and add buttons depending on that information.
Here is the part the code I'm using so in my onCreateMethod to add these buttons:
for (MyRemoteObject remoteObject: list) {
Button button = new Button(this);
button.setOnClickListener (listener);
button.setTag(remoteObject.id);
button.setText(remoteObject.id);
// button.setLayoutX(remoteObject.x) ????
// button.setLayoutY(remoteObject.y) ????
myLayout.addView(button);
}
Use the version of addView that takes LayoutParams:
LayoutParams params = mLayout.generateLayoutParams();
params.x = remoteObject.x;
params.y = remoteObject.y;
mLayout.addView(button, params);
In response to your comment on Mayra's answer, I had a similar issue with RelativeLayout instead of AbsoluteLayout. The solution was to use a similar method and cast it as your layout type. Something like this:
LayoutParams params = (android.widget.RelativeLayout.LayoutParams) this.generateDefaultLayoutParams();
params.height = 360;
params.width = 360;
params.addRule(CENTER_IN_PARENT);
this.addView(board, params);
It took me forever to figure this out so I wanted to post it here for someone else if they needed it.
I had just the same problem but found a somewhat different solution.
int width = 100, height = 50, x = 10, y = 20;
Button button = new Button(this);
AbsoluteLayout myLayout = (AbsoluteLayout)findViewById(R.id.myLayout);
myLayout.add(button, new AbsoluteLayout.LayoutParams(width, height, x, y));
And if you find out what to use for "fill_parent" (hint try -1) then you may use those constants for width and height.
I don't know why, but when moving top and left edges the Android keeps the right and bottom edges in same place. As I could not change the width and height properly (the image was disappearing), I've used this:
LayoutParams layoutParams=new LayoutParams(width, height);
layoutParams.leftMargin = newLeft;
layoutParams.topMargin = newTop;
imageView.setLayoutParams(layoutParams);
I don't know if it's the best way, but worked for me. I've tested it in 2.2+ and works fine!
You must import android.widget.LinearLayout.LayoutParams; because the default is ViewGroup's LayoutParams (by Davit T)
the above answers are right.but this one would be more perfect
AbsoluteLayout.LayoutParams layoutParams = new AbsoluteLayout.LayoutParams(AbsoluteLayout.LayoutParams.WRAP_CONTENT,AbsoluteLayout.LayoutParams.WRAP_CONTENT,DragLayer.int left_location,int top_location);
layout.addView(view,layoutParams)

Categories

Resources