0
The basic view hierarchy is this:
secondActivity
    linearLayout(LinearLayout)
        constLayout(ConstraintLayout)
            textbox(TextView)
            image(ImageView)
            image2
            image3
            ...

The textbox TextView has visibility GONE, and goal is to make it VISIBLE on clicking other visible siblings, change some colors and text, and when clicked again it must be invisible again and reverse all changes. Cant understand whatever it is that am missing. I have checked many older projects in which I did the same thing and cant find any visible differences as to why this code is not working now.

secondActivity.java

public class secondActivity extends Activity {

    public boolean isTextBoxHidden;
    public ConstraintLayout constLayout;
    public TextView textbox;

    @Override protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_second);

        constLayout = findViewById(R.id.constLayout);
        textbox = findViewById(R.id.textbox);
        isTextBoxHidden = false;

        // SETTING UP LISTENER
        View.OnClickListener clickListener = new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(!isTextBoxHidden) {
                    constLayout.setBackgroundColor(Color.BLACK);  //setting color on previously 
                    v.setBackgroundColor(Color.BLACK);            //setting color on visible view

                    textbox.setText("whatever");
                    textbox.setVisibility(View.VISIBLE);  //was gone
                    isTextBoxHidden = true;
                }
                else {
                    textbox.setVisibility(View.GONE);     //hide again
                    constLayout.setBackgroundColor(Color.WHITE);
                    v.setBackgroundColor(Color.WHITE);
                    isTextBoxHidden = false;
                }
            }
        };

        // INSERTING LISTENERS into all children
        for(int i=0; i<constLayout.getChildCount(); i++) {
                constLayout.getChildAt(i).setOnClickListener(clickListener);
        }
    }
}

activity_second.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".secondActivity">

    <android.support.constraint.ConstraintLayout
        android:id="@+id/constLayout"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@android:color/white">

        <TextView
            android:id="@+id/textbox"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:gravity="center"
            android:text="example"

            android:visibility="gone"

            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent"/>

        <ImageButton
            android:id="@+id/image"
            android:layout_width="0dp"
            android:layout_height="wrap_content"
            android:adjustViewBounds="true"
            android:scaleType="fitXY"
            android:src="@drawable/example"

            android:clickable="true"

            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"/>

        <!--few more clones of the first imageButton-->

    </android.support.constraint.ConstraintLayout>
</LinearLayout>

1 Answers1

0

I can't see where you're setting textbox reference, so maybe that's a clue.

Edit: Did compiled that example you have provided and everything works correctly, but i assume that [...] gone again for good meant you probably want this to be one shot action so instead of boolean just use Boolean and compare it to null.

Edit: On the second thought you can just remove isTextBoxHidden = false; in else branch

  • I agree i missed a reference but its super obvious that the code wont compile without that so thats not the answer. So please dont clutter the answer area with "maybe" comments on typos. – Frustrated Insaan Feb 19 '20 at 04:07
  • Updated my answer – Krzysztof Zgondek Feb 19 '20 at 05:13
  • thx for correcting me again. I definitely didnt mean this to be a one shot action, instead it was yet another typing frenzy here. You can see how the listener keeps alternating the value of `isTextBoxHidden` between `true` and `false` with each click. I have corrected my intentions in the post – Frustrated Insaan Feb 19 '20 at 05:22
  • hey that flag is needed for the loop to switch correctly. if the flag isTextBoxHidden=false is removed, than with all future clicks, the if() branch will never be called – Frustrated Insaan Mar 15 '20 at 22:56