LinearLayout height not getting set programmatically - android

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.

Related

dynamically increase and decrease the relative layout

dynamically increase and decrease the relative layout , i want to shrink the size of relative layout when recycler view popup from bottom of screen and after using it agian the screen will return into normal size(in recycler view im sung icons to put on relative view)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_weight="1">
////////tool bar
<include layout="#layout/toolbar" />
<RelativeLayout
android:id="#+id/rel1"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:layout_below="#+id/toolbar"
android:layout_weight="1">
<com.cmlibrary.CircleMenu
android:id="#+id/circle_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="180dp"
android:layout_marginTop="180dp"
/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#+id/rel1"
android:padding="5dp" />
</RelativeLayout>
Better way is to show and hide the visibility of views as required. But if you want to increase and decrease the size then below code will help you.
private void layoutParams() {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();
ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
/* When recyclerView shows*/
layoutParams.height = layoutParams.height - params.height;
relativeLayout.setLayoutParams(layoutParams);
/* When recyclerView hidden*/
layoutParams.height = RelativeLayout.LayoutParams.MATCH_PARENT;
relativeLayout.setLayoutParams(layoutParams);
}
Hope this will help.

Cannot resize LinearLayout in height

I have a LinearLayout inside of a Scrollview. After doing some animations, there is blank space at the bottom when I scroll down. So I want to resize the LinearLayout using this code
ViewGroup.LayoutParams params = _linearLayoutMainWrapper.getLayoutParams();
params.height = 100;
_linearLayoutMainWrapper.setLayoutParams(params);
It doesnt work though, just nothing is happening. When I try to resize the width it is working though.
The XML is
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:fillViewport="true"
android:id="#+id/scrollViewMain">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:paddingLeft="16dip"
android:paddingRight="16dip"
android:id="#+id/linearLayoutMainWrapper">
<!-- a lot of stuff -->
</LinearLayout>
</ScrollView>
Maybe you have some margin setted for this view ? Try to clear in params margins and the height for LinearLayout should be youScrollView.getMeasuredHeight().

layout height issue with weight and wrap_content

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>

full screen layout in Scroll View

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!

How to make scrollable layouts each one fits screen width?

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)

Categories

Resources