Try this way,hope this will help you to solve you problem.
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<Button
android:id="@+id/btnSelectGender"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Gender"/>
</LinearLayout>
MyActivity.java
public class MyActivity extends Activity {
private Button btnSelectGender;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnSelectGender = (Button) findViewById(R.id.btnSelectGender);
btnSelectGender.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
showDialog(MyActivity.this, "Select Gender", getResources().getStringArray(R.array.Gender),new onGenderSelected() {
@Override
public void genderSelection(int position) {
btnSelectGender.setText(getResources().getStringArray(R.array.Gender)[position]);
Toast.makeText(MyActivity.this,getResources().getStringArray(R.array.Gender)[position]+" >>> "+getResources().getStringArray(R.array.Gender_id)[position],Toast.LENGTH_SHORT).show();
System.out.print("Gender Title : " +getResources().getStringArray(R.array.Gender)[position]);
System.out.print("Gender Value : " +getResources().getStringArray(R.array.Gender_id)[position]);
}
});
}
});
}
public static void showDialog(Context c, String title, final String[] array,final onGenderSelected target) {
AlertDialog.Builder builder = new AlertDialog.Builder(c);
builder.setTitle(title);
builder.setItems(array, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
target.genderSelection(i);
dialogInterface.dismiss();
}
});
builder.show();
}
public interface onGenderSelected{
public void genderSelection(int position);
}
}
solved get the selected value position