I want to make an base calculator app, so i started to make buttons for each number and operation. To keep them align i used an RelativeLayout but under it i have five LinearLayouts, each LinerarLayout represents a row of buttons.
My problem is the following, after i use layout_alignBottom to the first row in order to keep it at the bottom of the screen, i tried to use layout_above to align the second row above the first but it seems that i can't do that because i get an No resource found that matches the given name (at 'layout_above' with value '#id/firstRow'). error, in fact i get this error for all the alignments attributes.
Here is a part of my xml
<LinearLayout
android:id="#+id/secondRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_above="#id/firstRow">
<Button
android:id="#+id/b1"
android:layout_width="85dp"
android:layout_height="85dp"
android:text="1" />
<!-- more buttons -->
</LinearLayout>
<LinearLayout
android:id="#+id/firstRow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/bP"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginLeft="0dp"
android:text="." />
<!-- more buttons -->
</LinearLayout>
How can i align the LinearLayouts in an way that the first row is at the bottom of the screen and the others are one above each others ?
The XML is rendered from top to bottom, therefore, if you want to add a reference to an element that is after the one you are adding the reference to, you need to call #+id instead of #id. In your case you should write this:
android:layout_above="#+id/firstRow"
This happens because at the time you are running the code from your first LinearLayout, Android doesn't have any element registered with the id "firstRow" since it wasn't compiled yet, so you need to add the + meaning that if it find any reference later on, it will be set, if not, it just aligns the view without the reference.
Try using android:layout_toStartOf and android:layout_toEndOf
You're doing this in a more complicated way than is necessary.
Just wrap them in a vertical oriented linear layout.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/b1"
android:layout_width="85dp"
android:layout_height="85dp"
android:text="1" />
<!-- more buttons -->
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/b1"
android:layout_width="85dp"
android:layout_height="85dp"
android:text="3" />
<!-- more buttons -->
</LinearLayout>
<!-- Your rows in order from top to bottom -->
</LinearLayout>
Related
Good day everyone, currently i'm trying to make my first Android app, and then the first thing i realized, that idiotic XML UI designing.
I have this 2 view (button) and i'd like to make them so the first one fill the half of the parent (RelativeLayout) and the second one fills the other half of the parent...
My Code is:
<Button
android:id="#+id/Top1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_toLeftOf="#id/Top2"
android:text="top1"/>
<Button
android:id="#+id/Top2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/Top1"
android:text="TOP2"/>
The problem, that i'm get this error:
"No resource found that matches the given name (at 'layout_toLeftOf' with value '#id/Top2')."
It seems, that not even Android want to use XML.
Like an old C program where if a method is written below the call, it will give an error...
So i have 2 question:
1: How to solve this problem in the XML?
2: Or can i avoid this XML designing, and use code-like design like in C# ?
There are a few ways you can go about this and I provided you with two ways to have buttons aligned side by side.:
Use a LinearLayout with orientation set as horizontal
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/btn_container">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Left Button"
android:layout_weight="1"
android:id="#+id/btn_left" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Right Button"
android:id="#+id/btn_right" />
</LinearLayout>
Use a RelativeLayout and an extra View to align your buttons.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<View
android:id="#+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/btn_left"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignRight="#id/strut"
android:text="Left Button" />
<Button
android:id="#+id/btn_right"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/strut"
android:layout_alignParentRight="true"
android:text="Right Button" />
</RelativeLayout>
You are correct, Top2 needs to be defined first in the XML before you can refer to it. That being said, if all you are doing is putting 2 buttons next to each other and have them fill the parent, you should consider using a LinearLayout. Order is important there, too: for horizontal orientation the children are laid out left to right, for vertical they are laid out top to bottom.
I have a LinearLayout that contains a lot of TextViews and ImageButtons, I want to align some of these elements to right, i had a look at this and this but i can't use their tips as i can't change the orientation and can't make android.gravity:right as i don't want to align all the elements to right, also i can't use nested layouts or but the desired elements into RelativeLayout because that shifts the rest of elements to the left and i want them at the center.
this is my code:
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0.15"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/media_mediabar"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/move_backward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:clickable="true"
android:scaleType="centerInside"
android:src="#drawable/media_button_rewind"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:tag="released"/>
<ImageButton
android:id="#+id/rmeote_mines"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:background="#android:color/transparent"
android:clickable="true"
android:scaleType="centerInside"
android:src="#drawable/remote_minus" />
<TextView
android:id="#+id/remote_plus_minus"
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:layout_marginBottom="15dp" />
.
.
.<!.. some other elements ..!>
</LinearLayout>
The desired result:
The simplest solution would be using empty views with weights as separators.
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<!-- Left button -->
<Button ...
... />
<View android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<!-- Middle button -->
<Button ...
... />
<View android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<!-- Right button -->
<Button ...
... />
</LinearLayout>
The separator views can be made invisible as an optimization, because they don't draw anything and are used only for layout. You can tweak the actual 'layout_weight' values to get the desired layout. Starting from API level 14 you can use instances of Space as separators which will improve performance and readability (there is also a version of Space in the support library).
For such a complex layout you'd be way better of using RelativeLayout instead.
i can't use nested layouts
Then you can't solve your problem.
Nested layout are the heart of Android layout, to create such complex view that you desire, I think you must use nested layouts.
#Ridcully suggested you to use RelativeLayout, this is a good idea. You can combine it with few linear layouts and you be fine.
I think that RelativeLayout should be your base layout.
I have this header:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dip">
<Button android:id="#+id/home"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Home"
/>
<Button android:id="#+id/questions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Questions"
/>
<Button android:id="#+id/questions"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:text="Questions"
android:layout_toRightOf="#+id/home" />
<Button android:id="#+id/businesses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Businesses"
android:layout_toRightOf="#+id/businesses"
/>
<Button android:id="#+id/learn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Learn"
android:layout_toRightOf="#+id/learn"
/>
<Button android:id="#+id/extra_help"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Help"
android:layout_toRightOf="#+id/learn"
/>
</RelativeLayout>
and for some reason, the buttons display 50% on top of each other all bunched up, each covering half of the other.
Any idea what is wrong with my layout?
Thanks!
You are using a relative layout, you need to manage both horizontal and vertical alignment. For example, all the android:layout_toRightOf are used for horizontal positioning, you need to also add attributes to handle the vertical positioning.
All the possible attributes are found here:
http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html
UPDATE
Seeing your comments below, it seems you just want to have one horizontal row. In that case, just make sure each is aligned to the previous one(But then why not use a linear layout?)
Check your id's though, and #+id/learn is aligned to the right of itself. Revisit that. And you also have a button right after "Home" that isn't handled at all.
I have a linear layout with several buttons in it. The button images are all the same size and have the same attributes... except for one button. This one button has a smaller font size. All the buttons except for this one are in a perfect line exactly the way I want. For some reason, the button with the smaller font appears a little lower on the screen than the other buttons. I'm having a hard time wrapping my head around the idea of a button that requires less space taking up additional space.
Might someone give me a hint on what to read up on?
EDIT
Here's main.xml (seems like SO filters some of it, all the important stuff is here...)
<ScrollView android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="300px">
<TextView
android:id="#+id/the_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:typeface="monospace"
android:textSize="9pt"
android:background="#color/paper"
android:paddingLeft="20dp"
android:paddingBottom="20dp"
android:textColor="#color/type"
/>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button style="#style/ASR33_button"
android:tag="Y"
android:text="Y"
/>
<Button style="#style/ASR33_button"
android:tag="N"
android:text="N"
/>
<Button style="#style/ASR33_button"
android:tag="E"
android:text="E"
/>
<Button style="#style/ASR33_button"
android:tag="W"
android:text="W"
/>
<Button style="#style/ASR33_button"
android:tag="S"
android:text="S"
/>
<Button style="#style/ASR33_button"
android:tag="F"
android:text="F"
/>
<Button style="#style/ASR33_button"
android:tag="R"
android:text="R"
/>
<Button style="#style/ASR33_button"
android:tag="M"
android:text="M"
/>
<Button style="#style/ASR33_button"
android:tag="T"
android:text="T"
/>
<Button style="#style/ASR33_button"
android:onClick="onEnterButtonClicked"
android:textSize="6pt"
android:text="RE-\nTURN"
/>
<Button style="#style/ASR33_button"
android:tag="U"
android:text="U"
/>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/instructions"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="9pt"
android:paddingLeft="10dp"
android:typeface="normal"
android:text="Commands: (Y)es, (N)o, (N)orth, (E)ast, (W)est, (S)outh, (M)ap, (ST)atus, (Fight), (R)un, (SU)icide. All commands must be followed by RETURN."
/>
</LinearLayout>
The one that's wonky is the 2nd from the bottom, with the different onclick event. The style has 11pt for the character size. If I use it (and a 1 letter button name, like the others) it behaves. But that's not what the ASR33 'enter' key has on it. So if I reduce the font size to say 6 pt, the weirdness happens.
The style can be seen here.
Again, just reading references or ideas please, I can figure it out if I have a word or two to search on. It's hard to know what you don't know...
RESOLUTION
Anurag has it right, see his answer below. Here's an excerpt of the updated LinearLayout:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false">
maybe the re sizing has happened due to the wrap_content property of your button. so what you should do is have a fixed height to the linear layout holding all the buttons while its with is set to fill parents.
and inside the linear layout let individual buttons have height set to wrap content which will give all the buttons the same height as that of the linear layout and also set the attribute android:adjustViewBounds="true" for the small button. this attribute will resize your image button to maintain the aspect ratio. i hope this helps.
EDIT:
So here is the solution to your problem, something that was caused due to the base alignment property of the linear layout. A horizontal LinearLayout aligns the baselines of all its child controls by default. So the first line of text in your multi-line button is vertically aligned with the single line of text in the other buttons. set android:baselineAligned="false" on the LinearLayout. This worked perfectly on my HTC.
I would like to know the best way of using Linear Layout or Table Layout for designing attached android screen.
I am using Linear layout as main layout and then adding the sub Linear layouts and Table Layouts in it. Is this the best way of doing???
Use RelativeLayout or GridLayout (ICS and above only).
This should get you started. It's not perfectly what you want but it hopefully is close. I didn't test the code so I don't even know if it compiles. Shouldn't be too far from the final code though. :)
<RelativeLayout>
<!-- Image, may want to size this as a square using dp pixels say, 64dpx64dp -->
<ImageView android:id="#+id/PhotoImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_alignParentLeft="true" />
<!-- TextViews at the right of the image. Stacked, caution this might end up being taller than the ImageView itself. -->
<TextView android:id="#+id/PersonNameTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_toRightOf="#id/PhotoImageView" />
<TextView android:id="#+id/TitleTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:layout_toRightOf="#id/PhotoImageView" android:layout_below="#id/PersonNameTextView" />
<!-- Stacked TextViews below, you can split this out to side by side TextViews but that's more work. -->
<TextView android:id="#+id/PhoneTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="#id/PhotoImageView" />
<TextView android:id="#+id/EmailTextView" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_below="#id/PhoneTextView" />
<!-- Delete button placed at the bottom. -->
<Button android:id="#+id/DeleteButton" android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" />
</RelativeLayout>