1

Please note that same code is working perfectly fine on Android Versions > KitKat

I have a dynamic view of questionnaire. All views are being added according to question coming up from the api I am using. What I am trying to do is, If question was required but user didn't filled it and trying to submit it, I am trying to setVisibility as 'VISIBLE' to my TextView having text "Required"

It works perfectly fine when I am either in Landscape or in Portrait mode, but right after changing the orientation, it stops working.

I have debugged the code and checked, status of visibility is being changed but UI is not being updated.

It is working fine other than KITKAT

I have tried view.invalidate(); as well.

TextView layout to be set as VISIBLE

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/required_prompt_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.goodsnitch.lib.ui.LatoRegularTextView
        android:id="@+id/required_prompt_dynamic"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/field_required_prompt"
        android:textColor="@android:color/holo_red_light"
        android:textSize="11dp"
        android:textStyle="italic"
        android:visibility="gone" />
</LinearLayout>

Java Code to make it visible

private void showRequiredPrompt(final View view) {
        final TextView requiredLabel = (TextView) ((LinearLayout) view.getParent()).findViewById(R.id.required_prompt_dynamic);
        if (requiredLabel != null) {
            mHandler.postDelayed(new Runnable() {
                @Override
                public void run() {
                    view.clearAnimation();
                    requiredLabel.clearAnimation();
                    requiredLabel.setVisibility(View.VISIBLE);
                    view.invalidate();
                    requiredLabel.invalidate();
                }
            }, 200);
        }
    }

I will appreciate your help :)

Editing question to explain further

This is code to show the label ON CLICK EVENT This is the toast showing visibility state of the label

Required label should appear just above first checkbox in RED color

  • 1
    You have to save state. Take a look at: [Saving Android Activity state](http://stackoverflow.com/questions/151777/saving-android-activity-state) – Aleksandar G Jan 21 '17 at 11:56
  • @Alexandar G: Its not about state, I am not trying to restore the state, I am trying to simply show the View on submit button click (if needed) – Zaheer Ahmad Jan 21 '17 at 12:00
  • "but right after changing the orientation, it stops working" As Alexandar mentioned, you have to save current state of the view. Whenever there is change in rotation, activity is re-created. Try to add android:configChanges="orientation" in AndroidManifest.xml for that activity – Yasir Tahir Jan 23 '17 at 07:39
  • @YasirTahir, I meant after orientation changed, ON MY BUTTON CLICK EVENT, my layout is not being refreshed – Zaheer Ahmad Jan 23 '17 at 09:04
  • @YasirTahir: Let me explain: Success Scenario 1. I came on the screen 2. Left field empty 3. Click on submit button 4. Required Label shows up. (Correct) Fail Scenario 1. I came on the screen 2. Rotated the screen 3. Left field empty 4. Click on submit button 5. Required label should show up. (It doesn't - Failure) It has nothing to do with saveState.. It is button click even which shows or hide the label Make sense? – Zaheer Ahmad Jan 23 '17 at 09:14

1 Answers1

0

use onSaveInstanceState() and onRestoreInstanceState() when u are using screen rotation.

AkhilGite
  • 400
  • 4
  • 17