Android XML resize / fit ImageButton different screens - android

I made ImageButton menu design in the hopes that its size can adjust the screen:
but after I test using with smartphone 3-inch 320x480
, 4-inch 480x800, 5-inch 720x1280, view ImageButton is mess.
I've tried using TableLayout, LinearLayout, GridLayout,
and FrameLayout, but the result is the same.
Thank you in advance.This picture my trial results
This my XML code :
<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="#drawable/bg"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/linear1">
<ImageView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitCenter"
android:layout_gravity="center"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:src="#drawable/title"
android:contentDescription="title"/>
</LinearLayout>
<ImageButton
android:id="#+id/imagebutton0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linear1"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:background="#drawable/image"
android:tag="0"
tools:ignore="contentDescription" />
<ImageButton
android:id="#+id/imagebutton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imagebutton0"
android:layout_toLeftOf="#+id/imagebutton4"
android:scaleType="fitXY"
android:background="#drawable/image"
android:tag="2"
tools:ignore="contentDescription" />
<ImageButton
android:id="#+id/imagebutton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imagebutton0"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:background="#drawable/image"
android:tag="4"
tools:ignore="contentDescription" />
<ImageButton
android:id="#+id/imagebutton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imagebutton0"
android:layout_toRightOf="#+id/imagebutton4"
android:scaleType="fitXY"
android:background="#drawable/image"
android:tag="3"
tools:ignore="contentDescription" />
<ImageButton
android:id="#+id/imagebutton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imagebutton4"
android:layout_centerHorizontal="true"
android:scaleType="fitXY"
android:background="#drawable/image"
android:tag="1"
tools:ignore="contentDescription" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:id="#+id/linear2"
android:layout_below="#+id/imagebutton1">
<TextView
android:id="#+id/ib1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:drawableTop="#drawable/ib1"
android:gravity="center"
android:text=""
android:scaleType="fitXY"/>
<TextView
android:id="#+id/ib2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:drawableTop="#drawable/ib2"
android:gravity="center"
android:text=""
android:scaleType="fitXY"/>
<TextView
android:id="#+id/ib3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:drawableTop="#drawable/ib3"
android:gravity="center"
android:text=""
android:scaleType="fitXY"/>
<TextView
android:id="#+id/ib4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:drawableTop="#drawable/ib4"
android:gravity="center"
android:text=""
android:scaleType="fitXY"/>
</LinearLayout>
</RelativeLayout>

I created a scratch which will define layout with linear and weight
I hope it will help you out
Also attached code with screen shots
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1"
android:gravity="center"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
</LinearLayout>

Related

Cant design pixel perfectly as like my Figma design, Constraint layout cuts out some of my views

I don't get the accurate view as like my Figma design. Constraint layout cuts out some of my views and also is not matching with top Search bar as I designed with Linear Layout. What should I do for pixel perfect design as like my Figma design?
The Figma design of my project:
Portrait view after run:
Landscape view after run:
My code:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/teal_200"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/menubar"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_marginTop="20dp"
android:orientation="vertical"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="#drawable/ic_circle_white"
android:layout_marginStart="20dp"
android:layout_marginEnd="13dp">
<ImageView
android:layout_width="16dp"
android:layout_height="9dp"
android:layout_centerInParent="true"
android:src="#drawable/ic_menu"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="265dp"
android:layout_height="46dp"
android:layout_weight="1"
android:background="#drawable/rounded_shape1"
android:verticalScrollbarPosition="right">
<ImageView
android:id="#+id/ic_search"
android:layout_width="12dp"
android:layout_height="14dp"
android:src="#drawable/ic_search"
android:layout_centerVertical="true"
android:layout_marginStart="15dp"/>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/search_txtinput"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_toEndOf="#id/ic_search"
android:background="#android:color/transparent"
android:fontFamily="#font/poppins_medium"
android:hint="Search or type address"
android:textSize="13dp"
android:paddingStart="13dp"
android:paddingTop="3dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="center"
android:background="#drawable/ic_circle_white"
android:layout_marginStart="13dp"
android:layout_marginEnd="20dp">
<ImageView
android:layout_width="13dp"
android:layout_height="13dp"
android:layout_centerInParent="true"
android:src="#drawable/ic_rounded_pg_bg"/>
</RelativeLayout>
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="40dp"
android:paddingStart="19dp"
android:paddingEnd="19dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/fb_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintEnd_toStartOf="#id/yt_btn"
tools:layout_editor_absoluteY="40dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/fblogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#drawable/ic_circle_white">
<ImageView
android:layout_width="8dp"
android:layout_height="18dp"
android:layout_centerInParent="true"
android:src="#drawable/fb_logo" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins_medium"
android:paddingTop="9dp"
android:text="Facebook"
android:textSize="12.3dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/yt_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="48dp"
app:layout_constraintStart_toEndOf="#id/fb_btn"
app:layout_constraintEnd_toStartOf="#id/twitter_btn"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.25"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/ytlogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#drawable/ic_circle_white">
<ImageView
android:layout_width="10dp"
android:layout_height="12dp"
android:src="#drawable/yt_logo"
android:layout_centerInParent="true"/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12.3dp"
android:text="Youtube"
android:paddingTop="9dp"
android:fontFamily="#font/poppins_medium"
/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/twitter_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingStart="50dp"
app:layout_constraintStart_toEndOf="#id/yt_btn"
app:layout_constraintEnd_toStartOf="#id/insta_btn"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.25"
tools:layout_editor_absoluteY="40dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/twtrlogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#drawable/ic_circle_white">
<ImageView
android:layout_width="18dp"
android:layout_height="14dp"
android:layout_centerInParent="true"
android:src="#drawable/twitter_logo" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins_medium"
android:paddingTop="9dp"
android:text="Twitter"
android:textSize="12.3dp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/insta_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingStart="48dp"
app:layout_constraintWidth_percent="0.25"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="#id/twitter_btn"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/instalogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#drawable/ic_circle_white">
<ImageView
android:layout_width="21dp"
android:layout_height="21dp"
android:src="#drawable/insta_logo"
android:layout_centerInParent="true"/>
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12.3dp"
android:text="Instagram"
android:paddingTop="9dp"
android:fontFamily="#font/poppins_medium"
/>
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Removed the start padding from each Linear Layout (parents of logos and names), and that solved the problem.
Code:
<LinearLayout
android:id="#+id/twitter_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
<!-- android:paddingStart="50dp"[Removed paddingstart] -->
app:layout_constraintStart_toEndOf="#id/yt_btn"
app:layout_constraintEnd_toStartOf="#id/insta_btn"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintWidth_percent="0.25"
tools:layout_editor_absoluteY="40dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/twtrlogo"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#drawable/ic_circle_white">
<ImageView
android:layout_width="18dp"
android:layout_height="14dp"
android:layout_centerInParent="true"
android:src="#drawable/twitter_logo" />
</RelativeLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="#font/poppins_medium"
android:paddingTop="9dp"
android:text="Twitter"
android:textSize="12.3dp" />
</LinearLayout>
</LinearLayout>

How can i show the items of listview listview in sqaure shape?

In the selected part of image these list of images are static, but i want to change it with dynamically which is come from the database. is there any good method to do 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">
<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:background="#color/colorPrimaryDark"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".6"
android:background="#color/colorPrimaryDark"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".6"
android:background="#color/colorPrimaryDark"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimaryDark"
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher" />
</LinearLayout>
</LinearLayout>
you can get image from the database and set to each of this imageview.

Setting position in a FrameLayout places it in a weird spot

I'm trying to overlap an ImageView on top of another one inside an Async. The idea is to pass an array of size 16 and to place an overlapping picture on spots that are marked true (the first square is 0, the last is 15). The constructor for the Async class takes in an array of booleans and creates an array of ImageViews with the picture otherwise leaves it blank. Since that wasn't working as intended I tried manually adding 2.
Instead when I place them, the starting point is not the top left corner as i expect. For a visual, I'm trying to get the green/blue box on top of one of the black and white ones http://i.imgur.com/yrez0t7.jpg
fl = (FrameLayout) findViewById(R.id.papa);
Bitmap bmp;
bmp=BitmapFactory.decodeResource(getResources(),R.drawable.effect_fire);
bmp=Bitmap.createScaledBitmap(bmp, btn1.getWidth(), btn1.getHeight(), true);
iv1.setImageBitmap(bmp);
iv2.setImageBitmap(bmp);
iv1.setLeft(0);
iv1.setTop(0);
iv2.setLeft(btn1.getWidth());
iv2.setTop(0);
iv1.setScaleType(ImageView.ScaleType.CENTER);
iv2.setScaleType(ImageView.ScaleType.CENTER);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(btn1.getHeight(), btn1.getWidth());
fl.addView(iv1);
fl.addView(iv2);
I'd expect two squares next to each other but i only get one and not even in the right place. I originally tried setX/setY but that didn't work either. My xml file consists of a FrameLayout (papa) with lots of LinearLayouts and other misc views inside.
edit: this is the xml file
<?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:id="#+id/papa"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/game"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ProgressBar
android:layout_width="match_parent"
android:layout_height="20dp"
android:id="#+id/healthBar"
android:progressDrawable="#drawable/healthbar_high"
style="?android:attr/progressBarStyleHorizontal"
android:indeterminate="false" />
<ProgressBar
android:layout_width="match_parent"
android:layout_height="10dp"
android:id="#+id/manaBar"
android:progressDrawable="#drawable/manabar"
style="?android:attr/progressBarStyleHorizontal"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2">
<ImageButton
android:id="#+id/btn1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
<ImageButton
android:id="#+id/btn2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
<ImageButton
android:id="#+id/btn3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
<ImageButton
android:id="#+id/btn4"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2">
<ImageButton
android:id="#+id/btn5"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
<ImageButton
android:id="#+id/btn6"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
<ImageButton
android:id="#+id/btn7"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
<ImageButton
android:id="#+id/btn8"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2">
<ImageButton
android:id="#+id/btn9"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
<ImageButton
android:id="#+id/btn10"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
<ImageButton
android:id="#+id/btn11"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
<ImageButton
android:id="#+id/btn12"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2">
<ImageButton
android:id="#+id/btn13"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
<ImageButton
android:id="#+id/btn14"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
<ImageButton
android:id="#+id/btn15"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
<ImageButton
android:id="#+id/btn16"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="hit"/>
</LinearLayout>
<ProgressBar
android:layout_width="match_parent"
android:layout_height="6dp"
android:id="#+id/experienceBar"
android:progressDrawable="#drawable/levelbar"
style="?android:attr/progressBarStyleHorizontal"
/>
<!-- Ability Buttons -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/ability0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="abilityCast"
android:clickable="false"
android:text="0" />
<Button
android:id="#+id/ability1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="abilityCast"
android:clickable="false"
android:text="1" />
<Button
android:id="#+id/ability2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="abilityCast"
android:clickable="false"
android:text="2" />
<Button
android:id="#+id/ability3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:background="#android:color/transparent"
android:padding="0dp"
android:onClick="abilityCast"
android:clickable="false"
android:text="SUPER" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/gold"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/gold"
android:textSize="20sp"/>
<TextView
android:id="#+id/attack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/attack"
android:textSize="20sp"/>
<TextView
android:id="#+id/defence"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/defence"
android:textSize="20sp"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level"
android:textSize="20sp"/>
<TextView
android:id="#+id/weapon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="20sp"/>
<TextView
android:id="#+id/armor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textSize="20sp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

Unable to force images to be at bottom

<?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="match_parent"
tools:context="com.MainActivity"
android:weightSum="4"
android:orientation="vertical"
android:background="#EDEDED">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#db4437"
android:weightSum="2"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imgHeader"
android:layout_height="80dp"
android:layout_width="200dp"
android:src="#drawable/cruise"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_weight="1"/>
<TextView
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Header"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white"
android:singleLine="true"
android:textAlignment="center"
android:layout_gravity="center_horizontal|center_vertical"
/>
</LinearLayout>
<LinearLayout android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_marginTop="-30dp"
android:background="#android:color/white"
android:orientation="vertical"
android:weightSum="3"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:elevation="30dp"
>
<EditText
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:layout_width="match_parent"
style="#style/textbox"
android:textColor="#android:color/black"
android:drawableStart="#drawable/user_male"
android:drawableLeft="#drawable/user_male"
android:adjustViewBounds="true"
android:maxHeight="10dp"
android:maxWidth="10dp"
android:scaleType="fitCenter"
android:hint="Email..." />
<EditText
android:layout_weight="0.5"
android:layout_height="wrap_content"
android:layout_width="match_parent"
style="#style/textbox"
android:textColor="#android:color/black"
android:drawableStart="#drawable/user_male"
android:drawableLeft="#drawable/user_male"
android:adjustViewBounds="true"
android:maxHeight="20dp"
android:maxWidth="20dp"
android:hint="Password..." />
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:orientation="horizontal"
android:background="#EDEDED"
android:weightSum="2"
>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#CCCCCC"
android:text="Forgot password?"
android:layout_marginLeft="20dp"
android:background="#android:color/transparent"/>
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#EDEDED"
/>
<Button
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Login"
android:layout_marginRight="10dp"
android:background="#db4437"/>
</LinearLayout>
</LinearLayout>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:orientation="horizontal"
android:id="#id/bottom"
>
<ImageView
android:id="#+id/bus_ruta1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center|bottom"
android:scaleType="fitEnd"
android:src="#drawable/vai_one"
/>
<ImageView
android:id="#+id/bus_ruta2"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center|bottom"
android:scaleType="fitEnd"
android:src="#drawable/vai_twi" />
<ImageView
android:id="#+id/bus_ruta3"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center|bottom"
android:scaleType="fitEnd"
android:src="#drawable/vai_three" />
</LinearLayout>
</LinearLayout>
Im using the following code The last linear layout contains three images and I want those images to be at the bottom of the screen but it is not being fixed at the bottom. How can I be able to sort this out?
try using a RelativeLayout instead to fill the whole screen and
the android:layout_alignParentBottom attribute
Possible dublicate of How to align views at the bottom of the screen?
try using RelativeLayout. Try this.
<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"
tools:context="com.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EDEDED"
android:orientation="vertical"
android:weightSum="4"
tools:context="com.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#db4437"
android:orientation="vertical"
android:weightSum="2" >
<ImageView
android:id="#+id/imgHeader"
android:layout_width="200dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_weight="1"
android:src="#drawable/cruise" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_weight="1"
android:singleLine="true"
android:text="Header"
android:textAlignment="center"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#android:color/white" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="32dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:layout_marginTop="-30dp"
android:layout_weight="1"
android:background="#android:color/white"
android:elevation="30dp"
android:orientation="vertical"
android:weightSum="3" >
<EditText
style="#style/textbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:adjustViewBounds="true"
android:drawableLeft="#drawable/user_male"
android:drawableStart="#drawable/user_male"
android:hint="Email..."
android:maxHeight="10dp"
android:maxWidth="10dp"
android:scaleType="fitCenter"
android:textColor="#android:color/black" />
<EditText
style="#style/textbox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:adjustViewBounds="true"
android:drawableLeft="#drawable/user_male"
android:drawableStart="#drawable/user_male"
android:hint="Password..."
android:maxHeight="20dp"
android:maxWidth="20dp"
android:textColor="#android:color/black" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:background="#EDEDED"
android:orientation="horizontal"
android:weightSum="2" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="#android:color/transparent"
android:text="Forgot password?"
android:textColor="#CCCCCC" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#EDEDED" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_weight="1"
android:background="#db4437"
android:text="Login" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#id/bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<ImageView
android:id="#+id/bus_ruta1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center|bottom"
android:scaleType="fitEnd"
android:src="#drawable/vai_one" />
<ImageView
android:id="#+id/bus_ruta2"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center|bottom"
android:scaleType="fitEnd"
android:src="#drawable/vai_twi" />
<ImageView
android:id="#+id/bus_ruta3"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center|bottom"
android:scaleType="fitEnd"
android:src="#drawable/vai_three" />
</LinearLayout>
</RelativeLayout>
Here android:layout_alignParentBottom="true" is key paramater.
Happy Coding.

Add scrollView and highlight on a Linear Layout in Android

I have Linearlayout that have have multiple linear layouts in which one of the main layout have a LinearLayout and Listview. LinearLayout contains multiple ImageView and TextView now I want to add ScrollView on this LinearLayout. But if I am adding the ScrollView my ListView got hidden and onClick of the ImageView I want to highlight that LinearLayout.
My XML is :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id ="#+id/mainFilterLayout"
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">
<ImageView
android:id="#+id/companyLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:layout_gravity="left"
android:adjustViewBounds="true"
android:background="#mipmap/ic_launcher" />
<View
android:clickable="true"
android:layout_gravity="center"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="#+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:gravity="right|center"
android:text="Clear" />
<Button
android:id="#+id/btnApply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:gravity="right|center"
android:text="Apply" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
//**I want to add scroll view on this linear layout**
<LinearLayout
android:id="#+id/categoryLayout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical">
<LinearLayout
android:id="#+id/brandLayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="vertical">
//**Here on click of imageviwew i want set as selected for this linear layout**
<ImageView
android:id="#+id/mobbrand"
android:layout_width="90dp"
android:layout_height="50dp"
android:background="#mipmap/ic_launcher"
android:layout_gravity="center" />
<TextView
android:id="#+id/txtBrand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Brand" />
</LinearLayout>
<LinearLayout
android:id="#+id/colorLayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="vertical">
<ImageView
android:id="#+id/mobColor"
android:layout_width="90dp"
android:layout_height="50dp"
android:background="#mipmap/ic_launcher"
android:layout_gravity="center" />
<TextView
android:id="#+id/txtColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="COLOUR" />
</LinearLayout>
<LinearLayout
android:id="#+id/sizeLayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="vertical">
<ImageView
android:id="#+id/mobSize"
android:layout_width="90dp"
android:layout_height="50dp"
android:background="#mipmap/ic_launcher"
android:layout_gravity="center" />
<TextView
android:id="#+id/txtSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Size" />
</LinearLayout>
<LinearLayout
android:id="#+id/offerLayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="vertical">
<ImageView
android:id="#+id/mobOffers"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#mipmap/ic_launcher" />
<TextView
android:id="#+id/txtPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Price" />
</LinearLayout>
<LinearLayout
android:id="#+id/othersLayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="vertical">
<ImageView
android:id="#+id/mobother"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#mipmap/ic_launcher" />
<TextView
android:id="#+id/txtOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="OTHER" />
</LinearLayout>
</LinearLayout>
<ListView
android:id="#+id/subCategory"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:cacheColorHint="#android:color/transparent"
android:divider="#fff"
android:dividerHeight="1dp"
android:fadingEdge="none">
</ListView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I hope this will work for u :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id ="#+id/mainFilterLayout"
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">
<ImageView
android:id="#+id/companyLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".1"
android:layout_gravity="left"
android:adjustViewBounds="true"
android:background="#mipmap/ic_launcher" />
<View
android:clickable="true"
android:layout_gravity="center"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<Button
android:id="#+id/btnClear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:gravity="right|center"
android:text="Clear" />
<Button
android:id="#+id/btnApply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center"
android:gravity="right|center"
android:text="Apply" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="2"
android:orientation="vertical">
<LinearLayout
android:id="#+id/brandLayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="vertical">
<ImageView
android:id="#+id/mobbrand"
android:layout_width="90dp"
android:layout_height="50dp"
android:background="#mipmap/ic_launcher"
android:layout_gravity="center" />
<TextView
android:id="#+id/txtBrand"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Brand" />
</LinearLayout>
<LinearLayout
android:id="#+id/colorLayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="vertical">
<ImageView
android:id="#+id/mobColor"
android:layout_width="90dp"
android:layout_height="50dp"
android:background="#mipmap/ic_launcher"
android:layout_gravity="center" />
<TextView
android:id="#+id/txtColor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="COLOUR" />
</LinearLayout>
<LinearLayout
android:id="#+id/sizeLayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="vertical">
<ImageView
android:id="#+id/mobSize"
android:layout_width="90dp"
android:layout_height="50dp"
android:background="#mipmap/ic_launcher"
android:layout_gravity="center" />
<TextView
android:id="#+id/txtSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Size" />
</LinearLayout>
<LinearLayout
android:id="#+id/offerLayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="vertical">
<ImageView
android:id="#+id/mobOffers"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#mipmap/ic_launcher" />
<TextView
android:id="#+id/txtPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="Price" />
</LinearLayout>
<LinearLayout
android:id="#+id/othersLayout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="vertical">
<ImageView
android:id="#+id/mobother"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:background="#mipmap/ic_launcher" />
<TextView
android:id="#+id/txtOther"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="OTHER" />
</LinearLayout>
</LinearLayout>
</ScrollView>
<ListView
android:id="#+id/subCategory"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="5"
android:cacheColorHint="#android:color/transparent"
android:divider="#fff"
android:dividerHeight="1dp"
android:fadingEdge="none">
</ListView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Simply do one thing cut the amount of code that you want in scrollview.... Now add scrollview in place of the code that u just "cut" ... now scrollview also have a child layout as linear layout.... in that linear layout make height to be wrap_content..... next place your code inside that linear layout... ListView outside the scrollayout...
[code]--------- Ctrl+X
replace with
<ScrollView>
<LinearLayout>
[code]-------- Ctrl+V
enter code here
</LinearLayout>
</ScrollView>
<ListView> -------- here add listview

Categories

Resources