Saturday, October 22, 2016

android - EditText in AlertDialog

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#b2b6be"
    >
    <Button
        android:id="@+id/btn_alert"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Show Alert Dialog"
        android:layout_gravity="top|center_horizontal"
        />
</android.support.design.widget.CoordinatorLayout>
MainActivity.java

package com.cfsuman.me.androidcodesnippets;

import android.app.Activity;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.Typeface;
import android.support.design.widget.CoordinatorLayout;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity {
    private Context mContext;
    private Activity mActivity;

    private CoordinatorLayout mCLayout;
    private Button mButton;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Get the application context
        mContext = getApplicationContext();
        mActivity = MainActivity.this;

        // Get the widget reference from XML layout
        mCLayout = (CoordinatorLayout) findViewById(R.id.coordinator_layout);
        mButton = (Button) findViewById(R.id.btn_alert);

        // Set a click listener for the button widget
        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Initializing a new alert dialog
                AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);

                // Specify the alert dialog title
                builder.setTitle("EditText in an AlertDialog.");

                // Initialize a new layout parameters
                LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
                        LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT
                );

                // Initialize a new linear layout as alert dialog message custom view
                // This is the container of alert dialog message contents
                LinearLayout LLayout = new LinearLayout(mContext);
                LLayout.setOrientation(LinearLayout.VERTICAL);
                params.setMargins(25,25,25,25);
                LLayout.setLayoutParams(params);
                LLayout.setPadding(25,25,25,25);
                LLayout.setBackgroundColor(Color.parseColor("#FFF4FFDE"));

                // Initialize a new TextView instance
                // This is the label of edit text
                TextView tv_label = new TextView(mContext);
                tv_label.setLayoutParams(params);
                tv_label.setText("Input your name here.");
                tv_label.setTextColor(Color.BLACK);
                tv_label.setTextSize(TypedValue.COMPLEX_UNIT_DIP,17);

                // Initialize a new EditText instance
                final EditText et_message = new EditText(mContext);
                et_message.setLayoutParams(params);
                et_message.setTextColor(Color.parseColor("#FFDE1C73"));
                et_message.setTextSize(TypedValue.COMPLEX_UNIT_DIP,17);
                et_message.setBackgroundColor(Color.parseColor("#FFDFECFF"));
                et_message.setTypeface(null,Typeface.BOLD);
                et_message.setPadding(15,15,15,15);

                // Add the two views to linear layout
                LLayout.addView(tv_label);
                LLayout.addView(et_message);

                // Set the custom view for alert dialog message
                builder.setView(LLayout);

                // Set the positive button
                builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialogInterface, int i) {
                        // Get the edit text inputted name
                        String name = et_message.getText().toString();
                        // Display the name in snack bar
                        Snackbar.make(mCLayout,"Hello "+ name,Snackbar.LENGTH_LONG).show();
                    }
                });

                // Set the negative button
                builder.setNegativeButton("No",null);

                // Create the alert dialog
                AlertDialog dialog = builder.create();

                // Finally, display the alert dialog
                dialog.show();
            }
        });
    }
}