Note that there are some explanatory texts on larger screens.

plurals
  1. POHoneycomb background drawing issues
    primarykey
    data
    text
    <p>I'm seeing drawing issues on Honeycomb that I can't figure out. For some reason when I add buttons to a view in code the background of the view disapears (becomes black).</p> <p>This works fine on my Galaxy tab running 2.3. It fails in the emulator or on my Motorolla Xoom running 3.2.</p> <p>Details:</p> <p>In onCreate() I'm setting either a background color or a background image on the relativeLayout defined in main.xml. My relativeLayout is defined as fill_parent.</p> <p>I have an OnTouchListener for my activity, in which I add two green buttons (one left aligned, one right aligned), to the relativeLayout.</p> <p>When I add those two buttons the background of the relativeLayout disappears (shows all black). I can't explain why.</p> <p>Clues:</p> <ul> <li><p>If I set the color of one of the green buttons to instead be Color.TRANSPARENT, everything works and my background doesn't disappear. This seems like a big clue, but I can't figure out what it means.</p></li> <li><p>When using color background, if I set the targetSdkVersion to "11" instead of "7" (I'm targetting 2.1/7) it works and my color background doesn't disappear. But images are still broken.</p></li> <li><p>When using color background, I can call setDrawingCacheBackgroundColor( Color.RED ) which results in a red background instead of black. I could use this as a solution, setting the cache color to my background color, but this doesn't work when using images.</p></li> </ul> <p>This really feels like a bug in android because I can't see that I'm doing anything wrong in my code. I'd appreciate any help or advice.</p> <p>Here's my simple main.xml layout:</p> <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/MyLayout"&gt; &lt;/RelativeLayout&gt; </code></pre> <p>Here's my activity code:</p> <pre><code>public class ViewTesterActivity extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate( savedInstanceState ); // set the relative layout as our view setContentView( R.layout.main ); // get the relative layout android.widget.RelativeLayout layout = (android.widget.RelativeLayout) findViewById( R.id.MyLayout ); // set an on touch listener layout.setOnTouchListener( (android.view.View.OnTouchListener) mOnTouchListener ); // set a background image // PROBLEM: this background image disappears when adding the buttons in our touch handler // GOOFY FIX: If I change the color of one of the buttons we add to be Color.TRANSPARENT, this // background image doesn't disappear. layout.setBackgroundResource( R.drawable.icon ); layout.setDrawingCacheEnabled( false ); // alternatively, set background color // PROBLEM: the color disappears (becomes black) when adding the button in our touch handler // GOOFY FIX: If I change the color of one of the buttons we add to be Color.TRANSPARENT, the color // doesn't disappear // GOOFY FIX 2: If I set android:targetSdkVersion to "11" in our manifest, the color doesn't disappear // CLUE?: Without any fixes above and using color as the background, if I set the drawing cache color // to Color.RED it'll show up instead of black. This would be a great solution to my problem but it // doesn't work for background images. // layout.setDrawingCacheBackgroundColor( Color.RED ); // layout.setDrawingCacheEnabled( true ); } // on touch listener, add two buttons to the view private android.view.View.OnTouchListener mOnTouchListener = new android.view.View.OnTouchListener() { public boolean onTouch(View v, MotionEvent event) { if ( v != null ) v.onTouchEvent( event ); if ( event.getAction() == MotionEvent.ACTION_UP ) { android.widget.RelativeLayout layout = (android.widget.RelativeLayout) findViewById( R.id.MyLayout ); // add a green button, left aligned Button btn = new Button( ViewTesterActivity.this ); btn.setBackgroundColor( Color.GREEN ); // GOOFY FIX: setting this instead of green solves the issue, no disappearing background // btn.setBackgroundColor( Color.TRANSPARENT ); RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( 100, 100 ); params.addRule( RelativeLayout.ALIGN_PARENT_LEFT ); layout.addView( btn, params ); // add a green button, right aligned btn = new Button( ViewTesterActivity.this ); btn.setBackgroundColor( Color.GREEN ); params = new RelativeLayout.LayoutParams( 100, 100 ); params.addRule( RelativeLayout.ALIGN_PARENT_RIGHT ); layout.addView( btn, params ); } return true; } }; } </code></pre>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload