Jan Knoblauch Jan Knoblauch - 5 months ago 32
Android Question

Android Set ImageView in front of Relative Layout

I have a

RelativeLayout
in my Android App. Now I want to show an
ImageView
in front of that Layout. The problem is that the
ImageView
is not in the front, it's a bit transparent and I can see things like
EditText
and
Button
. I can't change the Layout (setContentView), because the Layout is created dynamically and after
setContentView
, the Controls are away.

Answer

You can add view programmatically, and in the way that it will be on the top!

  1. create id for you top level layout
  2. Now some code (in my case it's relative layout):

    RelativeLayout relativeLayout = (RelativeLayout)findViewById(R.id.relative_layout_id);
    ImageView imageView = new ImageView(context)  
    Drawable rightArrowBlackDrawable = getResources().getDrawable(R.drawable.image); 
    imageView.setLayoutParams(getLayoutParams());
    relativeLayout.addView(imageView);
    imageView.bringToFront();    
    
    //here just example layout params, use yours params ;-) 
    private RelativeLayout.LayoutParams getLayoutParams() {
    
        RelativeLayout.LayoutParams layoutParams = 
        new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,             
        RelativeLayout.LayoutParams.WRAP_CONTENT);
    
        layoutParams.addRule(RelativeLayout.CENTER_VERTICAL);
        return layoutParams;
    }