Home » Android » android – How do I use getWindowManager() by another java file

android – How do I use getWindowManager() by another java file

Posted by: admin May 14, 2020 Leave a comment

Questions:

I want to use getWindowManager() in my MainActivity, but I don’t want to write this method directly in it.

Here is my MainActivity code

public class MainActivity extends Activity {
@Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  String d= new DisplayMessageActivity().getWeithAndHeight(d);
 }

and here is my DisplayMessageActivity code

public class DisplayMessageActivity extends Activity {

@SuppressLint("NewApi")
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Intent intent = getIntent();
    ....
    ....
    setContentView(textView);
}
public String getWeithAndHeight(String url) {  
    DisplayMetrics dm = new DisplayMetrics();  
    *getWindowManager().getDefaultDisplay().getMetrics(dm);*
    int width = dm.widthPixels;
    int height = dm.heightPixels;  
    String w=new String(""+width);
    String h=new String(""+height);
    url=url+"&23=w%3A"+w+"%20h%3A"+h+"%20d%3A"; 
    return url;
}  
}

my code break at getWindowManager(), please tell me why. Thank you very much.

How to&Answers:
public String getWeithAndHeight(Context context, String url) {  
    DisplayMetrics dm = new DisplayMetrics();  
    ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
    //....
}   

Answer:

Why don’t you create static method:

public static String getWeithAndHeight(Activity activity, String url) {  
    DisplayMetrics dm = new DisplayMetrics();  
    activity.getWindowManager().getDefaultDisplay().getMetrics(dm);
    int width = dm.widthPixels;
    int height = dm.heightPixels;  
    String w=new String(""+width);
    String h=new String(""+height);
    url=url+"&23=w%3A"+w+"%20h%3A"+h+"%20d%3A"; 
    return url;
} 

Your DisplayMessageActivity is not created successuffly. You cannot just create new activity object to use all of its methods.

Answer:

Way too late to the party here, posting the answer as someone might be able to benefit from it. you can get WindowManager()as a static reference using the context like this.

  DisplayMetrics displayMetrics = new DisplayMetrics();
        WindowManager windowManager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (windowManager != null) {
            windowManager.getDefaultDisplay().getMetrics(displayMetrics);
        }

now you can use display matrix to get width,height etc

Answer:

Answer of your first question “I want to use getWindowManager() in my MainActivity, but I don’t want to write this method directly in it.” You can write BaseActivity extends Activity & here you can define the code for

public String getWeithAndHeight(Context context, String url) {  
    DisplayMetrics dm = new DisplayMetrics();  
    ((Activity)context).getWindowManager().getDefaultDisplay().getMetrics(dm);
    //....
}   

And for DisplayMessageActivity & MainActivity you can extends with BaseActivity.

Answer:

Follow two steps:
First:
extend Activity class

class Example extends Activity

Second: Use these lines

DisplayMetrics displayMetrics = new DisplayMetrics();
WindowManager windowmanager = (WindowManager)getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
windowmanager.getDefaultDisplay().getMetrics(displayMetrics);
int deviceWidth = displayMetrics.widthPixels;
int deviceHeight = displayMetrics.heightPixels;

Answer:

If you are using Kotlin, this code might be helpful to you:

    private fun getDeviceWidthAndHeight() {

        val metrics = DisplayMetrics()
        val windowManager = context.getSystemService(Context.WINDOW_SERVICE) as WindowManager
        windowManager?.defaultDisplay?.getMetrics(metrics)
        viewWidth = metrics.widthPixels
        viewHeight = metrics.heightPixels
    }