Possible to set attributes of ActionViewClass through xml in Android Action Bar? - android

I have the following menu xml code:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/action_select_appshare"
android:showAsAction="always"
android:title="#string/action_select_appshare"
android:actionViewClass="android.widget.Spinner" />
</menu>
Because a Spinner seem to require the definition of the android:spinnerMode attribute from the get-go, and there doesn't seem to be a proper way to set it later on after the Spinner object is created. I was wondering if there's a way to specify additional attributes (such as android:spinnerMode in my e.g.) for an android:actionViewClass within the menu XML?
Thanks!

Finally found it:
Related links: 1, 2 and 3
Create a new res/layout file. Put the spinner in it, with all the properties you need/want. Note that the Spinner should be the root view.
In your menu file containing the actionClassProvider=spinner, remove actionClassProvider, and replace it with actionLayout="#layout/my_spinner"
All Done
I hope this helps somebody!

well you could solve it in 2 ways: create a custom spinner and put what you want there, or, get the actionView when inflating this menuItem, and then do what you want with it.

Related

How to use my own custom EditText layout as a searchView

I have just starting using an ActionBar, everything is well apart from the search box. I am now wanting to use a SearchView but have found it to be hard to customize, is there a way to use my existing EditText based layout in place of the expandable SearchView?
The top one is the new SearchView and the bottom is my old one which I would like to use, I am not worried about any extras like autocomplete.
I have looked on google for some time but cannot find any solution, my XML for the menu looks like so :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/btnSearch"
android:title="Search"
android:icon="#drawable/ic_action_search"
android:showAsAction="always|collapseActionView"
android:actionViewClass="android.widget.SearchView"
android:queryHint="blah blah"/>
...
</menu>
I have found that I can get the bottom line to look similar to my old search box by using some ugly looking reflection code, however it still does not look right, the padding etc is all wrong. I don't want any reflection, I just want to use my old layout.
I found something that could solve your problem:
http://www.techrepublic.com/blog/software-engineer/pro-tip-customize-the-android-search-view-widget/#.
I wrote this class to help with SearchView customization:
https://gist.github.com/jaredrummler/c408c9d897fd92d5d116
You could do something like this after inflating the menu in onCreateOptionsMenu:
SearchViewStyle.on(menu, R.id.your_search_id)
.setSearchPlateDrawableId(R.drawable.your_edit_text);

How to view default android simple_spinner_dropdown_item style

Often i like default android style defined for some widget.
Often i like to just slightly modify this default style, not create my own style from a scratch.
Now, i use android.R.layout.simple_spinner_dropdown_item as dropDownViewResource on my spinner adapter - like this:
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
I can open this layout xml file and copy its content to modify it. But the most important part of that xml file is attribute style, it looks like this: style="?android:attr/spinnerDropDownItemStyle"
Now, my question is: how do i find/view/copy this style spinnerDropDownItemStyle, so i can for example just change background color.
Thanx for your help
here you can find style spinnerDropDownItemStyle:
https://github.com/android/platform_frameworks_base/blob/master/core/res/res/values/themes.xml
But, if you looking for change background of Spinner, you'll need to search for 9 patch images:
https://developer.android.com/studio/write/draw9patch.html
And here a good example:
http://www.gersic.com/blog.php?id=57
Ty
My suggestion to you is . you can make a seprate custom layout and put it on your spinner setDropDownResource()
adapter.setDropDownViewResource(android.R.layout.CUSTOM_LAYOUT);
Now you can easily make a style for your layout

Setting background colour of Android layout element

I am trying to, somewhat clone the design of an activity from a set of slides on Android UI design. However I am having a problem with a very simple task.
I have created the layout as shown in the image, and the header is a TextView in a RelativeLayout. Now I wish to change the background colour of the RelativeLayout, however I cannot seem to figure out how.
I know I can set the android:background property in the RelativeLayout tag in the XML file, but what do I set it to? I want to define a new colour that I can use in multiple places. Is it a drawable or a string?
Additionally I would expect there to be a very simple way to this from within the Eclipse Android UI designer that I must be missing?
I am a bit frustrated currently, as this should be an activity that is performed with a few clicks at maximum. So any help is very appreciated. :)
You can use simple color resources, specified usually inside res/values/colors.xml.
<color name="red">#ffff0000</color>
and use this via android:background="#color/red". This color can be used anywhere else too, e.g. as a text color. Reference it in XML the same way, or get it in code via getResources().getColor(R.color.red).
You can also use any drawable resource as a background, use android:background="#drawable/mydrawable" for this (that means 9patch drawables, normal bitmaps, shape drawables, ..).
The above answers are nice.You can also go like this programmatically if you want
First, your layout should have an ID. Add it by writing following +id line in res/layout/*.xml
<RelativeLayout ...
...
android:id="#+id/your_layout_id"
...
</RelativeLayout>
Then, in your Java code, make following changes.
RelativeLayout rl = (RelativeLayout)findViewById(R.id.your_layout_id);
rl.setBackgroundColor(Color.RED);
apart from this, if you have the color defined in colors.xml, then also you can do programmatically :
rl.setBackgroundColor(ContextCompat.getColor(getContext(), R.color.red));
You can use android:background="#DC143C", or any other RGB values for your color. I have no problem using it this way, as stated here
The
res/values/colors.xml.
<color name="red">#ffff0000</color>
android:background="#color/red"
example didn't work for me, but the
android:background="#(hexidecimal here without these parenthesis)"
worked for me in the relative layout element as an attribute.
If you want to change a color quickly (and you don't have Hex numbers memorized) android has a few preset colors you can access like this:
android:background="#android:color/black"
There are 15 colors you can choose from which is nice for testing things out quickly, and you don't need to set up additional files.
Setting up a values/colors.xml file and using straight Hex like explained above will still work.
4 possible ways, use one you need.
1. Kotlin
val ll = findViewById<LinearLayout>(R.id.your_layout_id)
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white))
2. Data Binding
<LinearLayout
android:background="#{#color/white}"
OR more useful statement-
<LinearLayout
android:background="#{model.colorResId}"
3. XML
<LinearLayout
android:background="#FFFFFF"
<LinearLayout
android:background="#color/white"
4. Java
LinearLayout ll = (LinearLayout) findViewById(R.id.your_layout_id);
ll.setBackgroundColor(ContextCompat.getColor(this, R.color.white));
Android studio 2.1.2 (or possibly earlier) will let you pick from a color wheel:
I got this by adding the following to my layout:
android:background="#FFFFFF"
Then I clicked on the FFFFFF color and clicked on the lightbulb that appeared.
Kotlin
linearLayout.setBackgroundColor(Color.rgb(0xf4,0x43,0x36))
or
<color name="newColor">#f44336</color>
-
linearLayout.setBackgroundColor(ContextCompat.getColor(vista.context, R.color.newColor))
The answers above all are static. I thought I would provide a dynamic answer. The two files that will need to be in sync are the relative foo.xml with the layout and activity_bar.java which corresponds to the Java class corresponding to this R.layout.foo.
In foo.xml set an id for the entire layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/foo" .../>
And in activity_bar.java set the color in the onCreate():
public class activity_bar extends AppCompatActivty {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.foo);
//Set an id to the layout
RelativeLayout currentLayout =
(RelativeLayout) findViewById(R.id.foo);
currentLayout.setBackgroundColor(Color.RED);
...
}
...
}
I hope this helps.

Add style to ImageButton dynamicaly

I have added a imagebutton dynamically to my Tablerow, now i want to define the style for the image button to a style that i have previously defined. I have tried the setTheme method that doesnot seem to work
Thanks for any help
k after a lot of searching this is what i found and it works perfectly.
1) if you want to create a button/imagebutton/anyView at runtime with a predefined style the you can do so just follow the steps below
2a) Create a new xml file in res/layout and put that one element you want to create for example I wanted to create a minusButton. This is minusb.xml in res/layout
<?xml version="1.0" encoding="utf-8"?>
<ImageButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
style="#style/MinusButton"/>
2b) Then add the following lines to your java activity code where you would like to create it
ImageButton bt = (ImageButton) getLayoutInflater().inflate(R.layout.minusb, null);
bt.setLayoutParams(new TableRow.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,0f));
tr.addView(bt);

Android how to make View highlight when clicked?

I have a linear layout in which each row is inflated programatically and I want the rows to behave like the ListView when clicked on. That is, I want the row to highlight in the exact same way/colour that the default ListView does. How would I go about doing this?
Ok I have finally figured out how to do this...basically it is done using a selector like the color selector linked by style except instead of 'color' use a drawable for the states and you can refer to the default list drawable that is used in ListView by this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#android:drawable/list_selector_background" />
</selector>
and using this xml as the background for my View.
All the public default drawables can be found here: http://developer.android.com/reference/android/R.drawable.html
I was able to do the same with a text view that I wanted to behave like a list item by using:
<Textview
....
android:background="#android:drawable/list_selector_background"
/>
This might be a good place to start looking.
Although, i would advise you to use the ListView itself, rather than implementing it again.
if you still have a problem with that then please remember that some of UI elements are not clickable (RelativeLayout), so you have to add one more line:
<RelativeLayout
....
android:clickable="true"
...
To your listview set property
android:listSelector="#color/test"
and this test color set any transparent color you like. you can create any transparent color by using hex transparent color

Categories

Resources