skip to Main Content

Think I have 4 button on navbar.
First button contains some data or items and other buttons also contains some data or items.

Note: All four buttons under a same xml file.

I want if I click first button it will show me first button data or items and same time it will hide others three buttons data or items. And I want to use same method for all buttons.

How can I do that please help me.

3

Answers


  1. As you want to hide other three buttons you can use switch case where you can get the click of the button and on the click of one button you can hide all other buttons.

       Button b1 = (Button) findViewById(R.id.button1);       
       Button b2 = (Button) findViewById(R.id.button2);
       Button b3 = (Button) findViewById(R.id.button3);
       Button b4 = (Button) findViewById(R.id.button4);
    
        b1.setOnClickListener(this)         
        b2.setOnClickListener(this)
        b3.setOnClickListener(this)
        b4.setOnClickListener(this)
     
    

    when you define onclick method you will get the id of the clicked view.

    public void onClick(View v) {
               // Perform action on click
              switch(v.getId()) {
    
              case R.id.button1: 
                 b1.setVisibility(View.VISIBLE);
                 b2.setVisibility(View.GONE);
                 b3.setVisibility(View.GONE);
                 b4.setVisibility(View.GONE);
                 break;
    
            case R.id.button2: 
                 b1.setVisibility(View.GONE);
                 b2.setVisibility(View.VISIBLE);
                 b3.setVisibility(View.GONE);
                 b4.setVisibility(View.GONE);
                 break;
    
             case R.id.button3: 
                 b1.setVisibility(View.GONE);
                 b2.setVisibility(View.GONE);
                 b3.setVisibility(View.VISIBLE);
                 b4.setVisibility(View.GONE);
                 break;
    
           case R.id.button1: 
                 b1.setVisibility(View.GONE);
                 b2.setVisibility(View.GONE);
                 b3.setVisibility(View.GONE);
                 b4.setVisibility(View.VISIBLE);
                 break;
         }
    }
    
    Login or Signup to reply.
  2. You can achieve that by make all other button visibility GONE in onClick fun like that:

    1. if you are in Activity

      override fun onCreate(savedInstanceState: Bundle?) {
      
         val b1 = findViewById(R.id.button1)      
         val b2 = findViewById(R.id.button2)
         val b3 = findViewById(R.id.button3)
         val b4 = findViewById(R.id.button4)
      
         b1.setOnClickListener {
            b1.visibility = View.VISIBLE
            b2.visibility = View.GONE
            b3.visibility = View.GONE
            b4.visibility = View.GONE
        }
      
        b2.setOnClickListener {
           b1.visibility = View.GONE
           b2.visibility = View.VISIBLE
           b3.visibility = View.GONE
           b4.visibility = View.GONE
        }
      
        b3.setOnClickListener {
           b1.visibility = View.GONE
           b2.visibility = View.GONE
           b3.visibility = View.VISIBLE
           b4.visibility = View.GONE
        }
      
        b4.setOnClickListener {
           b1.visibility = View.GONE
           b2.visibility = View.GONE
           b3.visibility = View.GONE
           b4.visibility = View.VISIBLE
        }
      }
      
    2. If you are in Fragment

      override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
         super.onViewCreated(view, savedInstanceState)
      
         val b1 = view.findViewById(R.id.button1)      
         val b2 = view.findViewById(R.id.button2)
         val b3 = view.findViewById(R.id.button3)
         val b4 = view.findViewById(R.id.button4)
      
         b1.setOnClickListener {
            b1.visibility = View.VISIBLE
            b2.visibility = View.GONE
            b3.visibility = View.GONE
            b4.visibility = View.GONE
         }
      
         b2.setOnClickListener {
            b1.visibility = View.GONE
            b2.visibility = View.VISIBLE
            b3.visibility = View.GONE
            b4.visibility = View.GONE
         }
      
         b3.setOnClickListener {
            b1.visibility = View.GONE
            b2.visibility = View.GONE
            b3.visibility = View.VISIBLE
            b4.visibility = View.GONE
         }
      
         b4.setOnClickListener {
            b1.visibility = View.GONE
            b2.visibility = View.GONE
            b3.visibility = View.GONE
            b4.visibility = View.VISIBLE
         }
      }
      

    In this example, when you press on a button he will be visible and all other will be gone from the UI.

    You can you INVISIBLE instead of GONE, the different is that in GONE the view doesn’t "catchin" space in your screen, and INVISIBLE it does, just not visible to user (for more information read this)

    Login or Signup to reply.
  3. As you want to hide data/items not buttons.if you hide button directly. you can’t click on that.
    Suppose if you click on button1 and it hides other buttons. you can’t click on button2.
    you have to put all data in a child layout which you can hide.

    Button b1 = (Button) findViewById(R.id.button1);       
    Button b2 = (Button) findViewById(R.id.button2);
    Button b3 = (Button) findViewById(R.id.button3);
    Button b4 = (Button) findViewById(R.id.button4);
    
    RelativeLayout r1 = (RelativeLayout) findViewById(R.id.buttonlayout1);       
    RelativeLayout r2 = (RelativeLayout) findViewById(R.id.buttonlayout2);
    RelativeLayout r3 = (RelativeLayout) findViewById(R.id.buttonlayout3);
    RelativeLayout r4 = (RelativeLayout) findViewById(R.id.buttonlayout4);
    
    b1.setOnClickListener(this)         
    b2.setOnClickListener(this)
    b3.setOnClickListener(this)
    b4.setOnClickListener(this)
    

    hide/show views in onClick method

    public void onClick(View v) {
               // Perform action on click
              switch(v.getId()) {
    
              case R.id.button1: 
                 r1.setVisibility(View.VISIBLE);
                 r2.setVisibility(View.GONE);
                 r3.setVisibility(View.GONE);
                 r4.setVisibility(View.GONE);
                 break;
    
            case R.id.button2: 
                 r1.setVisibility(View.GONE);
                 r2.setVisibility(View.VISIBLE);
                 r3.setVisibility(View.GONE);
                 r4.setVisibility(View.GONE);
                 break;
    
             case R.id.button3: 
                 r1.setVisibility(View.GONE);
                 r2.setVisibility(View.GONE);
                 r3.setVisibility(View.VISIBLE);
                 r4.setVisibility(View.GONE);
                 break;
    
           case R.id.button4: 
                 r1.setVisibility(View.GONE);
                 r2.setVisibility(View.GONE);
                 r3.setVisibility(View.GONE);
                 r4.setVisibility(View.VISIBLE);
                 break;
         }
    }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search