Show view over existing view in same activity

| | August 8, 2015

I am newbie Android developer, and I am starting out with a simple Sudoku game. The problem I am facing right now is that I have my game showing on a dedicated Activity, which is displaying a custom View class. I have the code within the onDraw function. Now I want to show a custom keypad view, which is a separate class extending View, whenever a user clicks on a block in the Sudoku grid.

I am extremely confused as to how I should go about doing this. I am reluctant to create a dedicated activity for just this keypad, and want to show this keypad over the Sudoku grid view that the activity is currently displaying.

The activity is using setContentView(myCustomGridViewClass) to show the grid. Now I want something to the equivalent of

onTouch -> setContentView(myCustomKeypadView), though this should appear over the previous view.

I have tried reading about the LayoutInflater, but did not understand how to use it to show the keypad within this same activity. This is probably a very noob question, but any help, including any other questions, or sources I could refer to for an answer would be highly appreciated.


One Response to “Show view over existing view in same activity”

  1. You could place both views in a FrameLayout or a RelativeLayout and control the keypad’s visibility ( when needed.

    Change setContentView(myCustomGridViewClass) to setContentView(R.layout.id_of_my_layout), then write id_of_my_layout.xml that contains the layout (more help here

    Make sure to add your keypad view after the game view in your layout xml, otherwise the game view will be placed on top of it and you’ll never see the keypad :)

Leave a Reply