Showing posts with label AlertDialog. Show all posts
Showing posts with label AlertDialog. Show all posts

Thursday, November 1, 2012

How to show dialog yes/No before leaving the app via Back button ?



private boolean isLastActivity() {
   final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
    final List<RunningTaskInfo> tasksInfo = am.getRunningTasks(1024);

    final String ourAppPackageName = getPackageName();
    RunningTaskInfo taskInfo;
    final int size = tasksInfo.size();
    for (int i = 0; i < size; i++) {
        taskInfo = tasksInfo.get(i);
        if (ourAppPackageName.equals(taskInfo.baseActivity.getPackageName())) {
            return taskInfo.numActivities == 1;
        }
    }

    return false;
}

You wil also require below permission in manifest....

<uses-permission android:name="android.permission.GET_TASKS" />

Thus in your Activty you can just use the following:

public void onBackPressed() {
    if (isLastActivity()) {
         showDialog(DIALOG_EXIT_CONFIRMATION_ID);
    } else {
         super.onBackPressed(); // this will actually finish the Activity
    }
}

Wednesday, March 28, 2012

Avoid dialog leak issue in Android?



Dialog leak issue usually occur when the dialog is shown and later the activity destroy without dismissing it. Most common leaks happen while dialog is showing then the configuration or orientation change occur. So, it  causes the leak and sometime it may also cause the App to crash or go into an invalid state. You may notice the leak only by looking at the logcat. This mean that you do not dismiss your dialog correctly before your activity get destroy.
When the orientation change, the activity will be destroyed and re-created again. Before the activity is destroyed due to orientation change, it will save the current state first. So, when the activity is re-created it tries to restore instance state (onRestoreInstanceState()) from the previous activity. Notice that when the activity is re-created, it is a new activity and a new view. The exception may occur, if it try to update the old view (possibly, the dialog that did not dismiss) in the new activity, because the old view will have an invalid Context.
So, if your application have any pop up dialog, make sure that you dismiss it before your application get destroy. Here is what you can do.
1. Dismiss all dialogs completely if exist in onPause() or immediately after it is no longer need.
2. Since most common leaks happen while dialog is showing then the configuration or orientation change occur, another way to avoid dialog leak is NOT to destroy the activity when orientation change. If the activity is not destroyed, then there is no dialog leak and there is no re-creation of new activity, thus no issue neither. This can be achieved by using the attribute android:configChanges=”orientation|keyboardHidden” in the manifest file and using the Override method onConfigurationChanged() inside your activity. By using the attribute android:configChanges you will tell Android that you will handle the configuration change yourself so, don’t destroy the activity. And you can handle the configuration change in the Override method, or you may leave it blank if you prefer not to do anything.

Friday, March 23, 2012

How to Hide showed Toast Messages in Android?

In my App, there is a list, when a user clicks on an button, a toast message is being displayed, 10 items - 10 toast messages, so if the user clicks 10 times, then presses the button... he/she has to wait for some seconds, until he's able to read the menu option text.

I have found the below solution to hide the shows toast message.

public class SampleToastActivity extends Activity {
Toast myToast;
static int i=0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button b1 = (Button) findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
i++;
if(myToast!=null){
      myToast.cancel();
      myToast.setText("Count"+i);
      }else{
          myToast = Toast.makeText(SampleToastActivity.this, "Sample"+i, 10);
         }
      myToast.show();
   }
    });
  }
}

Tuesday, February 21, 2012

How to Create Custom Dialog in Android?


Step 1: Design the Custom Dialog Layout

First you need to design the content of your custom dialog in a layout resource file. In this case, our custom layout will be a password confirmation dialog, which means we will need twoEditText controls for inputting the password. We will also want some other text controls for labels and the like.
Create a layout resource called /res/layout/password_dialog.xml. The contents of this resource file are shown below:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
    android:layout_height="match_parent"
    android:layout_width="match_parent"
    android:orientation="vertical"
    android:id="@+id/root"
    xmlns:android="http://schemas.android.com/apk/res/android">
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/TextView_Pwd1"
        android:text="@string/settings_password"
        android:textStyle="bold" />
    <EditText
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/EditText_Pwd1"
        android:inputType="textPassword" />
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        android:id="@+id/TextView_Pwd2"
        android:text="@string/settings_password2"
        android:textStyle="bold" />
    <EditText
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/EditText_Pwd2"
        android:inputType="textPassword" />
    <TextView
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:id="@+id/TextView_PwdProblem"
        android:textStyle="bold"
        android:gravity="center" />
</LinearLayout>




This simple layout file relies upon a few other string resources you’ll need to define (labels, see the full source code for details if you need help defining string resources) as well as theEditText input type attribute called textPassword, which masks the password as it is typed into the control. This figure shows what the layout design looks like:


Step 2: Define a New Password Dialog within your Activity Class

Now let’s add the new Dialog to your basic Activity class.
Begin by editing your Activity class and adding the following class member variable:
private static final int MY_PASSWORD_DIALOG_ID = 4;


This defines a unique Dialog identifier for our Activity class. The value is an arbitrary number, but needs to be unique within the Activity.

Step 3: Inflate the Custom Dialog Layout

To create Dialog instances, you must provide a case for your custom password confirmation dialog in the onCreateDialog() method of your Activity class. When the showDialog() method is called, it triggers a call to this method, which must return the appropriate Dialog instance. Therefore, we begin the specific case statement for your new Dialog within the onCreateDialog()method here by inflating our custom layout file that was designed in the previous step and retrieving the important controls we will want to interact with from within it using the findViewById()method.
case MY_PASSWORD_DIALOG_ID:
       LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
 
       final View layout = inflater.inflate(R.layout.password_dialog, (ViewGroup) findViewById(R.id.root));
       final EditText password1 = (EditText) layout.findViewById(R.id.EditText_Pwd1);
       final EditText password2 = (EditText) layout.findViewById(R.id.EditText_Pwd2);
       final TextView error = (TextView) layout.findViewById(R.id.TextView_PwdProblem);
 
       // TODO: Create Dialog here and return it (see subsequent steps)
As you can see, we grab the two EditText controls that will store the password data, as well as the TextView control where we can display the password error text (passwords either match or do not match).

Step 4: Implement the Password TextWatcher

The next step of our onCreateDialog() case statement implementation for the custom dialog is to register a TextWatcher on the second EditText control so that we can listen for and detect password matches as the user types in order to display the proper string text in the error TextView control (matches/does not match).
Here’s the implementation of the TextWatcher, which is assigned to the second password EditText control using the addTextChangedListener() method.


password2.addTextChangedListener(new TextWatcher() {
   public void afterTextChanged(Editable s) {
      String strPass1 = password1.getText().toString();
      String strPass2 = password2.getText().toString();
      if (strPass1.equals(strPass2)) {
         error.setText(R.string.settings_pwd_equal);
      } else {
         error.setText(R.string.settings_pwd_not_equal);
      }
   }
 
   public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
   public void onTextChanged(CharSequence s, int start, int before, int count) {}
 });






The TextWatcher has three callback methods, but we’re really only interested in the afterTextChanged() method implementation. Here we check the text of the two EditText controls, compare them, and set the error text in the TextView. Here is what the TextWatcher might show when the passwords match:


Step 5: Use the Dialog Builder to Configure the Dialog

The next step of our onCreateDialog() case statement implementation for the custom password confirmation dialog is to use the AlertDialog.Builder class to begin to configure the dialog settings.
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Enter Password");
builder.setView(layout);


We set the title of the dialog using the setTitle() method, but the most important thing we do here is use the setView() method to attach our custom layout we just inflated to the dialog. This is how we modify the controls used within our dialog, making it have custom display and developer-defined behavior.

Step 6: Set the Positive and Negative Button Handlers for the Custom Password Dialog

Next, we need to configure the positive and negative buttons associated with our dialog. Recall that dialogs will be reused if displayed more than once. With our password confirmation dialog, we want to make sure to forcefully remove the dialog from the Activity’s dialog pool so that it cannot be reused. We do not want any residual password information being stored after our password dialog is dismissed, whether it is confirmed or dismissed.
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int whichButton) {
      removeDialog(MY_PASSWORD_DIALOG_ID);
   }
});
 
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
   public void onClick(DialogInterface dialog, int which) {
      String strPassword1 = password1.getText().toString();
      String strPassword2 = password2.getText().toString();
      if (strPassword1.equals(strPassword2)) {
         Toast.makeText(SuperSimpleDialogsActivity.this,
            "Matching passwords="+strPassword2, Toast.LENGTH_SHORT).show();
      }
      removeDialog(MY_PASSWORD_DIALOG_ID);
   }
});


We get rid of the dialog in both the negative and positive button click handlers using the removeDialog() method. However, in the positive button handler, we also retrieve the contents of theEditText controls and, if they match, display the password as a Toast message. In most cases, you would not use a Toast, but would save off the password using the method of your choice (preferences, etc.).

Step 7: Generate the Custom AlertDialog from the Builder

Your dialog is completely configured. Now use the create() method of the AlertDialog.Builder class to generate the appropriate AlertDialog and return it, thus ending your case statement for this dialog within the onCreateDialog() method of the Activity class.


AlertDialog passwordDialog = builder.create();
return passwordDialog;


Step 8: Triggering the Password Confirmation Dialog to Display

Finally, you are ready to trigger your password confirmation dialog to display as required. For example, you might add another Button control to your Activity’s screen to trigger your dialog to display. Its click handler might look something like this:
public void onPasswordDialogButtonClick(View v) {
    showDialog(MY_PASSWORD_DIALOG_ID);
}


How to show Custom toast in android?


toast is a notification view that contains a quick little message for the user. All it does is display the message, it's not interactive at all.
The toast class helps you create and show those.
When this view is shown to the user, it appears to float over the application. It will never receive focus as the user may be in the middle of typing something else. The idea is to be as unobtrusive as possible, while still showing the user the information you want them to see. Two possible examples are a volume control, and a brief message saying that your settings have been saved.
The easiest way to use this class is to call one of the static methods that constructs everything you need and returns a new Toast object.
A piece of toast is typically a simple text message, an image, or a combination of text and image.
You don't have control on the toast duration. There are only two states LENGTH_LONG and LENGTH_SHORT.

For this example we have added an image to one of the drawable folders under 'res' in our project, we refer to it simply like this:

view.setImageResource(R.drawable.flash);
Our image is actually called flash.png, note that we don't need to include the 'png' extension in our Android code, it already knows what we're talking about ;)

Here are 3 methods, demonstrating the above.

First, just show an image:
private void imageToast()
{
Toast toast = new Toast(getApplicationContext());
ImageView view = new ImageView(getApplicationContext());
view.setImageResource(R.drawable.flash);
toast.setView(view);
toast.show();
}
This method just displays a text toast:
public void textToast(String textToDisplay) {
Context context = getApplicationContext();
CharSequence text = textToDisplay;
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.setGravity(Gravity.TOP|Gravity.LEFT, 50, 50);
toast.show();
}
.. And this method displays a Toast that contains both an image and text:
public void textAndImageToast(String textToDisplay)
{
Toast toast = Toast.makeText(getApplicationContext(), textToDisplay, Toast.LENGTH_LONG);
View textView = toast.getView();
LinearLayout lay = new LinearLayout(getApplicationContext());
lay.setOrientation(LinearLayout.HORIZONTAL);
ImageView view = new ImageView(getApplicationContext());
view.setImageResource(R.drawable.flash);
lay.addView(view);
lay.addView(textView);
toast.setView(lay);
toast.show();
}

You can call each of these like this:
textToast("This is a text toast");imageToast();
textAndImageToast("This is a text and image toast");

And here are the results:

A text Toast



A text & image Toast


this is an image Toast


Here is the same image Toast with default Gravity for better demonstration purposes


Hope that makes sense.
.. Till next time, happy coding :)

Friday, February 10, 2012

How to prompt user input with an AlertDialog?

This code creates an input-dialog with AlertDialog.Builder where a user can enter text in an EditText field and press on "Ok" and "Cancel". 
AlertDialog.Builder alert = new AlertDialog.Builder(this);

alert.setTitle("Title");
alert.setMessage("Message");

// Set an EditText view to get user input 
final EditText input = new EditText(this);
alert.setView(input);

alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
  String value = input.getText();
  // Do something with value!
  }
});

alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
  public void onClick(DialogInterface dialog, int whichButton) {
    // Canceled.
  }
});

alert.show();