0

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?

Carl
  • 249
  • 2
  • 5
  • 13
  • if your trying to get a border could you not choose one of these android:layout_margin="5dp" android:padding="5dp" or not just put it within another view? – RuAware Aug 07 '13 at 08:34
  • @RuAware Well it's not about the border. It looks like a dialog just fine, but almost fullscreen (about 10px transparency on the sides and botton). If I size it down programmatically I am only able to do this with pixels as I don't understand the workings to convert it to dip. All the answers about it are too vague for me. – Carl Aug 07 '13 at 08:37

3 Answers3

0

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);
}
Jorge Cevallos
  • 3,667
  • 3
  • 25
  • 37
  • Wow, that was quick! I don't know how I would size the view using that. The only thing I've been keen on understanding is: WindowManager.LayoutParams params = getWindow().getAttributes(); params.height = 100; params.width = 550; this.getWindow().setAttributes(params); – Carl Aug 07 '13 at 08:34
  • Just replace it by: params.height = dps2pixels(100, YourActivity.this); params.width = dps2pixels(550, YourActivity.this); to set it to 550x100 dpi. – Jorge Cevallos Aug 07 '13 at 08:48
  • While that works (your edited comment), when used on a device with a low resolution compared to my Nexus 10 the app is still going out of screen while it looks correctly sized on the Nexus 10. It's still behaving like px. – Carl Aug 07 '13 at 08:54
  • I will accept your post as the answer. It is the closest I can get and it still works better than directly setting only pixels. Thank you very much! – Carl Aug 07 '13 at 09:19
0

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);
Renjith
  • 5,783
  • 9
  • 31
  • 42
Sushil
  • 8,250
  • 3
  • 39
  • 71
  • That looks incredible, and might be what I am after. It is alien to me, however, without comments. I will ponder on that code a bit and see if I can utilize it. Thank you. – Carl Aug 07 '13 at 08:40
  • I just picked the code from a place I used it. In case you have trouble understanding, can clarify more. You can ignore most of the calculations though. And do accept the anser in case it solves your purpose. It will help others to locate correct answer quickly. Thanks – Sushil Aug 07 '13 at 08:44
0

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());
user2652394
  • 1,686
  • 1
  • 13
  • 15
  • This is the code I've seen everywhere on SO. Problem is, I'm having a hard time applying this to my activity. I may be an idiot, but(and?) http://stackoverflow.com/a/6625029/2423004 is the way I size it now. Which, is horrible considering the app is for different sized devices. Is there a way for me to apply your posted code to the one I use so that the pixels are instead counted as dip? – Carl Aug 07 '13 at 08:43
  • OK, i got the point, multiple screen-sizes is alway a trouble for android development over ios development. As your problem, I suggest you write separated styles(value-small-medium-large/style.xml) for multiple screen-size or even layouts(layout-small-medium-large). Otherwise you have to deal with the fact that you have to calculate and apply for each case. Hope this helps. – user2652394 Aug 07 '13 at 09:06
  • I thought of that, but my activity doesn't use any xml beyond the viewpager, and from what I understand in styling, it will only let you change the width of text (android:width is only for text). Meaning I might be SOL on this. Thanks for the help, though! – Carl Aug 07 '13 at 09:11