display textview in alert dialog
41,731
Solution 1
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Title");
alert.setMessage("Message");
// Create TextView
final TextView input = new TextView (this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
input.setText("hi");
// Do something with value!
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
Solution 2
Here in the part custom layout - you can create a custom layout for your alertDialog, not only in textview, but anything.
Link to simplify: here is my code from my game
//create builder
AlertDialog.Builder builder = new AlertDialog.Builder(GWGPlay.this);
//inflate layout from xml. you must create an xml layout file in res/layout first
LayoutInflater inflater = GWGPlay.this.getLayoutInflater();
View layout = inflater.inflate(R.layout.guesskeyword /*my layout here*/, null);
builder.setView(layout);
//set 2 main buttons
builder.setPositiveButton("Answer", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
endGame(true);
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.show();
Author by
qwertzuiop
Updated on August 07, 2022Comments
-
qwertzuiop almost 2 years
In my code, I have an
AlertDialog
and aTextView
. I'd like display thisTextView
in myAlertDialog
but I don't know how to do it. I don't know how to add theView
in aAlertDialog
.I could show my code but I don't think it would be usefull.
Thank's
EDIT:
Thank's for all your answers. I just did a test and it works perfectly.
Here is my working code:
package com.example.testalertdialog; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.widget.LinearLayout; import android.widget.TextView; public class MainActivity extends Activity { LinearLayout layout; AlertDialog ad; TextView tv1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); layout = new LinearLayout(this); ad = new AlertDialog.Builder(this).create(); tv1 = new TextView(this); setContentView(layout); tv1.setText("Test"); ad.setView(tv1); ad.show(); } }
Edit2: But why doesn't this code work ?
package com.example.testalertdialog; import android.annotation.SuppressLint; import android.app.Activity; import android.app.AlertDialog; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.LinearLayout; import android.widget.TextView; @SuppressLint("HandlerLeak") public class MainActivity extends Activity implements OnClickListener{ LinearLayout layout; AlertDialog ad; TextView tv1; Button b1; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); layout = new LinearLayout(this); tv1 = new TextView(this); b1 = new Button(this); b1.setOnClickListener(this); layout.addView(b1); ad = new AlertDialog.Builder(this).create(); setContentView(layout); tv1.setText("Test"); } @Override public void onClick(View v) { if (v == b1) { ad.setMessage("Chargement"); ad.show(); ad.setView(tv1); } }
}
-
Yoda about 10 yearsThere is no TextView visible in this alert : (.
-
Mystery over 5 years@Yoda for EditText use final EditText input = new EditText (this);