Narrow Dialog when starting new Activity in "Theme.Holo.DialogWhenLarge" mode - android

I feel that I have tried anything to get my dialog fill about 75% (width) of the screen, when it starts. It was always looking great, but since I upgraded to Android 4.4 I cannot get my dialogs to look "normal" but instead they are very small.
When I open a dialog I use the Theme.Holo.DialogWhenLarge as the theme for an activity. All my dialogs are started as activities. I want them to be fullscreen on mobile devices and a dialog (popup) on tablets.
I tried the tips from the following post, but none seem to work:
Resize dialog to take up more screen
Theme.Dialog creates too small screen
Custom dialog too small
This issue is driving me nuts... Any advice?

I don't know what's wrong with me... ;-) It looks like that as soon as I post a question, I find a working solution:
#Override
protected void onStart() {
super.onStart();
// In order to not be too narrow, set the window size based on the screen resolution:
final int screen_width = getResources().getDisplayMetrics().widthPixels;
final int new_window_width = screen_width * 75 / 100;
LayoutParams layout = getWindow().getAttributes();
layout.width = Math.max(layout.width, new_window_width);
getWindow().setAttributes(layout);
}
I'm still not 100% happy with my solution, because I would like to avoid custom code to get the right screen size, but I guess I do have to work more on my layout. But for now, this solution is working for me.

Related

control dimensions of custom alert dialog in different screen orientations

I want to set dimensions for my custom alert dialog, based on screen orientation. My intention is to swap height and width values to keep the box look like being the same size, yet handled various screen sizes of various devices, thanks to Android fragmentation it seems difficult to achieve the same effect on all devices. Android's auto-resizing seems weird to me.
Portrait:
alertDialog.width=screen.width*0.8
alertDialog.height=screen.height=0.5
Landscape:
alertDialog.width=screen.width*0.5;
alertDialog.height=screen.height*0.8
Please note that the Custom Alert Dialog must use the same code and support Android versions from JellyBean (at least 4.2) to Nougat (7).
i am using android.support.v7.AlertDialog (the latest available thing)
i assume android.app.Dialog should be avoided now (being old)
Also, i need a black border and white background for the same. i am unable to achieve same effect on all devices, (i need transparency for rounded corners)
i have used android:windowMinWidthMajor and android:windowMinWidthMinor but they affect both layouts (portrait and landscape) and seem to be ignored if content does not fit within the specified constraints.
I wish there was android:windowMaxWidthMinor & android:windowMaxWidthMajor
This is something that I've used for resizing specific views on a page:
myView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
item.height = myView.getHeight();
});
Applying it to what you want to do:
myView.getViewTreeObserver().addOnGlobalLayoutListener(() -> {
LayoutParams params = (get the layout params according to the layout);
params.width = myView.getWidth() * 0.5;
params.height = myView.getHeight() * 0.8;
myView.setLayoutParams(params);
});
You have to use the listener because the object will be added to the view tree, will be measured and prepared for display, but won't yet be displayed, so you can change its dimensions before it is visible.
Update:
I could achieve it by setting android:layout_centerInParent="true" for my Layout. and layout values for each component in the style to
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
to achieve the center alignment.
XML Code available at https://github.com/computingfreak/CFTest/blob/master/app/src/main/res/layout/alert_popup.xml
Java Code available at
https://github.com/computingfreak/CFTest/blob/master/app/src/main/java/net/cf/sms/cftest/MainActivity.java
I wonder this line of code is redundant
view.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
and thus left it to Android System to decide dimensions and resizing based on content. It works for my case, but will fail in case of long text, maybe some kind of Scroll Layout will help. Cheers!

Android set layout depending screen size [duplicate]

This question already has answers here:
How to support different screen size in android
(9 answers)
Closed 8 years ago.
First, I must say that i am beginner. But have made an application, that works on my Nexus 7 tablet. But i want to make it to work in all devices. Yes, I know, that question has been there before. But I want your opinion, will it work if etc...
public void setLayout()
{
DisplayMetrics dm = new DisplayMetrics(); //avoid this it just gets screen size.
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width=dm.widthPixels;
int height=dm.heightPixels;
int dens=dm.densityDpi;
double wi=(double)width/(double)dens;
double hi=(double)height/(double)dens;
double x = Math.pow(wi,2);
double y = Math.pow(hi,2);
double ss = Math.sqrt(x+y);
if(ss <7) //n7 If screen size is nexus 7, application will set nexus7 layout
{
setContentView(R.layout.screen7);
}
else
{
setContentView(R.layour.default); //user is not using nexus 7 so we will set default layout
}
But i need to run this method in every activity, that need to set layout. Like in MainActivity, instead of using
setContentView(R.layout.activity_main);
I want to use setLayout(); //so method checks what screen size user is using so we can show the best layout available.
I hope you understand what i want to make ;)
Thank you :)
Please let me know, if you want LogCat error.
I just wanted to say that this way you will rediscover fire. You are taking the wrong path. u don't need to do these things in java. You can do all this in just XML files , android than does it for you. Try to use relational positioning of views , not exact pixels. Android has a nice measure called dp and it signifies to some extent as if u are telling the system where to position things in percentage.
Here is a link that helped me understand better(ignore the small dimensions):
http://android-developers.blogspot.com/2011/07/new-tools-for-managing-screen-sizes.html
Another advice i have to give you is try to use RelativeLayout and LinearLayout , you can use match_parent , wrap_content or u can use layout_gravity atribute for your positioning.

Android: intelligent way of sizing UI components for all screens?

I've been having trouble finding a generic way to size UI components so they look good on all devices. I have some ideas but could use any feedback on them or suggestions for better strategies.
Take a simple situation like a dialog with a single EditText which a user will enter a fairly long String into. On smaller devices I would like the EditText to be as wide as possible (because its likely that the maximum size possible on these devices isn't all that large). However, if I set the width of this EditText to FILL_PARENT, the dialog will look silly on tablets, because it will be much, much wider than it needs to be. Also, setting the width to some hard coded DIP value, like 500, isn't great because it doesn't maximize available space on smaller devices, won't expand to take up additional space if the device is rotated, and could be an issue on devices narrowers than 500 DIP.
What I would like to do is approximate the behavior FILL_PARENT by hardcoding the width of the EditText to of the screen's width, but to prevent the EditText from growing to wide on large devices by not allowing the width to be larger than some DIP value, like 500.
I have encountered 2 problems with this strategy:
1: If I set the EditText's width to the screen's width, the EditText doesn't fit on the screen (as shown in the below image). I fixed this by setting the width to 90% of screen width, but this is questionable (what if the device's dialogs have larger margins around them?).
2: My app sets android:configChanges flag in its application manifest such that dialogs don't get dismissed on rotation. But this means that dialogs don't get resized on rotation. So, if the device rotates from landscape to portrait while the dialog is visible, then the EditText will be too wide for the screen; and if it goes the opposite direction, then the EditText will not take up all the available space. I guess this would be fixable by relaying out my dialog on rotate, but it seems painful/ugly for my Activity to have to keep track of the visible Dialog and force it to relayout on rotation (particularly because each dialog would then have re-populate any field values when after relayout).
So, any thoughts on improving my solution, or on alternatives?
class SampleDialog extends Dialog
{
public SampleDialog(Context context)
{
super(context);
setContentView(getContentView());
}
private View getContentView()
{
TextView label = new TextView(getContext());
label.setText("A label:");
int fiveHundredDips = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 500, activity.getResources().getDisplayMetrics());
Display display = getWindowManager().getDefaultDisplay();
float width = .9f * display.getWidth();
Math.min(width, fiveHundredDips);
LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams((int) width, LinearLayout.LayoutParams.WRAP_CONTENT);
EditText editText = new EditText(getContext());
editText.setLayoutParams(linearParams);
LinearLayout layout = new LinearLayout(getContext());
layout.addView(label);
layout.addView(editText);
return layout;
}
}
}
You could use different folders in your resources folder, which define different qualifiers for different devices. You can then specify say on a small device you would like a dimension to be 'x' big, and on a tablet to be 'x' big.
Another solution would be to programatically detect if the device is a Tablet, and perform actions based on that.
I would recommend you look into resource qualifiers, which are a very powerful tool in Android.
Take a look at the android:minWidth property. It's available on TextView as well as others. It seems the minWidth (set using dip units) would be a good option for you. You can make the text boxes large enough but not too large.
These values can be set programmatically. You might also find the following code useful in determining what values you want to use for the minWidth:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
Log.d("density", metrics.density);
Log.d("width dip", (int) (metrics.widthPixels / metrics.density));
Log.d("height dip", (int) (metrics.heightPixels / metrics.density));

PopupWindow Width and Height

OK, so I am starting to get a hang of building Android apps, well at least as much a programmer can after a few days - I am proud of what I have learned so far.
Anyways, I want to force login on the main activity - this I am doing by fetching a SharedPrefernece and than checking if that piece of information is null and than getting a PopupWindow which holds the "login" fields and options.
This PopupWindow has a Flipper inside, which is fine and I got working fine when the certain options are choosen.
I am having problems displaying this PopupWindow to just be the size of the content (wrap_content) as when I set the PopupWindow.setAtLocation()
Now, here is what I have been trying to do to get the size of the popup - as mentioned a few times on here:
popup.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
final PopupWindow pw = new PopupWindow(popup,popup.getMeasuredHeight(),popup.getMeasuredWidth(), true);
Note that popup is the inflator of the actual layout of the Popup, pw is the actual PopupWindow object.
Now, I want to get the actual size of just the popup window (so that way it isn't streched out over the page, but rather just in the center looking like a normal popup should.
Also, with the ViewFlipper. I want it to update the size of the popup when it switch pages (so the page should be sizing up and down per page) is there a way to make this work as well? I tried pw.update() but that didn't work out very well.
You want to measure the actual layout you will be adding to your PopupWindow. I just solved a similar problem of putting a ListView inside a PopupWindow. The trick is to override onMeasure() in your View. Remeasure that ViewFlipper everytime it changes.
#Override
public void onMeasure(int x, int y)
{
super.onMeasure(x, y);
if(x == View.MeasureSpec.UNSPECIFIED && y == View.MeasureSpec.UNSPECIFIED)
{
//measure your child views here
//tell the view it has been measured
this.setMeasuredDimension(width, height);
}
}

Screen Size Honeycomb Menu android

Hi I am trying yo get the screen size of screen minus the menu bar at the bottom.
I know I could just subtract a constant number according to the resolution of the device but it is a super ugly hack. I am sure google people are not dumb enough to forget to give the user a function to get the height of the bottom menu bar so I can subtract it from the full screen size
This is a similar post.
Size of android notification bar and title bar?
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
what_i_need =metrics.heightPixels-buttomMenuHeight();
I need buttomMenuHeight();
i could not find it in the API. I don't really care about the backward comparability at this point
Thanks
Why not use the height of the activity's top-level View? e.g. if your activity contains a full-height LinearLayout, use its height, so you don't have to know the menu height, or noticiation bar height.
I think you're treading a dangerous road doing this kind of calculation, as you don't know what the future layouts for Android might be, or how they might differ on Google TV, or whatever.
Try calling getHeight() after onLayout. The view is not sized on inflate.
EDIT:
Or since Activity does not have onLayout you might wait for onSizeChanged.
(Reference How to know when activity is laid out?)
I am drawing image in native code and I need to pass the size of the screen to C.
I tried to get the Height by getHeight() of the view but that returns 0 always even after setContentView is called.
RelativeLayout masterLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT);
masterLayout.setLayoutParams(params);
setContentView(masterLayout);
when I try masterLayout.getHeight(), it returns 0.
I expected to get the full screen height as I said fill_parent.

Categories

Resources