Question

I've searched and found an exceptional amount of answers to this, but none that will show how they are inserted into the activity.

My activity has a tabsadapter and viewpager (fragmentpageradapter). That's it. And I've set it to be styled as a dialog, like this

    <style name="Theme.PopupTheme" parent="android:Theme.Holo.Light.Dialog">
    <item name="android:windowIsFloating">false</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowSoftInputMode">stateAlwaysHidden</item>
    <item name="android:windowActionModeOverlay">true</item>
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowNoTitle">false</item>
    <item name="android:windowActionBar">true</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="android:padding">2sp</item>
    <item name="android:divider">@drawable/transparent</item>

Now, this works just fine except that it takes up the full size of the display with barely any transparency on the sides and bottom. There is no layout for this activity, just a viewpager xml

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/viewPager"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

Because of this I'm tied to sizing the app programmatically. I found that I can only manage this (with my level of skill) by setting it with pixels. Which can look good if it was meant for a certain device, but the app looks atrocious on my Nexus 10 compared to my S3 when and vice-versa depending on the amount of pixels I set.

How can I do this programmatically with dip?

Was it helpful?

Solution

I don't think there's a way to set it directly in dip.

You can use this method to convert from dip to px and use the result to size the view.

private static float scale = 0;
...
public static int dps2pixels(int dps, Context context) {
    if (0 == scale) {
        scale = context.getResources().getDisplayMetrics().density;
    }
    return (int) (dps * scale + 0.5f);
}

OTHER TIPS

There is an existing utility method built called TypedValue.applyDimensions(int, float, DisplayMetrics) that does this.

Here's how to use it:

// returns the number of pixels for 123.4dip
int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
(float) 123.4, getResources().getDisplayMetrics());

There's a list of other types that you can use with it including COMPLEX_UNIT_SP, COMPLEX_UNIT_PT all found in the page I linked above. If you exclude the (int) typecast, you'll get the floating point number.

I encountered the same problem and found this through poking around the code.

Convert the pixel to dip and vice versa then apply to your calculation depending the screen-size. Try this, it should help:

Pixel to dip:

float scale = context.getResources().getDisplayMetrics().density;
int pixel = (int) (pixel * scale + 0.5f);

Dip to pixel:

int dip = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dip,
                getApplicationContext().getResources()
                        .getDisplayMetrics());

The code below is to add a progressbar progamatically as per screen size. May be you can get some hint from this code:

mProgressBar = (ProgressBar) findViewById(R.id.progress);

DisplayMetrics metrics = this.getResources().getDisplayMetrics();
int displayWidth = metrics.widthPixels;
int displayHeight = metrics.heightPixels;
int width = 0;
int topMargin = 0;
int leftRightMargin = 0;

float deviceWidth = (float)displayWidth/metrics.xdpi;
float perct = deviceWidth*76/100;
float finalWidth = perct*metrics.xdpi;

float deviceHeight = displayHeight/metrics.ydpi;

if(metrics.density == 1)
    perct = deviceHeight*16/100;
else if(metrics.density == 2)
    perct = deviceHeight*20/100;
else if(metrics.density == 1.5)
    perct = deviceHeight*18/100;

float finaltopMargin = perct*metrics.ydpi;

perct = deviceWidth*12/100;
float finalLeftMargin = perct*metrics.xdpi;

width = (int)finalWidth;
topMargin = (int)finaltopMargin;
leftRightMargin = (int)finalLeftMargin;

android.widget.RelativeLayout.LayoutParams params =  (android.widget.RelativeLayout.LayoutParams) new  android.widget.RelativeLayout.LayoutParams(width, width);

//params.setMargins(leftRightMargin, topMargin, 0, 0);
mProgressBar.setLayoutParams(params);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top