Reputation: 149
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
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
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