I'm using Android's relativelayout, trying to make two buttons align side by side.
But, when I use layout_toLeftOf, my button02 suddenly disappear.
It's fine if I use layout_toRightof. Anyone knows why?
<Button
android:id="#+id/Button01"
android:layout_alignParentLeft="true"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#+id/Button01">
</Button>
<Button
android:id="#+id/Button02"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="#+id/Button02"
android:layout_toLeftOf="#+id/Button01"
android:layout_alignTop="#+id/Button01"
android:layout_alignBottom="#+id/Button01"
>
</Button>
That's because the first one is aligned to the left; if you put the second button to the left of the first one, it will be outside the screen.
So, what if you change
android:layout_alignParentLeft="true"
to
android:layout_alignParentRight="true"
in the first button?
If you want Button2 to be left of Button1, why are you placing alignTop and alignBottom properties? Please remove that and see. Also remove alignParentLeft for Button1, if you want Button1 to be placed at the right side.
Related
image of current button
Hi, so i have a problem with a button i am working with...i attached an image for a more specified description...
I would like for the text to be shown horizontally...there is a lot of unused space in the button...and i can't figure out how to do that..
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/radioButton1"
android:paddingLeft="130dip"
android:paddingRight="130dip"
android:layout_alignBottom="#+id/Thequestion"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#android:color/white"
/>
you need to reduce paddingLeft and paddingRight. if you want a distance from left and right you should use margin instead of padding.
Edit: you can use theese instead of paddings.
android:layout_marginLeft="130dp"
android:layout_marginRight="130dp"
Please add:
android:singleLine="true"
to your Button
EDIT:
Change
android:paddingLeft="130dip"
android:paddingRight="130dip"
to
android:paddingLeft="10dip"
android:paddingRight="10dip"
hi i need to knw how to align button , text view and other things in ,
example when i add a button into the edge of the bottom of the screen bt when i turn screen into horizontal i want it to be in the same place.that means i want to be it in the edge of the bottom of the screen with same bottom padding.
How i could do this
this is the button code
<Button
android:id="#+id/btnEnter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_enter"
android:layout_marginTop="300dp"
android:layout_marginLeft="60dp"
android:textSize="45sp"
android:textColor="#000"
android:fontFamily="serif"
/>
i need when its turn into horizontal that button to be in the exact same place
thanks you
regards pran
add this for your button if it s in relative layout
android:layout_alignParentBottom="true"
otherwise
android:layout_gravity="bottom"
If Button is inside a RelativeLayout,
Use these properties for button:
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
If Button is inside a LinearLayout,
Use layout_gravity for button:
android:layout_gravity="right"
Hope it helps.
I'm having trouble putting buttons next to eachother. I have researched the topic, but nothing seems to have helped.
Here is my XML code: Link
Here is how it displays now:
My screen
If I try to move the enter button down, the clear button goes up and vice versa. I can move the buttons from side to side, but never on the same line as another button
Right now your two buttons are elements of a vertically oriented LinearLayout. All elements appear one above the next, so to get them to display side-by-side, you need to simply enclose them in a horizontal LinearLayout container.
<LinearLayout android:layout-width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter"
android:id="#+id/enter"
android:layout_gravity="center"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear"
android:id="#+id/clear"
android:layout_gravity="bottom"/>
</LinearLayout>
In my user interface, I have a fragment with a RelativeLayout. At the bottom of this RelativeLayout, I have two buttons: one should be on the left, the other on the right, with empty space between them. The left one has static text (but because the app will be translated, I don't know what width it will be). The text in the right one can change arbitrarily.
Since I already have a RelativeLayout, I started out trying to lay them out inside the RelativeLayout like this:
<Button
android:id="#+id/button_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/left" />
<Button
android:id="#+id/button_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="#string/right" />
But this has the problem that if the text in the right-hand button is too long, it will overlap the left-hand button.
I next tried to constrain the left-hand edge of the right-hand button by adding android:layout_toRightOf="#+id/button_left", but with this, the right-hand button would always fill the available width. When the text in the right-hand button is short, I want it to shrink to leave a gap between it and the left-hand button.
I next tried to use a LinearLayout, so I could set layout_gravity on the buttons, like this:
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/button_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/left" />
<Button
android:id="#+id/button_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:drawableLeft="#drawable/pass"
android:text="#string/right" />
</LinearLayout>
Still no joy. I expected this to work, but the right-hand button stays just to the right of the left-hand button, instead of sticking to the right edge of the screen. I can see in the layout editor that the LinearLayout correctly fills the width of the screen, but the button stubbornly stays next to its friend.
I tried adding android:layout_weight="1" to the right-hand button too, but again, that made it always expand to fill the available space.
Next, I tried to add an empty View between the buttons, to expand and force the right button to the right, like this:
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<Button
android:id="#+id/button_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/left" />
<View
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/button_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="#string/right" />
</LinearLayout>
This works fine when the text is short, just like my original RelativeLayout did, but now when the text on the right-hand button is long, its width is limited by the width of the screen, not the available space, so it extends off the right-hand edge of the screen. Again, I can see in the layout editor that the LinearLayout has the correct width, but the button is extending ourside its parent's bounds. This happens even if the button has android:layout_width="match_parent". Oddly enough, increasing the layout_gravity on the right-hand button makes it smaller until it fits inside the available space, but of course that also makes it fill the space when the text is small.
I can't believe it's this hard to get this right. I've seen half a dozen similar questions on SO, but they all have easy workarounds. If the button text is fixed, you can set the margin to a fixed width by hand. If the expanding widget is a TextView instead of a Button, you can just let it expand and use android:gravity to move the text inside the widget, but you can't do that with a button because the background and borders are visible on the screen.
It turns out that adding the LinearLayout was the wrong approach. Using android:layout_toRightOf="#+id/button_left" android:layout_alignParentRight="true" works fine with a TextView, because that can soak up the available space without changing its appearance. Instead of trying to change the layout, I just need to use something that can expand to fill the available space and contain the Button: a FrameLayout. Here's the working code, which still goes inside my root RelativeLayout:
<Button
android:id="#+id/button_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/left" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#+id/button_left" >
<Button
android:id="#+id/Turn_button_pass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="#string/right" />
</FrameLayout>
Now, the FrameLayout always takes up all the space to the right of the left-hand button, and lays out the right-hand button inside that space using android:layout_gravity="right".
This answer only adds one extra layout, but if someone has a way to do it only using the existing RelativeLayout, to minimise the number of ViewGroups in the layout, I'll accept that as a solution.
IF you can live with the constraint, that the right button only can take up to up half of the available space, this could be a solution for you:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_weight="1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A short text" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="A very long text which is limited to one half of the available space" />
</RelativeLayout>
</LinearLayout>
You could just use a TextView and make it look like a button. Create a dummy button, extract the background and set that background to the textfield programmatically.
(Not tested but should give it the apperance of a button)
Drawable d = button1.getBackground();
textView1.setBackground(d);
then you just set the onClickListener and that should yield what you're looking for. The TextView would take the place of the "button_right" in your first layout.
**Edit
Your xml would look something like this
<Button
android:id="#+id/button_left"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="#string/left" />
<TextView
android:id="#+id/button_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:maxEms="10"
android:text="TextView" />
I have a TextView1, buttons1 and buttons2 Horizontally.
When Button2 click Button1 is invisible.
When Button1 is invisible, I need to increase the width of the textview to cover the space created when button1 is invisible and vice versa.
Can Please anybody help How can I change the Textview width dynamically.
NB: Textview tv.setWidth(pix) is not working. when I tried to find the value of tv.getWidth() gives always zero.
`
<TextView
android:layout_width="odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/hello" />
<Button
android:id="#+id/btn1"
android:layout_width="odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/hello"
android:visibility="visible" />
<Button
android:id="#+id/btn2"
android:layout_width="odp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/hello" />
</LinearLayout>`you can use weight for three element and button1.setVisibility(View.GONE)
Please try to use GONE instead of using INVISIBLE. When using INVISIBLE even though you can't see it, it still takes a space.
Wrap your widgets in a RelativeLayout and then check. You will be able to bring your textview on top of your button if it is invisble. You cant do this if you are using LinearLayout because it sets widgets horizontaly or verticaly and does not overlap.