Reputation: 902
What I want to do is: When someone click at a TextView it opens an AlertDialog with a EditText, the person writes something in the EditText, clicks at "Ok" and then the TextView is set to what the person wrote on the EditText.
But I got two problemas.
First: It's not working. When I click in "Ok" my app crashes and I don't know what I'm doing wrong.
Second: I also have more than one TextView and I have no idea how to set the text of an especific textview without having to create a new alertdialog for each textview. How can I identify in the Dialog if the person has clicked on TextView A or B? Or should I just create another AlertDialog?
My code:
public class Mec extends Activity implements OnClickListener {
ImageView iv;
Button save;
TextView tSup, tInf;
EditText txt = null;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.loud);
bts();
Bundle extras = getIntent().getExtras();
String vds = extras.getString("omec");
if (vds.equals("Hello")) {
iv.setImageResource(R.drawable.world);
}
else {
return;
}
}
private void bts() {
iv = (ImageView) findViewById(R.id.ivID);
save = (Button) findViewById(R.id.btSave);
tSup = (TextView) findViewById(R.id.txtSuperior);
tInf = (TextView) findViewById(R.id.txtInferior);
txt = (EditText) findViewById(R.id.et1);
}
private void poptxt() {
AlertDialog InserirTXT = new AlertDialog.Builder(Mec.this).create();
LayoutInflater infle = getLayoutInflater();
View txtlayout = infle.inflate(R.layout.poptxt, null);
InserirTXT.setView(txtlayout);
InserirTXT.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String texto = (String) txt.getText().toString();
tSup.setText(texto);
}
});
InserirTXT.setCancelable(true);
InserirTXT.setCanceledOnTouchOutside(true);
InserirTXT.show();
}
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.btSave:
//After
break;
case R.id.txtSuperior:
poptxt();
break;
case R.id.txtInferior:
poptxt();
break;
}
}
}
Thank's for any help at all!
EDIT: Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical"
android:weightSum="100" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="73.68"
android:orientation="vertical" >
<ImageView
android:id="@+id/ivID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/badluck" />
<TextView
android:id="@+id/txtInferior"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/ivID"
android:layout_alignParentLeft="true"
android:clickable="true"
android:gravity="center"
android:onClick="onClick"
android:text="Bottom text"
android:textColor="#FFFFFF"
android:textSize="35dp"
style="@style/estilotxt" />
<TextView
android:id="@+id/txtSuperior"
style="@style/estilotxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/ivID"
android:clickable="true"
android:editable="true"
android:gravity="center"
android:onClick="onClick"
android:text="Top text"
android:textColor="#FFFFFF"
android:textSize="35dp" />
</RelativeLayout>
<Button
android:id="@+id/btSave"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="20"
android:text="Save" />
</LinearLayout>
Logcat:
08-08 22:23:55.101: W/dalvikvm(322): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-08 22:23:55.121: E/AndroidRuntime(322): FATAL EXCEPTION: main
08-08 22:23:55.121: E/AndroidRuntime(322): java.lang.NullPointerException
08-08 22:23:55.121: E/AndroidRuntime(322): at vds.cmc.Mec$1.onClick(Mec.java:89)
08-08 22:23:55.121: E/AndroidRuntime(322): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
08-08 22:23:55.121: E/AndroidRuntime(322): at android.os.Handler.dispatchMessage(Handler.java:99)
08-08 22:23:55.121: E/AndroidRuntime(322): at android.os.Looper.loop(Looper.java:123)
08-08 22:23:55.121: E/AndroidRuntime(322): at android.app.ActivityThread.main(ActivityThread.java:4627)
08-08 22:23:55.121: E/AndroidRuntime(322): at java.lang.reflect.Method.invokeNative(Native Method)
08-08 22:23:55.121: E/AndroidRuntime(322): at java.lang.reflect.Method.invoke(Method.java:521)
08-08 22:23:55.121: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
08-08 22:23:55.121: E/AndroidRuntime(322): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
08-08 22:23:55.121: E/AndroidRuntime(322): at dalvik.system.NativeStart.main(Native Method)
08-08 22:23:56.950: I/Process(322): Sending signal. PID: 322 SIG: 9
EDIT: Thank you guys.
I tried iniciating the txt inside the AlertDialog but nothing worked so I decided to delete this part and rewrite it from scratch with a tutorial that I found. Now it's working, but I still have no idea what I did wrong.
Thank you!
Upvotes: 0
Views: 1898
Reputation: 1298
public class Mec extends Activity implements OnClickListener {
AlertDialog.Builder builder;
LinearLayout lila;
EditText et;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.loud);
builder = new AlertDialog.Builder(this);
lila = new LinearLayout(this);
et = new EditText(this);
lila.addView(et);
builder.setView(lila);
builder.show();
}
}
That should give you a nice alert dialog with an EditText inside! :) Now if you place builder.show() inside of an onClickListener of another button, the alert dialog will only show up when that button is clicked. I hope that's what you're looking for.
EDIT: And to set the textviews, you would simply have to say something like textfield.setText(et.getText()); -- you might have to cast it as a String, but that's the basic idea...
Upvotes: 0
Reputation: 12642
make your code like this
private void poptxt() {
AlertDialog InserirTXT = new AlertDialog.Builder(Mec.this).create();
LayoutInflater infle = getLayoutInflater();
View txtlayout = infle.inflate(R.layout.poptxt, null);
InserirTXT.setView(txtlayout);
tSup = (TextView) InserirTXT.findViewById(R.id.txtSuperior); //THE PROBLEM
txt = (EditText) InserirTXT.findViewById(R.id.et1);
InserirTXT.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
String texto = (String) txt.getText().toString();
tSup.setText(texto);
}
});
InserirTXT.setCancelable(true);
InserirTXT.setCanceledOnTouchOutside(true);
InserirTXT.show();
}
You are initializing the tSup and txt
with the context of the Activity
in which you set layout loud
which does not have R.id.txtSuperior and R.id.et1
. These two view are in your layout poptxt
. Am I correct? And you are inflating poptxt
in poptxt()
. So initialize them with the dialog View not with the Activity's
View.
Upvotes: 0
Reputation: 1723
Do the following
create an xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<ImageView android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
Then create a dialog and set the layout as its contentview
Context mContext = getApplicationContext();
Dialog dialog = new Dialog(mContext);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
Upvotes: 1
Reputation: 15009
Your layout file and your LogCat output would be a help here, but even without it I can see a potential issue:
String texto = (String) txt.getText().toString();
tSup.setText(texto);
Given your description, I believe that txt
is the edit field in your AlertDialog
, so as it is initialised in the method bts()
, which is called before the AlertDialog
is created, it will be null
. Move its initialisation to after the AlertDialog
creation.
Upvotes: 0