Wednesday, 12 October 2016

CustomDialog

It is  a user defined dialog.
It is android widget.
It is similar to activity or user-interface(UI).
Using Dialog class
And create one more XML file and java file (its not mandatory)

activitymain.xml :-

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout 
 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:orientation="vertical" 
 tools:context="com.kiranapp.MainActivity">


<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content"       
 android:text="customdialog" 
 android:id="@+id/button" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>


customdialog :-

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
 android:orientation="vertical" android:layout_width="match_parent" 
 android:layout_height="match_parent">

<EditText 
 android:layout_width="match_parent" 
 android:layout_height="wrap_content" 
 android:id="@+id/editText" 
 android:layout_gravity="center_horizontal" 
 android:layout_marginTop="30dp" 
 android:hint="enter  name" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="enter" 
 android:id="@+id/okbtn" 
 android:layout_gravity="center_horizontal" />

<Button 
 android:layout_width="wrap_content" 
 android:layout_height="wrap_content" 
 android:text="cancel" 
 android:id="@+id/canbtn" 
 android:layout_gravity="center_horizontal" />
</LinearLayout>

MainActivity.java :-

package com.kiranapp;

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.Toast;

public class MainActivity extends AppCompatActivity {
 Button okbtn,canbtn;
 Dialog dialog;
 EditText e1;

@Override 
 protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 okbtn=(Button)findViewById(R.id.button);
 okbtn.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View v) {
 dialog=new Dialog(MainActivity.this);
 dialog.setTitle("Custom Dialog");
 dialog.setContentView(R.layout.customdialog);
 dialog.show();
 okbtn=(Button)dialog.findViewById(R.id.okbtn);
 canbtn=(Button)dialog.findViewById(R.id.canbtn);
 e1=(EditText)dialog.findViewById(R.id.editText);
 okbtn.setOnClickListener(new View.OnClickListener() {
@Override
 public void onClick(View v) {
 String s1=e1.getText().toString();
Toast.makeText(MainActivity.this, "Name is : "+s1, Toast.LENGTH_SHORT).show();
 }
});
canbtn.setOnClickListener(new View.OnClickListener() {
@Override 
 public void onClick(View v) {
 dialog.cancel();
 }
});
}
});
}
}


Customdialog.java :-

package com.kiranapp;

/** * Created by Dell on 10/12/2016. */public class customdialog {
}

Output :-

Custom dialog
Custom dialog fig :1





















custom dialog
custom dialog fig :2

No comments:

Post a Comment