mercredi 27 mai 2015

Removing RelativeLayout.END_OF which is added programmatically

I'm trying to remove RelativeLayout.END_OF from a view's layout parameters which is added programmatically, but it seems that just writing viewLayoutParams.removeRule(RelativeLayout.END_OF) doesn't work and I don't know why!

I'm trying to make a live preview based on options and here they are my codes.

XML:

<RelativeLayout
    android:id="@+id/liveExample"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/liveExampleLabel">

    <TextView
        android:id="@+id/iranianDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Monday, 8 Bahman 1370"
        android:textAppearance="@style/AppTheme.Text.IranianDate"
        tools:ignore="HardcodedText"/>

    <TextView
        android:id="@+id/islamicDate_label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentStart="true"
        android:layout_below="@id/iranianDate"
        android:text="@string/islamic"
        android:textAppearance="@style/AppTheme.Text.IslamicLabel"/>

    <TextView
        android:id="@+id/islamicDate"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="8 Rajab 1436"
        android:layout_below="@id/iranianDate"
        android:textAppearance="@style/AppTheme.Text.IslamicDate"
        tools:ignore="HardcodedText"/>
</RelativeLayout>

Java:

private void updateLivePreviewDatesLayout(boolean isChecked) {
RelativeLayout.LayoutParams iranianDateLayoutParams = (RelativeLayout.LayoutParams) iranianDate
        .getLayoutParams();
RelativeLayout.LayoutParams islamicDateLayoutParams = (RelativeLayout.LayoutParams) islamicDate
        .getLayoutParams();
if (isChecked) {
    iranianDateLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    islamicDateLayoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
    islamicDateLayoutParams.setMarginStart(0);
    islamicDateLayoutParams.removeRule(RelativeLayout.END_OF);
} else {
    iranianDateLayoutParams.removeRule(RelativeLayout.CENTER_HORIZONTAL);
    islamicDateLayoutParams.removeRule(RelativeLayout.CENTER_HORIZONTAL);
    if (displayIslamicLabel.isChecked()) {
        islamicDateLayoutParams.setMarginStart(8);
        islamicDateLayoutParams.addRule(RelativeLayout.END_OF,
                R.id.islamicDate_label);
    }
}
iranianDate.setLayoutParams(iranianDateLayoutParams);
islamicDate.setLayoutParams(islamicDateLayoutParams);
}

centerHorizontal.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        editor.putBoolean("w_centerHorizontal", isChecked).apply();
        updateLivePreviewDatesLayout(isChecked);
    }
});

It works for the first time (removing toEndOf layout parameter) but for the next time, it won't be removed because I added the parameter programmatically.

UPDATE

I need to add the layout_toEndOf layout parameter to the view because the default position of the view is related to another view which they should be next together and after that, the view may be positioned at the center of its layout so I should remove the RelativeLayout.END_OF and instead add RelativeLayout.CENTER_HORIZONTAL parameter. It's good to know that these two parameters won't work together and RelativeLayout.END_OF is more powerful that the other parameter.

Screenshots

Before checking the option: The default position of the "4 Sha'ban 1436" is end of the "Islamic" label.

Before checking the option

After checking the option: the "4 Sha'ban 1436" not positioned at center because the RelativeLayout.END_OF parameter is not removed correctly and that's the problem!

After checking the option

Aucun commentaire:

Enregistrer un commentaire