How to Add a Arrow to Spinner Drop Down? - java

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

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.

How to show a left colored border on each ListView row?

How to show only a left colored border on each ListView row?
I have a 5 degree termometer for user reputation and I want to show the respective colors at the left border of each listview row.
Like this link
Thanks guys!!
The easiest way would be to create a dummy View in your item layout. That way you can easily change the color of the background when you bind the layout to the data. Something like this:
item_list_layout.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android">
<View
android:id="#+id/termometherIndicator"
android:height="match_parent" android:width="4dp"
android:background="#color/default_color" />
<LinearLayout ....>
<!-- The rest of the item layout goes here -->
</LinearLayout>
</LinearLayout>
Then get the view reference in code behind using the Id and change the background color to whatever you need.
The ListView row should be an horizontal LinearLayout, and include this at the beginning, as a first child, followed by the TextView:
<View
android:id="#+id/reputationColor"
android:layout_width="4dp" //Aproximately the size from your image
android:layout_height="36dp" //About the size of a List Row, but less, as seen in your image
android:layout_gravity="top"
/>
You will need a CustomAdapter for your ListView, which you can find a full example in here
Inside the CustomAdapter, you can set the colour programatically with
View reputationColor = (View)findViewById(R.id.reputationColor);
switch (reputation) {
case 1:
reputationColor.setBackgroundColor(#ff0000);
break; //Include other cases up to 5
default:
break;
}
For more information on CustomAdapters, go to the following websites:
ONE
TWO
THREE
You can add a panel to the leftmost part of your app. In that panel you can place one of the predefined images of thermometer. You only have 5 levels, right? Once value changes - select corresponding image of thermometer reading and redraw gui element.

Android Search icon on keyboard WITHOUT moving to next element

This may seem like a dumb question, but I want the magnifying glass on the keyboard, but I don't want the focus to go right to the next element.
I've got an EditText that populates a ListView with possible matches to the user's query. The EditText's XML includes
"android:imeOptions="actionSearch"
which adds the search icon. But it also takes the user to the first element of the ListView which is really annoying.
Is there a way to include the search icon AND not force focus to the ListView?
EDIT:
By request, my EditText xml. Sorry in advance if I mess up the formatting somehow.
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editSearchWord"
android:imeOptions="actionSearch|normal"
android:inputType="text"
android:drawableLeft="#android:drawable/ic_menu_search"
android:layout_alignParentStart="true"/>

Changing AlertDialog buttons alignment

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?

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

Categories

Resources