Display Activity on part of screen

Have you ever wondered what is the best way to display whole new layout, with lot of new widgets but you didn’t want to close the previous screen and be able to quickly get back to it? I did, in my currently developed app ProgressUpp I needed screen responsible for adding new progresses in main list, but I wanted It to not feel like I leave the list screen, and separate its features. The first idea was to create Dialog, but to be honest I don’t like Dialogs xD And what is more important, Dialogs do a lot of mess as I treat them more like Widget not like View (in design pattern meaning). So to separate bussiness logic and create intended feeling of It, I remembered the look of Contact Intent which appears on part of screen quickly and after taking actions on It, disappears quickly. It doesn’t leave any mess in your primary Activitys bussiness logic class (ViewModel, Presenter etc.).

 

 

 

 

 

So how to make one like this?

  1. Firstly, in your root Activity layout, You must set your width and height as you wish, for me It will be height set as wrap_content as I want it to look similar to Contacts Intent, that way It will take as much place as it need, leaving empty space above It.
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
  2. What you need next is to set same params as windows LayoutParams in your Activitys class.
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    setupParams()
    setContentView(R.layout.activity_add)
    setup()
    }
    ...
    private fun setupParams() {
    window.setDimAmount(0.5f)
    val params: WindowManager.LayoutParams = window.attributes
    params.height = WindowManager.LayoutParams.WRAP_CONTENT
    params.gravity = Gravity.BOTTOM
    window.attributes = params
    }

    In the code above I have set height as WRAP_CONTENT because in my case the height won’t be fullscreen and it determines the size of window, but it may be also width.

  3. What is most important is to change the theme of your Activity, to do so, add new style in styles with this parameters:
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowActionBar">false</item>
    <item name="android:backgroundDimEnabled">true</item>
    <item name="colorPrimary">@color/primary</item>
    <item name="colorPrimaryDark">@color/primaryDark</item>
    <item name="colorAccent">@color/accent</item>
    windowIsTransclucent

     and

    windowBackground

     set to transparent are most needed to achieve wanted „on top” effect.

  4. What else you can to do to improve the feeling of the new activity is to
    setCancelable

     as it is in Dialogs, but there is not default method for it. Since we made it one side partial only it is pretty easy to implement. For my vertical partial fullscreen it looks like this:

    override fun dispatchTouchEvent(event: MotionEvent): Boolean {
    if (event.action == MotionEvent.ACTION_UP && outOfBounds(event)) {
    finish()
    }
    return super.dispatchTouchEvent(event)
    }
    private fun outOfBounds(event: MotionEvent): Boolean {
    return event.y < addRoot.y
    }

And that is all, not very hard to implement such not-fullscreen Activity but it isn’t common in tutorials so you may find it helpful.

 

Yeee, my English is not the best but whatever, if you need this knowledge you will read it anyway.

You can find me on twitter @KubenQPL

Leave a Comment

Twój adres email nie zostanie opublikowany. Pola, których wypełnienie jest wymagane, są oznaczone symbolem *