8

Normally, a activity will display a maximum screen. I hope to display a window with width 400px and height 500px, but the following is still display a full window, why?

MainActivity

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
    }
}

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="400px"
        android:layout_height="500px"
        tools:context=".MainActivity">

    <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World OK!"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

</android.support.constraint.ConstraintLayout>

To KeLiuyue

Thanks! but your code doesn't work, the window is full screen after run your code.

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        val m = windowManager
        val d = m.defaultDisplay
        val p = window.attributes
        p.height = dp2px(this, 500f)
        p.width = dp2px(this, 400f)
        window.attributes = p

    }

    private fun dp2px(context: Context, dp: Float): Int {
        val scale = context.getResources().getDisplayMetrics().density
        return (dp * scale + 0.5f).toInt()
    }

}
Cheticamp
  • 61,413
  • 10
  • 78
  • 131
HelloCW
  • 843
  • 22
  • 125
  • 310

3 Answers3

9

In order to achieve what you want you only need to add the following to you application theme:

<!-- Your application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">

    <!-- Your arguments -->
    <item name="android:windowIsFloating">true</item>
</style>

From the documentation:

Flag indicating whether this is a floating window. Maybe a boolean value, such as "true" or "false".

This means that your activity will be placed over the screen with the size you want. Check the above demo for a testing app the I have created.

Demo

App with windowIsFloating


For more info on how to implement, check this StackOverflow answer: https://stackoverflow.com/a/12275009/1574250

André Sousa
  • 1,692
  • 1
  • 12
  • 23
6

Yes, it is possible, and it's very simple! In your manifest change the application theme to

android:theme="@style/Theme.AppCompat.Dialog"

And you should get an application which has dimensions of your root layout.

N. Matic
  • 213
  • 1
  • 8
0

You can use this in your code

WindowManager m = getWindowManager();
Display d = m.getDefaultDisplay();
android.view.WindowManager.LayoutParams p = getWindow().getAttributes();
p.height = dp2px(this,500);
p.width = dp2px(this,400);
getWindow().setAttributes(p);

And convert dp to px

private int dp2px(Context context, float dp) {
    final float scale = context.getResources().getDisplayMetrics().density;
    return (int) (dp * scale + 0.5f);
}
KeLiuyue
  • 8,149
  • 4
  • 25
  • 42