Ali Amghar
Ali Amghar

Reputation: 25

How to make button disappear when certain TextView is displayed?

I'm having this problem. I created an activity with two Buttons and a TextView. The TextView is changed by pressing the Button "Next" and the Button "Previous" is used to change the TextView back to its original (which is textview1). But when "Previous" is pressed while textview1 is displayed or when "Next" is pressed while textview2 is displayed, the application gets an error and is shut down as a consequence. What I want to achieve now is to make the Button "Previous" disappear when textview1 is on and the Button "Next" when textview2 is on.

This is class I currently use:

package com.example.test8;

import android.os.Bundle;
import android.app.Activity;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageButton;
import android.widget.TextView;

public class Main extends Activity {
    private static final String[] Texts= 
        {"textview1",
         "textview2"
        };

int textIdx = 0;

TextView Text;
ImageButton Next;
ImageButton Previous;

public static String getText(int idx) { 
    return Texts[idx]; 
} 

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

    final TextView text = (TextView) findViewById(R.id.text);
    ImageButton Next = (ImageButton) findViewById(R.id.Next);
    ImageButton Previous = (ImageButton) findViewById(R.id.Previous);
    Text.setText(getText(textIdx));

    Next.setOnClickListener(new View.OnClickListener() {  
        public void onClick(View v) { 
        textIdx++; 
        Text.setText(getText(textIdx)); 
        } 
    }); 

    Previous.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
        textIdx--;
        Text.setText(getText(textIdx)); 
        }
        });

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}

I thought about using an else if statement, but I couldn't make it work, because I lack the knowledge and experience. If someone could help me out here, that would be great. Thanks in advance.

Upvotes: 1

Views: 750

Answers (3)

rasen58
rasen58

Reputation: 5091

You can just use a simple if statement

     Next.setOnClickListener(new View.OnClickListener() {  
            public void onClick(View v) { 
            if(textIdx == 0)
            {
                textIdx++; 
                Text.setText(getText(textIdx)); 
                Next.setVisibility(View.INVISIBLE);
                Previous.setVisibility(View.VISIBLE);
            }
            } 
        }); 

        Previous.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

            if(textIdx == 1)
            {
                textIdx--;
                Text.setText(getText(textIdx)); 
                Next.setVisibility(View.VISIBLE);
                Previous.setVisibility(View.INVISIBLE);
            }
            }
            });

Upvotes: 1

Frederic Blase
Frederic Blase

Reputation: 530

If you press Previous while textview1 is active (and textIdx is 0), textIdx becomes negative. You can't get element with negative index from array.

Same way, when you press Next while texview2 is active (textIdx is 1), textIdx becomes more than 1. You can't get element with index equal or greater than your array size.

I suggest you replace textIdx++ with textIdx = 1 and textIdx-- with textIdx = 0, that will solve crashing problem.

To make button disappear try Next.setVisibility(View.INVISIBLE), and Next.setVisibility(View.VISIBLE) to restore it.

Upvotes: 0

Woodsy
Woodsy

Reputation: 3377

You should disable the buttons to prevent a user from clicking it when there is nothing ahead or behind it in your array.

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

   final TextView text = (TextView) findViewById(R.id.text);
   ImageButton Next = (ImageButton) findViewById(R.id.Next);
   ImageButton Previous = (ImageButton) findViewById(R.id.Previous);
   Text.setText(getText(textIdx));

   Previous.setEnabled(false); //defaults the button to disabled

   Next.setOnClickListener(new View.OnClickListener() {  
       public void onClick(View v) { 
       textIdx++; 
       Text.setText(getText(textIdx));            
       Previous.setEnabled(true);
       if(textIdx>=Text.length-1)
          Next.setEnabled(false);  
       } 
   }); 

   Previous.setOnClickListener(new View.OnClickListener() {
       public void onClick(View v) {
       textIdx--;
       Text.setText(getText(textIdx));            
       Next.setEnabled(true);
       if(textIdx<=0)
          Previous.setEnabled(false);
       }
    });

Upvotes: 0

Related Questions