-1

I am new to android and searching for how to add a hint (which can't be selected ) in a customized spinner having an image and a text.Though I found it for simple spinner but I am unable to implement it with custom spinner. My code :

    package com.example.hp.login_stratup_labs;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.SpinnerAdapter;
import android.widget.TextView;

public class RegistrationActivity extends Activity {

    String[] codes = {"Select Country", "India  -  +91", "United States of America  -  +1"};
    // Declaring the Integer Array with resourse Id's of Images for the Spinners
    int[] flags = {0, R.drawable.f094, R.drawable.f222};
    Spinner country;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_registration);
        Spinner countrycode=(Spinner)findViewById(R.id.registerCountryCode);
        countrycode.setAdapter(new MyAdapter(RegistrationActivity.this, R.layout.custom, codes));

    }

    public class MyAdapter extends ArrayAdapter<String>{

        public MyAdapter(Context context, int textViewResourceId,   String[] objects) {
            super(context, textViewResourceId, objects);
        }

        @Override
        public View getDropDownView(int position, View convertView,ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            return getCustomView(position, convertView, parent);
        }

        public View getCustomView(int position, View convertView, ViewGroup parent) {

            LayoutInflater inflater=getLayoutInflater();
            View row=inflater.inflate(R.layout.custom, parent, false);
            TextView label=(TextView)row.findViewById(R.id.txt);
            label.setText(codes[position]);

            ImageView flag_image=(ImageView)row.findViewById(R.id.img);
            flag_image.setImageResource(flags[position]);

            return row;
        }
    }
}
  • visit this http://stackoverflow.com/questions/6602339/android-spinner-hint – H Raval Dec 22 '15 at 08:00
  • @HRaval this is not working. It is now showing only the hint. Also I want to add the hint to the left of the spinner . But it is aligned exactly same as the rest of the items which are in the spinner. – Swapnil Garg Dec 22 '15 at 08:22
  • @Swapnil Garg: U can't add hint to spinner instead u can first option as [select] and a label with appropriate Name. eg: City -> [Select] – kevz Dec 22 '15 at 08:37
  • @SwapnilGarg well its working...i have tried dat...this is the only way you can add hint to the spinner – H Raval Dec 22 '15 at 08:55
  • @HRaval yes we can add the hint without declaring it in the array of items to be put in the spinner.I have done that for simple spinner having only text. But this time I have to implement it for custom spinner. – Swapnil Garg Dec 22 '15 at 09:43
  • Pls checkout http://stackoverflow.com/q/6602339/2414073 – Abdul Rahman K Dec 22 '15 at 10:05

1 Answers1

-1

you can do this in this way

in main activity

String[] codes = {"India  -  +91", "United States of America  -  +1","Select Country"};

MyAdapter adapter = new MyAdapter(RegistrationActivity.this, R.layout.custom, codes);
Spinner countrycode=(Spinner)findViewById(R.id.registerCountryCode);
        countrycode.setAdapter(adapter);

countrycode.setSelection(adapter.getCount());

add this method your adapter to

@Override
    public int getCount() {
        return codes.length()-1;
    }
H Raval
  • 1,903
  • 17
  • 39
  • This is only showing the hint. Also this is not the solution of my question as I dont want to get the hint inserted in the array of the items. – Swapnil Garg Dec 22 '15 at 11:01