hide button if text length is < 1 in android studio

667 views Asked by At

I'm new to Android and Java and I'm busy editing an existing app. I am trying to hide a button if no text is getting pulled in from a webservice. I have made it work with another button when the text is being populated

if (textEvent.length() > 1) {
    buttonEventSetup.setVisibility(View.INVISIBLE);
}

but when I used:

if (textEvent.length() < 1) {
    buttonAccessControl.setVisibility(View.INVISIBLE);
}

nothing seems to happen.

I don't know if the code snippet is in the wrong place or something else is overwriting the code. Here is my activity code:

package com.example.dsouchon.myapplication;
    public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    Button buttonEventSetup =    (Button)findViewById(R.id.buttonEventSetup);

    if(Local.isSet(getApplicationContext(), "LoggedIn"))
    {
        String loggedInUser = Local.Get(getApplicationContext(), "LoggedIn");
        if(loggedInUser.length()>0)
        {
            buttonEventSetup.setVisibility(View.VISIBLE);
        }
        else
        {
            buttonEventSetup.setVisibility(View.GONE);
        }
    }
    else
    {
        buttonEventSetup.setVisibility(View.GONE);
    }

    if(Local.isSet(getApplicationContext(), "EventName"))
    {
        String event =  Local.Get(getApplicationContext(), "EventName");
        TextView textEvent = (TextView) findViewById(R.id.textEventName);
        textEvent.setText( event);
        Button buttonAccessControl = (Button)findViewById(R.id.buttonAccessControl);
        buttonAccessControl.setEnabled(true);

        //HIDES SET EVENT BUTTON WHEN EVENT IS SET
        if (textEvent.length() > 1) {
            buttonEventSetup.setVisibility(View.INVISIBLE);
        }
        if (textEvent.length() < 1) {
            buttonAccessControl.setVisibility(View.INVISIBLE);
        }
    }
    else
    {
        Button buttonAccessControl = (Button)findViewById(R.id.buttonAccessControl);
        buttonAccessControl.setEnabled(false);
    }

    if(Local.isSet(getApplicationContext(), "EventImage"))
    {
        TextView textEvent = (TextView) findViewById(R.id.textEventName);
        String result =  Local.Get(getApplicationContext(), "EventImage");

        ImageView imageViewEventImage = (ImageView)findViewById(R.id.imageViewEventImage);
        byte[] decodedString = Base64.decode(result, Base64.DEFAULT);
        Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
        imageViewEventImage.setImageBitmap(decodedByte);
    }
}

@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_main2, 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);
}

public void setupEvent(View view) {
    Intent intent = new Intent(MainActivity.this, SetupEvent.class );
    finish();
    startActivity(intent);
}

public void accessControl(View view) {
    Button buttonEventSetup = (Button)findViewById(R.id.buttonEventSetup);
    buttonEventSetup.setVisibility(View.GONE);

    Intent intent = new Intent(MainActivity.this, MainActivity21.class );
    finish();
    startActivity(intent);
}

public void Logoff(View view) {
    Local.Set(getApplicationContext(), "LoggedIn", "");
}

public void Login(View view) {
    final AlertDialog ad=new AlertDialog.Builder(this).create();
    MySOAPCallActivity cs = new MySOAPCallActivity();
    try {
        EditText userName = (EditText) findViewById(R.id.editUserName);
        EditText password = (EditText) findViewById(R.id.editPassword);
        String user = userName.getText().toString();
        String pwd = password.getText().toString();
        LoginParams params = new LoginParams(cs, user, pwd);

        Local.Set(getApplicationContext(), "UserName", user);
        Local.Set(getApplicationContext(), "Password", pwd);

        new CallSoapLogin().execute(params);
       // new CallSoapGetCurrentEvents().execute(params);

    } catch (Exception ex) {
        ad.setTitle("Error!");
        ad.setMessage(ex.toString());
    }
    ad.show();
}

public class CallSoapLogin extends AsyncTask<LoginParams, Void, String> {

    private Exception exception;

    @Override
    protected String doInBackground(LoginParams... params) {
        return params[0].foo.Login(params[0].username, params[0].password);
    }

    protected void onPostExecute(String result) {
        // TODO: check this.exception
        // TODO: do something with the feed
        try {

        TextView loginResult =(TextView)findViewById(R.id.labelLoginResult);
        loginResult.setVisibility(View.VISIBLE);
        loginResult.setText(result);

       // Button buttonUnsetEvent = (Button)findViewById(R.id.buttonUnsetEvent);
       // buttonUnsetEvent.setEnabled(true);

        //Spinner spinner2 = (Spinner)findViewById(R.id.spinner2);
        //spinner2.setEnabled(true);

        boolean LoginSuccessful = false;

        if(result.toLowerCase().contains("success"))
        {
            LoginSuccessful = true;
        }

        if (LoginSuccessful)
        {
            String user = Local.Get(getApplicationContext(), "UserName");
            Local.Set(getApplicationContext(), "LoggedIn", user);
            LinearLayout layoutLoggedIn = (LinearLayout)findViewById(R.id.layoutLoggedIn);
            layoutLoggedIn.setVisibility(View.VISIBLE);

            Button buttonEventSetup = (Button)findViewById(R.id.buttonEventSetup);
            buttonEventSetup.setVisibility(View.VISIBLE);

            LinearLayout layoutLogIn = (LinearLayout)findViewById(R.id.layoutLogIn);
            layoutLogIn.setVisibility(View.VISIBLE);

        }
        } catch (Exception ex) {
           String e3 = ex.toString();
        }
    }
}
private static class LoginParams {
    MySOAPCallActivity foo;
    String username;
    String password;

    LoginParams(MySOAPCallActivity foo, String username, String        password) {
        this.foo = foo;
        this.username = username;
        this.password = password;

       }
     }
}
2

There are 2 answers

0
Saurabh Padwekar On

You are getting the length of Textview that will not work.Use the text length.This should work

   String event =  Local.Get(getApplicationContext(), "EventName");
    //HIDES SET EVENT BUTTON WHEN EVENT IS SET
    if (event.length() > 1) {
        buttonEventSetup.setVisibility(View.VISIBLE);
    }
    if (event.length() < 1) {
        buttonAccessControl.setVisibility(View.INVISIBLE);
    }
0
Matthew Cawley On

In your code above textEvent is not a string value. It is an Object of type TextView. length() in this case will not return the length of the text contained within that element. You will need to explicitly get the text string before you can get the length of it. This is acquired using the getText() method on the TextView.

Your code should look like the following:

//HIDES SET EVENT BUTTON WHEN EVENT IS SET
        if (textEvent.getText().length() >= 1) {
            buttonEventSetup.setVisibility(View.VISIBLE);
        }
        if (textEvent.getText().length() < 1) {
            buttonAccessControl.setVisibility(View.INVISIBLE);
        }

You also have INVISIBLE on both cases in your posted code example.

Note that your code also does not handle cases where the text length == 1. You could simplify the whole block with the following.

Note: any value != 0 is classed as true

buttonEventSetup.setVisibility(textEvent.getText().length() ? View.VISIBLE : View.INVISIBLE);