Android Program : Develop an Android application that create custom Alert Dialog containing Friends Name and onClick of Friend Name Button greet accordingly.

Android Program : Develop an Android application that create custom Alert Dialog containing Friends Name and onClick of Friend Name Button greet accordingly.



custom_layout.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

android:orientation="vertical"

android:paddingLeft="20dp"

android:paddingRight="20dp"

android:layout_width="match_parent"

android:layout_height="match_parent">


<EditText

android:id="@+id/editText"

android:layout_width="match_parent"

android:layout_height="wrap_content"/>

</LinearLayout>



activity_main.xml

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout

xmlns:android="http://schemas.android.com/apk/res/android"

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

android:gravity="center"

android:id="@+id/root"

android:orientation="vertical"

tools:context=".MainActivity">


<Button

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:onClick="showAlertDialogButtonClicked"

android:text="Show Dialog"

/>

</LinearLayout>



MainActivity.java


import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.view.View; import android.widget.Button;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity { @Override protected void onCreate( Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); } public void showAlertDialogButtonClicked(View view) { // Create an alert builder AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setTitle("Name"); // set the custom layout final View customLayout = getLayoutInflater() .inflate( R.layout.custom_layout, null); builder.setView(customLayout); // add a button builder .setPositiveButton( "OK", new DialogInterface.OnClickListener() { @Override public void onClick( DialogInterface dialog, int which) { // send data from the // AlertDialog to the Activity EditText editText = customLayout .findViewById( R.id.editText); sendDialogDataToActivity( editText .getText() .toString()); } }); // create and show // the alert dialog AlertDialog dialog = builder.create(); dialog.show(); } // Do something with the data // coming from the AlertDialog private void sendDialogDataToActivity(String data) { Toast.makeText(this, data, Toast.LENGTH_SHORT) .show(); } }







Post a Comment

0 Comments