Canvas view Problem visibility

I have this layout:

<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <com.components.game.GameView android:id="@+id/game_id" android:layout_width="fill_parent" android:layout_height="fill_parent" /> <RelativeLayout android:id="@+id/ChatLayout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:visibility="invisible" android:focusable="true" android:focusableInTouchMode="true" > <Button android:id="@+id/ChatCancelButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="X" /> <Button android:id="@+id/ChatOkButton" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentRight="true" android:text="OK" /> <EditText android:id="@+id/ChatEditText" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toLeftOf="@+id/ChatOkButton" android:layout_toRightOf="@+id/ChatCancelButton" android:maxLength="50" android:singleLine="true" /> </RelativeLayout> </RelativeLayout> 

This is a RelativeLayout over the canvas. This is invisible during startup, but when the user clicks the button, the layout should become visible. The problem is that it does not become visible. There is a layout, but he just does not draw. If I click on the position where the layout should be displayed, it receives the event and opens the keyboard, but does not draw the entire layout. What is the problem?

If I set RelativeLayout to visible at the beginning, it works fine. it shows the layout, and if I switch between invisible and visible, it works great.

I made a workaround that almost always works. I start the layout visible, not the one in oncreate:

 chatLayout.postDelayed(new Runnable() { @Override public void run() { chatLayout.setVisibility(View.INVISIBLE); } }, 50); 

But I don’t like it and I want to understand what the problem is.

Code:

It starts with a canvas button that sends a message to the handler:

 public void showInputLayout() { Message.obtain(gameHandler, SHOW_INPUT_LAYOUT).sendToTarget(); } 

In the handler:

 case SHOW_INPUT_LAYOUT: gameActivity.setChatVisibility(true); break; 

setChatVisibility:

 public void setChatVisibility(boolean isVisible) { int visible = isVisible ? View.VISIBLE : View.INVISIBLE; chatLayout.setVisibility(visible); if(isVisible){ chatEditText.setFocusable(true); chatEditText.requestFocus(); } } 
+4
source share
2 answers

Add a click listener to RelativeLayout and switch visibility between GONE and VISIBLE . Try something like this:

 int visibility = View.VISIBLE; RelativeLayout layout = (RelativeLayout)findViewById(R.id.ChatLayout); layout.setVisibility(visibility); layout.setOnClickListener(new View.OnClickListener{ public void onClick(View v) { if(visibility == View.VISIBLE) visibility = View.GONE; else visibility = View.VISIBLE; v.setVisibility(visibility); } }) 
0
source

I recently ran into a similar problem, and in my case, the problem was actually in the onDraw () method for the view below it (should be com.components.game.GameView in your case). See if you can add calls to Canvas' getSaveCount() , save() and restoreToCount() in your drawing code, similar to this:

 @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); int saveCount = canvas.getSaveCount(); canvas.save(); // custom drawing code here ... // use Region.Op.INTERSECT for adding clipping regions canvas.restoreToCount(saveCount); } 

I believe it happened that sometimes the structure set clipping areas for elements on top of our Canvas-drawing widget before our onDraw () method is called, so we need to make sure that these areas are saved.

Hope this helps.

0
source

All Articles