• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps Android Studio: How to enable a button only after a text field is typed in?

Toddykins

Lurker
I followed a tutorial that helped me make a very simple calculator which looks like this (except I had the buttons enabled before). I played around with it a bit and added the -, *, and / buttons on my own. I noticed that keeping one or both text fields empty and clicking one of the buttons caused the app to crash, so I decided to fix that by disabling all of the buttons until values are entered into both text fields. Only problem is that I'm not sure how to enable a button after a text field is typed in. Any help?
 
Last edited:
I looked at that link and found a few others, and I came up with this. Only got one error. Hopefully I'm not too far off from what I'm trying to do. How do I resolve that error at line 32?

Code:
package com.example.todd.calculator;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.text.Editable;


public class MainActivity extends AppCompatActivity {


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

    }


    //  ---------------trying the thing---------------


    EditText Field1 = (EditText)findViewById(R.id.editText);
    //EditText Field2 = (EditText)findViewById(R.id.editText2);

    //ERROR: "Cannot resolve symbol 'addTextChangedListener'.
    Field1.addTextChangedListener(new TextWatcher() {

        @Override
        public void afterTextChanged(Editable s) {}

        @Override
        public void beforeTextChanged(CharSequence s, int start,
        int count, int after) {
        }

        @Override
        public void onTextChanged(CharSequence s, int start,
        int before, int count) {
            if(s.length() != 0)
                enableButtonIfReady();
        }
    });

    Button Button1 = (Button)findViewById(R.id.button);

    public void enableButtonIfReady() {

        boolean isReady = Field1.getText().toString().length()>0;
        Button1.setEnabled(isReady);
    }

    //  ---------------done trying the thing---------------

    public void onButtonClick1(View v) {
        EditText e1 = (EditText)findViewById(R.id.editText);
        EditText e2 = (EditText)findViewById(R.id.editText2);
        TextView t1 = (TextView)findViewById(R.id.textView);
        int num1 = Integer.parseInt(e1.getText().toString());
        int num2 = Integer.parseInt(e2.getText().toString());
        int ans = num1 + num2 ;
        t1.setText(Integer.toString(ans));
    }

    public void onButtonClick2(View v) {
        EditText e1 = (EditText)findViewById(R.id.editText);
        EditText e2 = (EditText)findViewById(R.id.editText2);
        TextView t1 = (TextView)findViewById(R.id.textView);
        int num1 = Integer.parseInt(e1.getText().toString());
        int num2 = Integer.parseInt(e2.getText().toString());
        int ans = num1 - num2 ;
        t1.setText(Integer.toString(ans));
    }

    public void onButtonClick3(View v) {
        EditText e1 = (EditText)findViewById(R.id.editText);
        EditText e2 = (EditText)findViewById(R.id.editText2);
        TextView t1 = (TextView)findViewById(R.id.textView);
        int num1 = Integer.parseInt(e1.getText().toString());
        int num2 = Integer.parseInt(e2.getText().toString());
        int ans = num1 * num2 ;
        t1.setText(Integer.toString(ans));
    }

    public void onButtonClick4(View v) {
        EditText e1 = (EditText)findViewById(R.id.editText);
        EditText e2 = (EditText)findViewById(R.id.editText2);
        TextView t1 = (TextView)findViewById(R.id.textView);
        double num1 = Integer.parseInt(e1.getText().toString());
        double num2 = Integer.parseInt(e2.getText().toString());
        double ans = num1 / num2 ;
        t1.setText(Double.toString(ans));
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}
 
Last edited:
Okay, I did both of those. The import you suggested is marked as unused though, and I have the same error as before.

Edit: Also, if I hover over 'TextWatcher' on line 32, it says 'Invalid method declaration; return type required'. All three @Overrides are also underlined in red with 'annotations not allowed here'.
 
Last edited:
What happens if you add the following variable declaration?

Code:
EditText Field1 = (EditText)findViewById(R.id.editText);
TextWatcher watcher = new TextWatcher();
 
It underlined 'TextWatcher' with error 'TextWatcher is abstract; cannot be instantiated'. I made a dumb mistake earlier and the 'import android.text.TextWatcher;' isn't marked as unused anymore, but the other problems I mentioned are still there. Here's a screenshot showing where the errors are, if that helps: http://prntscr.com/bo5x9r
 
I'm afraid it's difficult to help you with your problems in this forum. I think you would benefit from stepping back a bit, and taking the time to learn about Java, before getting into app development.
It's clear that you are lacking in basic knowledge about the programming language.
Sorry to be brutal, but you will just end up asking a long stream of basic questions, and won't really learn much.
 
It's alright. I was planning on putting this project on hold and coming back to it after writing something a little more on my level. I feel like I understand enough to have a little fun with Android Studio, so I don't think I can get myself to back off from app development. I will take your advice and try to improve on java though. Thanks a lot for your help.
 
Last edited:
Back
Top Bottom