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.
Related
I want to create a signal light of three lights which are made from the same resource and tinted in its individual color (red, yellow, green) and being controlled by java code. However, it looks like I cannot mutate and create independent instances of the source drawable.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/light1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/presence_invisible" />
<ImageView
android:id="#+id/light2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/presence_invisible" />
<ImageView
android:id="#+id/light3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#android:drawable/presence_invisible" />
</LinearLayout>
Attempt:
ImageView iv1 = (ImageView)findViewById(light1res);
Drawable dw = ContextCompat.getDrawable(activity, android.R.drawable.presence_invisible);
dw.mutate();
dw.setColorFilter(Color.RED, PorterDuff.Mode.SRC_IN);
iv1.setImageDrawable(dw);
Result: None of the three imageView get tinted. If I remove dw.mutate(), then all three imageViews get tinted though. If I replace dw.mutate() by dw=dw.getConstantState().newDrawable() then all imageViews get tinted either. If I do dw.mutate().getConstantState().newDrawable() the result is the same as with .mutate() (no effect on drawable editings) so nothing is being tinted. If i do dw.getConstantState().newDrawable().mutate() then all ImageViews get the same changes so it's like not doing mutating at all. I can do all this and similar things at any line and the results remain the same.
It does not matter wether I use the function setColorFilter(...) or DrawableCompat.tint(...) after wrapping, the resulting behaviour is exactly the same as described above.
So it looks to me like after doing mutate(), changes on the drawable do not have any effects and .getConstantState().newDrawable() provides a drawable which still shares its states with the original.
Obtaining the drawable by iv1.getDrawable() makes no difference either.
Any ideas what's going on and how I could solve this particular, strange issue?
I'm using build tools 25.0.3 (compile sdk version api 25 Android 7.1.1) and testing on Android 5 and Android 7 phones (same results).
I know that many devs have issues with drawable states and tinting but apparently all could be solved by mutating and using the codes I mentioned here, so please consider this before you think this is a duplicated question.
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?
I Have Added a spinner drop down and i want to add a arrow to it
something like this:https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSrv1J0z4bnBqPyV6XdPcqU46P4Liw60tbntm1Z8WXyHA7K8PYBsg
And Yes I Know i have to add a background image with the style of arrow i want but here's the thing, The Arrow color needs to be changed depending on the user set color, So creating one arrow for each color is not really a solution
I had the idea of using one of those Hex arrow's in a textbox and change the color that way but not sure if that is the right way
anyone have any ideas?
Add this line in your spinner code for down arrow which you shown in your image link.
android:background="#android:drawable/btn_dropdown"
<Spinner
android:id="#+id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown" />
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
I have a android layout in xml, and I have an image i placed using java/android code. I have placed the image without using xml, but I was wondering if I am able to also use XML to position buttons on the page as well. I don't know how to do this because I am already using setcontentview to draw my image to the screen... and if I use setcontentview(R.exampleXMLfile) it will overwrite my image. Does anyone know a solution or example for this? The reason I am looking is because its a pain to add a button without using XML and eventually I want to have animations on the screen. Thanks,
You could use ImageView in your xml file,
<ImageView
android:id = "#+id/my_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_drawable"
/>
or just View and set Background, or set background on any Layout in your xml.
<View
android:id = "#+id/my_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image"
/>