AndikaK
AndikaK

Reputation: 137

Android - Error Thread and a button still clickable

enter image description here

READ THIS : I want to like this.... if user have input name and choose game types. user can click 'OK' button. if user haven't input name and choose game types he can't click 'OK' button.

I create thread to solve this problem... But when I run this app. I can't go to this UI again..

Something wrong in method 'autoValidation'

And code userConfigOK.setClickable(false); doesn't work. I don't know why..

btw, android is hard. . . .

This is the source code :

public class UserConfig extends Activity {
private String gameType;
private String gameTime;
private String playerName;
private int IDChar = 0; 
Thread validation;

@Override
protected void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     setContentView(R.layout.user_config);
     userConfigOK.setClickable(false);
     inputName();
     chooseCharacter();
     setGameType();
     back();
     autoValidation();
     OK();
}

public void inputName() {
    playerName = userNameTextbox.getText().toString();
}

public void setGameType() {
    gameTypes.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> adapterView, View arg1,
                int arg2, long arg3) {

            gameType = (String) gameTypes.getSelectedItem();
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {

        }
    });

}

public void OK() {
    userConfigOK.setOnClickListener( new OnClickListener() {
        @SuppressWarnings("deprecation")
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
            Intent intent = new Intent(UserConfig.this, EnemyConfig1.class);
            startActivity(intent);
            validation.stop();
        }
    });
}

public void autoValidation() {

    validation = new Thread(new Runnable() {
        @Override
        public void run() {

            if( ( !gameType.trim().equals("") ) && ( !playerName.trim().equals("") )  )
            {
                userConfigOK.setClickable(true);
            }

            try {
                Thread.sleep(100);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        } 
    });
    validation.start();

}

}

Upvotes: 0

Views: 131

Answers (2)

Chirag Ghori
Chirag Ghori

Reputation: 4231

Try this

userConfigOK.setEnabled(false);

Instead of

userConfigOK.setClickable(false);

Upvotes: 2

user3260376
user3260376

Reputation: 11

You could just have the onClickListener check to see if there is any text entered, and if there is an item selected from the grid. If the user hasn't selected anything, you could make a Toast that prompts the user to enter the values required.

Upvotes: 1

Related Questions