Changing AlertDialog buttons alignment - java

Been fighting for hours about changing the alignment of the buttons inside AlertDialog (support.v7 one), since they won't align themselves according to the locale view direction, despite the whole app DOES align to left and also the text inside the AlertDialog.
(Why would this happen you say? I'm programatically configuring the locale language to be "en" since that's my default app language, even though the system locale might be something else).
So like I said, I don't need to touch the message inside the dialog, but as an example, that's how to change it's direction:
TextView messageView = (TextView)dialog.findViewById(android.R.id.message);
messageView.setGravity(Gravity.RIGHT); // or LEFT
Of course it doesn't work on the buttons, since I need to change the layout gravity instead.
Here's how I find the buttons (after I call show() on the AlertDialog.Builder of course, else they would be null):
AppCompatButton accept = (AppCompatButton)dialog.findViewById(android.R.id.button1);
AppCompatButton cancel = (AppCompatButton)dialog.findViewById(android.R.id.button2);
And here's how I attempt to change their alignment inside their parent LinearLayout:
((LinearLayout.LayoutParams)accept.getLayoutParams).gravity = Gravity.RIGHT;
((LinearLayout.LayoutParams)cancel.getLayoutParams).gravity = Gravity.RIGHT;
I chose RIGHT since the side of the buttons inside the dialog is always opposite to the side which the text is aligned to. (Yes - I tried LEFT also, nothing changed).
This doesn't work. Does anyone have an idea how to achieve this? It seems they are just stuck to their place.
Edit:
Title isn't aligned also, I just confirmed this (for some reason it appears on the right, like my system configuration, and not like my locale configuration).

The problem isn't the Gravity settings... When you look at the xml source (../sdk/platforms/android-23/data/res/layout/alert_dialog_material.xml), the layout's AlertDialog contains this:
<LinearLayout android:id="#+id/buttonPanel"
style="?attr/buttonBarStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" ...>
<Button android:id="#+id/button3"
style="?attr/buttonBarNeutralButtonStyle" ... />
<Space
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="invisible" />
<Button android:id="#+id/button2"
style="?attr/buttonBarNegativeButtonStyle" ... />
<Button android:id="#+id/button1"
style="?attr/buttonBarPositiveButtonStyle" ... />
</LinearLayout>
There is a Space view with the buttons. This view fills the container with its weight. Therefore, you actually have a widget which pushes the buttons at the right's parent container.
A simple solution might to get the parent's buttons container and remove the Space element before setting a gravity.
// get the container
LinearLayout containerButtons = (LinearLayout) dialog.findViewById(R.id.buttonPanel);
// remove the Space view
containerButtons.removeView(containerButtons.getChildAt(1));
// set a gravity to the children buttons
containerButtons.setGravity(Gravity.RIGHT);
However, you should create and use your own custom layout, in case of future Google's development changes might occurring.

You can completely customize an AlertDialog.
The trick here is probably to use a custom view for the dialog, and create your own buttons in that view.
For an example, see
How can can I add custom buttons into an AlertDialog's layout?

Related

Setting Spinner background to null programmatically

I'm trying to set the background of a Spinner to null programmatically as would the following XML code:
<Spinner
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/spinner1"
android:background="#null"
/>
I tried either of:
spinner1.setBackground(null);
spinner1.setBackgroundDrawable(null);
spinner1.setBackgroundResource(0);
But none of these three give the same output as the xml. Via xml, the whole arrow region disappears, including padding, whereas via code, only the arrow sign disappears but the full length of the arrow region remains.
Any clue?
Just set it to Transparent instead. Make a style for the spinner, or simply set it's background="#00000000"
or if you want to do it in code you could use a style swap or simply declare a drawable.
Drawable transparentDrawable = new ColorDrawable(Color.TRANSPARENT);
mySpinner.setBackground(transparentDrawable);
Note* make sure you use the correct setBackground for the correct OS as it deprecated and moved from setBackgroundDrawable after lollipop I believe it was.

Android Vertical Switch Widget

Using android, I'd like to get a vertical switch widget implementation in place. As far as I can tell, it looks like switches only have a horizontal orientation. I'd like to have something like the following:
After looking through the threads here and searching google, I have yet to find something that can give me this. Everything I search for gives me the horizontal implementation only.
so I can generate your typical horizontal switch
<Switch
android:id="#+id/switch_button"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/label"
android:layout_gravity="center"
android:switchMinWidth="130dp"
android:thumb="#drawable/switch_selector"
android:track="#drawable/track_selector" >
</Switch>
but there doesn't seem to be a good way to set the orientation. I know that the question is a bit high level. Is there some attribute that is immediately available that will allow me to have a vertical switch? Or will I have to create a custom switch and possibly modify the onDraw method so that it is flipped vertically? Any help is appreciated. Thanks.
Try android:rotation="90" like this:
<Switch
android:id="#+id/switch_button"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/label"
android:layout_gravity="center"
android:switchMinWidth="130dp"
android:thumb="#drawable/switch_selector"
android:track="#drawable/track_selector"
android:rotation="90">
</Switch>
There is no quick attribute for vertical orientation.. Sorry :)
First you can look at the code of the switch and see if you can copy and manipulate it.
Second you can just implement your on. Have a layout with a button inside it. use onTouchListener to slide it from side to side. No need to use "onDraw".
Try toggle button witch graphics and use pictures like this You included in Your post. Here is example of such toggle buttons: Toggle button using two image on different state

How to use two different font sizes for button text in Android?

How could I implement a button background for ImageButton or Button which contains 2 textview each using different font sizes ? I cant use static images as background and I need to achieve this either using xml or programmatically.
This is a numeric keypad and it carries a numeric value and a set of alphabets each of which uses different font sizes and needs to be set as button text.
Here are some of the suggestions that pop-up on my brain but seems to have limitations since I want to reuse the UI component and avoid code repetition.
Create a layout xml containing 2 textfields and set that as background on the button. But how can I get a reference of these textview fields to set the value on them in the MyActivity.java?
Use a layeredlist ? still same problem reference of textviews
Or create a custom view and inflate layout mentioned in step 1. This solution resolves my problem.
Does any other solution exist for this UI requirement?
Thanks.
Hopefully this will help. This off course is not the only solution but it is a pretty simple one. Add the xml part in your xml instead of the button.
XML
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="5"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="ABC"/>
</LinearLayout>
CODE
LinearLayout button= (LinearLayout)findViewById(R.id.button_layout);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO DO whatever you want to do here
}
});
For your button you can use code like this
don't forget put font in your Assets folder
Button btnA=(Button) findViewById(R.id.button1);
Typeface typeface = Typeface.createFromAsset(getAssets(), "yourFont.ttf");
btnA.setText("my custom font");
btnA.setTypeface(typeface);
btnA.setTextSize(20);//as per your size
and for more reference see this
do as your second button if this helps let me know thanks...

android:gravity fails on API 18+

I have some TextViews on my app, I don't know why but the android:gravity attribute is not centering the text content where it should be on devices running the API 18+ (4.3+).
There is the code I use on my custom TextView, this is a child of RelativeLayout:
<com.package.custom.CustomTextFont
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/seekbar"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/seekbar"
android:layout_marginLeft="#dimen/margin_tiny_double"
android:layout_toLeftOf="#+id/seekbar"
android:gravity="center_vertical|left"
android:paddingRight="#dimen/margin_tiny"
android:text="#string/text1"
android:textColor="#color/black"
android:textSize="#dimen/size_text_normal" />
This code should take the edges of this TextView and align it to the top and bottom and put it to the Left of the SeekBar, this is working, but the TextView gets big, so with android:gravity I center the text to the center and left from it self. It works, but I don't know why, the text is not centered at center|left on devices running android 4.3 and 4.4. This issue can be reproduced on real devices and as well on the layout preview (Graphic layout) of Eclipse.
There is any changes made on API 18+ or on android:gravity that I'm missing?
PS: I'm targetting the API 19 on the project and on AndroidManifest.xml
PS2: My TextView is custom just to set an external font.tff
This is how it looks like on API 17-
This is how it looks like on API 18+
Thanks in advance!
= UPTADATE =
On my Manifest, I changed the android:targetSdkVersion to 17 instead of 19 and the problem disappeared, but this is a "trick", not a solution, what can I do since it could be an issue from the API ? And yes, I have the latest version of the API 18 and 19 (today, 01/30/2014).
This appears to be a known issue in API 18+:
https://code.google.com/p/android/issues/detail?id=59368
https://code.google.com/p/android/issues/detail?id=59700
The problem seems to occur when a TextView is part of a scrollable container (e.g. ListView), making the view ignore the vertical gravity for some reason (some sources suggest this has to do with the TextView being the child of a RelativeLayout, though it's been my experience that this can happen even when no such layout is involved).
A possible workaround (albeit not a particularly elegant one), would be to wrap the TextView in a LinearLayout. You can then use "layout_gravity" on the TextView to center it inside the LinearLayout, instead of relying on "gravity" (just make sure to wrap_content so the text itself is properly centered).
E.g., in your example:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/seekbar"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/seekbar"
android:layout_toLeftOf="#+id/seekbar"
android:layout_marginLeft="#dimen/margin_tiny_double" >
<com.package.custom.CustomTextFont
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="left"
android:paddingRight="#dimen/margin_tiny"
android:text="#string/text1"
android:textColor="#color/black"
android:textSize="#dimen/size_text_normal" />
</LinearLayout>
This method does have the disadvantage of adding an otherwise-unnecessary level to your view hierarchy, but it currently seems to be the only way around this (other than reverting to an earlier API level).
Also see similar question at:
Android sdk 18 TextView gravity doesn't work vertically
Your textview height is "wrap_content", which means the height of the textview will be the same as the height of the text. If you change the background of the textview to black, it might be easier to see the bounds of the view. I'm guessing you'll find that the textview doesn't have as much height as you expect.
Try setting the height of the textview to match_parent. You can wrap the textview inside another view if needed and modify its height as appropriate.

How to position a button exactly how I want it?

I'm using RelativeLayout. I understand using Graphical Layout you can't position a button too well. How can I do it in XML? I've tried changing the marginLeft, but nothing moves.
All I want to do is position my buttons!
<Button
android:id="#+id/ListsButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/EditButton"
android:layout_alignBottom="#+id/EditButton"
android:layout_alignParentLeft="true"
android:layout_marginLeft="18dp"
android:text="Li" />
You can use marginLeft, marginRight, marginTop, and marginBottom for small adjustments. If you want to do larger adjustments you can use blank elements in combination with weights, or use a RelativeLayout in combination with alignParentBottom, alignParentTop, alignParentRight, and/or alignParentLeft
If you are beginner in android programming then I would recommend using graphical layout since you won't know every attribute for positioning and styling your views. You can see #Freelancer answer to see how many attributes you have to consider while positioning any view correctly.
Using graphical layout is very easy once you get a little hang of it. You can use Relative Layout which will help you moving any view like button or textView wherever you want. Direct xml coding will require some skills that you will no doubt get in time.

Categories

Resources