Set up the view with one message - Android User Interface

Android examples for User Interface:View Property

Description

Set up the view with one message

Demo Code

import android.app.AlertDialog;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Main {
  /**//from   w ww.ja va 2 s  .c  o m
   * Set up the view with one message
   * 
   * @param context
   *          Context
   * @param alert
   *          AlertDialog
   * @param message
   *          Message
   */
  public static void setDialogViewMessage(Context context,
      AlertDialog.Builder alert, String message) {
    alert.setMessage(message);
  }

  /**
   * Set up the view with two messages
   * 
   * @param context
   *          Context
   * @param alert
   *          AlertDialog
   * @param message1
   *          Message 1
   * @param message2
   *          Message 2
   */
  public static void setDialogViewMessage(Context context,
      AlertDialog.Builder alert, String message1, String message2) {
    // Log.e("setDialogViewMessage", "setDialogViewMessage");
    LinearLayout ll = new LinearLayout(context);
    LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT);

    layoutParams.setMargins(20, 10, 20, 10);

    ll.setOrientation(LinearLayout.VERTICAL);
    ll.setLayoutParams(layoutParams);
    TextView messageView1 = new TextView(context);
    TextView messageView2 = new TextView(context);
    TextView messageView3 = new TextView(context);
    messageView1.setLayoutParams(layoutParams);
    messageView2.setLayoutParams(layoutParams);
    messageView3.setLayoutParams(layoutParams);
    messageView1.setText(message1);
    messageView2.setText(message2);
    PackageInfo pInfo = null;
    String version = "";
    try {
      pInfo = context.getPackageManager().getPackageInfo(
          context.getPackageName(), 0);
      version = pInfo.versionName;
    } catch (PackageManager.NameNotFoundException e) {
      e.printStackTrace();
    }

    messageView3.setText("Card Safe Version " + version);
    ll.addView(messageView1);
    ll.addView(messageView2);
    ll.addView(messageView3);
    alert.setView(ll);

  }
}

Related Tutorials