This example have the almost same output in the previous post "Create custom dialog with dynamic content, updated in onPrepareDialog()". The difference is it create dialog using AlertDialog.Builder, instead of in onCreateDialog() and onPrepareDialog().
main.xml, the main layout of our activity.
package com.AndroidCaptureScreen; import android.app.Activity; import android.app.AlertDialog; import android.content.DialogInterface; import android.graphics.Bitmap; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.view.ViewGroup.LayoutParams; import android.widget.Button; import android.widget.EditText; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView; public class AndroidCaptureScreen extends Activity { Bitmap bmScreen; 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(); OpenScreenDialog(); }}); } private void OpenScreenDialog(){ AlertDialog.Builder screenDialog = new AlertDialog.Builder(AndroidCaptureScreen.this); screenDialog.setTitle("Captured Screen"); TextView TextOut = new TextView(AndroidCaptureScreen.this); TextOut.setText(EditTextIn.getText().toString()); LayoutParams textOutLayoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT); TextOut.setLayoutParams(textOutLayoutParams); ImageView bmImage = new ImageView(AndroidCaptureScreen.this); bmImage.setImageBitmap(bmScreen); LayoutParams bmImageLayoutParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT); bmImage.setLayoutParams(bmImageLayoutParams); LinearLayout dialogLayout = new LinearLayout(AndroidCaptureScreen.this); dialogLayout.setOrientation(LinearLayout.VERTICAL); dialogLayout.addView(TextOut); dialogLayout.addView(bmImage); screenDialog.setView(dialogLayout); screenDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() { // do something when the button is clicked public void onClick(DialogInterface arg0, int arg1) { } }); screenDialog.show(); } }
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>
No comments:
Post a Comment