I have one horizontal Linear Layout with Width = match_parent and weightsum=5.
If i insert 5 vertical Linear Layouts with each width=0 and weight=1 everything looks as expected, the layouts each get the same width.
If i add only 2 vertical with each width=0 and weight=1 they take more space than they should. I expected them to also take 1/5 of the space.
Maybe it is the correct behaviour that they take more space and I understood the concept of weight/weightsum wrong.
Thanks for any help!
edit:
I try to add some Code
LinearLayout linear=null;
LinearLayout.LayoutParams layoutParams= new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
linear=new LinearLayout(getApplicationContext());
linear.setOrientation(LinearLayout.HORIZONTAL);
linear.setLayoutParams(layoutParams);
linear.setPadding(15, 0, 15, 10);
linear.setWeightSum(Float.valueOf(modulo));
//modulo 5 in my example
LinearLayout linear2=new LinearLayout(getApplicationContext());
LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(0,
LinearLayout.LayoutParams.WRAP_CONTENT, 1f);
if(count%modulo!=modulo-1){
lp1.setMargins(0, 0, 15, 0);
} else {
lp1.setMargins(0, 0, 0, 0);
}
linear2.setLayoutParams(lp1);
linear2.setOrientation(LinearLayout.VERTICAL);
I add the layout linear 2 into linear in a loop
Why can you click run code :D
Try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="5">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light"
android:layout_weight="1"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_bright"
android:layout_weight="1"/>
</LinearLayout>
If the logical solution does not work fill the rest with
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="3"/> <!-- or whatever the rest is -->
or
View space = new View(this); // NEVER CREATE VIEWS WITH APP CONTEXT!
LinearLayout.LayoutParams spaceParams = new LinearLayout.LayoutParams(0, 0, 3f);
linear.addView(space, spaceParams);
NOTE: Space widget didn't work in this case.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="5">
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="Your bg"
android:layout_weight="2.5"/>
<View
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="Your bg"
android:layout_weight="2.5"/>
</LinearLayout>
divide weight equally.
Related
My view is like:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RecyclerView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
/>
</LinearLayout>
It will give 50dp to TextView and rest of area to RecyclerView.
But if items in RecyclerView are less than, say 1 or 2, then it should shrink to the height of it's child views other wise behave like layout specifies. Can any one help??
I think the best option would be to change LinearLayout.LayoutParams of the root view dynamically. When the RecyclerViewhas a low amount of children, then set LinearLayout params to:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT);
layout.setParams(params);
otherwise, leave as it is.
It is very easy to detect if RecyclerView should be wrapped. Obtain LinearLayout height:
int height = layout.getTop() - layout.getBottom()
If this height is lower than the expected maximum height (screen height or other predefined value).
A simple approach would be using RelativeLayout:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_below="#+id/recyclerView" />
</RelativeLayout>
I try to add a PlotView (custom View) into a LinearLayout. My LinearLayout has a weightSum of 8. Inside my .xml I defined a Space (weight of 3) that has to appear above my PlotView and a Button (weight of 1) that should follow my plot underneath. So far, so good.
Now the PlotView is added programmatically, with a weight of 4. However, it will always consume almost the entire screen, and I am not sure what I am doing wrong here.
My Code:
main_activity.xml (snippet)
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:layout_alignParentEnd="true"
android:id="#+id/linlayout"
android:weightSum="8">
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/calibrate"
android:id="#+id/calibration"
android:layout_weight="1"
android:layout_gravity="center_horizontal" />
</LinearLayout>
main_activity.java (snippet)
LinearLayout layout = (LinearLayout) findViewById(R.id.linlayout);
plotView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 4f));
layout.addView(plotView, 1);
Any idea what I am doing wrong?
The solution is to set the height, which is affected by the weight, to 0.
Code in .xml:
android:layout_height="0dp"
Code in .java:
plotView.setLayoutParams(new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT, 0, 4f));
Is it possible to have two views that are the same height of the activity screen inside of a linear_layout inside of a scrollview? I tried achieving this like so:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</ScrollView>
The only thing that appears one button with the height of the screen. The other button seems to have disappeared.
I suppose it is paradoxical to have two views to with the attribute match_parent.
However, I was hoping to get two views the same size of the available screen using xml, and having the scrollview make both views accessible. I am aware that it can be done via java, but I want an answer for xml.
In Linear Layout you have to define two things...
1 .android:orientation="vertical"
2 .android:weightSum="2"
weightSum is sum of all element in it's parent layout.It Enable you to define the proportion of width/height a element should take in a layout..
For further details go to weightSum
<ScrollView 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:layout_below="#+id/textView1"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2" >
<Button
android:id="#+id/but1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="but 1" />
<Button
android:id="#+id/but2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="but 1" />
</LinearLayout>
</ScrollView>
output
You cannot achieve this in XML only.
As android support multiple screen sizes, At runtime you need to check for each device , 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 above code you will get the height of screen and you need to set this height in dp to your view at runtime.
Do this in your activity for both the buttons:
// get view you want to resize
Button but1= (Button) findViewById(R.id.but1);
LinearLayout.LayoutParams listLayoutParams = new LinearLayout.LayoutParams(
height , LinearLayout.LayoutParams.MATCH_PARENT);
but1.setLayoutParams(listLayoutParams);
i need a vertical ScrollView which contains 4 Buttons.
Each Button should be of the Size of the Phone.
Something like this:
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="First Content."
android:textSize="50sp"/>
<Button
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="Second Content."
android:textSize="50dip"/>
</LinearLayout>
</ScrollView>
I tried a lot of options but dont get how to manage this.
All you have to do a dynamic layout
keep a layout in your xml
<LinearLayout
android:id="#+id/lnr_layout_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
and then in your activity/fragment java file
LinearLayout lnLayoutContainer=(LinearLayout)findViewById(R.id.lnr_layout_container);
Calculate height of your device
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int height = displayMetrics.heightPixels;
Now Inflate and add Buttons to your layout container
for(int i=0;i<4;i++)
{
Button btnView= new Button(context);
<set your button properties and listener here>
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, height);
layoutParams.weight = 1;
btnView.setLayoutParams(layoutParams);
lnLayoutContainer.addView(txtViewChar);
}
I have a splash screen which displays the app logo in Imageview at center of the screen .
I need to show the imageview at a distance 30% from the top of the screen , so that the 70% of the screen at bottom looks empty
I have idea about padding , but don't know how to use %( percentage ) with this condition
<ImageView
android:id="#+id/img_splashlogo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:contentDescription="SplashImage"
android:layout_margin="20dip"
android:src="#drawable/app_logo_splash" />
here is the code that you can use putting the image in the linear layout and then use this code
LinearLayout ll = new LinearLayout(this);
ll.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.setText("some text");
ll.addView(okButton, layoutParams);
try like this,
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/app_splash" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.30"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:id="#+id/img_splashlogo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:contentDescription="SplashImage"
android:layout_margin="20dip"
android:src="#drawable/app_logo_splash" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_width="0.70"
android:background="#android:color/transparent"/>
</LinearLayout>