简体   繁体   中英

How to set visibility GONE of a Linear Layout which is inside a scroll view and contains two TextViews inside?

The main layout is a Linear layout inside that a scroll view is there which contain sublayouts. Here is my layout [omitted everything except the specific layout (marked with red) as it will be very long]:

<ScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

        <androidx.cardview.widget.CardView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginStart="10dp"
            android:layout_marginTop="8dp"
            android:layout_marginEnd="10dp"
            android:layout_marginBottom="8dp"
            android:foreground="?android:attr/selectableItemBackground"
            app:cardCornerRadius="8dp"
            app:cardElevation="10dp">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical"
                android:padding="8dp">


                <LinearLayout
                    android:id="@+id/layoutIncomeTax"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:orientation="horizontal">

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:gravity="start"
                        android:text="Income Tax:"
                        android:textColor="@color/black"
                        android:textSize="16sp" />

                    <TextView
                        android:id="@+id/tvIncomeTax"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_weight="1"
                        android:gravity="end"
                        android:text="0"
                        android:textColor="@color/black"
                        android:textSize="16sp"
                        android:textStyle="bold" />

                </LinearLayout>

            </LinearLayout>

        </androidx.cardview.widget.CardView>

    </LinearLayout>

</ScrollView>

Here is my code (removed unnecessary codes):

public class ViewSalary extends AppCompatActivity {

private Spinner selectShift, selectYear, selectMonth;
private EditText edtEmployeeCode;
private Button viewSalaryBtn;
private String shift, year, month;


DatabaseReference rootDatabaseRef;

private LinearLayout layoutIncomeTax;

private TextView tvIncomeTax;

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


    viewSalaryBtn = findViewById(R.id.viewSalaryBtn);

    layoutIncomeTax = findViewById(R.id.layoutIncomeTax);

    tvIncomeTax = findViewById(R.id.tvIncomeTax);

    rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Salary");

    viewSalaryBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            viewSalary();

            String checkIncomeTax = tvIncomeTax.getText().toString();
            if (checkIncomeTax.equals("0.0")) {
                layoutIncomeTax.setVisibility(layoutIncomeTax.GONE);
            }

        }
    });


}

private void viewSalary() {

    final String empCode = edtEmployeeCode.getText().toString();

    DatabaseReference empRef = rootDatabaseRef.child(shift).child(year).child(month).child(empCode);

    empRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {

                String incomeTax = dataSnapshot.child("IncomeTax").getValue(String.class);

                tvIncomeTax.setText(incomeTax);

            } else {
                Toast.makeText(ViewSalary.this, "Data does not exist!", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(ViewSalary.this, error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

}

}

I want to hide all linear layouts on button click after getting the data loaded and if TextView value is "0.0" (like the one marked with red in screenshot)

Screenshot

Try this.

//use trim method to remove spaces in string 
    String checkIncomeTax = tvIncomeTax.getText().toString().trim();
        //hope you are not getting exactly 0.0.use log.i to see value then call if
                   log.i("checkIncomeTax",checkIncomeTax);
                    if (checkIncomeTax.equals("0.0")) {
                        layoutIncomeTax.setVisibility(layoutIncomeTax.GONE);
                    }
                        }

Perform the check inside the listener.

private Spinner selectShift, selectYear, selectMonth;
private EditText edtEmployeeCode;
private Button viewSalaryBtn;
private String shift, year, month;


DatabaseReference rootDatabaseRef;

private LinearLayout layoutIncomeTax;

private TextView tvIncomeTax;

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


    viewSalaryBtn = findViewById(R.id.viewSalaryBtn);

    layoutIncomeTax = findViewById(R.id.layoutIncomeTax);

    tvIncomeTax = findViewById(R.id.tvIncomeTax);

    rootDatabaseRef = FirebaseDatabase.getInstance().getReference().child("Salary");

    viewSalaryBtn.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            viewSalary();
            // Do not check here
        }
    });


}

private void viewSalary() {

    final String empCode = edtEmployeeCode.getText().toString();

    DatabaseReference empRef = rootDatabaseRef.child(shift).child(year).child(month).child(empCode);

    empRef.addValueEventListener(new ValueEventListener() {
        @Override
        public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
            if (dataSnapshot.exists()) {

                String incomeTax = dataSnapshot.child("IncomeTax").getValue(String.class);

                tvIncomeTax.setText(incomeTax);
                
                // Check here
                if (incomeTax.equals("0.0")) {
                    layoutIncomeTax.setVisibility(layoutIncomeTax.GONE);
                }
                
            } else {
                Toast.makeText(ViewSalary.this, "Data does not exist!", Toast.LENGTH_SHORT).show();
            }
        }

        @Override
        public void onCancelled(@NonNull DatabaseError error) {
            Toast.makeText(ViewSalary.this, error.getMessage(), Toast.LENGTH_SHORT).show();
        }
    });

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM