Buttons in dialog boxes sticking to the bottom - android

I'm building a custom dialog box for my Application. While doing so, my OK and Cancel buttons are going right to the bottom of this view i.e. to the bottom edges of the dialog box. How can I pull these up a little so that there is a slight gap between the buttons and the bottom edge of the dialog box.
My XML code is as follows:
<TextView
android:id="#+id/settingsview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Select Settings" />
<RadioButton
android:id="#+id/onemin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/settingsview"
android:layout_below="#+id/settingsview"
android:layout_marginTop="14dp"
android:text="#string/radio_one" />
<RadioButton
android:id="#+id/twomin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/onemin"
android:layout_below="#+id/onemin"
android:text="#string/radio_two" />
<RadioButton
android:id="#+id/tenmins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/twomin"
android:layout_below="#+id/twomin"
android:text="#string/radio_three" />
<Button
android:id="#+id/set"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/onemin"
android:layout_below="#+id/tenmins"
android:layout_marginRight="54dp"
android:layout_marginTop="30dp"
android:background="#drawable/custom_btn_livid_brown"
android:text="#string/submit_btn" />
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/set"
android:layout_alignBottom="#+id/set"
android:layout_toRightOf="#+id/twomin"
android:background="#drawable/custom_btn_livid_brown"
android:text="#string/cancel" />

have you tried using gravities? this is my solution when i have trouble with positioning my widgets.
basically, you need some parent "container" where your buttons are in and then position them with changing the gravities within the container for your widgets.
Vogella has a good tutorial for this topic!!
vogella-tutorial

Though you don't provide your full code here.Not sure whether you use above things in a LinearLayout or RelativeLayout.But as you say..
How can I pull these up a little so that there is a slight gap between
the buttons and the bottom edge of the dialog box
So i think android:layout_marginTop="" can do the trick,if you decrease those values.
Use
android:layout_marginTop="10dp" //change this value according to you need
And if you want to move you buttons a little bit left rather than right then change..
android:layout_marginRight="30dp" //change this value according to you need
I show here 2 examples of how can you improve those condition.If you still face problem then post full xml code here so that i can test that in my environment.

Related

Android components are not matched in position when running the app using phone

I placed a button in the middle of design layout from activity_main.xml but when i run the app through my phone, the positioning is not the same as the design inside activity_main.xml.
Tried to change layout to relative and the problem still exist.
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="160dp"
android:layout_marginLeft="160dp"
android:layout_marginTop="276dp"
android:text="Button"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
I expect that the design position from activity_main is the same when run through an actual android phone.
From the design layout
from the actual phone
First of all, you might want to adjust button relatively with other components. i.e. try to align your button on top of other component or maybe on bottom of another component.
Refer this link, it has all information
https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams#attr_android:layout_alignBottom
If you have confusion, share whole xml file for us to refer.
You need just to change the <ConstraintLayout/> in <RelativeLayout/> and replace the Button code by this:
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />
Hope this will help.
First of all remove that margins from attributes. There is the left margin which make your button to go right and top margin which move button below the actual center point in small density devices.
So If you want to add button in center of screen in all devices. Use below code without any margin. But if your want to add spacing between other view then use small margin in other views.
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Refer this guide to learn basics of constraints : guide
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#efeff1">
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Button" />
</RelativeLayout>

How can I use RelativeLayout to align buttons to the right of an ImageView

I'm struggling with a simple relative layout. It is supposed to have an image on the left and a column of other views on the right. The vertical alignment is correct but the horizontal alignment is very puzzling with the buttons on the left of the image even though I have asked for them to be on the right.
Can anyone explain:
why this happens,
how to achieve the arrangement I want.
Here is the layout xml and a screenshot:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/widget51"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:id="#+id/ivLove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:id="#+id/tvDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:hint="Image name"
android:layout_toRightOf="#+id/ivImage"
/>
<TextView
android:id="#+id/tvPage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Page URL"
android:layout_below="#+id/tvDate"
android:layout_toRightOf="#+id/ivImage"
/>
<Button
android:id="#+id/btnNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Next"
android:layout_below="#+id/tvPage"
android:layout_toRightOf="#+id/ivImage"
/>
<Button
android:id="#+id/btnPrevious"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previous"
android:layout_below="#+id/btnNext"
android:layout_toRightOf="#+id/ivImage"
/>
</RelativeLayout>
In case you are wondering about the image see Android : Simple HTML parsing & image downloader using AsyncTask
You are putting those controls (TextView, Button) on the right side of the ImageView with id ivImage.
However, your layout does not contain any ImageView with that id (but has one with ivLove).
The controls appear on the left because that's the default docking for views inside a RelativeLayout.
Try updating your references (replace each "ivImage" with "ivLove".
Note -- if you use Eclipse, that the reason you didn't get compile errors for this is that you probably have an ImageView with android:id="ivImage" somewhere in your project. Enabling (and maybe updating) lint will help you filter these kinds of issues.

Positioning text within button in Android

I have a button with a symbol of an arrow. Besides, I want to have some margin between the button and the adjacent elements:
<Button
android:id="#+id/TSPrev"
style="?android:attr/buttonStyleSmall"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="10dp"
android:background="#android:color/holo_orange_light"
android:text="#string/left"
android:textSize="60sp" />
However, when I do this, the arrow appears like this:
How can I keep the text centered vertically when I have margins? Without them, the button text appears correctly.
Thanks
May be This will Help You :
<Button
android:id="#+id/notification"
style="#style/Wrap"
android:layout_margin="8dp"
android:background="#drawable/red_shape_corner"
android:drawableRight="#drawable/notification_bell"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/str_notification"
android:textColor="#android:color/white" />
You Need to use
// Nothing but Image like Arrow etc.
android:drawableRight="#drawable/notification_bell"
// Here you can put you text
android:text="#string/str_notification"
Try using android:gravity="center".
android:gravity attribute documentation

Right-align button with dynamic text

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" />

Most efficient way to draw Buttons on top of a View?

The app I am working on consists of one large custom view that fills the screen. I placed one button in the center of each side of the custom view that can be used to move the drawn object in that direction. Think of the custom view as a "viewfinder" of a camera and I would like the user to be able to pan the viewfinder with buttons on all four sides.
The buttons work perfectly, but when one is pressed, the custom view underneath undergoes lots of redrawing (4 redraws instead of 1) and the UI lags quite a bit.
Is there some way to stop the custom view from redrawing because of a button's animation?
Forgive me if I am missing something obvious.
Thanks in advance for any help... Android rocks!!!
XML Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<com.app.CustomView
android:id="#+id/CustomView" android:layout_above="#+id/menuButton" android:layout_height="fill_parent" android:layout_width="fill_parent"/>
<Button android:layout_height="wrap_content" android:layout_below="#+id/CustomView" android:layout_width="wrap_content" android:layout_alignRight="#+id/CustomView" android:id="#+id/toolsButton" android:text="Tools" android:textSize="24sp"></Button>
<Button android:layout_height="wrap_content" android:id="#+id/outButton" android:text="-" android:textSize="30sp" android:layout_toLeftOf="#+id/inButton" android:layout_alignTop="#+id/inButton" android:layout_width="40sp"></Button>
<Button android:layout_height="wrap_content" android:layout_alignRight="#+id/CustomView" android:layout_alignBottom="#+id/CustomView" android:text="+" android:textSize="30sp" android:id="#+id/inButton" android:layout_width="wrap_content"></Button>
<Button android:layout_alignTop="#+id/CustomView" android:layout_centerHorizontal="true" android:layout_height="40sp" android:layout_width="60sp" android:text="^" android:id="#+id/upButton"></Button>
<Button android:layout_alignBottom="#+id/CustomView" android:layout_centerHorizontal="true" android:text="v" android:layout_height="40sp" android:layout_width="60sp" android:id="#+id/downButton"></Button>
<Button android:layout_alignRight="#+id/CustomView" android:text=">" android:id="#+id/rightButton" android:layout_height="60sp" android:layout_width="40sp" android:layout_centerVertical="true"></Button>
<Button android:id="#+id/leftButton" android:text="<" android:layout_height="60sp" android:layout_width="40sp" android:layout_centerVertical="true"></Button>
<Button android:layout_height="wrap_content" android:layout_width="wrap_content" android:layout_alignLeft="#+id/CustomView" android:id="#+id/menuButton" android:textSize="24sp" android:text="Functions" android:layout_alignParentBottom="true"></Button>
</RelativeLayout>
Why are the buttons part of the custom view? Why not make them part of your activities layout, so that the layout consists of all the buttons and your custom view?
This may not completely solve your problem, since you may need to further optimize/rethink how your custom view is implemented if invoking its onDraw method blocks the UI thread.
I found that I was actually making my calculations too accurate and was working my poor Droid too hard. Everything draws smoothly now.

Categories

Resources