My Dialog layout contains:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
The dialog still appears as small as its contents.
I had to do:
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
... within the the onCreateDialog callback in order for it to fill the screen.
I can't find any reference to this behaviour on the google dev site.
Answering my own question after much experimenting. Issue has to do with the usage of a Dialog class as opposed to a AlertDialog.
In short, it didn't work because I didn't use the right tools and approach. Solution was to either: continue using a Dialog class but then force it by using d.getWindow() and modifying the dimensions from there OR to use an AlertDialog and inflating the layout as such:
LayoutInflater inflater = (LayoutInflater) getLayoutInflater();
View layout = inflater.inflate(R.layout.about, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
This linked helped a lot: Why does LayoutInflater ignore the layout_width and layout_height layout parameters I've specified?
Related
I have an android dialog box and I want to make the Title box smaller preferably to single line because right now it is too large. I been searching around for a fix and can not find it, this is how my dialog title box looks, as you can see I only have 1 line and a lot of padding on top and bottom, how can I fix this?
I have been able to programmatically fix some things by using this
TextView Dialog_Title = (TextView)dialog.findViewById(android.R.id.title);
Dialog_Title.setPadding(2,2,2,2);
Dialog_Title.setMaxHeight(1);
Dialog_Title.setTextSize(18);
Dialog_Title.setTextColor(Color.BLACK);
Any suggestions on this
I think you can use the custom layout for the dialog.
If you want a custom layout in a dialog, create a layout and add it to an AlertDialog by calling setView() on your AlertDialog.Builder object.
By default, the custom layout fills the dialog window, but you can still use AlertDialog.Builder methods to add buttons and a title.
For example, here's the layout file for a dialog.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/header_logo"
android:layout_width="match_parent"
android:layout_height="64dp"
android:scaleType="center"
android:background="#FFFFBB33"
android:contentDescription="#string/app_name" />
</LinearLayout>
To inflate the layout in your DialogFragment, get a LayoutInflater with getLayoutInflater() and call inflate(), where the first parameter is the layout resource ID and the second parameter is a parent view for the layout. You can then call setView() to place the layout in the dialog.
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
// Get the layout inflater
LayoutInflater inflater = getActivity().getLayoutInflater();
// Inflate and set the layout for the dialog`enter code here`
// Pass null as the parent view because its going in the dialog layout
builder.setView(inflater.inflate(R.layout.dialog_signin, null))
for further guide on this refer to 'Dialog' description at developer.android.com
link is provided below
http://developer.android.com/guide/topics/ui/dialogs.html#CustomLayout
Basically I have a RecyclerView in my AlertDialog (I've tried both the AppCompat one and the one from here) and when the list only has a few items the dialog is still as tall as it can be. Is there something I can do to fix this?
My layout is this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></android.support.v7.widget.RecyclerView>
</LinearLayout>
and I'm adding it to the AlertDialog like this:
LayoutInflater myLayout = LayoutInflater.from(context);
final View dialogView = myLayout.inflate(R.layout.list, null);
dialog.setView(myLayout);
How can I make it wrap_content?
Here is another similar question I found. Using #PiyushMishra's solution:
alertDialog.show();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(alertDialog.getWindow().getAttributes());
lp.width = 150;
lp.height = 500;
lp.x=-170;
lp.y=100;
alertDialog.getWindow().setAttributes(lp);
I was able to control the dialog's size. You could get creative with this and determine the size needed based on the # of list items.
I'm sorry I'm unsure how to get the Dialog to wrap_content. You may have some luck using a custom dialog instead of AlertDialog.
I've an activity.java file in which my setContentView(R.layout.x); Now,I've an y.xml in which I've an Linear Layout,I've to attach an onclick() method to my view.
Attaching onclick() has to be in my activity.java file, How do I include y.xml.
I tried this,
1. layout = (LinearLayout) findViewById(R.layout.y);
eView = (EditText)layout. findViewById(R.id.editview);
2. eView = (EditText)findViewById(R.id.editview);
but both gives my null pointer exception, How do I include my editText
Update
final LayoutInflater lyInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
showLinearLayout = (LinearLayout) lyInflater.inflate(R.layout.y, null);
showView = (EditView) showLinearLayout.findViewById(R.id.edittext);
You can use inflation as shown below:
final LayoutInflater lyInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout yLayout = (LinearLayout) lyInflater .inflate(
R.layout.y, null);
eView = (EditText)yLayout.findViewById(R.id.editview);
So you won't get exception anymore. Hope it helps.
LayoutInflater is used to instantiate layout XML file into its corresponding View objects.in other words, it takes as input a XML file and builds the View objects from it.
in your scenario, you have to use LayoutInflater. read this article.
Ram.
If you want to include y xml file into your x xml file then follw this steps.
I am assuming that you want to include Linear Layout into your Activity on click of onclick() method of the button or whatever, then add the Linear Layout into your x xml file and add the android:visibility="gone" so at begin you can not show the linearlayout.
<LinearLayout
android:id="#+id/history_value_body"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone" > <<<<<<<<<<< HERE
----------------------------
-----------------------------
</LinearLayout>
Now, From the java class make it visible when needed, in your case into onclick method.
Like...
linear.setVisibility(View.VISIBLE); // linear is the object of your Linearlayout
If any prob then ask me.
Good Luck.
If I understand the question correctly, your Activity uses x.xml, and you also want to include another layout that is defined in y.xml.
You can do so using the <merge> or <include> tags, as described in the documentation.
Alternately, you can use a ViewStub to conditionally inflate another layout in a given place in a layout. For example, you can include a ViewStub tag in x.xml, and inflate y.xml in the same spot in the view hierarchy. Then, you may attach any click listeners you need (by using findViewById()).
You can use addView method of ViewGroup.
addView(layout, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
what i am trying to do:
Create a custom Alert Dialog. Buttons just like any Alert Dialog but above are two TextEdit input boxes. I don't want to create a custom Dialog but a customized Alert Dialog
Here is what I am trying #3:
http://developer.android.com/guide/topics/ui/dialogs.html
It says:
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!");
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
Documentation says:
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
where the first parameter is the layout resource ID and the second is the ID of the root View.
Problem is I don't know what the layout root is? this is a dialog I am going to kick of in an Activity. Should I use the layout id if the activity? Is layout_root pulled out of a hat?
Also tried:
View layout = inflater.inflate(R.layout.my_custom_layout,
(ViewGroup) findViewById(android.R.id.content).getRootView());
result null pointer.
Even though an older question, this article might be useful for others who search for this answer:
Layout Inflation as Intended:
If you’ve ever written something like the following code using
LayoutInflater in your Android application:
inflater.inflate(R.layout.my_layout, null);
PLEASE read on, because you’re doing it wrong and I want to explain to
you why.
...BUT...
Every Rule Has An Exception
There are of course instances where you can truly justify a null
parent during inflation, but they are few. One such instance occurs
when you are inflating a custom layout to be attached to an
AlertDialog. Consider the following example where we want to use our
same XML layout but set it as the dialog view:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View content = LayoutInflater.from(context).inflate(R.layout.item_row, null);
builder.setTitle("My Dialog");
builder.setView(content);
builder.setPositiveButton("OK", null);
builder.show();
The issue here is that AlertDialog.Builder supports a custom view, but
does not provide an implementation of setView() that takes a layout
resource; so you must inflate the XML manually. However, because the
result will go into the dialog, which does not expose its root view
(in fact, it doesn’t exist yet), we do not have access to the eventual
parent of the layout, so we cannot use it for inflation. It turns out,
this is irrelevant, because AlertDialog will erase any LayoutParams on
the layout anyway and replace them with match_parent.
The article has an explanation on why you should supply a parent ViewGroup in most other cases than Dialog building.
Ok. The root view in the documentation refers to the element in the custom layout. So the custom layout will have an outermost view called the root view. You need to give this an Id and than you can pass it in as shown. So first argument is the custom view id, the second argument is id of the root layout element within the custom view.
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
So in this example given in the documentation above, R.id.layout_root refers to the id you give to say for example the outermost LinearLayout within the custom_dialog layout.
Have you tried this?
View layout = inflater.inflate(R.layout.custom_dialog,null);
builder.setView(layout);
layout.getRootView();
Should give LinearLayout.
I made a custom alert dialog box to be displayed at the end of my game so that the player can enter its name to save it. The problem is when I call show() on the dialog appears but it's not vertically centered! It's a bit lower than it should and no matter what properties I set in the xml or when using setGravity().
I think this is the same problem as the one mentioned here, but no one gave a proper answer.
Thanks for your help.
For more details, here is my code:
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));
builder = new AlertDialog.Builder(this);
builder.setView(layout);
newRecDialog = builder.create();
And here is the code of the first element of the XML layout of newrecord.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:gravity="center"
android:padding="10dp"
android:baselineAligned="true">
Here is the output screenshot:
(source: free.fr)
The bug is described here. The AlertDialog is reserving space for the title/icon panel even where there is neither a title nor an icon.
The fix is, I think, quite simple: it should set the top panel layout to GONE, just as it does for the button panel in the event of there being no buttons. Until that's done, the only workaround is to implement your own Dialog subclass.
If you implement your own dialog the line requestWindowFeature(Window.FEATURE_NO_TITLE) hides the title panel and the dialog is centered on the screen. Maybe it works with a AlertDialog too.
You can use an Activity instead of a custom alert for this. You have to set the theme of activity as dialog in the android manifest file:
android:theme="#android:style/Theme.Dialog"
And you can adjust the activity xml layout as per your need.
android:theme="#android:style/Theme.Dialog"
If you are dealing with any height and width attributes, you must make sure not to alter the height, since it will alter the position, here is a sample.
myProgressDialog.show();
float widthPecent = 0.60f;
//order matters
DisplayMetrics displayMetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displayMetrics);
int displayWidth = displayMetrics.widthPixels;
//dont do any adjustments to the height. ************************** <<<<<<<<<<
WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams();
layoutParams.copyFrom(myProgressDialog.getWindow().getAttributes());
int dialogWindowWidth = (int) (displayWidth * widthPecent);
layoutParams.width = dialogWindowWidth;
//dont do any changes to the height. ************************** <<<<<<<<<<
myProgressDialog.getWindow().setAttributes(layoutParams);
layoutParams.height = dialogWindowHeight; //comment or remove this line.
set the attribute on your AlertDialog as android:gravity="center" or programmatically as setGravity(Gravity.CENTER). This Gravity is for your layout only not for the display of your mobile. if you use Custom Title its did not look like center vertical.
Not really an answer, but I was having a similar issue. It appears that it is centered, but it is assuming the AlerterDialog has a title set. In my case, I just set a title.
Can you try using AlertDialog.Builder.setCustomTitle(View); instead of setView? We use it because alert dialog looks a bit better than dialog with empty title.
you should use the following line in the xml
firstly remove that padding line from your xml and after that
android:layout_gravity="center"
after this your dialog will appear in center and if you are using the margin from left etc than remove that and also if you are using that
android:layout_width="fill_parent"
than change it with the
android:layout_width="wrap_content"
after that your dialog will be appear in the center.
Try:
newRecDialog.getWindow().getAttributes().gravity = Gravity.CENTER;
You need to disable the window title.
First create a custom style for your dialog (Make sure to use the parent theme that fits your needs):
<style name="CustomDialog" parent="#android:style/Theme.Dialog">
<item name="android:windowNoTitle">true</item>
</style>
Then apply your style to the dialog builder:
AlertDialog.Builder builder;
LayoutInflater inflater = (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.newrecord,(ViewGroup)findViewById(R.layout.shoot));
builder = new AlertDialog.Builder( new ContextThemeWrapper(this, R.style.CustomDialog));
builder.setView(layout);
newRecDialog = builder.create();