I have looked over the recommended similar questions and tried many different approaches but so far they haven't worked and I am not sure why. Maybe I implemented them wrong but unsure.
What I am trying to do is change the drawable of a button to a brighter color if two Edittext fields both go from being empty to having text in them.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".RegistrationActivity"
android:orientation="vertical">
<TextView
android:layout_width="204dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:layout_marginTop="33dp"
android:text="My email and password are"
android:textSize="32dp"
android:fontFamily="@font/roboto_black"
android:textColor="@android:color/holo_green_light" />
<EditText
android:id="@+id/emailReg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:fontFamily="@font/roboto_lightitalic"
android:hint="email"
android:inputType="textEmailAddress"
android:layout_marginLeft="33dp"
android:layout_marginTop="27dp"/>
<EditText
android:id="@+id/passwordReg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="password"
android:ems="10"
android:fontFamily="@font/roboto_lightitalic"
android:inputType="textWebPassword"
android:layout_marginLeft="33dp"
android:layout_marginTop="17dp"/>
<TextView
android:id="@+id/textView"
android:layout_width="323dp"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:layout_marginTop="23dp"
android:ems="10"
android:justificationMode="inter_word"
android:text="Clicking 'Continue' will send a verification email to the address above. This will be used to log you in at future dates." />
<ImageButton
android:id="@+id/registerReg"
android:layout_width="wrap_content"
android:layout_height="52dp"
android:layout_marginLeft="32dp"
android:layout_marginTop="17dp"
android:layout_marginRight="32dp"
android:background="@null"
android:scaleType="fitCenter"
/>
</LinearLayout>
and the activity:
public class RegistrationActivity extends AppCompatActivity {
//VARIABLES
private ImageButton mRegister;
private EditText mEmail;
private EditText mPassword;
private FirebaseAuth mAuth;
private FirebaseAuth.AuthStateListener fireBaseAuthStateListener;
private DatabaseReference userRef;
String emailStr;
String passwordStr;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_registration);
//VARIABLE IDS
mEmail = (EditText) findViewById(R.id.emailReg);
mPassword = (EditText) findViewById(R.id.passwordReg);
mRegister = (ImageButton) findViewById(R.id.registerReg);
mAuth = FirebaseAuth.getInstance();
mEmail.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if (hasFocus) {
emailStr = mEmail.getText().toString().trim();
}
}
});
mPassword.setOnFocusChangeListener(new View.OnFocusChangeListener() {
@Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
passwordStr = mPassword.getText().toString().trim();
}
}
});
changeButtonDrawable(emailStr, passwordStr);
//REGISTER BUTTON
mRegister.setOnClickListener(new View.OnClickListener() {
final String email = mEmail.getText().toString().trim();
final String password = mPassword.getText().toString().trim();
@Override
public void onClick(View v) {
mAuth.createUserWithEmailAndPassword(email, password).addOnCompleteListener(RegistrationActivity.this, new OnCompleteListener<AuthResult>() {
@Override
public void onComplete(@NonNull Task<AuthResult> task) {
firebaseUtility fU = new firebaseUtility();
String currentUserId = fU.getUserIdString();
DatabaseReference userRef = fU.getUserDatabaseRef(currentUserId);
Map userInfo = new HashMap();
userInfo.put("email", email);
userInfo.put("profileImageUrl", "default");
userRef.updateChildren(userInfo);
if (task.isSuccessful()) {
Intent intent = new Intent(RegistrationActivity.this, MainActivity.class);
startActivity(intent);
finish();
}
}
});
}
});
}
@Override
public void onStart() {
super.onStart();
mAuth = FirebaseAuth.getInstance();
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
if(user != null) {
Intent intent = new Intent(RegistrationActivity.this, NameAgeZipActivity.class);
startActivity(intent);
finish();
}
}
private void changeButtonDrawable (String email, String password){
if (TextUtils.isEmpty(email) && TextUtils.isEmpty(password)){
mRegister.setImageResource(R.drawable.continue_btn_grey_on_grey);
} else if (!TextUtils.isEmpty(email) && !TextUtils.isEmpty(password)) {
mRegister.setImageResource(R.drawable.continue_btn_white_on_purple);
}
}
}
Basically, the ImageButton should change to the drawable continue_btn_white_on_purple if there is text in both fields, but right now it just stays grey. Been trying to figure this out for a while. Any help much appreciated.
Make a drawable file for your button like this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false">
<shape>
<!--if button is disabled it will show this color-->
<solid android:color="@color/disabledBtnBg" />
<corners android:radius="5dp" />
</shape>
</item>
<item>
<shape>
<solid android:color="#000000" />
<corners android:radius="5dp" />
</shape>
</item>
</selector>
Now set it to your button
<Button
...
android:background="@drawable/primary_btn_bg"
...
/>
then go to your Java file and Add this :
if (TextUtils.isEmpty(email) && TextUtils.isEmpty(password)){
mRegister. mVerifyBtn.setEnabled(false);
else if (TextUtils.isEmpty(email) && TextUtils.isEmpty(password)){
mRegister. mVerifyBtn.setEnabled(true);
A more real time-ish approach is to use TextChangedListener
mEmail.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() != 0 && mPassword.getText().toString().length != 0) {
mRegister.setImageResource(R.drawable.continue_btn_grey_on_grey);
} else {
mRegister.setImageResource(R.drawable.continue_btn_white_on_purple);
}
}
public void afterTextChanged(Editable s) {
}
});
mPassword.addTextChangedListener(new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.length() != 0 && mEmail.getText().toString().length != 0) {
mRegister.setImageResource(R.drawable.continue_btn_grey_on_grey);
} else {
mRegister.setImageResource(R.drawable.continue_btn_white_on_purple);
}
}
public void afterTextChanged(Editable s) {
}
});
So the changes on imageButton reflects instantly
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.