Juwei
Juwei

Reputation: 261

Programmatically RelativeLayout.ALIGN_RIGHT with a given View.getId() not working

i try to align a TextView programmatically to the right/bottom edge of a button. However, this static xml code is working:

<RelativeLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Button"
        android:id="@+id/button" />

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="New Text"
        android:id="@+id/textView10"
        android:layout_alignRight="@+id/button"
        android:layout_alignBottom="@+id/button" />
</RelativeLayout>

If i try to do it by code, it is not working. The TextView is getting aligned top/left as it is by default. The RelativeLayout.ALIGN_RIGHT and ALIGN_BOTTOM seems to be ignored...

            // Adding the LinearLayout
  LinearLayout linearLayout = new LinearLayout(getContext());
  linearLayout.setOrientation(LinearLayout.VERTICAL);
  linearLayout.setLayoutParams(new LinearLayout.LayoutParams(
          LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f));

            final LinearLayout.LayoutParams LLParamsCont = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.WRAP_CONTENT, Utils.dpToPx(getContext(), 50));

                    // Adding the RelativeLayout
        RelativeLayout relativeLayout = new RelativeLayout(getContext());
        relativeLayout.setLayoutParams(LLParamsCont);

                    // Adding the Button
        Button button = new Button(getContext());
        button.setLayoutParams(new RelativeLayout.LayoutParams(
                RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
        relativeLayout.addView(button)

                    // Adding the TextView
        TextView textView = new TextView(getContext());
        textView.setGravity(Gravity.CENTER_HORIZONTAL);
        RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(
                Utils.dpToPx(getContext(), 20),
                Utils.dpToPx(getContext(), 20));
        layoutParams.addRule(RelativeLayout.ALIGN_RIGHT, button.getId());        // <== THIS DOESN'T SEEM TO WORK
        layoutParams.addRule(RelativeLayout.ALIGN_BOTTOM, button.getId());       // <== THIS DOESN'T SEEM TO WORK
        textView.setLayoutParams(layoutParams);
        textView.setTextSize(TypedValue.COMPLEX_UNIT_SP, 12);
        textView.setGravity(Gravity.CENTER);
        textView.setBackgroundResource(R.drawable.score_count);
        relativeLayout.addView(textView);

        linearLayout.addView(relativeLayout)

The TextView is just not beeing aligned to the right/bottom line of the Button.

Does anyone has an idea why that is not working by code, but with static xml? What did i miss?

Thank you!

With best regards, Juergen

Upvotes: 3

Views: 2020

Answers (2)

Juwei
Juwei

Reputation: 261

I found the solution at this post: How to lay out Views in RelativeLayout programmatically?

I need to set an id with setId(int) to the Button, then it is working.

Upvotes: 0

bonnyz
bonnyz

Reputation: 13548

Try to set a unique id to the Button before create the rule (more info here).

Button button = new Button(getContext());
button.setId(View.generateViewId()); //this utility method is API 17!

Upvotes: 2

Related Questions