How to use another layout id? - android

In my application I have used one alert box.. In this alert box, I give another layout to be show.. using setView() method.. I have a problem. How to use layout elements id in my activity?
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.volume, null);
builder.setTitle("Volume");
builder.setView(layout);
builder.setPositiveButton("OK", null);
builder.setNegativeButton("Cancel", null);
AlertDialog alert1 = builder.create();
alert1.show();

Something like this:
In your activity:
....
View layout = inflater.inflate(R.layout.volume, null);
.....
View some = layout.findViewById(YOUR_VIEW_ID);
.....

if you want to refer to view inside your custom layout, then do the following:
EditText editText = (EditText) layout.findViewById(R.id.your_editText);
Button editText = (Button) layout.findViewById(R.id.your_button);
.
.
.
//and so one with other views

This code shows you how to show a custom alert and get a view from that custom layout:
AlertDialog mDialog = null;
Context mContext = getApplicationContext();
final AlertDialog.Builder my_Dialog = new AlertDialog.Builder(this);
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.my_dialog, (ViewGroup) findViewById(R.id.layout_root));
ImageView imageViewInsideLayout = (ImageView) layout.findViewById(R.id.imageviewLayout);
my_Dialog.setView(layout);
mDialog = my_Dialog.create();
mDialog.show();

You can get reference to view by builder.findViewById( R.id.restart );

Related

How to get the inflated view from AlertDialog when using setView(int layoutResId)?

I use this code to create a custom AlertDialog:
val dialog = AlertDialog.Builder(context)
.setView(R.layout.layout)
.create()
The problem is I cannot get the inflated view. dialog.findViewById(R.id.a_view_in_the_layout) returns null.
Alternatively, I can use .setView(View.inflate(context, R.layout.layout, null) but this sometimes makes the dialog fill the screen and take more space than setView(int layoutResId).
If I remember correctly, create sets up the Dialog, but its layout is not inflated until it needs to be shown. Try calling show first then, then finding the view you're looking for.
val dialog = AlertDialog.Builder(context)
.setView(R.layout.layout)
.create()
dialog.show() // Cause internal layout to inflate views
dialog.findViewById(...)
Just inflate the layout yourself (its Java code but I think you know what to do):
AlertDialog.Builder dialog = new AlertDialog.Builder(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
View view = inflater.inflate( R.layout.layout, null );
dialog.setView(view);
dialog.create().show();
Your inflated view is now view and you can use it to find other views in it like:
EditText editText = view.findViewById(R.id.myEdittext);
Instead of using alert dialog use simple Dialog its Easy and very simple
final Dialog dialog = new Dialog(context);
dialog.setContentView((R.layout.layout);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
TextView tvTitle = (TextView) dialog.findViewById(R.id.tvTitle);
tvTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
You don't have to need to inflate the View.
Try this;
View dialogView; //define this as a gobal field
dialogView = LayoutInflater.from(context).inflate(R.layout.your_view, null);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle("Title");
builder.setView(dialogView);
View yourView = dialogView.findViewById(R.id.a_view_in_the_layout);
TextView yourTextView = dialogView.findViewById(R.id.a_textView_in_the_layout);
Button yourButton = dialogView.findViewById(R.id.a_button_in_the_layout);

getting edittext value from listview showing nullpointerexception

I have an activity which consists a Dialog and Dialog has a listview. Trying to retrive value of edittext which is a row item of listview but getting nullpointerexception.
dialog = new Dialog(this);
dialog.setContentView(R.layout.dialog_layout);
listView = (ListView)dialog.findViewById(R.id.ListView);
for(int i = 0;i<listView.getCount();i++)
{
View v = listView.getChildAt(i);
EditText tempet = (EditText)v.findViewById(R.id.et);
String value = tempet.getText().toString();
}
This code is working fine when Listview is in activtiy layout. But not for dialog.
Try to inflate the layout first. Example:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = ((Activity)context).getLayoutInflater();
View view = inflater.inflate(R.layout.your_layout, null);
alertDialog = builder.create();
ListView listView = (ListView)view.findViewById(R.id.listView);
listViewParadas.setAdapter(YourAdapter);
//Here try obtain editText value...
alertDialog.setView(view);
alertDialog.show();
I hope this helps. Cheers

How to add LinearLayouts to a Parent View in a custom AlertDialog?

So far I have been able to add a few LinearLayouts to another LinearLayout using this bit of code:
setContentView(R.layout.view_editscore);
ViewGroup container = (ViewGroup) findViewById(R.id.container1);
for (String s : PlayersActivity.playersList) {
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.edit_duplicate, null);
TextView tv = (TextView) view.findViewById(R.id.userName);
tv.setText(s);
container.addView(view);
}
And then I am also able to fill a custom AlertDialog with the R.layout.view_editscore View using this code:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle(R.string.editScore);
LayoutInflater in = getLayoutInflater();
builder.setView(in.inflate(R.layout.view_editscore, null));
But for some reason, when I use the setView() to put the R.layout.view_editscore in the AlertDialog it only adds the version that was the "outer shell" before I added each of the LinearLayouts.
I have tried building the custom AlertDialog both before and after I fill the View with the other LinearLayouts but both ways are still yielding the same result.
What could I be doing wrong?

Dynamically set custom AlertDialog content

I have a custom dialog that I set up as a function:
public void customDialog(Bitmap bm, String title){
TextView dialogtext = (TextView)findViewById(R.id.layout_root);
//For above also tried r.id.popup which is the containing file for the layout
ImageView dialogimage = (ImageView)findViewById(R.id.popupimage);
dialogtext.setText(title);
dialogimage.setImageBitmap(bm);
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.layout_root));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(layout);
builder.show();
}
The dialog fails whenever I try to set the XML fields dynamically with a Null Pointer Exception. I'm stumped, any ideas? Do I have to add something to the manifest for a custom dialog?
do this first:
View layout = inflater.inflate(R.layout.popup, (ViewGroup) findViewById(R.id.layout_root));
Then, after you define layout, do this:
ImageView dialogimage = (ImageView) layout.findViewById(R.id.popupimage);
Call findViewByID() on the new layout you created, not on the parent content view.
So two changes: Ordering, and layout.findView not findView

How to open an activity in a popup window?

I have a ListActivity which shows list of items. I prepared another layout for detailed view that contains items' name, address, phone number and image. I want to show these items detailed in a popup window if one is clicked without closing my ListActivity.
How can i do that?
You can use AlertDialog to do this. Look here http://developer.android.com/guide/topics/ui/dialogs.html. And scroll to Creating a Custom Dialog. Example is:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) layout.findViewById(R.id.image);
image.setImageResource(R.drawable.android);
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
You can use a quickAction like the Twitter app or start a new Activity with android:theme="#android:style/Theme.Dialog" specified in your manifest.
Creating dialogs is described on this page: http://developer.android.com/guide/topics/ui/dialogs.html

Categories

Resources