Dharni
Dharni

Reputation: 149

How to create a spinner with image buttons?

I wanted to create a drop down menu on clicking an image button.Do i need to create a spinner for this?Could you please tell me how to create a drop down menu using spinner?

Upvotes: 0

Views: 4934

Answers (4)

Jigar Pandya
Jigar Pandya

Reputation: 2147

USE BELOW GIVEN CODE IN YOUR PROJECT :

You need to take one button and set any image as its background.Then on click of the button call Spinner.performClick() to open the spinner.

Below is code to implement the same thing. In xml file:

 <Button
    android:id="@+id/font"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="50dp"
    android:layout_weight="0.5"
    android:background="@drawable/textsel" /> 

 <Spinner
    android:id="@+id/spin"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center_vertical"
    android:layout_marginLeft="10dp"
    android:layout_weight="0.5"
    android:dropDownHorizontalOffset="0dp"
    android:dropDownVerticalOffset="20dp"
    android:dropDownWidth="500dp"
    android:paddingTop="2sp"
    android:spinnerMode="dropdown" >
</Spinner>

In Java class:

  Spinner spin = (Spinner) findViewById(R.id.spin);

  Button typetext = (Button) findViewById(R.id.font);      


  typetext.setOnClickListener(new OnClickListener() {

    @Override
    public void onClick(View v) {

        spin.performClick();

    }
});

You can ask if you have any further queries!

Upvotes: 1

droid
droid

Reputation: 414

Better use list view , making it look like a drop down.

Upvotes: 0

jyomin
jyomin

Reputation: 1967

Add this attribute in the button in the xml file

 android:background="@android:drawable/btn_dropdown"

and you get a drop down image in the button and in the click of the button you can perform your operation

Upvotes: 2

Sunny
Sunny

Reputation: 14828

Here is an example of creating custom spinner. Also you can open an AlertDialog with with single choice selection. It is similar to drop down menu here is an example.

Upvotes: 0

Related Questions