title: Single choice selection dialog in android
body: <p>I am making an android program. In my app, I am using a single choice selection AlertDialog whose items are added programmatically. What I want to do is:</p>
  
  <ul>
  <li>set a background color to the selected item next time user opens the dialog,</li>
  <li>show selected item in the middle of the dialog(this is a problem because there are about 20 items). </li>
  </ul>
  
  <p>Here is what I have:XML:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
   &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" &gt;
  
  &lt;LinearLayout
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:paddingTop="5dp" &gt;
  
  
  
      &lt;Button
          android:id="@+id/selectDateButton"
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="Please, select date" /&gt;
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p></p>
  
  <p>JAVA:</p>
  
  <pre><code>public class ExperimentListView extends Activity {
  
  private static final DateFormat DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd");
  private static Calendar calendar = Calendar.getInstance();
  private static Button selectDateButton;
  private static String[] items;
  private static int selectedDatePosition = 0;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.list_view_ex);
  
      selectDateButton = (Button)findViewById(R.id.selectDateButton);
      selectDateButton.setOnClickListener(new OnClickListener() {
          @Override
          public void onClick(View v) {
              items = new String[20];
              for (int i = 0; i &lt; 20; i++) {
                  items[i] = DATE_FORMAT.format(calendar.getTime());
                  calendar.add(Calendar.DATE, 1);
              }
              showListView();
          }
      });
  
  }
  
  private void showListView() {
      AlertDialog.Builder builder = new AlertDialog.Builder(ExperimentListView.this);
      builder.setTitle("Select date");
      builder.setItems(items, new DialogInterface.OnClickListener() {
          @Override
          public void onClick(DialogInterface dialog, int which) {
              selectedDatePosition = which;
              selectDateButton.setText(items[selectedDatePosition]);
          }
      });
      AlertDialog alertDialog = builder.create();
      alertDialog.getListView().setSelection(selectedDatePosition);
      alertDialog.show();
   }
  }
  </code></pre>
  
  <p>I haven't been able to find a solution to this so far and I would be grateful if someone can help.
  Thanks in advance.</p>