0

I need to create a drop down which has only three values. I have done this using spinner. But it looks like This

But I am trying to create like This

So while click on this it needs to show the values like normal spinner.

I tried with style and setting selector as background but nothing worked and i thought of custom spinner but did not get anything useful.

Update

activity_main.xml

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

<Spinner
    android:id="@+id/spinner1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:entries="@array/spinner_consigntype"
    android:prompt="@string/spinner_consign" />
</LinearLayout>

Activity_Test.Java

package com.mkyong.android;

import android.app.Activity;
import android.os.Bundle;

public class Activity_Test extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_activity__test);
    addListenerOnSpinnerItemSelection();
}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity__test, menu);
    return true;
}


public void addListenerOnSpinnerItemSelection(){

    Spinner spinner1 = (Spinner) findViewById(R.id.spinner1);
    spinner1.setOnItemSelectedListener(new CustomOnItemSelectedListener());
}
}

Listener

public class CustomOnItemSelectedListener implements OnItemSelectedListener {

public void onItemSelected(AdapterView<?> parent, View view, int pos,
        long id) {
    Toast.makeText(parent.getContext(), 
            "OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
            Toast.LENGTH_SHORT).show();
}

@Override
public void onNothingSelected(AdapterView<?> arg0) {
    // TODO Auto-generated method stub

}

}

In my case I am getting the output as like the first image. But with the downloaded code it is showing the output as the second image.

Community
  • 1
  • 1
Giri
  • 931
  • 11
  • 31
  • 51
  • in ur activity u have to add some item.(i.e if u want to see some value)...then implement listener in ur spiiner. – Sam Jun 11 '13 at 12:02
  • Thanks, Now i have added the listener, For values I am assigning in xml itself. Now onclick of the item it is displaying the selected values as toast. But the UI view is still looking as first image for spinner. – Giri Jun 11 '13 at 12:14
  • For the record, what you want is the **Holo** theme, which is the default theme starting with Android 3.0 (Honeycomb). It has nothing to do with ASP.Net – nicopico Jun 11 '13 at 12:40
  • I have tried setting the theme like this android:theme="@android:style/Theme.Holo". But after this background is become black and spinner is still look like the first image that i have posted. – Giri Jun 11 '13 at 13:01

1 Answers1

1

make a selector_spinner.xml :

item android:state_pressed="true" android:drawable="@drawable/spinner_normal"
item android:state_focused="true" android:drawable="@drawable/spinner_normal"
item android:drawable="@drawable/spinner_normal"

use this selector in your layout : main.xml

          <Spinner
            android:id="@+id/spinner_selection"
            android:layout_width="0px"
            android:layout_height="wrap_content"
            android:layout_weight="1"
           **android:background="@drawable/selector_spinner"**
            android:entries="@array/contract_rate_type" />

Then in class file,

   Spinner sp = (Spinner).findViewById(R.id.spinner_selection);
Prachi
  • 3,584
  • 2
  • 24
  • 39
  • Thanks, I have added the 9patch image for pressed and focused, which look like dropdown. – Giri Jun 20 '13 at 06:37