I am currently trying to create a highlighter function for my app where on a button being clicked by the user the program checks to see if the editText TextColor is black, default, or Yellow, highlighted, then switches between the two.
I am using an onClickListener if Statement:
hB0.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (ed1.getTextColors().equals(Color.BLACK)){
ed1.setTextColor(Color.YELLOW);
} else {
ed1.setTextColor(Color.BLACK);
}
}
});
I have set the textColor of the EditText to Color.Black in the XML file and I have looked for similar problems with little to show for it. Thank you for your time.
Edit: The problem is when I press the button the text stays the same colour and doesn't switch between the two.
Edit2: To take it further, when I comment out the if statement and make the button simply change the text colour to Color.YELLOW it works perfectly. I just want to be able to switch the colour BACK to black.
Edit 3: The XML Code for the highlighter button and the textView I want to change the text color in.
<Button
android:id="@+id/highLighter0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="9dp"
android:minWidth="4dp"
android:minHeight="10dp"
android:text="H"
android:textSize="12sp"
app:layout_constraintBottom_toBottomOf="@+id/slot0"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintLeft_toRightOf="@+id/slot0"
app:layout_constraintTop_toTopOf="@+id/slot0" />
<EditText
android:id="@+id/slot0"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:ems="10"
android:gravity="center"
android:hint="6 AM"
android:inputType="textCapSentences|textNoSuggestions|textMultiLine"
android:lines="4"
android:maxLines="4"
android:minLines="1"
android:rotationX="0"
android:textColor="@color/black"
app:layout_constraintEnd_toStartOf="@+id/highLighter0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/shoppingListButton"
tools:layout_conversion_absoluteHeight="45dp"
tools:layout_conversion_absoluteWidth="370dp" />
EditText @id=slot0 is ed1 in the java code.
You need to change your code with below code:
if (ed1.getCurrentTextColor() == Color.BLACK) {
ed1.setTextColor(Color.YELLOW);
} else {
ed1.setTextColor(Color.BLACK);
}
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.