Reputation: 4430
I have to take the text from an AlertDialog.
For the AlertDialog I use a specific layout: custom_alertdialog
Instead for all the project use: another layout
I tried like this:
View layout = getLayoutInflater().Inflate(R.layout.custom_alertdialog,null);
Or so:
View layout = View.inflate(SimpleVideoStream.this, R.layout.custom_alertdialog, null);
But the text is not taken in either case. How can I do?
AlertDialog.Builder builder = new AlertDialog.Builder(SimpleVideoStream.this);
builder.setTitle("Add Subtitle");
builder.setCancelable(true);
builder.setPositiveButton(
"Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
View inflatedView = getLayoutInflater().inflate(R.layout.custom_alertdialog, null);
final View layout = View.inflate(SimpleVideoStream.this, R.layout.custom_alertdialog, null);
EditText url_ele = (EditText) layout.findViewById(R.id.url);
String sub = url_ele.getText().toString();
Log.v("Ok:","/"+sub);
if (!sub.equals("")) {
showSub = !showSub;
Format textFormat = Format.createTextSampleFormat(null, MimeTypes.APPLICATION_SUBRIP,
null, Format.NO_VALUE, Format.NO_VALUE, "en", null, Format.OFFSET_SAMPLE_RELATIVE);
MediaSource textMediaSource = new SingleSampleMediaSource.Factory(dataSourceFactory)
.createMediaSource(Uri.parse(sub), textFormat, C.TIME_UNSET);
videoSource = new MergingMediaSource(videoSource, textMediaSource);
player.prepare(videoSource, false, false);
}
dialog.cancel();
}
});
builder.setNegativeButton(
"Close",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);
alert.setView(dialoglayout);
alert.show();
Upvotes: 0
Views: 933
Reputation: 13539
In your builder
instance, you inflated a layout( layout A). Before setView()
method, you inflated another layout(layout B). These two layout are different.
You could do like this:
AlertDialog.Builder builder = new AlertDialog.Builder(SimpleVideoStream.this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView= inflater.inflate(R.layout.custom_alertdialog, null);
builder.setView(inflater.inflate(R.layout.custom_alertdialog, null))
final EditText editText = (EditText)dialogView.findViewById(R.id.url);
builder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
//Do something
}
}
});
.setNegativeButton(){
//Do something
};
builder.create();
builder.show();
Upvotes: 1
Reputation: 17824
When you inflate a View, it doesn't share an instance with any other times you've inflated it. So what you passed to setView()
is completely different from what you're calling findViewById()
on. You need to create only one instance and then reference that.
For example:
View view1 = inflater.inflate(R.layout.some_layout, null);
View view2 = inflater.inflate(R.layout.some_layout, null);
EditText text1 = view1.findViewById(R.id.some_edittext);
EditText text2 = view2.findViewById(R.id.some_edittext);
text1.setText("Hello");
String text2text = text2.getText().toString(); //this will be null, because text1 and text2 aren't the same instance, because view1 and view2 aren't the same instance.
Move
View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);
above where you set the positive button, and make it final:
final View dialoglayout = inflater.inflate(R.layout.custom_alertdialog, null);
builder.setPositiveButton(...
Remove both inflatedView
and layout
from inside your listener, and use dialogLayout
:
EditText url_ele = (EditText) dialogLayout.findViewById(R.id.url);
Upvotes: 1