Reputation: 1056
I have button and text fields like below in my layout file
<EditText
android:id="@+id/et_password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView4"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="@+id/btn_app_signup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/et_password"
android:layout_centerHorizontal="true"
android:layout_marginTop="63dp"
android:background="@drawable/button"
android:text="SIGN UP"
android:textColor="@color/white" />
How can I change this background into @drawable/button_success
to if all edit text fields filled my Activity class like below
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
btn_app_signup.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String name = et_name.getText().toString();
String email = et_email.getText().toString();
String password = et_password.getText().toString();
registerAccount(name, email, password);
}
});
}
private void registerAccount(String name, String email, String password) {
if(TextUtils.isEmpty(name) || TextUtils.isEmpty(email) || TextUtils.isEmpty(password)) {
Toast.makeText(SignupActivity.this, "All fields required", Toast.LENGTH_LONG).show();
}
else {
//save data in datbase
}
}
How can I do that if I fill all my edit text fields fill change the button button background
Upvotes: 2
Views: 1987
Reputation: 9732
Try this
private void registerAccount(String name, String email, String password) {
if(!TextUtils.isEmpty(name)&& !TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)){
btn_app_signup.setBackground(ContextCompat.getDrawable(this,R.drawable.ic_android_black_24dp));// set here your backgournd to button
// save data in database
}else {
Toast.makeText(this, "All fields required", Toast.LENGTH_LONG).show();
}
}
Edit
et_password.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (!TextUtils.isEmpty(et_name.getText().toString()) && !TextUtils.isEmpty(et_email.getText().toString()) && !TextUtils.isEmpty(et_password.getText().toString())) {
btn_app_signup.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_android_black_24dp));// set here your backgournd to button
}else {
btn_app_signup.setBackgroundResource(android.R.drawable.btn_default);
}
}
});
et_email.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (!TextUtils.isEmpty(et_name.getText().toString()) && !TextUtils.isEmpty(et_email.getText().toString()) && !TextUtils.isEmpty(et_password.getText().toString())) {
btn_app_signup.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_android_black_24dp));// set here your backgournd to button
}else {
btn_app_signup.setBackgroundResource(android.R.drawable.btn_default);
}
}
});
et_name.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable editable) {
if (!TextUtils.isEmpty(et_name.getText().toString()) && !TextUtils.isEmpty(et_email.getText().toString()) && !TextUtils.isEmpty(et_password.getText().toString())) {
btn_app_signup.setBackground(ContextCompat.getDrawable(MainActivity.this, R.drawable.ic_android_black_24dp));// set here your backgournd to button
}else {
btn_app_signup.setBackgroundResource(android.R.drawable.btn_default);
}
}
});
Upvotes: 3
Reputation: 779
et_name.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){
String name=et_name.getText().toString();
String email=et_email.getText().toString();
String password=et_password.getText().toString();
valiadte(name,email,password);
}
@Override
public void afterTextChanged(Editable s){
}
});
et_email.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){
String name=et_name.getText().toString();
String email=et_email.getText().toString();
String password=et_password.getText().toString();
valiadte(name,email,password);
}
@Override
public void afterTextChanged(Editable s){
}
});
et_password.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){
String name=et_name.getText().toString();
String email=et_email.getText().toString();
String password=et_password.getText().toString();
valiadte(name,email,password);
}
@Override
public void afterTextChanged(Editable s){
}
});
and invalidate function do
private void valiadte(String name,String email,String password){
if(!TextUtils.isEmpty(name)&&!TextUtils.isEmpty(email)&&!TextUtils.isEmpty(password){
btn_app_signup.setBackground(ContextCompat.getDrawable(this,R.drawable.ic_android_black_24dp));
// set here your backgournd to button
}
}
Upvotes: 2
Reputation: 402
You can simply change background with drawable
like this:
btn_app_signup.setBackground(getResources().getDrawable(R.drawable.button_success));
When you check your texts:
if(et_name.getText().toString().equals("") || et_email.getText().toString().equals("") || et_password.getText().toString().equals("")) {
Toast.makeText(SignupActivity.this, "All fields required", Toast.LENGTH_LONG).show();
}
Hope it helps.
Upvotes: 0