0

I have a task to implement the shown Spinner, where when you select country with its full Name from the dropdown its country code(GB, AU...) has to be shown on the selected Spinner item. I have no clue how to implement it like this. Just some hints of doing it will be great. Regards to all.

enter image description here

  • 1
    You can't make this using `Spinner` however you can use some thing like [pop up menu](http://stackoverflow.com/questions/21329132/android-custom-dropdown-popup-menu) OR take a look at this [library](https://github.com/lorensiuswlt/NewQuickAction3D) – Sharp Edge Jan 16 '15 at 09:56
  • @Sharp edge , I will make it with pop up menu. Thanks for the hint. – Vladimir Abazarov Jan 16 '15 at 10:02
  • I assume you want exactly the same *pointy arrow* menu, for that you have to use some custom library which I mentioned in my previous comment, but for a simple item list a `Spinner` will suffice. – Sharp Edge Jan 16 '15 at 10:08
  • @Sharpedge , I will use PopupWindow with LinearLayout on click of a TextView in my Activity. Thanks for the help. – Vladimir Abazarov Jan 16 '15 at 10:13

3 Answers3

1

You can't make the exact same pointy pop up menu using Spinner ! You have to either use some library like this

OR

You can extend PopupWindow class and mess around with it.

Sharp Edge
  • 3,895
  • 2
  • 23
  • 38
1

To do this you need to have a custom spinner adapter and a custom class to hold these two variables.

Create a class which holds the name and country code for each of the items you will show.

Something like this:

public class Country {
    public name;
    public code;
}

Use an adapter of your choice for the Spinner. I would recommend BaseAdapter .

Override the getView and getDropdownView on the Adapter. All adapters have these methods.

The getView method will determine what is shown after the spinner is closed, so here you'll set the text of your TextView to the country code of the selected item.

On the getDropDownView you'll set the text of each of the options based on their positions to the name of the country you'll be showing.

Below you can find a minimal Adapter that will do what I have described above.

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import java.util.ArrayList;
import java.util.List;

public class CountryAdapter extends BaseAdapter {

private List<Country> countryList;

public CountryAdapter() {
    //Initialize the list however you need to.
    countryList = new ArrayList<>();
}

@Override
public int getCount() {
    return countryList.size();
}

@Override
public Object getItem(int position) {
    return countryList.get(position);
}

@Override
public long getItemId(int position) {
    return 0;
}

@Override
public boolean hasStableIds() {
    return false;
}

@Override
public View getView(int position, View view, ViewGroup parent) {
    Context context = parent.getContext();
    if (view == null || !view.getTag().toString().equals("NON_DROPDOWN")) {
        view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.spinner_item, parent, false);
        view.setTag("NON_DROPDOWN");
    }

    String countryCode = countryList.get(position).code;
    //Here you can set the label to the country code.

    return view;
}

@Override
public View getDropDownView(int position, View view, ViewGroup parent) {
    Context context = parent.getContext();
    if (view == null || !view.getTag().toString().equals("DROPDOWN")) {
        view = ((LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.spinner_item_dropdown,
                parent, false);
        view.setTag("DROPDOWN");
    }

    String countryName = countryList.get(position).name;
    //Here you set the text of your label to the name of the country.

    return view;
}

private class Country {

    public String name;
    public String code;
}

}

Gent Ahmeti
  • 1,521
  • 13
  • 16
0

In string.xml first declare all the country list in array

<string-array name="countries">
    <item>India</item>
    <item>Australia</item>
    <item>England</item>
    <item>Pakistan</item>
</string-array>

Then declare that spinner in the layout

<Spinner
    android:id="@+id/spinnerPlayerType"
    android:layout_width="0dp"
    android:layout_height="wrap_content"
    android:entries="@array/countries"        
    android:focusable="false" />
Murtaza Khursheed Hussain
  • 14,714
  • 7
  • 52
  • 78
Akash
  • 621
  • 3
  • 17