1

I am new to android,I know access of spinner but I want to add default text in spinner and want set lay out like below image,can any one help me with this,

enter image description here

    final String[] items = new String[] {"One", "Two", "Three"};
    final ArrayAdapter<String> adapter123 = new ArrayAdapter<String>(this,
            android.R.layout.simple_spinner_dropdown_item, items);

    sp3.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View w) {
              new AlertDialog.Builder(RegistrationForm.this)
              .setTitle("the prompt")
              .setAdapter(adapter123, new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {

                  dialog.dismiss();
                }
              }).create().show();
            }
    });
  • can you put your code? – Akash Moradiya Nov 29 '14 at 05:38
  • Just add your default text at 0 position of spinner data. – Haresh Chhelana Nov 29 '14 at 05:39
  • @HareshChhelana how to do it can you give code? –  Nov 29 '14 at 05:41
  • Just add your default value at first item to each String resource array or post string resource array. – Haresh Chhelana Nov 29 '14 at 05:59
  • @Johnson u can take a look at this [How to make an Android Spinner with initial text “Select One”](http://stackoverflow.com/questions/867518/how-to-make-an-android-spinner-with-initial-text-select-one) – Kaushik Nov 29 '14 at 06:10
  • @kaushik i followed your link it help me somewhat,but can you tell me that,I used textview and set the text in my xml as "day",but I want to change the value after selection from alert dailog,check HRJ's answer i followed his answer –  Nov 29 '14 at 06:35

5 Answers5

3

My suggestion for your case is use Button initially set text and set gravity (not layout_gravity) to left|center_vertical instead of opening an AlertDialog open a PopupWindow set that Button as anchor of that PopUpWindow. In that PopUpWindow place a ListView and in OnItemClick change text with selected value in that Button using setText(java.lang.CharSequence)

code snippet

XML for that Button

<Button
    android:id="@+id/propertyBtn"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginRight="@dimen/dp_4"
    android:background="@drawable/property_btn_large"
    android:ellipsize="marquee"
    android:gravity="left|center_vertical"
    android:marqueeRepeatLimit="marquee_forever"
    android:minHeight="0dp"
    android:minWidth="0dp"
    android:onClick="showPropertyPopUp"
    android:paddingLeft="42dp"
    android:paddingRight="22dp"
    android:shadowColor="@android:color/white"
    android:shadowDx="1"
    android:shadowDy="1"
    android:shadowRadius="1"
    android:text="@string/select_property" />

Java code for opening PopUpWindow in that Button click

//don't forget to initialize that button in onCreate(...)
public void showPropertyPopUp(View v) {
        propertyList = dbHelper.getAllProperties();
        dbHelper.closeDB();

        if(propertyList != null && propertyList.size() > 0) {
            LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            View popUpView = inflater.inflate(R.layout.pop_up, null, false);
            Bitmap bitmap = BitmapFactory.decodeResource(getResources(),
                    R.drawable.select_dropdown);
            popupWindowProperty = new PopupWindow(popUpView, propertyBtn.getWidth(),
                    300, true);         
            popupWindowProperty.setContentView(popUpView);
            popupWindowProperty.setBackgroundDrawable(new BitmapDrawable(getResources(),
                    bitmap));
            popupWindowProperty.setOutsideTouchable(true);
            popupWindowProperty.setFocusable(true);
            popupWindowProperty.showAsDropDown(propertyBtn, 0, 0);
            ListView dropdownListView = (ListView) popUpView.
                    findViewById(R.id.dropdownListView);
            PropertyDropdownAdapter adapter = new PropertyDropdownAdapter(
                    AddIncomeActivity.this, 
                    R.layout.row_pop_up_list, propertyList);
            dropdownListView.setAdapter(adapter);
            dropdownListView.setOnItemClickListener(this);
        }   
    }

code for setting text on that Button in OnItemClick

PropertyInfo addPropertyInfo = propertyList.get(position);
String propertyName = addPropertyInfo.getPropertyName();
propertyBtn.setText(propertyName);
popupWindowProperty.dismiss();

pop_up layout

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <ListView
        android:id="@+id/dropdownListView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:cacheColorHint="@null"
        android:fadeScrollbars="false" >
    </ListView>

</LinearLayout>

Screenshot on clicking on that Button

enter image description here

Screenshot after item click of ListView enter image description here

Kaushik
  • 6,150
  • 5
  • 39
  • 54
0

Add one more element in array which you are passing in spinner and if you want to add validation you can check it by runtime using -

if (spnType.getSelectedItemPosition() == 0) {
            View view = spnType.getSelectedView();
              SpinnerView adapter = (SpinnerView) spnType.getAdapter();
            adapter.setError(view, getString(R.string.err_leadtype));
            return false;
        } else {
            strType = arllistType.get(spnType.getSelectedItemPosition()).get(
                    WebServiceResponseParameter.LIST_VALUE);
        }
coder
  • 13,002
  • 31
  • 112
  • 214
0

You can not do that, for that you have to create custom spinner or you have to show alertdialog as a action when text view is clicked.

Edited :

Create a array

String a [] = new String [4] ;
a[0] = "ram";
a[1] = "shyam";
a[2] = "mohan";
a[3] = "krishna";

use this array as a source of listview data, now as a action listview will be displayed and set a listener for listview which will provide you a position of clicked item in the listview,

use that position for array[position], lets position = 2, then clicked item text will be mohan set this text as a item of textview .

Edited 2:

Create custom Dialog with listview inside it. set onItemClickListener in listview.

onCreate  
{
     dayTextView.setText("day");
}

onItemClickListener ()
{
     dayTextView.setText("Sunday");
}

Edited 3 :

Follow this tutorial for custom dilaog : http://rajeshvijayakumar.blogspot.in/2013/04/alert-dialog-dialog-with-item-list.html

Tushar Pandey
  • 4,557
  • 4
  • 33
  • 50
  • create a textview and set a action in it, at action generate alert dialog with listview inside it, wichever listitem is selected add that item as your textview text, thats it thanks. – Tushar Pandey Nov 29 '14 at 05:55
  • ok as u say by default it will show day and when I select 13 how can I change text? –  Nov 29 '14 at 05:57
  • i am using textview,and after click on textview alertdailog will appear,and i want to set selected text in my textview,but by default i want to set "day" text,how can i do this? i edited my question –  Nov 29 '14 at 06:47
0

As Nitish explained, You need to add default value on top of your array R.array.day_birthdate. Suppose you your array is day_birthdate then add day in top where you define

<string-array name="day_birthdate">
        <item name="0">day</item>
        <item name="1">1</item>
        ...
</string-array>

Add validaton on Spinner, if option first is selected then,

if (mSpinnerBirthDate.getSelectedItemPosition() == 0) { //where mSpinnerBirthDate is Spinner for Birthdate
//show invalid selection message       
}else{
//get selected value from spinner
}
Vikalp Patel
  • 10,669
  • 6
  • 61
  • 96
  • if user will click on day text on spinner,how can i prevent click event? –  Nov 29 '14 at 05:59
  • @Johnson : AFAIK, you cant prevent from clicking any specific position in Spinner Adapter. Although you can set `onItemSelectedListener` on Spinner and Validate the position. – Vikalp Patel Nov 29 '14 at 06:03
0

Add one more element in array which you are passing in spinner and then write this code in java file.

String array[]=getResources().getStringArray(R.array.name_Of_array);

ArrayAdapter<String> ad=new ArrayAdapter<String>(this,layoutid, array);
spinner.setAdapter(ad);     
Deepak Singh
  • 144
  • 6