I’ve tried converting an EditText value to string repeatedly, and have tried everything I can find, but I am unable to correctly get the string value from an EditText value, then display it in a dialog. I made a basic activity to test it. Where am I going wrong? All this program is is one edittext box and one button.
package com.bensherman.example.etts;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class EditTextToStringActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
EditText text = (EditText)findViewById(R.id.eTb);
String convtext = text.getText().toString();
final Builder vars = new AlertDialog.Builder(this);
vars.setTitle("Saved Information");
vars.setMessage("Entered Text: " + convtext);
Button savedvars = (Button) findViewById(R.id.showButton);
savedvars.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
vars.show();
}
});
}
}
the 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" >
<EditText
android:id="@+id/eTb"
android:hint = "Enter Text"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<requestFocus />
</EditText>
<Button
android:id="@+id/showButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Text" />
</LinearLayout>
write this 2 line into the onClick() method
or you can also write like this way
and than