Sunday, October 23, 2011

CustomDialog in Android

MyCustomForm Class that extends dialog to use our own controls in it.

public interface ReadyListener {
  public void ready(String ads, String location,boolean gps);
 }
 private String ads;
 private String location;
 private ReadyListener readyListener;
 public MyCustomForm(Context context, String ads, String location, ReadyListener readyListener) {
  super(context);
     this.ads = ads;
  this.location = location;
  this.readyListener = readyListener;
 }
 
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(com.example.CustomDialogForm.R.layout.main);
  setTitle("Search");
  Button buttonConfrim = (Button) findViewById(R.id.btnConfirm);
  buttonConfrim.setOnClickListener(new OKListener());
  
  Button buttonCancel = (Button) findViewById(R.id.btnCancel);
  buttonCancel.setOnClickListener(new CancelListener());
  
 }
 private class OKListener implements android.view.View.OnClickListener {
  @Override
  public void onClick(View v) {
   EditText add = (EditText) findViewById(R.id.txtAdPost);
   EditText locat = (EditText) findViewById(R.id.txtLocation);
   CheckBox cbgps = (CheckBox) findViewById(R.id.chkGps);
   readyListener.ready(add.getText().toString(),locat.getText().toString(), cbgps.isChecked());
   MyCustomForm.this.dismiss();
  }
 }
 
 
 private class CancelListener implements android.view.View.OnClickListener {
  @Override
  public void onClick(View v) {
   MyCustomForm.this.dismiss();
  }
 }
Main class which extends activity to call that dialog and to get data back from the form to activity

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 MyCustomForm dialog = new MyCustomForm(this, "", "", new OnReadyListener());
 dialog.show();
}


private class OnReadyListener implements MyCustomForm.ReadyListener {
 @Override
 public void ready(String ads, String location,boolean gps) {
  //Log.d("Third", "checkbox beer is " + beer);
  //Log.d("Third", "checkbox wine is " + wine);
  
   Toast.makeText(CustomDialogForm.this, ads + "---" + location+"--" + gps , Toast.LENGTH_LONG).show();
  
  
  
 }
}
and finally layout of form 
   <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    
     
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Advertisement Post"
    />
    
    <EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text=""
    android:id="@+id/txtAdPost"
    />
    
    <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="Location"
    />
    
    <EditText  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text=""
    android:id="@+id/txtLocation"
    />
    
      <CheckBox android:id="@+id/chkGps"
            android:layout_width="fill_parent" 
            android:layout_height="wrap_content"
            android:text="User current GPS location"
            />  
       
          <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="horizontal"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
    <Button android:id="@+id/btnConfirm" android:layout_width="100px"
  android:layout_height="wrap_content" 
   android:layout_marginLeft="10px"   android:text="Confrim" />
 <Button android:layout_width="100px" android:id="@+id/btnCancel"
  android:layout_height="wrap_content"  android:text="Cancel"  />
     </LinearLayout> 
  
   
    
</LinearLayout>

Download code:

No comments:

Post a Comment