Custom spinner dialog for Android - android

How do I do a custom theme android spinner dialog?

This blog has an example that probably covers your question:
http://www.mokasocial.com/2011/03/easily-create-a-default-custom-styled-spinner-android/
Edit:
These blog posts has a pretty similar answer:
http://karanbalkar.com/2013/07/tutorial-39-create-custom-spinner-in-android/
http://stephenpengilley.blogspot.no/2013/01/android-custom-spinner-tutorial.html
The key to customizing an Android Spinner is to create a custom resource for the spinner rows, and then passing that row layout into a custom adapter. In the custom adapter, one can override the following two functions:
#Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
// Custom view generation
}
#Override
public View getView(int pos, View cnvtView, ViewGroup prnt) {
// Custom view generation
}
To determine how the Android Spinner will look to the user.

Related

Get View reference in a custom spinner adapter

I have this:
ArrayAdapter<String> spinnerAdapter=new ArrayAdapter<String>(context,R.layout.spinneritem,arrayListWithData);
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Inside the R.layout.spinneritem I have only a TextView. I need to access that TextView to set a custom typeFace to it.
Is there any way I can get a reference to the TextView, so I can set the typeface programmatically to it?
Thank you.
You need to implement a custom ArrayAdapter and change the typeface inside the
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
or
#Override
public View getView(int position, View convertView, ViewGroup parent)
method, based on your needs.
If you need to simply change the font of your TextView, you can follow the Android guide on How to change font in XML in the developer site

Android how to use different layouts for each listview item

In my application I can only use the same layout for each listview item in each row. This is not what I want for my application and I would like to have a different layout for each of the listview items.
As an example of what I would like to achieve please see the screenshot below.
You can do it via android Listview BaseAdapter. In BaseAdapter getView method, you can added code like this.
public View getView(final int position, View convertView, ViewGroup parent) {
if(item1 or your condition)
{
convertView=inflater.inflate(R.layout.from_right_row.xml, parent, false);
}else if(item2 or your condition) {
convertView=inflater.inflate(R.layout.from_left_row.xml, parent, false);
}
return convertView;
}

Android: Get view reference to ActionBar navigation?

Currently, I need to access parts of the ActionBar in order to attach contextual help bubbles in appropriate locations. That seems fairly simple for Menu Items as menuItem.getActionView is available.
However, I'm not sure how to get similar access to the navigation list in the ActionBar. Instead of including items in the menu xml file, the navigation options and listeners are added like this:
getActivity().getActionBar().setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
getActivity().getActionBar().setListNavigationCallbacks(mNavigationListAdapter, this);
With Menu Items, the following works:
MenuItem menuItem = menu.findItem(R.id.my_nifty_menu_item);
View viewToAttachHelpBubbleTo = menuItem.getActionView();
How can I get a similar view reference to the navigation menu used in the ActionBar?
It is possible to store the view in the Adapter and then pull it from there.
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Stuff happens...
mLastParentView = parent;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
// Stuff happens...
mLastParentView = parent;
}
public View getLastParentView() {
return mLastParentView;
}
and then after
getActivity().getActionBar().setListNavigationCallbacks(mNavigationListAdapter, this);
it is possible to just request the view for the navigation list from the adapter with getLastParentView()

Handling button event in each row of Listview issue

I am new to android and am trying to develop a new android app. But I am struggling to siolve one of the problems in my project.
I am using a listview extended from baseadapter and need to add a button in each row of thelistview. When I click on the button in any row of the listview, I want that it should be removed. However when I do so, some of the other buttons also get removed in the listview.
How can I solve this problem? Thank you..
You have an adapter, activity and some sort of data source
In your adapter you attach some data to buttons to be able to tell one from another:
public class ExpAdapter extends ListAdapter {
#Override
public View getView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
/* SOME CODE HERE*/
convertViewButton.setTag(buttonId);
return convertView;
}
/* SOME CODE HERE*/
}
in your activity you mark button id as the one to be hidden:
public boolean onItemLongClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
storageOfHiddenButtonsIds.add((Long)arg1.getTag());
}};
and then ListAdapter changes like this:
#Override
public View getView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
/* SOME CODE HERE*/
convertViewButton.setTag(buttonId);
if(storageOfHiddenButtonsIds.contains(buttonId))
{
convertViewButton.setVisiblity(View.GONE);
}
return convertView;
}
and when you want your adatper to change you, don't forget to call
this.expAdapterAllTaks.notifyDataSetChanged();
Sorry for any errors in my code, but i just wanted to give you an idea.
I faced same type of problem. ListView's setOnItemClickListener not works if you add item like a button on every listView item. Solution is use onClick in the list Item layout(which you use in custom adapter file) as
<ImageButton
android:id="#+id/my_delete"
android:onClick="onDeleteButtonClickListener"
... and so on />
where onDeleteButtonClickListener is a method in the activity where you set the adapter in listview.
public void onDeleteButtonClickListener(View v) {
// your code
}
here listItem means the individual row item of a ListView
Helpful Link: Button in ListView item

How to get the spinner id or tag from getDropDownView method in android

I have several spinners that I have created a custom ArrayAdapter for so I can change the drop down menu look. I want to manipulate the view depending on what spinner the dropdown belongs to. I thought I would be able to do something like parent.getTag() but it is returning null.
The custom array adapter looks like:
class BackgroundColorAdapter extends ArrayAdapter<String> {
BackgroundColorAdapter() {
super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textColors);
}
public View getDropDownView (int position, View convertView, ViewGroup parent){
View row=super.getView(position, convertView, parent);
if(parent.getTag().equals("background"){
//Do custom stuff here
}
return(row);
}
}
and I'm setting the tag:
settingsSpinner.setTag("bg_color_spinner");
settingsSpinner.setAdapter(new BackgroundColorAdapter());
I think I'm confused how the view hierarchy works but it seems logical that the parent of the spinner drop down would be the spinner. Anyone know how I can find out what spinner the drop down belongs to in getDropDownView?
edit: made the settingsSpinner a single spinner instead of an array of spinners to make it less confusing
Eventually got this to work, here is the code for example that changes the text font for each item in the drop down.
class TextSizeAdapter extends ArrayAdapter<String> {
TextSizeAdapter() {
super(SettingsActivity.this, R.layout.settings_spinner_item, R.id.item_text, textSizes);
}
public View getDropDownView (int position, View convertView, ViewGroup parent){
View row=super.getView(position, convertView, parent);
TextView text = (TextView)row.findViewById(R.id.item_text);
text.setTextSize(TypedValue.COMPLEX_UNIT_PX,appState.FONTSIZES[position]);
RadioButton radio = (RadioButton)row.findViewById(R.id.item_radio);
if(settingsSpinners[2].getSelectedItemPosition() == position){
radio.setChecked(true);
}else{
radio.setChecked(false);
}
return(row);
}
}
I'm unfamiliar with getDropDownView(), and don't know why you use it. Documentation for getDropDownView() states the following about the parent:
parent the parent that this view will eventually be attached to
This doesn't sound like the 'parent' you are looking for...
Since the 'parent' in the getView() call is indeed a Spinner, you could use that to store an instance variable of the parent like below:
public Spinner mParent = null;
public View getView (int position, View convertView, ViewGroup parent)
{
this.mParent = parent;
return super.getView(position, convertView, parent);
}
public View getDropDownView(int position, View convertView, ViewGroup parent)
{ // Your code here -> but use 'mParent'
}
I haven't tried, but maybe it's a workaround to get what you need. Please let me know if you found the solution.

Categories

Resources