0

Both pieces are to place two objects near each other with a fixed size for one of them

Piece one. Works as supposed: dynamically filled scrollView takes all the space possible above the keyboard

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
root.addView(keyboardView, params);
params = new RelativeLayout.LayoutParams(
        ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ABOVE, keyboardView.getId());
root.addView(scrollView, params);

Piece two. Supposed to place a button on the right and an editText taking the rest of the place to the left of the button. Instead, the editText ignores the button and takes the whole screen width, so the button is under it

RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
container.addView(deleteButton, params);
params = new RelativeLayout.LayoutParams(
        RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.LEFT_OF, deleteButton.getId());
container.addView(editText, params);

Piece two remade in XML. For some reason, working as needed

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="wrap_content" >

    <Button
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:text="Delete"
        android:id="@+id/item_deleteButton"
        android:layout_alignParentRight="true"
        android:layout_alignBottom="@+id/item_editText" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/item_editText"
        android:layout_toLeftOf="@+id/item_deleteButton" />
</RelativeLayout>
Alex
  • 1,077
  • 9
  • 24
  • Are you creating those `Views` (for example `deleteButton`) dynamically, or through an xml? – Bartek Lipinski Jul 30 '15 at 06:34
  • Dynamically. XML code has no connection with the java piece two - it's just to illustrate what I wanted to do. I can't see the diffefence between java and XML implementations though – Alex Jul 30 '15 at 10:22
  • Ok, so are you sure your `View` has viable `id`? – Bartek Lipinski Jul 30 '15 at 10:23
  • Was sure until this moment. Isn't an `id` attached to every `View` object on its construction? – Alex Jul 30 '15 at 10:25
  • No. `Id` is attached to a `View` only if you want it to be. So through `xml` if you use `android:id="@+id/some_id` or if you create a `View` dynamically with a method call `setId(int id)`. – Bartek Lipinski Jul 30 '15 at 10:28
  • Okay. And how to choose an `id` properly so that there will be no collisions with the others assigned before? – Alex Jul 30 '15 at 10:37
  • http://stackoverflow.com/questions/8460680/how-can-i-assign-an-id-to-a-view-programmatically – Alex Jul 30 '15 at 11:44
  • 1
    In my code, I've been using something similar to this: http://stackoverflow.com/a/15442898/1993204 – Bartek Lipinski Jul 30 '15 at 12:04
  • What is the role of the AtomicInteger in this ID generator? What problems can be encoutered if it is replaced with a usual `int` and a method similar to `compareAndSet`? – Alex Aug 03 '15 at 13:15
  • You should ask the author of the post, not me. – Bartek Lipinski Aug 03 '15 at 13:19

0 Answers0