-1

I want to get some integer value from the user he/she click on the an alert box will show and asks for enter the total budget. When he/she puts the total budget in the alert box field then it'll be show in text view in the same activity.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Please include the code that you have tried so far as part of the question, so that it would be easy for people to identify and correct the issue. – Gautham M Dec 19 '20 at 07:45

2 Answers2

0

Each dialog has a positive button, and that positive button has a callback where you can get the text from edit text, like Edittext.getText and display it for textview like TextView.setText.

AlertDialog alertDialog = new AlertDialog.Builder(MainActivity.this).create();
alertDialog.setTitle("Alert");
alertDialog.setMessage("Alert message to be shown");
alertDialog.setButton(AlertDialog.BUTTON_POSITIVE, "OK",
    new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int which) {
        // your work here
        textview.setText = Edittext.getText

            dialog.dismiss();
        }
    });
alertDialog.show();
halfer
  • 19,824
  • 17
  • 99
  • 186
Nitin Tej
  • 353
  • 1
  • 7
0

Do refer to topic regarding custom dialog box: How to create a Custom Dialog box in android?

You can create a custom dialog box that have a custom layout R.layout.budget_dialog:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:orientation="vertical"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:background="#ffffffff">


  <EditText
    android:layout_width="250dp"
    android:layout_height="wrap_content"
    android:id="@+id/edit_budget"
    android:hint="Enter budget"
    android:layout_below="@+id/a"
    android:layout_marginTop="20dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="20dp"
    android:textSize="18sp"
    android:textColor="#ff000000"
    android:layout_centerHorizontal="true"
    android:gravity="center_horizontal" />

  <Button
    android:layout_width="wrap_content"
    android:layout_height="30dp"
    android:text="OK"
    android:id="@+id/btn_dialog"
    android:gravity="center_vertical|center_horizontal"
    android:layout_below="@+id/text_dialog"
    android:layout_marginBottom="20dp"
    android:background="@drawable/btn_flat_red_selector"
    android:layout_centerHorizontal="true"
    android:textColor="#ffffffff" />

</RelativeLayout>

And then obtain the value by:

public void showDialog(Activity activity, String msg){
    final Dialog dialog = new Dialog(activity);
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
    dialog.setCancelable(false);
    dialog.setContentView(R.layout.budget_dialog);

    final EditText edtBudget = dialog.findViewById(R.id.edit_budget);
    Button dialogButton = dialog.findViewById(R.id.btn_dialog);
    dialogButton.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Get budget string value
            String budget = edtBudget.getText().toString();
            //Do something with your value
            dialog.dismiss();
        }
    });

    dialog.show();

}
Lee Boon Kong
  • 1,007
  • 1
  • 8
  • 17