if a buttons text is empty, then - android

In this code I want to make a buttons visibility gone, if the text of the button is "":
if (button TEXT IS "")
{
button.setVisibility(View.GONE);
}
else
{
button.setVisibility(View.VISIBLE);
}
How can I form the if-Statement to get a result?
Thank you!

The answer is in the question, try with the Empty() method to check if the length of the characters in your String is 0 or greater :
if(text.isEmpty()){
//String is empty
}else{
//String includes characters
}

Button b = (Button)findViewByID("your button id");
String buttonText = b.getText().toString();
if (buttonText.equals("Your Text"))
b.setVisibility(View.GONE);
else
b.setVisibility(View.VISIBLE);
If you want to check if empty you may use
if(buttonText.isEmpty())
b.setVisibility(View.GONE);
else
b.setVisibility(View.VISIBLE)

if(button.getText() == null)
{
button.setVisibility(View.GONE);
}
else
{
button.setVisibility(View.VISIBLE);
}

Related

I want to perform many tasks in one Edittext

I want to perform many task in one Edittext. Like when I edit the first number in edittext it store in string value and edittext may be null after that when I enter 2nd number in same edittext 2nd number may also save in another string. Then after some action perform answer show in Textview.
Please help me out...
private void save_on_cick_listen(){
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(save == 0){
// save first string
editText.getText().clear(); //or you can use editText.setText("");
save+;
} else if (save == 1){
// save second string
editText.getText().clear(); //or you can use editText.setText("");
save+;
}
}
});
}

Check if EditText Number Decimal is Empty

I tried everything.
if (editText == null)...
if (etNumero1.getText().toString().isEmpty())...
if (etNumero1.getText().toString().equals("")...
if (etNumero1.getText().toString().trim().length() >= 0))
Nothing works. Even my teatcher cant tell whats wrong.
Sorry for bad english im Brazilian.
Check, If the value is not an Empty
if (!etNumero1.getText().toString().isEmpty())
{
if(etNumero1.getText().toString().contains("."))
{
//Do your functionality here
}
}
else
{
//
}
Try
if (Integer.parseInt(etNumero1.getText().toString()) > 0)
If you specify your question, I could answer in better condition.
Use TextUtils.isEmpty("") check for null if not convert to formart to ###.##
Make sure that you are not making mistake on editText etNumero1??
I know editText is Edit Text but what is etNumero1?? please replace etNumero1 to editText, it should work
if (editText.getText().toString().isEmpty())...
Example:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText editText = findViewById(R.id.editText);
Button button = findViewById( R.id.button );
button.setOnClickListener( new View.OnClickListener(){
#Override
public void onClick (View v) {
if(editText.getText().toString().isEmpty()){
System.out.println("empty string");
}else{
System.out.println("" + editText.getText().toString());
}
}
});
}
if(String.valueOf(editText.getText() != null){
Double double = Double.parseDouble(editText.getText().toString);
}

How to validate several TextView field with infromation before going into the next activity

What I am trying to do is check if all the TextView and Spinner(Drop down) have been filled and selected before going into the next activity. If any or some of them are empty I want to highlight the respective field. Any suggestions or help coding would be great.
I have something like this:
value_1=tv1.getSelectedItem().toString();
value_2=tv2.getText().toString();
value_3=tv3.getText().toString();
value_4=tv4.getText().toString();
value_5=tv5.getText().toString();
Intent intent= new Intent(FormActivity.this,MapsActivity.class);
intent.putExtra("key1",value_1);
intent.putExtra("key2",value_2);
intent.putExtra("key3",value_3);
intent.putExtra("key4",value_4);
intent.putExtra("key5",value_5);
startActivity(intent);
define a view
private View focusView = null;
OnClick of button call following method
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (validate()) {
//do your work
} else {
focusView.requestFocus();
}
}
});
//code to validating each editext
private boolean validate() {
if (TextUtils.isEmpty(getEmail)) {
emailText.setError(getString(R.string.error_field_required));
focusView = emailText;
return false;
} else if (!getEmail.matches(EMAIL_REGEX)) {
emailText.setError("Invalid Email Address");
focusView = emailText;
return false;
} else if (TextUtils.isEmpty(getName)) {
nameText.setError(getString(R.string.error_field_required));
focusView = nameText;
return false;
}
else{
return true;
}
}
this will focus you view if particular edittext is empty and invalid
For your spinner problem look at the following tutorial.
Spinnerdrop
That's a good tutorial. You need a listener to listen to a onclick method. Follow that and I think you will get there. If you need more help, feel free to ask
The following is for if you need to check whether a string is zero or not.
The method length() wil return a int value of the length of the given string. In your case that value musn't become 0 (zero).
if((value_1.length() != 0 ) && (value_2.length() != 0) && (value_3.length() != 0) && (value_4.length() !=0) && (value_5.length() !=0))
{
//Your code
} else {
//handle error
}
PS: Don't use android-studio tag, that's only for questions about the ide. Stick to android tag.

Do not change focus if edit text is empty

I am working with validations in edit text. I have 5 edit text in my form and what I want is if the any of the edit text is null then it should not break its focus. I have tried verified answer from this link.
but in my case of 5 edit text its not working properly.
Please Help, Thanks.
Try this
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String val1=editText1.getText().toString().trim();
String val2=editText2.getText().toString().trim();
String val3=editText3.getText().toString().trim();
String val4=editText4.getText().toString().trim();
String val5=editText5.getText().toString().trim();
if(TextUtils.isEmpty(val5)){
editText5.requestFocus();
editText1.setFocusable(false);
editText2.setFocusable(false);
editText3.setFocusable(false);
editText4.setFocusable(false);
}else {
editText4.setFocusable(true);
}
if(TextUtils.isEmpty(val4)){
editText4.requestFocus();
editText1.setFocusable(false);
editText2.setFocusable(false);
editText3.setFocusable(false);
}else {
editText3.setFocusable(true);
editText1.setFocusable(false);
editText2.setFocusable(false);
editText4.setFocusable(false);
}
if(TextUtils.isEmpty(val3)){
editText3.requestFocus();
editText1.setFocusable(false);
editText3.setFocusable(false);
editText4.setFocusable(false);
}else {
editText2.setFocusable(true);
}
if(TextUtils.isEmpty(val2)){
editText2.requestFocus();
}else {
editText1.setFocusable(true);
}
if(TextUtils.isEmpty(val1)){
editText1.requestFocus();
}
if(!TextUtils.isEmpty(val1) &&
!TextUtils.isEmpty(val2) &&
!TextUtils.isEmpty(val3) &&
!TextUtils.isEmpty(val4) &&
!TextUtils.isEmpty(val5) ){
Toast.makeText(MainActivity.this, "PASS", Toast.LENGTH_SHORT).show();
}
}
});
First check EditText empty,null or matches to some specific text
based on your requirement.
Check 5 EditTexts using if & else if statements. if a condition is true, change things to EditText which
you want to do.
In else part, set things you want to set if, if or else if conditions false.
According to my understanding in your question, I wrote a code. try it.
final EditText et1 = (EditText) findViewById(R.id.et1); //get referance to all 5 EditTexts. I showed for 2 EditTexts
final EditText et2 = (EditText) findViewById(R.id.et2);
if (et1.getText().toString().length() < 1) { //check EditText empty or not.
//do what you want to edit/change in EditText. like below.
et1.setFocusableInTouchMode(true);
et1.requestFocus();
} else if (et2.getText().toString().length() < 1) {
//Check for second EditText empty or not. Do this for all 5 edit texts.
et2.setFocusableInTouchMode(true);
et2.requestFocus();
}
else{
//if All EditTexts not empty. do what you want to edit/change in edittext.
et1.setFocusableInTouchMode(false);
et2.setFocusableInTouchMode(false);
}

While setting value in edit text change the color of the value?

I have 8 edittext fields some of which are set by the user, on pressing the CALCULATE button the edittext field which are having zero get filled with the calculated value. I want while setting the value in the edittext that has zero the color of the values will change, just to display the change to user.
I am new to android that why i dont know the proper functons.
go.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (setFlag) {
eg1.setText("");
eg2.setText("");
eg3.setText("");
eg4.setText("");
eg5.setText("");
eg6.setText("");
eg7.setText("");
eg8.setText("");
tg.setText("");
}
}
});
bg.setOnClickListener(new OnClick());
}
setFlag = true;
if (ng1 == 0)
{
if(Goal>10)
eg1.setText("");
else
eg1.setText(Float.toString(Goal));
}
if (ng2 == 0)
{
if(Goal>10)
eg2.setText("");
else
eg2.setText(Float.toString(Goal));
}
if (ng3 == 0){
if(Goal>10)
eg3.setText("");
else
eg3.setText(Float.toString(Goal));
}
if (ng4 == 0){
if(Goal>10)
eg4.setText("");
else
eg4.setText(Float.toString(Goal));
}
if (ng5 == 0){
if(Goal>10)
eg5.setText("");
else
eg5.setText(Float.toString(Goal));
}
if (ng6 == 0){
if(Goal>10)
eg6.setText("");
else
eg6.setText(Float.toString(Goal));
}
if (ng7 == 0){
if(Goal>10)
eg7.setText("");
else
eg7.setText(Float.toString(Goal));
}
if (ng8 == 0){
if(Goal>10)
eg8.setText("");
else
eg8.setText(Float.toString(Goal));
}
May this help you.
EditText editText1=(EditText) findViewById(R.id.edittext1);
String textVal=editText1.getText().toString();
if(textVal.equals("0")){
editText1.setTextColor(Color.RED);
}
Hi Puneet i am just giving you the idea, Just before setting the values in different 8 Edittext. You can check for the value whether it is 0. If 0 then change the color.
if(eg1.getText().toString().trim().equals("0"))
eg1.setTextColor(Color.RED);
else
eg1.setTextColor(Color.BLACK);
Above is just a sample code.if you have any query, Please comment.

Categories

Resources