Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on May 2nd, 2012  |  syntax: None  |  size: 2.13 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. Back button in dialog
  2. package my.dlog;
  3.  
  4. import android.app.Activity;
  5. import android.app.Dialog;
  6. import android.content.Intent;
  7. import android.os.Bundle;
  8. import android.view.View;
  9. import android.widget.Button;
  10. import android.view.View.OnClickListener;
  11.  
  12. public class DlogActivity extends Activity {
  13.   /** Called when the activity is first created. */
  14.   Dialog dialog;
  15.   @Override
  16.   public void onCreate(Bundle savedInstanceState) {
  17.     super.onCreate(savedInstanceState);
  18.     setContentView(R.layout.main);
  19.     dialog = new Dialog(this);
  20.     dialog.setContentView(R.layout.main2);
  21.     dialog.setTitle("This is my custom dialog box");
  22.     dialog.setCancelable(true);
  23.     Button b=(Button)findViewById(R.id.button1);
  24.  
  25.     b.setOnClickListener(new OnClickListener() {
  26.       public void onBackPressed() {
  27.         Intent intent = new Intent(DlogActivity.this, DlogActivity.class);
  28.         startActivity(intent);
  29.         finish();
  30.       }
  31.  
  32.       public void onClick(View v) {
  33.         dialog.show();
  34.       }
  35.     });
  36.   }
  37. }
  38.        
  39. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  40.     android:layout_width="fill_parent"
  41.     android:layout_height="wrap_content"
  42.     android:baselineAligned="false"
  43.     android:minHeight="400dp"
  44.     android:minWidth="300dp" android:background="@drawable/mint1">
  45.  
  46.   <Button
  47.       android:id="@+id/button1"
  48.       android:layout_width="wrap_content"
  49.       android:layout_height="wrap_content"
  50.       android:text="Button" />
  51.  
  52.   <ImageView
  53.       android:layout_width="236dp"
  54.       android:layout_height="220dp"
  55.       android:layout_marginRight="100dp" android:background="@drawable/carsee"/>
  56.  
  57.   <RelativeLayout
  58.       android:id="@+id/relativeLayout1"
  59.       android:layout_width="fill_parent"
  60.       android:layout_height="fill_parent" >
  61.   </RelativeLayout>
  62. </LinearLayout>
  63.        
  64. b.setOnClickListener(new OnClickListener() {
  65.     public void onBackPressed() {
  66.        dialog.cancel();
  67. // Simply Dismiss the dialog to make it close and return to back..
  68. /*What you are using is not a valid construct */  
  69. }
  70.        
  71. public void onBackPressed() {
  72. Intent intent = new Intent(DlogActivity.this, DlogActivity.class);
  73. startActivity(intent);
  74. finish();
  75. }