May 27, 2011

Create custom dialog with dynamic content, updated in onPrepareDialog()

In the post "Create custom dialog with EditText", the dialog is created in onCreateDialog(). Please notice that the call-back function onCreateDialog() will be called when a dialog is requested for the first time, such that the content defined in onCreateDialog() will not be updated next time.

If you have some dynamic content changed in runtime, you can override the call-back method onPrepareDialog(), it will be called every time a dialog is opened. Override this method if you want to change any properties of the dialog each time it is opened.

In this example, we will get dynamic content to the dialog; including the Bitmap by screen capture(refer to the post "Capture Screen, using View.getDrawingCache()"), and the user input from a EditText - both will be changed each time the dialog is opened.

Create custom dialog with dynamic content, updated in onPrepareDialog()

Create /res/layout/screendialog.xml, it's the layout of our dialog
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="10dip"
android:paddingRight="10dip"
>
<TextView
android:id="@+id/textout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ImageView
android:id="@+id/image"
android:layout_width="300px"
android:layout_height="300px"
/>
<Button
android:id="@+id/okdialogbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="OK"
/>
</LinearLayout>


main code, AndroidCaptureScreen.java
package com.AndroidCaptureScreen;

import android.app.Activity;
import android.app.Dialog;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

public class AndroidCaptureScreen extends Activity {

Bitmap bmScreen;

Dialog screenDialog;
static final int ID_SCREENDIALOG = 1;

ImageView bmImage;
Button btnScreenDialog_OK;
TextView TextOut;

View screen;
EditText EditTextIn;

 /** Called when the activity is first created. */
 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.main);
     screen = (View)findViewById(R.id.screen);
     Button btnCaptureScreen = (Button)findViewById(R.id.capturescreen);
     EditTextIn = (EditText)findViewById(R.id.textin);
  
     btnCaptureScreen.setOnClickListener(new OnClickListener(){

  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   screen.setDrawingCacheEnabled(true);
   bmScreen = screen.getDrawingCache();
   showDialog(ID_SCREENDIALOG);
  }});
 }


@Override
protected Dialog onCreateDialog(int id) {
 // TODO Auto-generated method stub

 screenDialog = null;
 switch(id){
 case(ID_SCREENDIALOG):
  screenDialog = new Dialog(this);
  screenDialog.setContentView(R.layout.screendialog);
  bmImage = (ImageView)screenDialog.findViewById(R.id.image);
  TextOut = (TextView)screenDialog.findViewById(R.id.textout);
  btnScreenDialog_OK = (Button)screenDialog.findViewById(R.id.okdialogbutton);
  btnScreenDialog_OK.setOnClickListener(btnScreenDialog_OKOnClickListener);
 }
 return screenDialog;
}

@Override
protected void onPrepareDialog(int id, Dialog dialog) {
 // TODO Auto-generated method stub
 switch(id){
 case(ID_SCREENDIALOG):
  dialog.setTitle("Captured Screen");
  TextOut.setText(EditTextIn.getText().toString());
  bmImage.setImageBitmap(bmScreen);
  break;
 }
}

private Button.OnClickListener btnScreenDialog_OKOnClickListener
 = new Button.OnClickListener(){

 @Override
 public void onClick(View arg0) {
  // TODO Auto-generated method stub
  screenDialog.dismiss();
 }};
}


main.xml, the main layout of our activity.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 android:id="@+id/screen"
 >
<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="@string/hello"
 />
<Button
 android:id="@+id/capturescreen"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Capture Screen"
 />
<TextView
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="\nScreen can be captured using: \n\n
screen.setDrawingCacheEnabled(true);\n
bmScreen = screen.getDrawingCache();\n"
 />
<EditText
 android:id="@+id/textin"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />
</LinearLayout>


Related:
- Create custom dialog using AlertDialog.Builder, with dynamic content

Noted:
- This code capture the first screen only! Read Capture screen with getDrawingCache() repeatly


6 comments:

  1. thanks for this post

    Ankur Kumar

    ReplyDelete
  2. Thanks for the app, but the screen capture doesn't update the contents, if we edit the close the dialog box, the again edit the texts and then click on Capture Screen button, do we need to create a new instance of the bitmap to redraw the screen in the current activity cycle?

    ReplyDelete
  3. this code can not take snap from video view,,, i waana take snap from video view help me plzz

    ReplyDelete

Infolinks In Text Ads