Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to Change Screen Resolution Programmatically?

Let me tell you my problem. I want to change my screen resolution. I can change it in an application but it changes only application's screen. I wanna set system's resolution so it won't be important which application is running on front. My device's resolution is set as 1280 * 720 p. Can I make it 1260 * 680? If it requires to make changes in Android source code, I can. Just tell me where to change. Waiting for your help.

like image 346
sjor Avatar asked Mar 12 '26 10:03

sjor


2 Answers

This thread on xda-developers should set you on the right track.

like image 151
fredley Avatar answered Mar 14 '26 23:03

fredley


Searching too a valid answer to this, but I have a lead to the solution : WARNING Experimental buggy stuff :

/*
Requires android.permission.WRITE_SETTINGS
and android.permission.WRITE_SECURE_SETTINGS, thus it requires the app to be a system app.
 */
public void changeResolution(int x, int y){
    try { Class c = Class.forName("android.os.ServiceManager");
        try { Method method = c.getDeclaredMethod("checkService", String.class);
            try {
                IWindowManager mWindowManager = IWindowManager.Stub.asInterface((IBinder) method.invoke(null,Context.WINDOW_SERVICE));
                try { mWindowManager.setForcedDisplaySize(Display.DEFAULT_DISPLAY,x,y);
                } catch (RemoteException e) {e.printStackTrace();}
            }  catch (IllegalAccessException e) {e.printStackTrace();}
            catch (InvocationTargetException e) {e.printStackTrace();}
        } catch (NoSuchMethodException e) {e.printStackTrace();}
    } catch (ClassNotFoundException e) {e.printStackTrace();}
}

Add a reduced AIDL version of IWindowManager to your project : /app/src/main/aidl/android/view/IWindowManager.aidl

package android.view;
interface IWindowManager
{
    boolean startViewServer(int port);   // Transaction #1
    boolean stopViewServer();            // Transaction #2
    boolean isViewServerRunning();       // Transaction #3

    void setForcedDisplaySize(int displayId, int width, int height);
    void clearForcedDisplaySize(int displayId);
    void setForcedDisplayDensity(int displayId, int density);
    void clearForcedDisplayDensity(int displayId);
}

The app will require to be in the system apps folder. It does something for sure, but right now it also lead to severe bugs. Rebooting seems to cancel changes.

Waiting for feedback on this.

like image 35
J.Jacobs-VP Avatar answered Mar 15 '26 00:03

J.Jacobs-VP



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!