Here is my 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"
>
<Button
android:text="button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
/>
<TextView
android:text="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
when API>20,TextView was cover by the button,how to fix it?if the first child is a TextView,it does not happen.Does it a bug in android?
You have root layout match_parent in height and width
you are occupying complete space by button with android:layout_height=match_parent
and for textview you are not using reference of button to place over the layout Relative layout provides android:layout_below attribute you can use this for placing your textview below button
check below example will give you more details
<?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"
>
<Button
android:text="button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button"
android:background="#ffffff"
/>
<TextView
android:text="right"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/button"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
To Highlight : This is not bug in android you are not using it in proper way
Try this,
<?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"
>
<Button
android:text="button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:layout_toLeftOf="#+id/tv_right"
/>
<TextView
android:id="#+id/tv_right"
android:text="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
/>
</RelativeLayout>
Related
I have 2 buttons but both of them are on the same line, so one of them is not clickable and not even readable.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="#343535"
tools:context=".fragments.GeneralFragment">
<Button
android:id="#+id/hello"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:text="#string/hello"
/>
<Button
android:id="#+id/observed"
android:layout_width="match_parent"
android:layout_height="60dp"
android:drawableBottom=""
android:gravity="center_vertical"
android:text="#string/observed" />
</FrameLayout>
How can I display them one below the other?
You are currently using FrameLayout, which should only be used when you have only one view under the FrameLayout.
To fulfill your requirements, you should use LinearLayout with vertical orientation.
You have to use RelativeLayout or LinearLayout
<RelativeLayout 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:background="#343535"
tools:context=".fragments.GeneralFragment">
<Button
android:id="#+id/hello"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:text="#string/hello"
/>
<Button
android:id="#+id/observed"
android:layout_below="#+id/hello" //Added Layout below
android:layout_width="match_parent"
android:layout_height="60dp"
android:drawableBottom=""
android:gravity="center_vertical"
android:text="#string/observed" />
</RelativeLayout>
Using LinearLayout you can set android:orientation="vertical" to get element adjusted to below automatically
The most reliable layout is the constraint layout you can work with just like that
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="Button2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/button1" />
</androidx.constraintlayout.widget.ConstraintLayout>
The easiest way is just using LinearLayout:
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:background="#343535"
android:orientation="vertical"
tools:context=".fragments.GeneralFragment">
<Button
android:id="#+id/hello"
android:layout_width="match_parent"
android:layout_height="60dp"
android:gravity="center_vertical"
android:text="#string/hello" />
<Button
android:id="#+id/observed"
android:layout_width="match_parent"
android:layout_height="60dp"
android:drawableBottom=""
android:gravity="center_vertical"
android:text="#string/observed" />
</LinearLayout>
How do I make LinearLayout in Android Studio at the bottom of the page. The result from the code below
I want the highlighted darken layout to be at the bottom of the page.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
tools:context=".MainPage"
android:orientation="vertical"
android:background="#ffffff">
<Button
android:id="#+id/taptoscan"
android:layout_marginTop="40dp"
android:layout_marginBottom="10dp"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#drawable/barcode"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap to Scan"
android:fontFamily="#font/antic"
android:layout_gravity="center"
android:textSize="18sp"/>
<!--here is the layout i want to put at the bottom of the page-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/colorPrimaryDark"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:gravity="bottom">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RelativeLayout>
</LinearLayout>
In the above code I tried the below and did not worked.
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:gravity="bottom"
You can use RelativeLayout at the outside. Something like this, and then ADD new one LinearLayout after that.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:background="#ffffff"
android:orientation="vertical"
tools:context=".MainPage">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/taptoscan"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:layout_marginBottom="10dp"
android:background="#drawable/barcode" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="#font/antic"
android:text="Tap to Scan"
android:textSize="18sp" />
</LinearLayout>
<!--here is the layout i want to put at the bottom of the page-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:background="#color/colorPrimaryDark"
android:gravity="bottom"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
Use Frame Layout as base layout to do it in the best way, it's gonna work definitely
Just use layout_gravity="bottom" in the LinearLayout which you want at bottom and
Just like this :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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:background="#ffffff"
android:orientation="vertical"
tools:context=".MainPage">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/taptoscan"
android:layout_width="120dp"
android:layout_height="120dp"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:layout_marginBottom="10dp"
android:background="#drawable/barcode" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="#font/antic"
android:text="Tap to Scan"
android:textSize="18sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:background="#color/colorPrimaryDark"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</FrameLayout>
The approach below would help you achieve your design specifications with zero overlappings. Basically, there are four key steps:
Change your root layout to a RelativeLayout.
Inside the RelativeLayout, add a LinearLayout that would contain your Button and your TextView.
Inside the same RelativeLayout, add the LinearLayout that you intend to pin to the bottom of the screen.
Make sure yoU set the first LinearLayout on top of the second by using the android:layout_above="" attribute.
Now, here's the code:
<?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"
tools:context=".MainPage">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
android:layout_above="#+id/bottomView">
<Button
android:id="#+id/taptoscan"
android:layout_marginTop="40dp"
android:layout_marginBottom="10dp"
android:layout_width="120dp"
android:layout_height="120dp"
android:background="#drawable/barcode"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tap to Scan"
android:fontFamily="#font/antic"
android:layout_gravity="center"
android:textSize="18sp"/>
</LinearLayout>
<!--here is the layout i want to put at the bottom of the page-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:id="#+id/bottomView"
android:background="#color/colorPrimaryDark"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
android:gravity="bottom">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
I hope this helps.
I want to put button like above picture. But I have below image. I can't success this. Search a lot of but can't find it.
LinearLayout in LinearLayout has transparent background and there is button left of this.
Activity.xml :
<?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="#drawable/genel_arkaplan"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_width="750dp"
android:layout_height="wrap_content"
android:paddingLeft="40dp"
android:layout_gravity="center_vertical|center_horizontal"
android:background="#drawable/lay_transparan_aramasecenek"
android:layout_marginTop="100dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/lay_transparan_aramasecenek_beyaz">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:background="#drawable/kod_ile_arama"
android:id="#+id/imageButton" />
</LinearLayout>
</LinearLayout>
remove android:paddingLeft="40dp from LinearLayout Tag
Just remove below code from your "LinearLayout"
android:paddingLeft="40dp"
This is adding extra padding to your parent layout.
Below will be your final 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="match_parent"
android:background="#drawable/genel_arkaplan"
android:orientation="vertical" >
<LinearLayout
android:orientation="vertical"
android:layout_width="750dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:background="#drawable/lay_transparan_aramasecenek"
android:layout_marginTop="100dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/lay_transparan_aramasecenek_beyaz">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitCenter"
android:background="#drawable/kod_ile_arama"
android:id="#+id/imageButton" />
</LinearLayout>
</LinearLayout>
Try this code for batter result for your Button Icon. I Hope it is helpful for You.
<?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="#color/color_green"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/dimen_50"
android:layout_gravity="center"
android:layout_marginLeft="#dimen/dimen_10"
android:layout_marginRight="#dimen/dimen_10"
android:layout_marginTop="100dp"
android:background="#android:color/holo_blue_light">
<Button
android:id="#+id/imageButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#mipmap/ic_share"
android:scaleType="fitCenter" />
</RelativeLayout>
</LinearLayout>
Hey I'm making a simple chat application. I'm having these two layouts to pass for the inflater to add to the RecyclerView:
received_msg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#f00">
<TextView
android:text="Hello"
android:textColor="#fff"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
sent_msg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#fff"
android:layout_gravity="right">
<TextView
android:text="Text"
android:textColor="#f00"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
RecyclerView:
<android.support.v7.widget.RecyclerView
android:padding="10dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
The layout_gravity property seem to have no effect on the sent_msg.xml's LinearLayout. What to do?
You can make the two item XML's full width (match_parent) and move the layout_gravity and background attributes to the child TextView:
sent_msg.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent" (* Changed)
android:layout_height="wrap_content">
<TextView
android:text="Text"
android:background="#fff" (* Changed)
android:textColor="#f00"
android:layout_gravity="right" (* Changed)
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I have tried almost everything from Gravity to alignParentTop and layout_above but nothing seems to be working here.
I have an ImageView and a TextView I am using ViewPager to display different images and their respective text.
The Problem is that, its displaying Text at the top and image after it.
I want it to display Image and beneath it display TextView.
This is what i am getting after adding following xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<TextView
android:id="#+id/image_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Image Text"
/>
</RelativeLayout>
Kindly guide me how to put Text after the Image
Try this it may help you
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="300sp"
android:src="#drawable/wallpaper"
android:scaleType="centerCrop"
android:id="#+id/imageView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is test"
android:textColor="#000"
android:textSize="20dp"
android:gravity="center"
android:layout_alignBottom="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Using the android:layout_below attribute should solve your problem:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<TextView
android:id="#+id/image_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_below="#+id/image_view"
android:text="Image Text"
/>
</RelativeLayout>
Add android:layout_below="#+id/image_view" in your TextView
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:layout_height="200dp"
/>
<TextView
android:id="#+id/image_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/image_view"
android:paddingBottom="8dp"
android:text="Image Text"
/>
</RelativeLayout>
Hope it helps you!!
You can define this structure tow way, first one define Textview property android:layout_below as ImageView id like bellow
<?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:orientation="vertical">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<TextView
android:id="#+id/image_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/image_view"
android:layout_gravity="bottom"
android:text="Image Text" />
</RelativeLayout>
And other one use LineLinearLayout and define orientation vertical like following
<?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:orientation="vertical">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
/>
<TextView
android:id="#+id/image_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image Text" />
</LinearLayout>