facebook twitter hatena line email

Android/画面サイズ

提供: 初心者エンジニアの簡易メモ
移動: 案内検索
import android.graphics.Point;
import android.view.WindowManager;
import android.view.Display;
// 画面幅サイズ(ステータスバーを含まない)
Point point = new Point();
// Android 3.2~
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    WindowManager wm = (WindowManager) context.getSystemService(WINDOW_SERVICE);
    Display disp = wm.getDefaultDisplay();
    disp.getSize(point);
}
Log.i("test", "x=" + point.x);
Log.i("test", "y=" + point.y);
// 画面幅サイズ(ステータスバーを含む)
WindowManager windowManager = (WindowManager) context.getSystemService(WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
    // Android 4.2~
    try {
        Method getRealSize = Display.class.getMethod("getRealSize", Point.class);
        getRealSize.invoke(display, point);
        Log.i("test", "width=" + point.x);
        Log.i("test", "height=" + point.y);
    } catch (Exception e) {
        e.printStackTrace();
    }
} else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
    // Android 3.2~
    try {
        Method getRawWidth = Display.class.getMethod("getRawWidth");
        Method getRawHeight = Display.class.getMethod("getRawHeight");
        int width = (Integer) getRawWidth.invoke(display);
        int height = (Integer) getRawHeight.invoke(display);
        point.set(width, height);
        Log.i("test", "width=" + point.x);
        Log.i("test", "height=" + point.y);
    } catch (Exception e) {
        e.printStackTrace();
    }
}

参考

http://qiita.com/a_nishimura/items/f557138b2d67b9e1877c

http://techbooster.org/android/hacks/16066/

タイトルバーを削除

Activityを使う場合

MainActivity.java

- public class MainActivity extends ActionBarActivity {
+ public class MainActivity extends Activity {

AndroidManifest.xml

- android:theme="@style/AppTheme"
+ android:theme="@android:style/Theme.NoTitleBar"

参考:https://qiita.com/Mocacamo/items/9849d5590d2109eaf4b8

AppCompatActivityを使う場合

MainActivity.java

public class MainActivity extends AppCompatActivity {

AndroidManifest.xml

- android:theme="@style/AppTheme"
+ android:theme="@style/Theme.AppCompat.Light.NoActionBar"

参考:https://qiita.com/furu8ma/items/4328c793250b10313cd7

タイトル&ステータスバーを削除

AppCompatActivityを使う場合

@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);
        if (hasFocus) {
            hideSystemUI();
        }
    }

    private void hideSystemUI() {
        View decorView = getWindow().getDecorView();
        decorView.setSystemUiVisibility(
                View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
                | View.SYSTEM_UI_FLAG_FULLSCREEN
                | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_STABLE
                | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
                | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
        );

        decorView.setOnSystemUiVisibilityChangeListener
                (new View.OnSystemUiVisibilityChangeListener() {
                    @Override
                    public void onSystemUiVisibilityChange(int visibility) {
                        // Note that system bars will only be "visible" if none of the
                        // LOW_PROFILE, HIDE_NAVIGATION, or FULLSCREEN flags are set.
                        if ((visibility & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
                            Log.d("debug","The system bars are visible");
                        } else {
                            Log.d("debug","The system bars are NOT visible");
                        }
                    }
                });
    }

参考:https://akira-watson.com/android/theme-notitlebar.html