Declan Morgan Declan Morgan - 1 month ago 14
Android Question

Update TextView from AlertDialog

I am trying to use the AlertDialog in android, specifically I want the user to input some information in an EditText field, then once finished to press OK and for the TextView to be updated to the inputted value.

Everything worked well up until I wanted to update the TextView when the user clicks OK. Now the app keeps crashing when the user clicks OK.

Any ideas would be greatly appreciated.

MainActivity.java

package com.simple.plain.calendar;

import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;



public class MainActivity extends AppCompatActivity {


String input = "";
EditText edit;
TextView text;


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

//Declaring the textview and button.
TextView tv = (TextView) findViewById(R.id.calendar_tv);
Button but = (Button) findViewById(R.id.surprise);

//When button is clicked, do the following.
but.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog();
}
});
}


private void showDialog(){
// Create the Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);

// Inflate and set the layout for the dialog
LayoutInflater inflater = MainActivity.this.getLayoutInflater();
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null));

edit = (EditText) findViewById(R.id.username);
text = (TextView) findViewById(R.id.new_text);

// Add OK button
builder.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked OK button
input = edit.getText().toString();
text.setText(input);
}
});


builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});

// Set other dialog properties
builder.setMessage(R.string.dialog_message)
.setTitle(R.string.dialog_title);



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


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
switch (item.getGroupId()){

case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}


}


dialog_signin.xml

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


<EditText
android:id="@+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="@string/username" />
<EditText
android:id="@+id/password"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="@string/password"/>
</LinearLayout>


activity_main.xml

<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:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#e0e2e5">

<android.support.v7.widget.CardView
android:id="@+id/card_view"
android:layout_gravity="center"
android:layout_width="match_parent"
android:layout_height="200dp" >


<TextView
android:text="@string/hello_world"
android:id="@+id/calendar_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="FireMissilesDialogFragment"
android:text="@string/surprise_me"
android:id="@+id/surprise"
android:layout_marginTop="20dp"/>

<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="@+id/new_text"/>

</android.support.v7.widget.CardView>

</RelativeLayout>

Answer

Your EditText is null, because you are trying to find it by id from the Activity's layout, while it is actually in the dialog's view.

Replace these two lines:

builder.setView(inflater.inflate(R.layout.dialog_signin, null));

edit = (EditText) findViewById(R.id.username);

With those:

View dialogView = inflater.inflate(R.layout.dialog_signin, null);
builder.setView(dialogView );

edit = (EditText) dialogView .findViewById(R.id.username);