How to Add a Hint in Spinner in Xml

Is it possibile to set hint Spinner in Android

Here's a solution which is probably a bit simpler than Ravi Vyas code (thanks for the inspiration!):

ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_spinner_dropdown_item) {

@Override
public View getView(int position, View convertView, ViewGroup parent) {

View v = super.getView(position, convertView, parent);
if (position == getCount()) {
((TextView)v.findViewById(android.R.id.text1)).setText("");
((TextView)v.findViewById(android.R.id.text1)).setHint(getItem(getCount())); //"Hint to be displayed"
}

return v;
}

@Override
public int getCount() {
return super.getCount()-1; // you dont display last item. It is used as hint.
}

};

adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
adapter.add("Item 1");
adapter.add("Item 2");
adapter.add("Hint to be displayed");

spinner.setAdapter(adapter);
spinner.setSelection(adapter.getCount()); //display hint

How to add Hint in Spinner widget populated with data from FireBase Database?

Try this

myRef.child("Stations").addListenerForSingleValueEvent(new 
ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
final List<String> stationname = new ArrayList<String>();
stationname.add("Select Station");
for (DataSnapshot stationSnapshot : dataSnapshot.getChildren()) {

String stationName = stationSnapshot.child("Name").getValue(String.class);
stationname.add(stationName);
}

Spinner sp2= findViewById(R.id.destination);

ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(FairPage.this, android.R.layout.simple_spinner_item, stationname);
arrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

sp2.setAdapter(arrayAdapter);

and also check when spinner item selected

if (stationname.get(postion).equals("Select Station"){
//Toast please select station
}

How to make an Android Spinner with initial text Select One?

Here's a general solution that overrides the Spinner view. It overrides setAdapter() to set the initial position to -1, and proxies the supplied SpinnerAdapter to display the prompt string for position less than 0.

This has been tested on Android 1.5 through 4.2, but buyer beware! Because this solution relies on reflection to call the private AdapterView.setNextSelectedPositionInt() and AdapterView.setSelectedPositionInt(), it's not guaranteed to work in future OS updates. It seems likely that it will, but it is by no means guaranteed.

Normally I wouldn't condone something like this, but this question has been asked enough times and it seems like a reasonable enough request that I thought I would post my solution.

/**
* A modified Spinner that doesn't automatically select the first entry in the list.
*
* Shows the prompt if nothing is selected.
*
* Limitations: does not display prompt if the entry list is empty.
*/
public class NoDefaultSpinner extends Spinner {

public NoDefaultSpinner(Context context) {
super(context);
}

public NoDefaultSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
}

public NoDefaultSpinner(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}

@Override
public void setAdapter(SpinnerAdapter orig ) {
final SpinnerAdapter adapter = newProxy(orig);

super.setAdapter(adapter);

try {
final Method m = AdapterView.class.getDeclaredMethod(
"setNextSelectedPositionInt",int.class);
m.setAccessible(true);
m.invoke(this,-1);

final Method n = AdapterView.class.getDeclaredMethod(
"setSelectedPositionInt",int.class);
n.setAccessible(true);
n.invoke(this,-1);
}
catch( Exception e ) {
throw new RuntimeException(e);
}
}

protected SpinnerAdapter newProxy(SpinnerAdapter obj) {
return (SpinnerAdapter) java.lang.reflect.Proxy.newProxyInstance(
obj.getClass().getClassLoader(),
new Class[]{SpinnerAdapter.class},
new SpinnerAdapterProxy(obj));
}

/**
* Intercepts getView() to display the prompt if position < 0
*/
protected class SpinnerAdapterProxy implements InvocationHandler {

protected SpinnerAdapter obj;
protected Method getView;

protected SpinnerAdapterProxy(SpinnerAdapter obj) {
this.obj = obj;
try {
this.getView = SpinnerAdapter.class.getMethod(
"getView",int.class,View.class,ViewGroup.class);
}
catch( Exception e ) {
throw new RuntimeException(e);
}
}

public Object invoke(Object proxy, Method m, Object[] args) throws Throwable {
try {
return m.equals(getView) &&
(Integer)(args[0])<0 ?
getView((Integer)args[0],(View)args[1],(ViewGroup)args[2]) :
m.invoke(obj, args);
}
catch (InvocationTargetException e) {
throw e.getTargetException();
}
catch (Exception e) {
throw new RuntimeException(e);
}
}

protected View getView(int position, View convertView, ViewGroup parent)
throws IllegalAccessException {

if( position<0 ) {
final TextView v =
(TextView) ((LayoutInflater)getContext().getSystemService(
Context.LAYOUT_INFLATER_SERVICE)).inflate(
android.R.layout.simple_spinner_item,parent,false);
v.setText(getPrompt());
return v;
}
return obj.getView(position,convertView,parent);
}
}
}

How to set hint text for spinner?

There are lot of way :

1.) Android Hint Spinner

Use this dependency in build.gradle file.

dependencies {
compile 'me.srodrigo:androidhintspinner:1.0.0'
}

Output :

Sample Image

for more detail visit this : https://github.com/srodrigo/Android-Hint-Spinner

2.) second Way

visit here :
https://github.com/ravivyas84/AndroidSpinnerHint

Unable to add hint in android spinner

probably what you are doing is, you are clicking on first item of you spinner, where the position = 0 and doing this position - 1 in your setText which makes the index -1 that is actually not exists and crashes.
Secondly you are doing this

 userList.add("Select a username");// i add this as a hint 

in your loop, which is adding new item on each iteration.
what you need to do is

once you have complete list of your data,
just add hint on its 0 index like this

userList.add(0, "Select a username");

and in your onClick, check weather position > 0 then do your work otherwise ignore as user clicked on hint.

how to add hint of AppCompatSpinner

May be you can use an edit text and set a hint to it..when users select an item display it in the edit text ..just keep the spinner below the edit text in xml file ...and fire onclick event of spinner when clicked on edit text(basically you just need to hide/show the dropdown)



Related Topics



Leave a reply



Submit