I want to customize an EditText which should have "constants" like in the picture below, so that I get the minutes and the seconds. (the number interface is irrelevant).
Try by this code this code are here i have try by this code .This code is working .
<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"
android:orientation="vertical">
<EditText
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginTop="50dp"
android:textColor="#000000"
android:background="#CCCCCC"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
>
<Button
android:id="#+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:id="#+id/btn4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:id="#+id/btn5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<Button
android:id="#+id/btn6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:id="#+id/btn7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:id="#+id/btn8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />
</LinearLayout>
show here is image below
First of all, I see no EditText on the screen you posted, that's plain TextViews that are updating on clicking the numbers.
Anyway, you could use multiple TextViews/EditTexts, which would allow you to easily change their formatting as well, or you could programatically change the formatting yourself. If you want to have a string resource that accepts parameters see here on how to do that.
Related
A picture will help understand what I'm talking about:
I have three horizontal LinearLayouts, each of which contains six Buttons:
On the first row, each button has its layout_width set to "0dp" and it's layout_weight set to "1", except for button 4 where it's "2". The result is pretty much exactly what I'd expect.
The second row is identical to the first, except that all of the layout_width values are set to "wrap_content". My expectation was that each button would be assigned a width according to how much space it needs, and then the leftover space would be distributed equally among them, giving extra space to button 4. However, button 4 is actually smaller than its peers. Why is that happening?
In the third row, I deleted all of the weights and left the widths set to "wrap_content". I would have expected that each button would only be as wide as it needs to be, with some blank space at the end of the row. Instead, all of the buttons are actually too wide, and don't even fit on the screen!
Can anybody explain what I'm missing here?
By comparison, the next three rows are identical to the first three, except that Button has been changed to TextView. These rows all look exactly as I would expect.
For reference, the layout.xml file is as follows:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".SecondFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button12"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:id="#+id/button13"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:id="#+id/button14"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:id="#+id/button15"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="4" />
<Button
android:id="#+id/button16"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<Button
android:id="#+id/button17"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/button12"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="1" />
<TextView
android:id="#+id/button13"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="2" />
<TextView
android:id="#+id/button14"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="3" />
<TextView
android:id="#+id/button15"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="4" />
<TextView
android:id="#+id/button16"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="5" />
<TextView
android:id="#+id/button17"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="6" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="3" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="5" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="6" />
</LinearLayout>
</LinearLayout>
OK, figured it out. Leaving this up in case it helps someone in the future.
Buttons have a minWidth attribute which is 88dp by default. Changing this to zero made everything come out the way it should.
I am working on creating an educational app, like a preschool app for kids.
The interface should look something like this
If you can see from my picture, there are a 2x3 sets on left and right and a midle dispaly.
There are 3 buttons underneath the top section and on the bottom section there is a 2x5 table.
I figured if I could create this UI, make it a Jpeg, and move it to my drawable folder, I could apply it to my project then work on top of it.
That did not quite go as I expected, as when I started adding buttons to the corresponding table cells and run the program - They came up all over the place.
I then realized Android wasn't treating my jpeg as a layout.
Can anybody tell me what I am doing wrong or point me in the direction to fix this please?
Should I even forget about bringing in this jpeg in and just try to create it in Android?
you can make your ui using gridLayout.
simply use
android:layout_rowSpan="2"
android:layout_gravity="fill"
I make this.
<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"
tools:context=".MainActivity">
<GridLayout
android:rowCount="2"
android:columnCount="7"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_rowSpan="2"
android:layout_gravity="fill"
android:text="button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button12" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button13" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button11" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button12" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button13" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button11" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button12" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button13" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button13" />
</GridLayout>
<GridLayout
android:rowCount="1"
android:columnCount="3"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button11" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button12" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button13" />
</GridLayout>
<GridLayout
android:layout_width="wrap_content"
android:rowCount="2"
android:columnCount="5"
android:layout_gravity="center"
android:orientation="horizontal"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button11" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button12" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button13" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button11" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button12" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button13" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button11" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button12" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button13" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button13" />
</GridLayout>
</LinearLayout>
this xml will show this. you can change buttons whatever as you want. and make background your jpeg.
I am trying to learn android development.As a learning exercise i am trying to develop a simple calculator app. My UI is ready it renders perfectly in android studio but when ever i am trying to run the app the android studio is giving me
Error:(189) Error parsing XML: not well-formed (invalid token)
from the help of internet i found it can happen if i have any spelling mistake. but i could not find any in my code. I rebuilded the project but the error still exists. my code is below please help me to find where i m going wrong.
<?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:orientation="vertical"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.ahnaf.awsomecalc.MainActivity"
android:background="#373D4D">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="73dp"
android:background="#7069F2">
<TextView
android:id="#+id/result"
android:text="Result"
android:textColor="#FFFFFF"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:gravity="right" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="73dp">
<TextView
android:id="#+id/number"
android:text="Number"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:textColor=" #373D4D"
android:background="#FFFFFF"/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#373D4D">
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/plus"
android:layout_weight="1"
/>
<Button
android:text="-"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/minus"
android:layout_weight="1" />
<Button
android:text="="
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/equals"
android:layout_weight="1" />
<Button
android:text="/"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/divide"
android:layout_weight="1" />
<Button
android:text="*"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/multiply"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#373D4D">
<Button
android:text="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/one"
android:layout_weight="1" />
<Button
android:text="2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/two"
android:layout_weight="1" />
<Button
android:text="3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/three"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#373D4D">
<Button
android:text="4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/four"
android:layout_weight="1" />
<Button
android:text="5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/five"
android:layout_weight="1" />
<Button
android:text="6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/six"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#373D4D">
<Button
android:text="7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/seven"
android:layout_weight="1" />
<Button
android:text="8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/eight"
android:layout_weight="1" />
<Button
android:text="9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/nine"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#373D4D"
>
<Button
android:text="."
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dot"
android:layout_weight="1" />
<Button
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/zero"
android:layout_weight="1" />
<Button
android:text="<<"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/curr"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
You have 2 problems.
First one : remove white space in textColor attribute :
<TextView
android:id="#+id/number"
android:text="Number"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:textColor="#373D4D"
android:background="#FFFFFF"/>
Second one is :
<Button
android:text="<<"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/curr"
android:layout_weight="1" />
You should extract the String << causing an issue at compilation. extract string resource like so :
<Button
android:text="#string/yourstring"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/curr"
android:layout_weight="1" />
Then in strings.xml (/res/values/strings.xml) add the created resource :
<string name="yourstring"><![CDATA[<<]]></string>
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>
sorry for this newbie question. I have googled a while and cannot find an answer.
I have 10 buttons in a row. This looks ok on my tablet. But when I start the app on my phone only the first 5 buttons are shown, the rest is cut off.
If there is not enough space the remaining buttons should be displayed in a second row. How can I do that?
Thanks.
<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"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
/>
Interesting! take scrollview parents of LinearLayout#
<ScrollView...
<LinearLayout...//android:orientation="horizontal"
<Button... // your all buttons
</Button>
</LinearLayout>
</ScrollView>
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="10"
/>
</LinearLayout>
</ScrollView>
You have a few options:
Place all your buttons into a GridView - this will work across any screen size and is relatively straightforward and neat.
Put them into a ScrollView. This is quite popular but a bit annoying for the user as it can lead to mis-clicks and they may not realise you can scroll the buttons.
If you only ever want exactly 1 or 2 rows depending on screen size, put each row of buttons into a separate layout file. Then create 2 additoinal screen-dependent layouts containing LinearLayout which include both rows, but give the portrait version a vertical orientation. So when that layout is loaded, you get two rows.
Work out if you can present the buttons in a different way - maybe using just icons, or combine them into dropdowns.
// try this way
<?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"
android:orientation="horizontal"
tools:context=".MainActivity">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="1"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="2"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="3"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="4"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="5"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="6"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="7"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="8"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="9"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.2"
android:text="10"/>
</LinearLayout>