简体   繁体   中英

How to Hide and show Items using a toggle button in android studio

I'm trying to display 4 charts on a single activity. I want to give the user the option of choosing what kind of graph to show by providing the toggle button.

Eg:

These are the following graphs which show energy consumption:

  • Elec Bar Graph
  • Elec Line Chart
  • Gas Bar Graph
  • Gas Line Chart

There are two toggle buttons, one for electricity and one for Gas. When clicking on the electricity toggle button, you should be able to switch between bar graph and line chart only for electricity. Same thing is applied for the gas button.

Problem: After clicking on the Electricity Toggle Button to see line chart for electricity and then clicking on the Gas Toggle Button to see line chart for gas consumption the output is: two line charts for both electricity and gas, whereas it should just show the line chart for gas consumption upon clicking the gas toggle button.

This is a brief snippet of the code:

switch (v.getId()) {
            case R.id.electricityButton:

boolean check = ((ToggleButton) v).isChecked();
                if (check) {

                    CreateElectricityLineGraph();
                    GasLineGraph.setVisibility(View.INVISIBLE);
                    ElecbarChart.setVisibility(View.INVISIBLE);
                    ElecLineChart.setVisibility(View.VISIBLE);
else {
                                CreateElectricityBarGraph();
                                GasbarChart.setVisibility(View.INVISIBLE);
                                     ElectricityLineChart.setVisibility(View.INVISIBLE);
                                GasLineChart.setVisibility(View.INVISIBLE);
                                ElectricitybarChart.setVisibility(View.VISIBLE);
                  System.out.println("Elec Bar Chart Now Visible");
                            }
break;
case R.id.GasButton:


                boolean c = ((ToggleButton) v).isChecked();
                if (c) {
                    ElectricityLineChart.setVisibility(View.INVISIBLE);

                    CreateGasLineGraph();
                    ElectricitybarChart.setVisibility(View.INVISIBLE);
                    GasLineChart2.setVisibility(View.VISIBLE);

                    System.out.println("Reached the end of if statement 2");

                }
                else{
                    CreateGasBarGraph();
                    ElectricitybarChart.setVisibility(View.INVISIBLE);
                    ElectricityLineChart.setVisibility(View.INVISIBLE);
                    GasLineChart2.setVisibility(View.INVISIBLE);
                    GasbarChart.setVisibility(View.VISIBLE);

                    System.out.println("The Gas Bar graph has been made visible");



                }
                break;

Please help me.

This is due to GasLineChart and GasLineChart2 both are visible in checked condition. you have not make GasLineChart invisible in any condition and also make GasLineChart2 visible on 2nd Condition

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