0

My .xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.eleizo.firstapplication.MainActivity"
android:orientation="vertical">

<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_weight="1"
    android:id="@+id/topRL">
</RelativeLayout>
....

I added my custom ImageView object in RelativeLayout topRL.addView(map); Constructor of the custom ImageView is:

public CustomImageView(Context context) {
        super(context);
        init();
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM,1);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,1);
        setBackgroundColor(Color.BLUE);
        this.setWillNotDraw(false);

        setLayoutParams(params);

    }

On create:

@Override
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState);
    setContentView(com.example.eleizo.firstapplication.R.layout.activity_main); 

    RL = (RelativeLayout) findViewById(R.id.topRL);

    Context context = getApplicationContext();
    map = new CustomImageView(context);
    RL.addView(map,map.params);

But RelativeLayout.ALIGN_PARENT_RIGHT,RelativeLayout.ALIGN_PARENT_BOTTOM doesn't work: After map.setImageBitmap(input); I see my image in left-top corner.

I want bottom-right corner of the image is on botton-right corner of the RelativeLayout independently of size of the image.

Where did I make the mistake?

Elena
  • 25
  • 7

2 Answers2

0

You have your addRule wrong. Check this links:

a) https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int) b) https://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html#addRule(int, int)

If you want to pass multiple rules you have to call consecutively:

.addRule(RelativeLayout.ALIGN_PARENT_RIGHT) .addRule(RelativeLayout.ALIGN_PARENT_BOTTOM)

If you want a subject that represents the value of the param to set you have to use the 2 parameters method addRule(int, int/bool).

  • doesn't work params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT); params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM); – Elena Jul 31 '17 at 12:29
  • I add the bitmap to my custom ImageView after adding imageView to relativelayout. Maybe because of that I have some error? – Elena Jul 31 '17 at 12:55
0

Here is the solution which worked for me.

CustomeImageView.java

public class CustomImageView extends AppCompatImageView {
    public CustomImageView(Context context) {
        super(context);
        init();
    }

    public CustomImageView(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public CustomImageView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
     }

    private void init() {
        RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
        params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
        setLayoutParams(params);
    }
}

main_activity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="com.icpl.threadsample.MainActivity"
    tools:showIn="@layout/activity_main">

    <RelativeLayout
        android:id="@+id/relativeLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:background="@android:color/black"/>

</LinearLayout>

MainActivity.java

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    final RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relativeLayout);

    FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
    fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            CustomImageView imageView = new CustomImageView(MainActivity.this);
            imageView.setImageResource(R.mipmap.ic_launcher);
            relativeLayout.addView(imageView);
        }
    });
}
}

Checkout this Answer as well.

Mohit Charadva
  • 2,555
  • 1
  • 22
  • 30