Custom Alert Dialog
In the previous Blog we have discussed about
Alert Dialog. Today I am sharing how to create alert Dialog in android.
- First we have to create custom dialog Layout in xml.
- Attach the layout to Dialog
- Show the Dialog
custom_alert_layout.xml
|
| <?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android='http://schemas.android.com/apk/res/android' |
| android:layout_width='fill_parent' |
| android:layout_height='fill_parent' |
| android:orientation='vertical' |
| android:padding='10sp' > |
| |
| <EditText |
| android:id='@+id/etUser' |
| android:layout_width='fill_parent' |
| android:layout_height='wrap_content' |
| android:hint='@string/username' |
| android:singleLine='true' > |
| |
| <requestFocus /> |
| </EditText> |
| |
| <EditText |
| android:id='@+id/etpassword' |
| android:layout_width='match_parent' |
| android:layout_height='wrap_content' |
| android:ems='10' |
| android:hint='@string/password' |
| android:inputType='textPassword' > |
| </EditText> |
| |
| <RelativeLayout |
| android:layout_width='match_parent' |
| android:layout_height='wrap_content' > |
| |
| <Button |
| android:id='@+id/btn_login' |
| android:layout_width='wrap_content' |
| android:background="@color/colorPrimary" |
| android:textColor="@android:color/white" |
| android:layout_height='wrap_content' |
| android:text='@string/login' /> |
| |
| <Button |
| android:id='@+id/btn_cancel' |
| android:layout_width='wrap_content' |
| android:layout_height='wrap_content' |
| android:layout_alignParentTop='true' |
| android:background="@color/colorPrimary" |
| android:textColor="@android:color/white" |
| android:layout_marginLeft='10dp' |
| android:layout_toRightOf='@+id/btn_login' |
| android:text='@string/cancel' /> |
| </RelativeLayout> |
| |
| </LinearLayout>
activity_main.xml
|
| <?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
| xmlns:tools="http://schemas.android.com/tools" |
| android:layout_width="match_parent" |
| android:layout_height="match_parent" |
| android:paddingBottom="@dimen/activity_vertical_margin" |
| android:paddingLeft="@dimen/activity_horizontal_margin" |
| android:paddingRight="@dimen/activity_horizontal_margin" |
| android:paddingTop="@dimen/activity_vertical_margin" |
| tools:context="com.example.prabhu.customalertdialog.MainActivity"> |
| |
| <Button |
| android:id="@+id/btnLogin" |
| android:layout_width="wrap_content" |
| android:layout_height="wrap_content" |
| android:background="@color/colorPrimary" |
| android:textColor="@android:color/white" |
| android:text="Login" /> |
| </RelativeLayout> |
MainActivity.java
|
| |
| package com.example.prabhu.customalertdialog;
import android.app.Dialog; |
| import android.support.v7.app.AppCompatActivity; |
| import android.os.Bundle; |
| import android.view.View; |
| import android.widget.Button; |
| import android.widget.EditText; |
| import android.widget.ImageView; |
| import android.widget.TextView; |
| import android.widget.Toast; |
| |
| public class MainActivity extends AppCompatActivity { |
| private Button btnLogin; |
| EditText etUser,etpassword; |
| @Override |
| protected void onCreate(Bundle savedInstanceState) { |
| super.onCreate(savedInstanceState); |
| setContentView(R.layout.activity_main); |
| btnLogin = (Button)findViewById(R.id.btnLogin); |
| btnLogin.setOnClickListener(new View.OnClickListener() { |
| @Override |
| public void onClick(View v) { |
| final Dialog dialog = new Dialog(MainActivity.this); |
| |
| //setting custom layout to dialog |
| dialog.setContentView(R.layout.custom_alart_layout); |
| dialog.setTitle("Login Here !!"); |
| |
| //initilize the edittext |
| etUser = (EditText) dialog.findViewById(R.id.etUser); |
| |
| etpassword = (EditText) dialog.findViewById(R.id.etpassword); |
| //adding button click event |
| Button btnCancel = (Button) dialog.findViewById(R.id.btn_cancel); |
| btnCancel.setOnClickListener(new View.OnClickListener() { |
| @Override |
| public void onClick(View v) { |
| dialog.dismiss(); |
| } |
| }); |
| |
| Button btnLogin = (Button) dialog.findViewById(R.id.btn_login); |
| btnLogin.setOnClickListener(new View.OnClickListener() { |
| @Override |
| public void onClick(View v) { |
| if(etUser.getText().toString().equals("admin") && etpassword.getText().toString().equals("admin")); |
| { |
| Toast.makeText(MainActivity.this,"Login successfully!!",Toast.LENGTH_LONG).show(); |
| dialog.dismiss(); |
| } |
| } |
| }); |
| |
| |
| |
| dialog.show(); |
| } |
| }); |
| } |
| } |
output
You can Download the Project from Github
|
0 comments:
Post a Comment