1

I want to put an imageView on the center_vertically of FrameLayout. But, I can not do it. I have tried FrameLayout.LayoutParams but, it does not have .addRule() method.

public class P023Deneme extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FrameLayout frameLayout=new FrameLayout(this);
    ImageView imageViewBackgroud=new ImageView(this);
    imageViewBackgroud.setImageResource(R.drawable.sample_6);

    ImageView imageViewLeftArrow=new ImageView(this);
    imageViewLeftArrow.setImageResource(R.drawable.slidersolok_logo);

    frameLayout.addView(imageViewBackgroud);
    frameLayout.addView(imageViewLeftArrow,50,50);
    setContentView(frameLayout);


}

}

hikmet_anil
  • 89
  • 3
  • 14

1 Answers1

0

Do it in XML, not in code, it's easier

<?xml version="1.0" encoding="utf-8"?>

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:src="@drawable/background_drawable"/>

    <ImageView
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/arrow_drawable"/>
</FrameLayout>

If you still want to do it programmatically, then:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    FrameLayout frameLayout=new FrameLayout(this);
    ImageView imageViewBackgroud=new ImageView(this);
    imageViewBackgroud.setImageResource(R.drawable.background);

    ImageView imageViewLeftArrow=new ImageView(this);
    imageViewLeftArrow.setImageResource(R.drawable.arrow);

    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
            FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.CENTER_VERTICAL);
    frameLayout.addView(imageViewBackgroud);
    frameLayout.addView(imageViewLeftArrow, layoutParams);
    setContentView(frameLayout);
}
hikmet_anil
  • 89
  • 3
  • 14
Francesc
  • 25,014
  • 10
  • 66
  • 84