vendredi 22 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.

Aucun commentaire:

Enregistrer un commentaire