Home » Android » Can I design a customized size activity in Android Studio 3.0?

Can I design a customized size activity in Android Studio 3.0?

Posted by: admin June 15, 2020 Leave a comment

Questions:

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()
    }

}
How to&Answers:

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

Answer:

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.

Answer:

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);
}