11. ALERTDIALOGS AND BACK BUTTON
11.1 DIALOGS
A dialog is usually a small window that appears in front of
the current Activity. The underlying Activity loses focus and the
dialog accepts all user interaction.
Dialogs are normally used for notifications that should interupt the user and
to perform short tasks that directly relate to the application in progress
(such as a progress bar or a login prompt).
The Dialog class is the base class for creating dialogs. However, you typically
should not instantiate a Dialog directly. Instead, you should use one of the following subclasses:
A dialog that can manage zero, one, two, or
three buttons, and/or a list of selectable items that can include checkboxes or
radio buttons. The AlertDialog is capable of constructing most dialog user
interfaces and is the suggested dialog type.
A dialog that displays a progress wheel or
progress bar. Because it's an extension of the AlertDialog, it also supports
buttons.
A dialog that allows the user to select a
date.
A dialog that allows the user to select a
time
In this session we
will be dealing with alertdialogue box only and is the most commonly used among
the four types.
11.2 ALERTDIALOG
It’s very familiar to see dialogue box in our android phones while
opening certain apps and all. Now we are going to discuss about dialog box,
along with it we going to give/edit the function of back button (in phone).
Before jumping into program, You will be very familiar with back button
on your phone we can add function to it by writing code. In our progam we give
a name in edittext in first activity then got to second activity there on
clicking the back button our alertdialog pops up.
An example is shown below
package a.b.c;
import android.app.Activity;
import android.content.Intent;
import
android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class
BackbttnActivity extends Activity {
TextView t1;
EditText e1;
Button b1,b2;
String s1,s2;
SQLiteDatabase mydb1=null;
/** Called when the activity is first created. */
@Override
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
t1=(TextView)findViewById(R.id.tv1);
e1=(EditText)findViewById(R.id.ed1);
mydb1 =
BackbttnActivity.this.openOrCreateDatabase("Database1", MODE_PRIVATE, null);
mydb1.execSQL("CREATE TABLE IF NOT EXISTS Table1 (usrnme varchar(10));");
}
public void
Submit(View v)
{
s1= e1.getText().toString();
mydb1.execSQL("INSERT INTO Table1
VALUES('"+s1+"') ");
Intent i = new Intent(BackbttnActivity.this,second.class);
Bundle bundle = new Bundle();
bundle.putString("KEY", s1);
i.putExtras(bundle);
startActivity(i);
}
public void
Cancel(View v )
{
e1.setText(" ");
}
}
|
second.java
package a.b.c;
import android.app.Activity;
import android.app.AlertDialog;
import
android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.widget.TextView;
import android.widget.Toast;
public class second
extends Activity{
TextView t1;
public void
onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Intent i = getIntent();
Bundle bundle2 =
i.getExtras();
String s =
bundle2.getString("KEY");
t1=(TextView)findViewById(R.id.tv3);
t1.setText("Welcome "+s);
}
//back button control
public boolean
onKeyDown(int keyCode, KeyEvent event) {
if (keyCode ==
KeyEvent.KEYCODE_BACK) {
AlertDialog
alertbox = new AlertDialog.Builder(second.this).create();
alertbox.setMessage("Do you really want to exit");
alertbox.setTitle("Warning");
alertbox.setButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface arg0, int arg1) {
Intent
yes = new Intent(second.this,BackbttnActivity.class);
startActivity(yes);
Toast.makeText(getApplicationContext(),
"You clicked Yes", Toast.LENGTH_LONG).show();
}
});
alertbox.setButton2("No", new DialogInterface.OnClickListener() {
@Override
public void
onClick(DialogInterface arg0, int arg1) {
Toast.makeText(getApplicationContext(),
"You clicked No", Toast.LENGTH_LONG).show();
}
});
alertbox.show();
return true;
}
return super.onKeyDown(keyCode,
event);
}
}
|
No comments:
Post a Comment