-2

sory for newbie question i have 2 .java main.java for switchcase

public boolean onOptionsItemSelected(MenuItem item){
    switch (item.getItemId()) {
    case R.id.hint:

        coba = new GameBoard();
        coba.deklarasigambar();


        break;
........................

and other .java is going to get width screen

public void deklarasigambar(){
    DisplayMetrics met = new DisplayMetrics();
    getWindowManager().getDefaultDisplay().getMetrics(met);
    layar = met.heightPixels;
    Toast.makeText(getApplicationContext(), ""+layar, 3);

}

i got force close...but when i put

DisplayMetrics met = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(met);
layar = met.heightPixels;
Toast.makeText(getApplicationContext(), ""+layar, 3);

in

 case R.id.hint:

i got the value, and sorry for bad english :(

1 Answers1

0

I use this

public static int getScreenWidth(Context ctx) {
        Configuration conf = ctx.getResources().getConfiguration();
        if (android.os.Build.VERSION.SDK_INT >= 13)
        {
            return conf.screenWidthDp;
        } else
        {
            Display display = ((WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();

            @SuppressWarnings("deprecation")
            return display.getWidth();
        }
    }

I call this from any actvity using MyClass.getScreenWidth(this) where this is the Activity's context (from a fragment call getActivity() )

Hope this helps

Ruben Weerts
  • 313
  • 1
  • 6