Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

android

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

}
like image 929
HelloCW Avatar asked Oct 31 '18 01:10

HelloCW


2 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

like image 99
André Sousa Avatar answered Oct 29 '22 06:10

André Sousa


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.

like image 25
N. Matic Avatar answered Oct 29 '22 06:10

N. Matic