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">
......
Related
I mean how can i locate a ImageView in two layouts? i have 2 relative layouts one is up, one is down. The up one has a ImageView but i want half of this ImageView located in down layout. How can i do that?
EDIT::
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/activity_horizontal_margin"
android:background="#d2aff4">
<Space
android:id="#+id/center"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_centerInParent="true" />
<RelativeLayout
android:id="#+id/bottom"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/center"
android:background="#87c96d" />
<RelativeLayout
android:id="#+id/top"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerInParent="true"
android:src="#mipmap/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
In this scenario the "top" layout needs to be created at last to be displayed on top of the rest of the views. If you need it to have a background colour, you need to apply the colour to the main container.
Here is the result:
I agree with #Booger answer's but If your parent layout is RelativeLayout then add this below property in you ImageView.
android:layout_centerInParent="true"
Or you can use below code to create that kind of layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/gray_font">
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#color/colorAccent">
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher"
android:layout_centerInParent="true"/>
</RelativeLayout>
Take a look at this library https://github.com/umano/AndroidSlidingUpPanel
if you want something easy you can put those layouts in to a parent relative layout and then you can add a image view in parent relative layout as last child and position it how you need
I think you should wrap both your RelativeLayout in another Parent layout, and in that layout, you will place your ImageView (which will span both the other layouts.
Something like this pseudo-code:
<LinearLayout>
<RelativeLayout1>
<RelativeLayout2>
<ImageView gravity=center> //Order counts, this needs to be after the RelativeLayouts to show on top of them
</LinearLayout>
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'm trying to create 3 cardviews, each have the same height, weight etc..
I managed to create one:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="8dp"
android:padding="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerInside"
android:src="#drawable/moon20"
android:background="#android:color/white"
android:padding="8dp"/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"
android:padding="8dp"
android:text="20 min Power Nap"
android:textColor="#color/colorSecondaryText"
android:textStyle="bold"
android:textSize="20dp"
android:textAlignment="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
I need two more of this cardview, but I'm getting
"multiple root tag" error
Do I have to create a base layout like Relative layout for all cardviews?
You need a root view to put inside your CardViews. Right now you are putting one CardView inside the other (Because your root is a CardView). Try to put them inside a LinearLayout.
<LinearLayout>
<CardView>
<CardView>
<CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<CardView>
<CardView>
<CardView>
</LinearLayout>
Try to make your cardView element inside a LinearLayout or some other Layout views.
example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android" .......
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)
I try to decorate ListView items which have overlayed orange bar at the leftside of the item as attached image below.
The layout XML file is as below.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:descendantFocusability="blocksDescendants"
>
<LinearLayout
android:id="#+id/linearLayoutQuestionItemBody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="3dp"
android:paddingBottom="4dp"
android:paddingRight="3dp"
android:paddingLeft="10dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:background="#android:color/white"
>
<!-- SOME VARIABLE HEIGHT CONTENTS -->
</LinearLayout>
<!-- leftside orange bar decoration -->
<LinearLayout
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#color/orange"
android:orientation="vertical"
android:layout_marginLeft="12dp"/>
</RelativeLayout>
The XML code above has LinearLayout as a decorate child, but it doesn't matter to use RelativeLayout or other methods.
StackOverflow articles have activity-based approach but I need to use in ListView items with Adapter. what can I do?
== EDIT ==
Sadly, I cannot accept both 2 answers below, because two answers are identical to use LinearLayout (stacked left to right) as parent. Maybe there is no way to fill child height to parent as overlayed with RelativeLayout....
// 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="wrap_content"
android:layout_margin="5dp"
android:background="#android:color/white"
android:descendantFocusability="blocksDescendants">
<LinearLayout
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#android:color/holo_orange_dark"
android:orientation="vertical"/>
<LinearLayout
android:id="#+id/linearLayoutQuestionItemBody"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
Have you try this..
change the main relativelayout as LinearLayout
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="blocksDescendants"
>
and LinearLayout height match_parent
<LinearLayout
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#color/orange"
android:orientation="vertical"
android:layout_marginLeft="12dp"/>