0

So, I check in my main_activity class if my screen size is mdpi or hdpi and depending on that I need to start appropriate method within my game activity. I have two tables in my database with mdpi and hdpi images. But I get nothing. Only my blank main activity. What's the problem? Here's my main activity:

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

            Display display = getWindowManager().getDefaultDisplay(); 
            int width = display.getWidth();
            int height = display.getHeight();

            if((width>320) && (width<480)){
                Intent i = new Intent(MainActivity.this, GameDanska.class);
                i.putExtra("myMethod", "nextQuestionMDPI");
                startActivity(i);

            }
            else if((width>480) && (width<720)){
                Intent i2 = new Intent(MainActivity.this, GameDanska.class);
                i2.putExtra("myMethod", "nextQuestionHDPI");
                startActivity(i2);
            }

    }
marjanbaz
  • 1,052
  • 3
  • 18
  • 35
  • check by printing your width value – stinepike Apr 16 '13 at 15:26
  • I suspect that width==0. – Patrick Apr 16 '13 at 15:28
  • have you checked what your screen res really is? You're allowing only widths 321->479 and 481->719. those are very weird resolutions. e.g. my GNex has 1280x720 so falls completely outside the bounds of what you're allowing. – Marc B Apr 16 '13 at 15:28
  • I recommend you to check which density is on the device by doing something like [this, look the answer with a switch](http://stackoverflow.com/questions/5099550/how-to-check-an-android-device-is-hdpi-screen-or-mdpi-screen), and then start the activity you want. – AlexBcn Apr 16 '13 at 15:34
  • My screen is 320X480. I'm 100% sure. It's HTC Magic. @Marc B I'm checking only width, not the hight. I'm checking just to check if it's mdpi or hdpi, and width is enough for that. – marjanbaz Apr 16 '13 at 15:41
  • @marjanbaz: that's fine, but since your if() clauses don't allow 320, your screen will never work. maybe you meant `(width >= 320)` (note the `=`...), but right now 320 is not a permitted value by your logic. – Marc B Apr 16 '13 at 15:41
  • OK, i'll correct that. you are right. – marjanbaz Apr 16 '13 at 15:44
  • @AlexBcn I tried that but I get the same result, my activity gets loaded but my method is not started. – marjanbaz Apr 16 '13 at 15:45
  • OK, I've solved it by placing the switch statement in my game class and by calling appropriate method. Thanks guys. – marjanbaz Apr 16 '13 at 15:51

3 Answers3

1

seems like this can do the trick, you can use the Configuration.screenLayout bitmask.

Example:

if ((getResources().getConfiguration().screenLayout & 
    Configuration.SCREENLAYOUT_SIZE_MASK) == 
        Configuration.SCREENLAYOUT_SIZE_LARGE) {
    // on a large screen device ...

}
Marko Niciforovic
  • 3,561
  • 2
  • 21
  • 28
1

Use a switch as follows

Intent i = new Intent(this, GameDanska.class);

switch (getResources().getDisplayMetrics().densityDpi) {
    case DisplayMetrics.DENSITY_MEDIUM:
        i.putExtra("myMethod", "nextQuestionMDPI");
        startActivity(i);
        break;
    default:
        i.putExtra("myMethod", "nextQuestionHDPI");
        startActivity(i);
        break;
}

If your app is only abailable for MDPI, HDPI, XHDPI remember to put the screen compabilities on you manifest.xml file.

<compatible-screens>
    <screen android:screenSize=["small" | "normal" | "large" | "xlarge"]
            android:screenDensity=["ldpi" | "mdpi" | "hdpi" | "xhdpi"] />
    ...
</compatible-screens>

In your case

 <compatible-screens>
    <screen android:screenSize="small" android:screenDensity="mdpi"/>
    <screen android:screenSize="small" android:screenDensity="hdpi"/>
    <screen android:screenSize="small" android:screenDensity="xhdpi"/>

    <screen android:screenSize="normal" android:screenDensity="mdpi"/>
    <screen android:screenSize="normal" android:screenDensity="hdpi"/>
    <screen android:screenSize="normal" android:screenDensity="xhdpi"/>

    <screen android:screenSize="large" android:screenDensity="mdpi"/>
    <screen android:screenSize="large" android:screenDensity="hdpi"/>
    <screen android:screenSize="large" android:screenDensity="xhdpi"/>

    <screen android:screenSize="xlarge" android:screenDensity="mdpi"/>
    <screen android:screenSize="xlarge" android:screenDensity="hdpi"/>
    <screen android:screenSize="xlarge" android:screenDensity="xhdpi"/>
 </compatible-screens>

API Screen reference

Switch reference

Community
  • 1
  • 1
AlexBcn
  • 2,450
  • 2
  • 17
  • 28
  • I will use only mdpi and hdpi. I don't think anyone has ldpi screen anymore, and for xhdpi I will use hdpi images. I tried it on emulator and it looks good. – marjanbaz Apr 24 '13 at 11:07
  • At 2of april 10.4% uses ldpi [http://developer.android.com/about/dashboards/index.html#Screens](http://developer.android.com/about/dashboards/index.html#Screens) – AlexBcn Apr 24 '13 at 11:21
0

instead of it you can directly put your images inside res->mdpi,hdpi,xhdpi,ldpi folder

for ldpi 320dp: a typical phone screen (240x320 ldpi, 320x480 mdpi, 480x800 hdpi, etc)

for mdpi 480dp: a tweener tablet like the Streak (480x800 mdpi).

for hdpi 600dp: a 7” tablet (600x1024 mdpi).

for xhdpi 720dp: a 10” tablet (720x1280 mdpi, 800x1280 mdpi, etc).

Aamirkhan
  • 5,746
  • 10
  • 47
  • 74