How to make a numpad similar layout in android - android

How do i make the numpad layout as shown in the uploaded picture.
This is my current code which would not show anything in the design view. I used this code previously on other views which work but this time it does not work and none of the buttons were visible at all in the design view.And also how do i make the 0 button as it is in the picture. This is my 2nd time asking questions in stackoverflow so do let me know if my questions are not clear and also english is not my first language. Thank you in advanced!
Edit: I have to make this layout through xml coding and not the usual thing
where the users click on the textview and the numpad of the phone
shows up. I hope this clarify the doubts of my question. I have to
make the clear, cancel and exit button as well.
<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"
android:orientation="vertical"
android:weightSum="4"
android:layout_weight="4"
android:background="#drawable/background"
tools:context=".insert_amount">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="250dp">
<VideoView
android:id="#+id/videoview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Player"
android:textSize="30dp"
android:layout_marginTop="10dp"
android:layout_gravity="center_horizontal"
android:textColor="#color/white"
/>
<TextView
android:id="#+id/textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Display ID"
android:textSize="30dp"
android:textStyle="bold"
android:layout_marginTop="20dp"
android:layout_gravity="center_horizontal"
/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/content_box"
android:layout_marginTop="10dp"
android:layout_marginStart="50dp"
android:layout_marginEnd="50dp"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/in"
android:layout_gravity="center_horizontal"
android:layout_marginTop="50dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Top Up Amount (IN)"
android:textSize="30dp"
android:layout_marginTop="10dp"
android:gravity="center_horizontal"
/>
<TextView
android:id="#+id/textviewnumber"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:hint="0.00"
android:textSize="40dp"
android:textStyle="bold"
android:gravity="center"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:weightSum="4"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#drawable/login_signoutbutton"
android:layout_height="fill_parent"
android:text="1"
/>
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:background="#drawable/login_signoutbutton"
android:text="2"
/>'
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#drawable/login_signoutbutton"
android:layout_height="fill_parent"
android:text="3"
/>
<ImageButton
android:id="#+id/buttonclear"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:scaleType="fitCenter"
android:background="#drawable/login_signoutbutton"
android:src="#drawable/clear_button"
android:layout_margin="20dp"
/>

Try this
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Player"
android:textColor="#FFFFFF" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="12345679"
android:textColor="#FFFFFF" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:src="#drawable/kid_goku" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="10dp"
android:text="Top up Amount" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:hint="Top up Amount" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="7" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="0" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_marginHorizontal="2dp"
android:layout_weight="1"
android:text="9" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.5"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginHorizontal="2dp"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:background="#9b5006"
android:drawableLeft="#drawable/ic_camera"
android:text="Clear"
android:textColor="#FFFFFF"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginHorizontal="2dp"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:background="#9b5006"
android:drawableLeft="#drawable/ic_camera"
android:text="Cancel"
android:textColor="#FFFFFF"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="35dp"
android:layout_marginHorizontal="2dp"
android:layout_marginTop="12dp"
android:layout_weight="1"
android:background="#9b5006"
android:drawableLeft="#drawable/ic_camera"
android:text="Next"
android:textColor="#FFFFFF" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
OUTPUT

Your layout height exceeding the device display. You have to use ScrollView here. Also for making the number keypad using GridLayout link will be better choice and you can easily get '0' button position using this view.

Related

Stop text view from going out of screen

Im trying to make an app in android studio. I have a textview with a circular background. The thing is that when i install it on ta physical device the Text view goes of the screen.
Hers the preview in android studio:
The Xml 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"
android:background="#000000"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="55dp"
android:orientation="horizontal"
android:background="#drawable/top_bar">
<TextView
android:id="#+id/textView3"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_weight="0"
android:drawableLeft="#drawable/ic_keyboard_arrow_left_black_24dp"
/>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:fontFamily="#font/segoeui"
android:gravity="center_vertical|start"
android:text="App name and image"
android:textColor="#FFFFFF"
android:textSize="30dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView6"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fontFamily="#font/segoeui"
android:gravity="center"
android:text="Pakistan vs Australia T20"
android:textColor="#ffff"
android:textSize="24dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1.5dp"
android:background="#ffff"
android:orientation="vertical"
android:layout_marginLeft="47dp"
android:layout_marginRight="47dp"></LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="300dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="247dp"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:gravity="center"
android:orientation="horizontal">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="#font/segoeui"
android:gravity="center"
android:text="Match Odds"
android:textColor="#ffff"
android:textSize="17dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="#font/segoeui"
android:paddingLeft="10dp"
android:text="Favourite"
android:textColor="#ffff"
android:textSize="17dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="30dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView12"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0"
android:textColor="#ffff"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView11"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="-"
android:textColor="#ffff"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView10"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0"
android:textColor="#ffff"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="30dp"
android:fontFamily="#font/segoeui"
android:gravity="center"
android:text="WestIndies"
android:textColor="#ffff"
android:textSize="16dp"
android:visibility="visible" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView4"
android:layout_width="100dp"
android:layout_height="1dp"
android:layout_marginLeft="6dp"
android:background="#color/margins"
android:text="TextView" />
<TextView
android:id="#+id/textView7"
android:layout_width="100dp"
android:layout_height="1dp"
android:layout_marginLeft="30dp"
android:background="#color/margins"
android:text="TextView" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="#font/segoeui"
android:gravity="center"
android:text="Session"
android:textColor="#ffff"
android:textSize="17dp" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fontFamily="#font/segoeui"
android:gravity="center"
android:text="Runs/Balls"
android:textColor="#ffff"
android:textSize="17dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:orientation="horizontal">
<TextView
android:id="#+id/textView17"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0"
android:textColor="#FF0000"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView18"
android:layout_width="20dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="-"
android:textColor="#ffff"
android:textSize="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView19"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0"
android:textColor="#ffff"
android:textSize="20dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="0dp"
android:orientation="vertical"
>
<TextView
android:id="#+id/TextViewID"
android:layout_width="110dp"
android:layout_height="110dp"
android:layout_marginTop="80dp"
android:layout_marginLeft="35dp"
android:background="#drawable/green_circle"
android:gravity="center"
android:text="2"
android:textColor="#ff2800"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
What im getting on the screen:
ive tried using different devices with different screen sizes but all give the same result. How can i solve this.
TIA..!!!
I think it's because the LinearLayout where that round TextView is, is actually a child of another LinearLayout with horizontal orientation, and you have another empty LinearLayout of width 250dp right before it, which pushes the relevant layout out of screen. Can you try removing the empty LinearLayout and see if it helps?

How to view only two TextView in Android

I have a questionnaire, in which I want the user to see only the previous and current question. How to achieve this in Android.
This is my XML View.
The user can see maximum two questions -- previous and current.
I have used a RelativeLayout which contains ScrollView, and in that I'm using multiple TextView for questions and Button for true/false
<?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"
style="#android:style/Widget.DeviceDefault.Light.ScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:fillViewport="false"
android:scrollbarStyle="outsideOverlay"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.scanqr_android.ScrollingActivity"
tools:showIn="#layout/activity_scrolling">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#3f51b5"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="MY PAGE TITLE"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#fff" />
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Caterpillars turn into butterflies."
tools:text="Caterpillars turn into butterflies." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="true" />
<Button
android:id="#+id/button6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="false" />
</LinearLayout>
<TextView
android:id="#+id/textView9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Bubble gum contains rubber."
tools:text="Bubble gum contains rubber." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="true" />
<Button
android:id="#+id/button8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="false" />
</LinearLayout>
<TextView
android:id="#+id/textView10"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="The deadliest earthquake of 2017 took place in Iran and Iraq." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/Button9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="true" />
<Button
android:id="#+id/button10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="false" />
</LinearLayout>
<TextView
android:id="#+id/textView11"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Electrons are larger than molecules." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button11"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="true" />
<Button
android:id="#+id/button12"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="false" />
</LinearLayout>
<TextView
android:id="#+id/textView12"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Thunderstorms have particular sound frequencies that can hurt a dog’s ears." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="true" />
<Button
android:id="#+id/button14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="false" />
</LinearLayout>
<TextView
android:id="#+id/textView13"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=" There are 30 days in May." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<Button
android:id="#+id/button15"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="true" />
<Button
android:id="#+id/button16"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="false" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Firstly register your layout id by using findViewById() in java file.
Then you can hide the layout of textviews using like this -
linearLayout.setVisibility(View.GONE); by checking conditions.
You just hide the textview like this
textView.setVisibility(View.INVISIBLE);
or
textView.setVisibility(View.GONE);
Take a look at this How to set visibility of TextView?

Android: Layout alignment

I have 3 'groups' of items in my layout:
Button + TextView (should be on the right side)
ImageView + ImageView + TextView (should be in center)
TextView + Button (should be on the left side)
It's like a footer for tablet.
But all my items are next to each other starting from left side.
I tried grouping them to separate layouts but it doesn't help.
Here is my code:
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="40dp"
android:orientation="horizontal"
android:layout_gravity="bottom"
android:background="#android:color/black">
<Button
android:background="#drawable/arrow_left"
android:layout_width="50dp"
android:layout_height="match_parent"
android:id="#+id/footer_left_arrow"
android:layout_margin="10dp"/>
<TextView
android:text="text"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/footer_left_text"/>
<ImageView
android:background="#drawable/logo"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_width="50dp"
android:layout_height="20dp"
android:layout_gravity="center"/>
<ImageView
android:background="#drawable/footer_vertical_line"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<TextView
android:text="text"
android:textColor="#FFFFFF"
android:textSize="12sp"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center" />
<TextView
android:text="text"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<Button
android:background="#drawable/arrow_right"
android:layout_width="50dp"
android:layout_height="match_parent"
android:layout_margin="10dp" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:background="#android:color/black"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<Button
android:id="#+id/footer_left_arrow"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/arrow_left" />
<TextView
android:id="#+id/footer_left_text"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="text"
android:textColor="#FFFFFF"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/footer_vertical_line" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:drawable/ic_input_add" />
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:text="text"
android:textColor="#FFFFFF"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:text="text"
android:textColor="#FFFFFF"
android:textSize="20sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#drawable/arrow_right" />
</LinearLayout>
</LinearLayout>
Use this.

How to align ImageButtons in a layout evenly

I currently have 4 ImageButtons in a layout that takes only half of the screen height. I would like the ImageButtons to be placed evenly in the layout. Here is what I have:
<?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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="#mipmap/feed_button"
android:background="#android:color/transparent"
android:layout_weight="1" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="#mipmap/feed_button"
android:background="#android:color/transparent"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:orientation="horizontal">
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="#mipmap/feed_button"
android:background="#android:color/transparent"
android:layout_weight="1" />
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:src="#mipmap/feed_button"
android:background="#android:color/transparent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
This works, since here is the result:
The issue is, 1: I would like to add text under each button and I am not sure how that is going to work and 2: the white spots to the right and left of the ImageButtons are active, as if they were parts of the button. Is there a better way to do this?
You can do something like this
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/section1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent">
<ImageButton
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:background="#android:color/transparent"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/img1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="info 1"
android:textColor="#fff"
android:textSize="25sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent">
<ImageButton
android:id="#+id/img2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:background="#android:color/transparent"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/img2"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="info 2"
android:textColor="#fff"
android:textSize="25sp" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent">
<ImageButton
android:id="#+id/img3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:background="#android:color/transparent"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/img3"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="info 3"
android:textColor="#fff"
android:textSize="25sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent">
<ImageButton
android:id="#+id/img4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_weight="1"
android:background="#android:color/transparent"
android:src="#mipmap/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/img4"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="info 4"
android:textColor="#fff"
android:textSize="25sp" />
</RelativeLayout>
</LinearLayout>
Result
and for second part of question you should set click listener on entire relative layout. for example for tap on first cell :-
xml
android:id="#+id/section1"
JAVA
findviewbyid(R.id.section1).setOnClickListener(...)
Another (and better) alternative is to work with TableLayout:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:stretchColumns="1">
<TableRow
android:layout_height="0dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent">
<ImageButton
android:id="#+id/btn1"
android:contentDescription="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#mipmap/feed_button"
android:background="#android:color/transparent"/>
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn1"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:text="Text 1"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent">
<ImageButton
android:id="#+id/btn2"
android:contentDescription="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#mipmap/feed_button"
android:background="#android:color/transparent"/>
<TextView
android:id="#+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn2"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:text="Text 2"/>
</RelativeLayout>
</TableRow>
<TableRow
android:layout_height="0dp"
android:layout_weight="1">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent">
<ImageButton
android:id="#+id/btn3"
android:contentDescription="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#mipmap/feed_button"
android:background="#android:color/transparent"/>
<TextView
android:id="#+id/txt3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn3"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:text="Text 3"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/transparent">
<ImageButton
android:id="#+id/btn4"
android:contentDescription="#null"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#mipmap/feed_button"
android:background="#android:color/transparent"/>
<TextView
android:id="#+id/txt4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/btn4"
android:layout_marginTop="10dp"
android:layout_centerHorizontal="true"
android:text="Text 4"/>
</RelativeLayout>
</TableRow>
</TableLayout>

Position of button changes when caption is too long

I currently work on a quiz app. There is a question and four possible answer options. Each answer options is represented by a button. The four buttons are aligned in a rectangle.
Problem: If the button caption is too long, the button position slightly changes NS the button slips downward.
Question: Is there is a solution for this problem? I dont want the button to change its position.
XML Code
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/background" >
<TextView
android:id="#+id/tv_voc_trainer_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:gravity="center"
android:text="This is my title!"
android:textSize="20sp" />
<LinearLayout
android:id="#+id/ll_voc_trainer_first_answer_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/tv_voc_trainer_question"
android:layout_marginTop="40dp"
android:orientation="horizontal" >
<Button
android:id="#+id/bu_first_answer_possibility"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#drawable/blue_button"
android:text="Answer 1" />
<Button
android:id="#+id/bu_second_answer_possibility"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#drawable/blue_button"
android:text="Answer 2 is very long!" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_voc_trainer_second_answer_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/ll_voc_trainer_first_answer_row"
android:orientation="horizontal" >
<Button
android:id="#+id/bu_third_answer_possibility"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#drawable/blue_button"
android:text="Answer 3" />
<Button
android:id="#+id/bu_fourth_answer_possibility"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_weight="1"
android:background="#drawable/blue_button"
android:text="Answer 4" />
</LinearLayout>
<ImageView
android:id="#+id/img_trainer_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ll_voc_trainer_second_answer_row"
android:layout_marginTop="25dp"
android:layout_centerHorizontal="true"
android:contentDescription="answer" />
</RelativeLayout>
This screenshot illustrates the problem. Button 2 has a longer caption and therefore his position automatically changed.
Try using this in your linear layout:
android:baselineAligned="false"
Try this way,hope this will help you to solve your problem.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<TextView
android:id="#+id/tv_voc_trainer_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="This is my title!"
android:textSize="20sp" />
<LinearLayout
android:id="#+id/ll_voc_trainer_first_answer_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:gravity="center">
<Button
android:id="#+id/bu_first_answer_possibility"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blue_button"
android:gravity="center"
android:text="Answer 1" />
<Button
android:id="#+id/bu_second_answer_possibility"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="#drawable/blue_button"
android:gravity="center"
android:text="Answer 2 is very long!" />
</LinearLayout>
<LinearLayout
android:id="#+id/ll_voc_trainer_second_answer_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:gravity="center">
<Button
android:id="#+id/bu_third_answer_possibility"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/blue_button"
android:gravity="center"
android:text="Answer 3" />
<Button
android:id="#+id/bu_fourth_answer_possibility"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:background="#drawable/blue_button"
android:gravity="center"
android:text="Answer 4" />
</LinearLayout>
<ImageView
android:id="#+id/img_trainer_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:contentDescription="answer" />
</LinearLayout>

Categories

Resources