Aditya Rao Aditya Rao - 3 months ago 16
Java Question

How to do enable a button and change its colour when textfield has some value?

I have tried many answers already given to this, but nothing seems to work properly. And the answers are only for changing state of the button, not colour and text. What am I missing?

I am a very VERY new learner to both Android and programming. And this is my first question on Stack Overflow. Hope it's as per guidelines.

I have a login page which looks like below (img1) [disabled][1]. If textfield has any value, the button should get enabled (img2) [enabled][2]. The Java and XML files are given below.

package io.kaapi.kaapimobileassistant.Activities;
import android.content.Intent;
import android.net.Uri;
import android.support.design.widget.TextInputLayout;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.SpannableString;
import android.text.style.UnderlineSpan;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.TextView;
import io.kaapi.kaapimobileassistant.Misc.StorageManager;
import io.kaapi.kaapimobileassistant.R;

public class LoginActivity extends AppCompatActivity {

private final static String TAG = "LoginActivity";
private Button login_button;
private TextInputLayout login_activation_layout;
private EditText login_activation_code;
private LinearLayout login_signup;

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

login_button = (Button) findViewById(R.id.login_button);
login_activation_layout = (TextInputLayout) findViewById(R.id.login_acitivation_layout);
login_activation_code = (EditText) findViewById(R.id.login_activation_code);
login_signup = (LinearLayout) findViewById(R.id.login_signup);

login_button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Log.v(TAG, "Login pressed");
String activation_code_layout = login_activation_layout.getEditText().getText().toString();
Log.v(TAG, "Layout "+activation_code_layout);
String activation_code = login_activation_code.getText().toString();
Log.v(TAG, "Code "+activation_code);
if(activation_code.equalsIgnoreCase("")){
Log.v(TAG, "It's blank");
login_activation_layout.setError("Please enter an activation code");
} else {
Log.v(TAG, "Call login API, validate and show errors or login");
//StorageManager.write(LoginActivity.this, null, "client_domain", "http://ankit50.kaapi.io");
//StorageManager.write(LoginActivity.this, null, "client_logo", "http://cdn.kaapi.io/static");
startActivity(new Intent(LoginActivity.this, HomeActivity.class));
}
}
});

login_activation_code.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {

}

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
enableSubmitIfReady();
}

@Override
public void afterTextChanged(Editable s) {

}

public void enableSubmitIfReady() {

Button login_button = (Button) findViewById(R.id.login_button);

if(login_activation_code.toString().trim().length()==0){
login_button.setEnabled(false);
} else {
login_button.setEnabled(true);

}
}
});



login_signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Uri uri = Uri.parse("https://business.kaapi.io");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
startActivity(intent);
}
});
}
}


The XML file is below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="@android:color/white"
android:gravity="center_vertical|center_horizontal"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="io.kaapi.kaapimobileassistant.Activities.LoginActivity">

<ImageView
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_marginLeft="71dp"
android:layout_marginRight="71dp"
android:layout_marginTop="66dp"
android:src="@drawable/kaapi_logo_login"
android:scaleType="fitCenter"/>

<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="28dp"
android:textStyle="bold"
android:gravity="center"
android:textColor="@color/colorTitle"
android:text="Activate Mobile Assistant"
android:textSize="24sp" />


<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:layout_marginTop="36dp"
android:orientation="horizontal">


<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorText"
android:src="@drawable/ic_info_outline_black_24dp"/>

<TextView

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/colorText"
android:textSize="14sp"
android:textStyle="italic"
android:layout_marginLeft="5dp"
android:text="The code was sent to you in sign up email and your web dashboard." />

</LinearLayout>

<android.support.design.widget.TextInputLayout
android:id="@+id/login_acitivation_layout"
android:layout_width="match_parent"
android:layout_marginTop="29dp"
android:layout_height="wrap_content">

<EditText
android:id="@+id/login_activation_code"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:layout_marginLeft="24dp"
android:layout_marginRight="24dp"
android:maxLength="20"
android:hint="Activation code"
android:maxLines="1"
android:textSize="14sp" />
</android.support.design.widget.TextInputLayout>

<Button
android:id="@+id/login_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
style="@style/Widget.AppCompat.Button"
android:text="Activate"/>


<LinearLayout
android:layout_marginTop="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|top"
android:orientation="horizontal">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12sp"
android:text="New to Kaapi? " />

<LinearLayout
android:id="@+id/login_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center|top"
android:orientation="vertical">

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center|top"
android:text="Sign up first"
android:textColor="@color/colorPrimary"
android:textSize="12sp" />

<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="@color/colorPrimary" />
</LinearLayout>
</LinearLayout>

</LinearLayout>

Answer

try this

public void enableSubmitIfReady() {

        Button login_button = (Button) findViewById(R.id.login_button);

        if(login_activation_code.toString().trim().length()==0){
            login_button.setClickable(false);
            login_button.setBackgroundColor(getResources().getColor(R.color.holo_light_green));// change color here so it's look like button disable
        } else {
            login_button.setClickable(true);
            login_button.setBackgroundColor(getResources().getColor(R.color.holo_dark_green));
        }
    }