I want the ImageView keep its aspect ratio with layout_height=match_parent
but I don't know why this happen ?
<?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"
android:layout_height="match_parent">
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:background="#drawable/bob" />
<TextView
android:gravity="center"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bob" />
</LinearLayout>
</LinearLayout>
<?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"
android:layout_height="match_parent">
<LinearLayout
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/bob" />
<TextView
android:gravity="center"
android:textAppearance="#style/TextAppearance.AppCompat.Headline"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Bob" />
</LinearLayout>
</LinearLayout>
Use for this to set in full image view scaletype=fitxy
Related
In my Android application, I have an ExpandableListView with each group having some childs, which contains a checkbox. I'm having trouble in setting the position of the checkboxes.
Curent, my checkbox on the right of the Childs When run on phone, even though it's on the left in Childs.XML, now I want to move the checkbox to the left of the Childs.
Guide me. plsss.. Thanks all so much.
My main XML:
<?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"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
tools:context=".discover.DiscoverFilterFragment">
<ProgressBar
android:id="#+id/filter_progressBar"
style="?android:attr/progressBarStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="666dp"
android:orientation="vertical"
android:padding="0dp"
android:id="#+id/expLayout">
<ExpandableListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/expFilter"
android:groupIndicator="#drawable/expand_icon"
android:layoutDirection="rtl"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:indicatorLeft="?android:attr/expandableListPreferredItemIndicatorLeft"
/>
</LinearLayout>
<androidx.appcompat.widget.LinearLayoutCompat
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="6dp"
android:background="#drawable/btn_view_attachments">
<Button
android:id="#+id/btnApply"
android:layout_width="328dp"
android:layout_height="wrap_content"
android:background="#00FFFFFF"
android:backgroundTint="#00FFFFFF"
android:text="Apply"
android:textColor="#FFFFFF"
android:textSize="16sp" />
</androidx.appcompat.widget.LinearLayoutCompat>
</RelativeLayout>
My Parent XML:
<?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:id="#+id/titleLayout"
android:orientation="horizontal">
<TextView
android:id="#+id/listTitle"
android:text="#string/title"
android:textSize="22sp"
android:textAppearance="#style/TextAppearance.AppCompat.Medium.Inverse"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="10dp"
android:layout_marginStart="25dp"
android:paddingTop="10dp"
android:textColor="#android:color/black"/>
</LinearLayout>
My Child XML:
<?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_height="wrap_content"
android:layout_width="match_parent"
android:id="#+id/itemLayout"
android:orientation="horizontal"
android:descendantFocusability="blocksDescendants">
<CheckBox
android:id="#+id/itemsCheckbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:focusable="false"
android:clickable="false"
style="#android:style/Widget.CompoundButton.RadioButton"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="16dp"
android:id="#+id/listItem"
android:layoutDirection="ltr"
android:textAppearance="#style/TextAppearance.AppCompat.Small"
tools:text="Item 0" />
</LinearLayout>
My activity has 3 elements. Linearlayout, Gridview and the Linearlayout. See picture. How to make Gridview items is completely visible even if there is a scroll on all screens? p.s. Sorry for my English.
Well use this code for such a layout design...
<?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:orientation="vertical"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/layoutTopLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/btn_br_color"
android:gravity="center"
android:layout_alignParentTop="true"
android:padding="3dp">
<TextView
android:id="#+id/primiumGameNameText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:maxLines="4"
android:text="Layout1"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
<GridView
android:id="#+id/gridViewID"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:layout_below="#+id/layoutTopLay"
android:layout_above="#+id/layoutBottomLay"
android:paddingRight="5dp"
android:paddingTop="3dp" />
<LinearLayout
android:id="#+id/layoutBottomLay"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/btn_br_color"
android:gravity="center"
android:layout_alignParentBottom="true"
android:padding="3dp">
<TextView
android:id="#+id/layoutBottomText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:gravity="center"
android:maxLines="24"
android:text="Layout2"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
Another way how to do it is to use layout_weight attribute:
<?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"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/holo_blue_dark"
/>
<GridLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:background="#android:color/white"
>
<!-- put content here -->
</GridLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/holo_blue_dark"
/>
</LinearLayout>
Here's my MainActivity.xml where I'm 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffe6cc"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.zorgan.app.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#333"
android:textSize="30sp"
android:textStyle="bold"
tools:layout_editor_absoluteY="116dp" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="300dp"
app:srcCompat="#drawable/front"
android:contentDescription="#string/front_desc" />
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
tools:layout_editor_absoluteY="496dp" />
</LinearLayout>
How would I nest these 3 views into one view, so they all sit vertically central?
To vertically center the child views, give android:gravity="center_vertical" to the parent view. In your case, for the LinearLayout.
here try this layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffe6cc"
android:gravity="center"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.zorgan.app.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#333"
android:textSize="30sp"
android:textStyle="bold"
tools:layout_editor_absoluteY="116dp" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="300dp"
app:srcCompat="#drawable/front"
android:contentDescription="#string/front_desc" />
<com.facebook.login.widget.LoginButton
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout_editor_absoluteY="496dp" />
</LinearLayout>
in this android:gravity="center" is the key parameter.
Replace your code with,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="#ffe6cc"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#333"
android:textSize="30sp"
android:textStyle="bold"
tools:layout_editor_absoluteY="116dp" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_gravity="center_vertical|center_horizontal"
app:srcCompat="#drawable/front"
android:contentDescription="front_desc" />
<Button
android:id="#+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal" />
</LinearLayout>
It is tested and working.
I am trying to have two text and an arrow mark to the end. So I tried the following:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal"
android:layout_weight="0.25"
android:gravity="center_vertical">
<TextView
android:id="#+id/labeltext"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Specification"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:src="#drawable/ic_next"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/labelselected"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Specification down"/>
</LinearLayout>
</LinearLayout>
But I am still getting :
I thought if we put android:gravity="right" the whole image goes to the right but this didn't move?
Can somebody help me out with this?
NOTE :
first put LinearLayoutwith horizontal orientation
then put another 2 LinearLayoutinside it and set the layout_weight as your desire
then add anything that you want inside them.
thats all....
here is the corrected code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="4"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_vertical"
android:layout_weight="3">
<TextView
android:id="#+id/labeltext"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Specification" />
<TextView
android:id="#+id/labelselected"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Specification down"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:src="#android:drawable/sym_def_app_icon"/>
</LinearLayout>
</LinearLayout>
You can do this way.
Screenshot reference:
I did it by set match_parent width to TextView and assigning drawableRight property.
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/labeltext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:drawableRight="#drawable/ic_next"
android:gravity="center_vertical"
android:text="Specification" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/labelselected"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Specification down" />
</LinearLayout>
</LinearLayout>
Hope this will help you.
Instead of using an ImageView you can set your icon as a drawable to your TextView and set its width to match_parent
<TextView
...
android:drawableRight="#drawable/your_icon"/>
also I think your IDE will suggest you to remove your LinearLayout , TextView and ImageView and use my solution because using one widget instead of three is always better ;)
Change your layout and try. I am just giving hint. to place text and image in same line with distributed layout_weight. You can tweak as per your need
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:id="#+id/labeltext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="Specification"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:src="#drawable/ic_next"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/labelselected"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Specification down"/>
</LinearLayout>
</LinearLayout>
Replace this with your Upper Layout. you need to use layout_gravity="right" for imageView.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_vertical">
<TextView
android:id="#+id/labeltext"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left|center"
android:text="Specification"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:layout_gravity="right"
android:src="#drawable/ic_next"/>
</LinearLayout>
Use this it will work 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="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/labeltext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Specification" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_next" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/labelselected"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Specification down" />
</LinearLayout>
</LinearLayout>
Use FrameLayout instead of LinearLayout like here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.25"
android:gravity="center_vertical">
<TextView
android:id="#+id/labeltext"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:text="Specification"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="right"
android:src="#drawable/ic_next"
android:layout_gravity="right|center_vertical"/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/labelselected"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Specification down"/>
</LinearLayout>
Just add weight to lateltext textView as 1
<TextView
android:id="#+id/labeltext"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="left|center_vertical"
android:text="Specification"
android:layout_weight="1"/>
How to horizontally center TextView inside ImageView?
Currently is positioned in the lower right corner of ImageView.
<?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="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/img" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:layout_alignBottom="#+id/imageView"
android:layout_alignRight="#+id/imageView"
android:layout_alignEnd="#+id/imageView" />
</RelativeLayout>
If you want the TextView to fit always the ImageView size independently where it is positioned on the RelativeLayout you should try this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/img" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/textView"
android:text="hsjsdjhf"
android:gravity="center"
android:layout_alignTop="#id/imageView"
android:layout_alignBottom="#id/imageView"
android:layout_alignLeft="#id/imageView"
android:layout_alignRight="#id/imageView" />
</RelativeLayout>
Try FrameLayout instead of RelativeLayout and set gravity="center".
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/img" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView" />
</FrameLayout>
<?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">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/texts" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:text="hello world"
android:layout_centerInParent="true"/>
</RelativeLayout>
</RelativeLayout>
Try this...
To align the elements on the top of each other you should use frame layout
<?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" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/img" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
</RelativeLayout>
Try this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/img" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:text="hsjsdjhf"
android:layout_centerInParent="true" />
</RelativeLayout>