I have a simple linear layout and want to divide the screen into two equally sized parts.
In the upper one, I want to put a SurfaceView that has exactly the same size as its parent, i.e. half of the screen. Unfortunately, it always either takes all the screen or nothing (depending on the configuration).
Why is that and how can I change it?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="vertical">
<LinearLayout android:id="#+id/layoutUpperDaw"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#192832">
<com.example.MySurvfaceView
android:id="#+id/id_mySurfaceView"
android:layout_width="match_parent"
android:layout_height="0pt"
android:layout_weight="1"
OR
android:layout_height="match_parent"
>
</com.example.MySurvfaceView>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#193222">
...
</LinearLayout>
</LinearLayout>
When you provide weight you have to set height or weight to 0dp as below.
<LinearLayout android:id="#+id/layoutUpperDaw"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#192832">
and
<com.example.MySurvfaceView
android:id="#+id/id_mySurfaceView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
Update: Answer above is correct about weight usage, make sure either hight or width is 0dp at all places where you used weight.
You don't really need nested LinearLayout. Following should work.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.MySurvfaceView
android:id="#+id/id_mySurfaceView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</com.example.MySurvfaceView>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#193222">
</LinearLayout>
</LinearLayout>
Programmatically:
Find the parent layout.
GridLayout gridLayout =(GridLayout)findViewById(R.id.gridLayout);`
Get measured height and Width of parent.
int height = gridLayout.measuredHeight();
int width = gridLayout.measuredWidth();
3.Calculate child view height and width.For example two child each taking 50% of height in the parent but taking full width of parent:
int childHeight = height/2;
int childWidth = width;
4.Get instanceof LayoutParams depending on parent type.
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
5. Set height and width on layout params.
params.width = childWidth;
params.height = childHeight;
6.Create child view and set Layout params.
ImageView imageview = new ImageView();
imageview.setLayoutParams(params);
7.Add child View to parent.
gridLayout.addView(ImageView);
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>
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 want to have 2 layouts inside ScrollView. First layout should be on the full screen and second layout below first one. When activity starts it is not possible see second layout. Second layout is possible see after scroll screen. Please take a look on my image for better understand.
Layout code: `
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" >
<!-- this layout can be any height you want -->
</LinearLayout>
</LinearLayout>
</ScrollView>`
Get the device screen height:
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
int screenHeight = size.y;
Find the LinearLayout:
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
Set the height of the layout based on the screen height you got earlier:
LayoutParams params = layout.getLayoutParams();
params.height = screenHeight - getStatusBarHeight();
Done. Be sure to call all of those methods in your onCreate method.
Hope this helps!
I'm trying to make android app which has a scrollable layout that scrolls different other layouts vertically and horizontally, I want each layout of theses different layouts to fits the screen width and nearly fits the Height, look at the following figure that illustrates what I'am trying to do:
I just tried to set all width and height of these layouts to match_parent and put all of them in a main_layout assigned with match_parent parameters also, then I create a Layout and put the Layouts(4,1,5) in it and put this Layout in a ScrollView , then i put the main_layout layout in a HorizontalScrollView that contains three layouts(Layout3, Layout(4,1,5), Layout2), but that gave me an interfered view of the Layouts.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9" >
<!-- just to give it 0.9 of the screen in order to give a space to the top layout (it takes 0.1 and the weight sum is 1) -->
<LinearLayout
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/Layout3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/Layouts(4,1,5)"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/Layout4"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="#+id/Layout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="#+id/Layout5"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/Layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
i'd try that:
RelativeLayout instead of ScrollView
all LinearLayouts with match_parent, transformed programmatically:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int sw = size.x; // screen width
int sh = size.y; // screen height
LinearLayout lay2 = (LinearLayout) findViewById(R.id.Layout2);
lay2.setTranslationX = sw;
LinearLayout lay3 = (LinearLayout) findViewById(R.id.Layout3);
lay2.setTranslationX = -sw;
LinearLayout lay4 = (LinearLayout) findViewById(R.id.Layout4);
lay2.setTranslationY = -sh;
LinearLayout lay5 = (LinearLayout) findViewById(R.id.Layout5);
lay2.setTranslationY = sh;
and then touch listener on RelativeLayout that would control swipes, changing it's position.
I found all i needed in that link , it provides a sample project source code that achieves the approach i needed by depending on Android fragmentation in a way easy and efficient way (but this way can provide horizontal navigation only).
And so, after a searching i found this library that provides the process in both directions(vertically & horizontally)
In my app, I've a layout like:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/belowAcionBar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal">
</LinearLayout>
</FrameLayout>
I need to set the height of LinearLayout belowAcionBar dynamically. This view needs to be shown just below ActionBar.
To set the height I'm using below code:
View panelView = findViewById(R.id.belowAcionBar); // This gives object of LinearLayout
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) panelView.getLayoutParams();
params.height = 48; // In dp
panelView.setLayoutParams(params);
Issue 1:
Since belowAcionBar is a LinearLayout, first I tried of using LayoutParams as LinearLayout.LayoutParams, rather than FrameLayout.LayoutParams.
But it thre a runtime error Can't cast FrameLayout.LayoutParams to LinearLayout.LayoutParams.
Why it is getting panelView.getLayoutParams() as FrameLayout?
Issue 2:
Even after using code above, my view belowAcionBar is appearing to be much less than 48dp. Seems like it is being partially hidden behind ActionBar.
Issue 3:
If I change the layout to:
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout android:orientation="vertical"
android:id="#+id/belowAcionBar"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/purple_dark" >
</LinearLayout>
</LinearLayout>
</FrameLayout>
And change the java code to:
View panelView = findViewById(R.id.belowAcionBar);
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) panelView.getLayoutParams();
params.height = 48; // In dp
panelView.setLayoutParams(params);
Then my code works perfectly, but adds extra Layout. Why it is working now?
Thanks in advance.