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

Apps Checking EditText view input ?

DenPit5

Newbie
Apr 23, 2014
11
0
I am developing an App which has an input screen containing two EditText views one accepts numbers only and the other decimal numbers. I need to test if either has no text entered when the user clicks a Button. I can extract numbers from them OK but I cannot find any acceptable tests for empty text. Can anyone one show me a test method which checks for no text available ?
 
Hope this helps out.

Using the button and setting an onClickListener to listen for the click, which will then run a check for the two strings (Set to the EditText values.toString() ) for .isEmpty()

Code:
package com.example.breakable;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import android.view.View;
import android.view.View.OnClickListener;

public class MainActivity extends Activity
{
    Button btnCheck;
    EditText etNum;
    EditText etNumDec;
    String strEtNum;
    String strEtNumDec;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        etNum = (EditText)findViewById
                    (R.id.etNum);
        etNumDec = (EditText)findViewById
                         (R.id.etNumDec);
        btnCheck = (Button)findViewById
                        (R.id.btnCheckET);
        
        btnCheck.setOnClickListener
            (new OnClickListener(){
        	@Override
        	public void onClick(View view)
        	{
        		strEtNum = etNum.getText()
                                    .toString();
        		strEtNumDec = etNumDec
                                    .getText()
                                    .toString();
        		if(strEtNum.isEmpty() 
                            || strEtNumDec.isEmpty())
        		{
                            Toast.makeText(
                              MainActivity.this, 
                              "Error: Empty EditText",
                              Toast.LENGTH_SHORT)
                            .show();
        		}
        	}
        });
    }
}
 
Upvote 0
Hi CodeMonkey for your reply I am sorry that I ahve not answered until but I have been away for a few days holiday and have only just downloaded your reply. As a matter of fact I had already managed to sort out the correct test to make, but thanks for taking the trouble to answer my post anyhow.
 
Upvote 0

BEST TECH IN 2023

We've been tracking upcoming products and ranking the best tech since 2007. Thanks for trusting our opinion: we get rewarded through affiliate links that earn us a commission and we invite you to learn more about us.

Smartphones