I have simple layout with 4 different levels/modes you can play. Problem is when i preview layout on different screen sizes it doesn't appear same:
Image
Here is layout code:
<?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="match_parent"
android:background="#000000">
<TextView
android:layout_width="match_parent"
android:layout_height="120dp"
android:text="play"
android:gravity="center"
android:textSize="50sp"
android:id="#+id/one"
android:layout_alignParentTop="true"
android:background="#drawable/mode1background"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:textColor="#000000" />
<TextView
android:layout_width="match_parent"
android:layout_height="120dp"
android:text="play"
android:gravity="center"
android:textSize="50sp"
android:id="#+id/two"
android:background="#drawable/mode2background"
android:layout_below="#+id/one"
android:layout_alignParentRight="true"
android:layout_alignParentLeft="true"
android:textColor="#000000" />
<TextView
android:layout_width="match_parent"
android:layout_height="120dp"
android:text="play"
android:gravity="center"
android:textSize="50sp"
android:id="#+id/three"
android:background="#drawable/mode3background"
android:layout_below="#+id/two"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:textColor="#000000" />
<TextView
android:layout_width="match_parent"
android:layout_height="120dp"
android:text="play"
android:gravity="center"
android:textSize="50sp"
android:id="#+id/four"
android:background="#drawable/mode4background"
android:layout_below="#+id/three"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:textColor="#000000" />
</RelativeLayout>
How can I make each TextView be 1/4 of screen size.
You could use a vertical LinearLayout with android:layout_weight attributes.
Something 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:background="#000000"
android:orientation="vertical">
<TextView
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:background="#drawable/mode1background"
android:gravity="center"
android:textColor="#000000"
android:textSize="50sp"/>
<TextView
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:background="#drawable/mode2background"
android:gravity="center"
android:textColor="#000000"
android:textSize="50sp"/>
<TextView
android:id="#+id/three"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:background="#drawable/mode3background"
android:gravity="center"
android:textColor="#000000"
android:textSize="50sp"/>
<TextView
android:id="#+id/four"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:background="#drawable/mode4background"
android:gravity="center"
android:textColor="#000000"
android:textSize="50sp"/>
</LinearLayout>
Check out the developer's guide on LinearLayout and Layout Weight:
https://developer.android.com/guide/topics/ui/layout/linear.html
Change parent layout to LinearLayout with vertical orientation. Then set each child's height to 0dp and weight to 1
Example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="1"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="2"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="3"/>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="4"/>
</LinearLayout>
create the following directories in resourcefolder and place different resolution images of background in respective directories
drawable-ldpi
drawable-mdpi
drawable-hdpi
drawable-xhdpi
drawable-xxhdpi
drawable-xxxhdpi
note the name of image in all folders should be same, android will automatically pick the image from the respective folder depending upon the dpi of phone
As you want to occupy the full height of the device you will need to use linear layout with vertical oreintation and giving attribute 'layout_weight' = 1 for all the TextView.
Sample code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical">
<TextView
android:id="#+id/one"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:background="#drawable/mode1background"
android:gravity="center"
android:text="play"
android:textColor="#000000"
android:textSize="50sp" />
<TextView
android:id="#+id/two"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/one"
android:layout_weight="1"
android:background="#drawable/mode2background"
android:gravity="center"
android:text="play"
android:textColor="#000000"
android:textSize="50sp" />
<TextView
android:id="#+id/three"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/two"
android:layout_weight="1"
android:background="#drawable/mode3background"
android:gravity="center"
android:text="play"
android:textColor="#000000"
android:textSize="50sp" />
<TextView
android:id="#+id/four"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/three"
android:layout_weight="1"
android:background="#drawable/mode4background"
android:gravity="center"
android:text="play"
android:textColor="#000000"
android:textSize="50sp" />
</LinearLayout>
Related
I've got an app I'm working on that I inherited. I'm trying to make it so the box in the image provided below labeled "RelativeLayout" stretches all the way to edge of the screen horizontally, so that there's no white space in between it and the screen. Any way to do that? I've tried "match_parent" and "wrap_content" but neither of those do the trick
enter image description here
<?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:orientation="vertical"
android:id="#+id/view_cont"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_margin="1dp"
android:elevation="1dp"
card_view:cardCornerRadius="15dp"
card_view:cardElevation="1dp"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#406490">
<TextView
android:id="#+id/leg_name"
android:layout_width="207dp"
android:layout_height="44dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="12dp"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:fontFamily="sans-serif-thin"
android:gravity="right"
android:text="Name"
android:textColor="#color/colorPrimary"
android:textSize="18sp" />
<TextView
android:id="#+id/leg_office"
android:layout_width="207dp"
android:layout_height="64dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="64dp"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:fontFamily="sans-serif-thin"
android:gravity="right"
android:text="President of the United States Amercia"
android:textColor="#color/colorPrimary" />
<ImageView
android:id="#+id/leg_photo"
android:layout_width="127dp"
android:layout_height="121dp"
android:layout_below="#+id/leg_name"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="-47dp"
card_view:srcCompat="#drawable/icon1" />
</RelativeLayout>
</android.support.v7.widget.CardView>
this is happening because you added a margin of 1dp to your parent CardView. Also because card_view:cardUseCompatPadding is set to TRUE
Try the following code:
<?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:orientation="vertical"
android:id="#+id/view_cont"
xmlns:card_view="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
android:elevation="1dp"
card_view:cardCornerRadius="15dp"
card_view:cardElevation="1dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#406490">
<TextView
android:id="#+id/leg_name"
android:layout_width="207dp"
android:layout_height="44dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="12dp"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:fontFamily="sans-serif-thin"
android:gravity="right"
android:text="Name"
android:textColor="#color/colorPrimary"
android:textSize="18sp" />
<TextView
android:id="#+id/leg_office"
android:layout_width="207dp"
android:layout_height="64dp"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="64dp"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:fontFamily="sans-serif-thin"
android:gravity="right"
android:text="President of the United States Amercia"
android:textColor="#color/colorPrimary" />
<ImageView
android:id="#+id/leg_photo"
android:layout_width="127dp"
android:layout_height="121dp"
android:layout_below="#+id/leg_name"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="-47dp"
card_view:srcCompat="#drawable/ic_directions_walk_black_24dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Hope this works, good luck!
Hi i had to make a layout with card with recyclerview so for that i set an image as a background to linear layout but now i'm unable to center crop that image
My problem is that i cannot use imageview because using that the height of card increases as well and i don't want that so please if someone can assit me..
my xml
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/primary_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="15dp"
app:cardCornerRadius="115dp">
<ImageView
android:id="#+id/background_image_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:visibility="gone" />
<LinearLayout
android:id="#+id/background_image"
android:layout_width="match_parent"
android:layout_height="210dp"
android:background="#drawable/club1"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:src="#drawable/ellipse" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<com.ct.listrtrial.Custom.CustomTextViewMedium
android:id="#+id/first_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:padding="5dp"
android:text="John Doe"
android:textColor="#color/White"
android:textSize="15sp" />
<com.ct.listrtrial.Custom.CustomTextViewMedium
android:id="#+id/second_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:padding="5dp"
android:text="checked in to"
android:textColor="#color/White"
android:textSize="10sp" />
<com.ct.listrtrial.Custom.CustomTextViewMedium
android:id="#+id/third_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:padding="5dp"
android:text="W south"
android:textColor="#color/White"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<com.ct.listrtrial.Custom.CustomTextViewMedium
android:id="#+id/fourth_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="9dp"
android:text="beach mumbai"
android:textColor="#color/White"
android:textSize="15sp" />
<com.ct.listrtrial.Custom.CustomTextViewMedium
android:id="#+id/fifth_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/second_text"
android:layout_toRightOf="#+id/fourth_text"
android:text="30 mins ago."
android:textColor="#color/White"
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="85dp">
<com.ct.listrtrial.Custom.CustomTextViewMedium
android:id="#+id/sixth_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:padding="10dp"
android:text="reply to abc............"
android:textColor="#color/White" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true">
<ImageView
android:id="#+id/favourite_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/ic_favorite_border_black_24dp" />
<com.ct.listrtrial.Custom.CustomTextViewMedium
android:id="#+id/seventh_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:text="40 likes"
android:textColor="#color/White" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
You can't center crop a background image of linear layout,
But you can achieve the same using these changes:
Replace you LinearLayout with RelativeLayout
Place a ImageView as a first child inside relative layout and use property for center crop.
<RelativeLayout
android:id="#+id/background_image"
android:layout_width="match_parent"
android:layout_height="210dp"
android:orientation="vertical">
<ImageView
android:scaleType="centerCrop"
android:src="#drawable/club1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
.....
......
</RelativeLayout>
If you Want to control The Image View Scale
you need to put it in ImageView As Src Not Background
You can use Frame layout To Achieve it
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/primary_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="19dp"
android:layout_marginRight="19dp"
android:layout_marginTop="15dp"
app:cardCornerRadius="115dp">
<ImageView
android:id="#+id/background_image_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:visibility="gone" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="210dp">
<ImageView
android:layout_width="match_parent"
android:src="#mipmap/ic_launcher"
android:layout_height="match_parent" />
<LinearLayout
android:id="#+id/background_image"
android:layout_width="match_parent"
android:layout_height="210dp"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/profile_image"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="20dp"
android:src="#drawable/playstore_icon" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp">
<TextView
android:id="#+id/first_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:padding="5dp"
android:text="John Doe"
android:textSize="15sp" />
<TextView
android:id="#+id/second_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:padding="5dp"
android:text="checked in to"
android:textSize="10sp" />
<TextView
android:id="#+id/third_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:padding="5dp"
android:text="W south"
android:textSize="15sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/fourth_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="9dp"
android:text="beach mumbai"
android:textSize="15sp" />
<TextView
android:id="#+id/fifth_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/second_text"
android:layout_toRightOf="#+id/fourth_text"
android:text="30 mins ago."
android:textSize="10sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="85dp">
<TextView
android:id="#+id/sixth_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:padding="10dp"
android:text="reply to abc............"
android:textColor="#color/white" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true">
<ImageView
android:id="#+id/favourite_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/ic_close" />
<TextView
android:id="#+id/seventh_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:text="40 likes"
android:textColor="#color/white" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</FrameLayout>
</android.support.v7.widget.CardView>
CenterCrop logic is baked inside ImageView. The correct solution is to extract it into a Drawable which wraps another Drawable and centerCrops it.
If you use CoordinatorLayout, then just to put your ImageView with centerCrop parameter to top.
<CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:scaleType="centerCrop"
android:background="#drawable/background_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:elevation="0dp">
<include layout="#layout/header_main" />
<TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabIndicatorColor="#android:color/white"
android:background="#drawable/white_border_background"
app:tabIndicatorHeight="3dp"/>
</AppBarLayout>
<ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</CoordinatorLayout>
I am trying to set my admob banner on bottom of the screen but it's not showing. I have tried all possible way to set it as bottom but I am unable to set it...its going down side of expandable list view.
My XML is like below...let me know if someone can help me to sort out issue.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#e7e7e7"
android:gravity="center"
android:orientation="vertical"
android:weightSum="1">
<include
layout="#layout/top_points_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<View
android:layout_width="match_parent"
android:layout_height="#dimen/five">
</View>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical">
<ImageView
android:layout_width="#dimen/laytop_imgwidthheight"
android:layout_height="#dimen/laytop_imgwidthheight"
android:contentDescription="#string/app_name"
android:src="#drawable/app_icon"
android:visibility="gone" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:gravity="left"
android:padding="#dimen/five"
android:text="Country of Purchase"
android:textColor="#color/Gray"
android:textSize="18sp"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/layout_country_selection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:layout_marginTop="5dp"
android:background="#drawable/list_item_background"
android:orientation="horizontal"
android:padding="6dp">
<com.commonutility.RoundImageView
android:id="#+id/imageView_country_logo"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_gravity="center_vertical"
android:layout_marginBottom="4dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:src="#drawable/country_default_logo"
android:visibility="visible" />
<TextView
android:id="#+id/textView_country_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_toRightOf="#+id/imageView_country_logo"
android:padding="5dp"
android:text="Select Your Country"
android:textColor="#color/Blue"
android:textSize="19sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:layout_marginRight="5dp"
android:padding="3dp"
android:src="#drawable/dropdown" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="gone"
android:weightSum="2">
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="#dimen/five"
android:layout_weight="1"
android:gravity="right"
android:paddingBottom="#dimen/five"
android:paddingTop="#dimen/five"
android:text="Country"
android:textColor="#fe4080"
android:textSize="#dimen/rewards_countrytextsize"
android:textStyle="bold" />
<TextView
android:id="#+id/txtcountry"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left"
android:paddingBottom="#dimen/five"
android:paddingTop="#dimen/five"
android:text="USA"
android:textColor="#color/material_color_primary_dark"
android:textSize="#dimen/rewards_countrytextsize"
android:textStyle="bold" />
</LinearLayout>
<ExpandableListView
android:id="#+id/lvExp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#drawable/list_item_background"
android:cacheColorHint="#android:color/transparent"
android:divider="#color/md_white_1000"
android:dividerHeight="0dp"
android:groupIndicator="#null"
android:scrollbars="none" />
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:clickable="false"
android:nestedScrollingEnabled="true"
ads:adSize="SMART_BANNER"
ads:adUnitId="#string/banner_home_footer">
</com.google.android.gms.ads.AdView>
</LinearLayout>
Let me know what is wrong in this.
Thanks :)
android:layout_alignParentBottom="true"
works only in RelativeLayout, but it's inside a LinearLayout.
You could change the root layout to RelativeLayout and wrap everything but your AdView inside another LinearLayout (or better handle the behaviour of the other views as expected inside a RelativeLayout to avoid deep layouts).
I'm making a layout:
Here is a code:
<?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:background="#373c5c"
android:gravity="center_vertical"
android:orientation="vertical" >
<LinearLayout android:id="#+id/list_offer_item_container"
android:layout_marginTop="15dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="#+id/imgcreditcompany"
android:layout_width="36dp"
android:layout_marginRight="4dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:contentDescription="#string/app_name"
android:layout_height="36dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="start"
android:orientation="vertical" >
<TextView
android:id="#+id/txtname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="1dp"
android:textColor="#48899f"
android:textSize="#dimen/textsizeearncredit_title"
android:text="Name"
android:textAllCaps="false"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/txtdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="1dp"
android:maxLines="1"
android:textColor="#80869c"
android:textSize="#dimen/textsizeearncredit_desc"
android:text="This is a description of the offer and this is just a demo to show off 3 lines stacking correctly on top of each other"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="20dp"
android:gravity="end"
android:layout_gravity="center_vertical">
<TextView android:id="#+id/list_offer_badge_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/list_offer_item_container"
android:textColor="#color/md_amber_700"
android:textSize="12sp"
android:visibility="gone"
android:text=""/>
</LinearLayout>
<ImageView
android:id="#+id/nextArrow"
android:layout_alignParentRight="true"
android:tint="#color/md_grey_300"
android:rotation="180"
android:layout_width="32dp"
android:visibility="gone"
android:layout_marginRight="16dp"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_height="32dp"
android:contentDescription="#string/app_name"
android:padding="#dimen/two" />
</LinearLayout>
</RelativeLayout>
How to add an text above the 'button style' linear layout?
I want for example "Hello dear user! Check it below".
Where put textview, where add more layout or whatever?
It is very simple!
You have to add TextView, with id (text, for example) and, in your LinearLayout you need to add line android:layout_below="#+id/text". Optional, you can specify attribute android:layout_alignParentTop="true" in TextView.
Just use code 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:background="#373c5c"
android:gravity="center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:text="Text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
/>
<LinearLayout android:id="#+id/list_offer_item_container"
android:layout_below="#+id/text"
android:layout_marginTop="15dp"
android:layout_marginLeft="50dp"
android:layout_marginRight="50dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/shape"
android:orientation="horizontal"
android:padding="10dp">
<ImageView
android:id="#+id/imgcreditcompany"
android:layout_width="36dp"
android:layout_marginRight="4dp"
android:layout_marginTop="0dp"
android:layout_marginBottom="0dp"
android:contentDescription="#string/app_name"
android:layout_height="36dp" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:gravity="start"
android:orientation="vertical" >
<TextView
android:id="#+id/txtname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="1dp"
android:textColor="#48899f"
android:textSize="#dimen/textsizeearncredit_title"
android:text="Name"
android:textAllCaps="false"
android:textStyle="normal|bold" />
<TextView
android:id="#+id/txtdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="1dp"
android:maxLines="1"
android:textColor="#80869c"
android:textSize="#dimen/textsizeearncredit_desc"
android:text="This is a description of the offer and this is just a demo to show off 3 lines stacking correctly on top of each other"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginRight="20dp"
android:gravity="end"
android:layout_gravity="center_vertical">
<TextView android:id="#+id/list_offer_badge_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/list_offer_item_container"
android:textColor="#color/md_amber_700"
android:textSize="12sp"
android:visibility="gone"
android:text=""/>
</LinearLayout>
<ImageView
android:id="#+id/nextArrow"
android:layout_alignParentRight="true"
android:tint="#color/md_grey_300"
android:rotation="180"
android:layout_width="32dp"
android:visibility="gone"
android:layout_marginRight="16dp"
android:layout_gravity="center"
android:layout_centerVertical="true"
android:layout_height="32dp"
android:contentDescription="#string/app_name"
android:padding="#dimen/two" />
</LinearLayout>
</RelativeLayout>
Above
<LinearLayout android:id="#+id/list_offer_item_container"
put your TextView or whatever you want. So something like
<TextView android:id="#+id/myTV"
// other attributes />
Then in
<LinearLayout android:id="#+id/list_offer_item_container"
add android:layout_below="#id/myTV"
By default, RelativeLayout puts it's elements at the top-left until you specify otherwise. So adding this will put your LinearLayout below the TextView.
I think the confusion is on how RelativeLayout and LinearLayout work. You have orientation in your RelativeLayout which does nothing. RelativeLayout puts it's elements in the order you tell it by the attributes such as layout_below and layout_above. LinearLayout, however, adds them in the order in which they show in the xml file which is why orientation matters in LinearLayout but not RelativeLayout.
You'll want to look at RelativeLayout.LayoutParams and other parts of the documentation to better understand how the layouts work and which is best in your specific cases.
I am stuck in a problem and not able find any solution. This problem may sound very naive but I am really unable to figure out the exact cause. I have a ScrollView ans inside that I have a RelativeLayout. But my RelativeLayout is not scrolling irrespective of the content. I have followed https://stackoverflow.com/relativelayout-with-scrollview-not-scrolling and https://stackoverflow.com/relativelayout-inside-scrollview-not-working and few more also. Please check where I am doing wrong. Thanks.
xml code :
<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="wrap_content"
tools:context=".MainActivity"
android:scrollbars="vertical"
android:background="#drawable/cover_image"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/cover_img_labels"
android:layout_width="300dp"
android:layout_height="105dp"
android:background="#drawable/cover_image_labels"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<RelativeLayout
android:id="#+id/rl_login"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:layout_below="#+id/cover_img_labels"
android:layout_centerHorizontal="true">
<EditText
android:id="#+id/et_login_userName"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:background="#drawable/edittext_style"
android:textColor="#7593B0"
android:singleLine="true"
android:paddingLeft="10dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:hint="#string/hint_login_user" />
<EditText
android:id="#+id/et_login_password"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/et_login_userName"
android:textColor="#7593B0"
android:layout_marginTop="10dp"
android:paddingLeft="10dp"
android:background="#drawable/edittext_style"
android:singleLine="true"
android:hint="#string/hint_login_password"
android:imeOptions="actionDone"
android:inputType="textPassword" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_marginTop="100dp"
android:orientation="horizontal"
android:id="#+id/linearLayout">
<Button
android:id="#+id/bt_login_submit"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#string/text_login_submit"
android:layout_marginRight="2dp"
android:textColor="#android:color/white"
android:background="#drawable/button_drawable"
android:layout_below="#+id/et_login_password" />
<Button
android:id="#+id/bt_register_submit"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#drawable/button_drawable"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:textColor="#android:color/white"
android:text="Register your card" />
</LinearLayout>
<ProgressBar
android:id="#+id/pb_login_progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:visibility="gone" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Forgot Password"
android:id="#+id/bt_forgot_password"
android:textColor="#color/pink"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/forgot_button_drawable"
android:layout_below="#+id/linearLayout"
android:layout_centerHorizontal="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_below="#+id/bt_forgot_password"
android:gravity="center"
android:id="#+id/lll"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2"
android:layout_alignRight="#+id/button"
android:layout_alignEnd="#+id/button" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
I just run your code, it cannot scroll because height of relative layout not enough. You only scroll when height of ScrollView smaller than height of ScrollView's child.
You can change RelativeLayout's height from wrap_content to 1000dp to test scroll.