Main(container) LinearLayout:
<LinearLayout
android:id="#+id/LinearHome"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
Child LinearLayouts (I'm adding them dynamically by inflating new one):
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/lnote"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" />
What's the problem?
change the child to this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/lnote"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" />
Set the second parameter of the inflate() method to the parent (main container)
LinearLayout lNewNote = (LinearLayout) getLayoutInflater().inflate(R.layout.note_layout, HomeContainer);
HomeContainer.addView(lNewNote);
I believe it did the trick for me. (I guess HomeContainer ist an object, not a class)
Related
I have Custom Layout extending FrameLayout with custom XML. Parent children views have android:layout_gravity and android:gravity set to center. It is looking exactly as It should in layout preview, but in App, TextView and ProgressBar is not centered correctly. It is centered horizontally but not vertically.
Any suggestions?
Here is my XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/buttonParent"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center">
<ImageView
android:id="#+id/buttonIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_gravity="center"
android:scaleType="fitCenter"/>
<TextView
android:id="#+id/buttonText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textAllCaps="true"/>
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar"
android:layout_width="40dp"
android:layout_height="40dp"
android:indeterminate="true"
android:layout_gravity="center"
android:gravity="center"/>
</FrameLayout>
Output from LayoutInspector
UPDATE - SOLUTION:
I found out solution. I have to add this to my class extending FrameLayout
val rootParams = LayoutParams(buttonWidth.toInt(), buttonHeight.toInt())
rootParams.gravity = Gravity.CENTER
layoutParams = rootParams
It looks like that FrameLayout parent inside XML inflated layout is not considered as parent. Parent is not even visible in XML it is class where I inflated XML. I have to access parent parameter by this.parameter not in XML.
Set android:layout_gravity="center to your FrameLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:layout_height="wrap_content">
Or set android:layout_height="match_parent" and android:layout_width="match_parent" to your FrameLayout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
Try to set android:layout_height="match_parent" instead of android:layout_height="wrap_content" in your FrameLayout, like following.
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="wrap_content"
android:layout_height="match_parent">
......
I have LinearLayout inside of DrawerLayout which renders drawer item and views according to layout_weight. I am trying to hide that view programmatically by setting its height to 0 or with View.Visibility - GONE property.
Tried
By setting its Visibilty as GONE it acts as INVISIBLE
By getting its LayoutParams and setting its height to 0. Though it acts as INVISIBLE
ParentLayout : mother.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="#+id/activityMotherDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<include layout="#layout/activity_mother" />
<include layout="#layout/motherdrawer" />
</android.support.v4.widget.DrawerLayout>
Layout : motherdrawer.xml
<?xml version="1.0" encoding="utf-8"?>
<com.app.ui.view.ScrimInsetsFrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawerRootLayout"
android:layout_width="#dimen/activity_mother_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/background_application"
android:fitsSystemWindows="false"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/activityMotherDrawerInnerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="#dimen/activity_mother_drawer_layout_padding">
<LinearLayout
android:id="#+id/activityMotherDrawerInnerInnerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/activityMotherDrawerMyAttendanceLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:id="#+id/activityMotherDrawerMyAttendanceIv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_weight="1.5"
android:padding="#dimen/activity_mother_drawer_image_padding"
android:scaleType="centerInside"
android:src="#drawable/ic_drawer_attendance" />
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/activityMotherDrawerMyAttendanceIv"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:gravity="top|center"
android:text="#string/activity_mother_drawer_attendance"
android:textColor="#color/colorGray"
android:textSize="#dimen/activity_mother_drawer_text_size" />
</LinearLayout>
<LinearLayout
android:id="#+id/activityMotherDrawerDashboardLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:id="#+id/activityMotherDrawerDashboardIv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_weight="1.5"
android:padding="#dimen/activity_mother_drawer_image_padding"
android:scaleType="centerInside"
android:src="#drawable/ic_drawer_dashboard" />
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/activityMotherDrawerDashboardIv"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:gravity="top|center"
android:text="#string/activity_mother_drawer_dashboard"
android:textColor="#color/colorGray"
android:textSize="#dimen/activity_mother_drawer_text_size" />
</LinearLayout>
<LinearLayout
android:id="#+id/activityMotherDrawerSettingsLayout"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:clickable="true"
android:orientation="vertical">
<ImageView
android:id="#+id/activityMotherDrawerSettingsIv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerInParent="true"
android:layout_weight="1.5"
android:padding="#dimen/activity_mother_drawer_image_padding"
android:scaleType="centerInside"
android:src="#drawable/ic_drawer_settings" />
<android.support.v7.widget.AppCompatTextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_below="#id/activityMotherDrawerSettingsIv"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:gravity="top|center"
android:text="#string/activity_mother_drawer_setting"
android:textColor="#color/colorGray"
android:textSize="#dimen/activity_mother_drawer_text_size" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</com.app.ui.view.ScrimInsetsFrameLayout>`
Whenever I tried to fetch the layoutParam using getLayoutParams it returns FrameLayout where its ParentView is LinearLayout.
Using Java :
mDashboardLayout = (LinearLayout)mDrawerInnerRootLayout.findViewById(R.id.activityMotherDrawerDashboardLayout);
LinearLayout.LayoutParams mParams = (LinearLayout.LayoutParams) mDashboardLayout.getLayoutParams();//returns FrameLayout instead where it should return LinearLayout
mDashboardLayout.setVisibility(View.GONE); //it acts as INVISIBILITY
Can anyone enlighten me why it returns FrameLayout instead of
LinearLayout Or Why its Visibility changes to INVISIBLE instead of GONE?
Though changing View - Visibility to either GONE or INVISIBLE still View is always INVISIBLE. It does accept onClick event. As per SO, I want to completely remove that view. I am able to achieve it using following method.
I am adding View to ParentView hierarchy accordingly programmatically and refreshing the whole layout.
View inflatedDashboardView;
inflatedDashboardView = mInflater.inflate(R.layout.layout_drawer_dashboard, mDrawerInnerRootLayout, false);
mDashboardLayout = (LinearLayout) inflatedDashboardView.findViewById(R.id.activityMotherDrawerDashboardLayout);
mDrawerInnerRootLayout.addView(inflatedDashboardView);
mDrawerInnerRootLayout.forceLayout();
Still I'm not able to understand why InnerView Layout inside of DrawerLayout always return FrameLayout.
I am unable to scroll my scrollview. It has a textView, an imageview and few linear layouts inside of it. When I replace the imageview and linear layouts with textview it is working. Here is my code:
<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" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:scrollbars="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Drop Text Down"
android:textAppearance="?android:attr/textAppearanceLarge" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="555dp"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
</LinearLayout>
<LinearLayout
android:id="#+id/ln"
android:layout_width="match_parent"
android:layout_height="100dp"
android:background="#000000"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextViewTextView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
Put an empty view with fixed height
<View
android:layout_width="match_parent"
android:layout_height="50dp" />
as your last item in the linear layout which is a child of scroll view..
This is just a trick, I don't know the logic. Maybe, in the presence of bottom navigation bar, the scrollview behaves like that. So if we add another view with height that matches the bottom navigation bar's height(40-50) it performs well.
The child view of a ScrollView should be set to wrap_content. If you set it to match_parent, it will fill the area of the ScrollView and never scroll, because it won't be larger than the ScrollView.
Try changing the child LinearLayout layout_height to either wrap_content or a specific size (in dp) instead of match_parent.
Your ScrollView child needs to have its height as wrap_content :
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:scrollbars="vertical" >
...
...
</LinearLayout>
</ScrollView>
I was able to solve it by adding (android:windowSoftInputMode="adjustResize|stateHidden") in the activity manifest, like below
<activity
android:name=".ui.main.MainActivity"
android:label="#string/app_name"
android:windowSoftInputMode="adjustResize|stateHidden"
android:screenOrientation="portrait"
android:theme="#style/AppTheme.NoActionBar">
</activity>
Maybe i'm too late for this, but the only method that helped me fix the scrollview not scrolling after hours of searching and trial and error method is, setting scrollview's height to 0dp.
Here's an example xml code for reference.
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:fillViewport="true">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
The top constraint layout having "match_parent" will fit the screen.
The Scroll View inside having "0dp" adjusts the height to the content inside and creates a scrollable view when the content goes beyond the top/ parent constraint layout.
The inner most constraint layout (child of scroll view) will contain the views and actual contents.
You should set the height of LinearLayout (child of Scrollview) to wrap_content.
When the child is taller than the ScrollView, then android:fillViewport="true" attribute has no effect.
If you set the height of the Linear layout inside the scrollview to match_parent, there is nothing to scroll if the height is the same as the scrollview.
You have to set it to wrap-content. This way the height of the LinearLayout will be greater than the scrollview, and you will then be able to scroll.
Corect Code
<ScrollView
android:id="#+id/scrollView1"
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:animateLayoutChanges="true"
android:orientation="vertical"
android:scrollbars="vertical" >
Aditional Note:
You also cannot use weight and weightsum inside a scrollview. Doing so will make the scrollview unscrollable.
use Table Layout instead of linear layout something like this:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:fadeScrollbars="false" android:padding="6dip">
<TableLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:stretchColumns="1"
>
//your stuff
</TableLayout>
</ScrollView>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
Remove android:fillViewport="true" from above element
I am trying to make a two pane layout for my app, and i'm trying to use layout_weight but its not working correctly. It looks like it's reversed so that the view with more weight is smaller but when I checked it didn't even do that. Here is my xml:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1">
<EditText
android:id="#+id/etSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/search" >
<requestFocus />
</EditText>
<ListView
android:id="#+id/bookList"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<fragment
android:id="#+id/fmtEditBook"
android:layout_weight="2"
android:layout_height="match_parent"
android:layout_width="0dp"
android:name="com.perlib.wmbg.fragments.EditBookFragment"
tools:layout="#layout/fragment_edit_book" />
</LinearLayout>
The outer LinearLayout has width wrap_content, change it to match_parent
I don't know the exact reason why this happens, but if you change the layout_width of the root element to match_parent it will work correctly.
I'm creating dynamically views containing a LinearLayout and add them to an outer LinearLayout. I would like to set a margin around the created views, but the layout_margin in the XML file is ignored. It works, if I set the parameters in the code, but I would like to specify the margin in the layout XML.
Setting the margin in the XML layout will be ignored:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:orientation="vertical" >
...
</LinearLayout>
Setting the margin while creating is honored:
LinearLayout productView = (LinearLayout) getLayoutInflater().inflate(R.layout.product_preview, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.setMargins(50, 50, 50, 50);
productView.setLayoutParams(params);
This is the outer layout. The views are added to dealer_activity_product_list.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/dealer_activity_dealer_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:contentDescription="#string/dealer_activity_dealer_image_desc" />
<TextView
android:id="#+id/dealer_activity_dealer_address"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/dealer_activity_product_list1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/dealer_activity_product_list2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" />
</LinearLayout>
</LinearLayout>
Are you setting the attribute for the inner LinearLayout or the containing LinearLayout?
At least, the following does work inside a LinearLayout:
<TextView
android:id="#+id/xxx"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_margin="10dp"
/>
There is a common "nested layouts" pattern.
That is, you create an auxiliary container layout and achieve the desired effect positioning the internal layout within the container layout.
Sort of:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
...
android:layout_margin="20dp"
>
...
</LinearLayout>
</LinearLayout>
set padding instead of layout_margin in outer view.
Hope it will work