Search code examples
javaandroidxmlcolorsandroid-edittext

How can I change the color of my edit text which is entered by user?


I have created a signup page and I select the hint color blue now I want to change the color of text which is entered in the edit text please tell me how I can change the color of text

Here is the code of my XML file

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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="match_parent"
tools:context=".Signup"
android:background="@color/purple_500">

<LinearLayout
    android:id="@+id/linearLayout2"
    android:layout_width="match_parent"
    android:layout_height="660dp"
    android:layout_marginTop="80dp"
    android:background="@drawable/linear_round"
    android:orientation="vertical"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent">

</LinearLayout>

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:text="Please Signup..."
    android:textColor="@color/white"
    android:textStyle="bold"
    android:textSize="29sp"
    android:gravity="center"
    android:layout_marginTop="16dp"
    android:id="@+id/textView">
</TextView>

<EditText
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:textColor="@color/black"
    android:hint="Enter your name"
    android:layout_marginTop="250dp"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:gravity="center"
    android:textColorHint="@color/purple_500"
    android:id="@+id/name"/>

<EditText
    android:id="@+id/pass"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:gravity="center"
    android:hint="Enter your password"
    android:inputType="textPassword"
    android:textColor="@color/black"
    android:textColorHint="@color/purple_500"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/name" />

<EditText
    android:id="@+id/email"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:gravity="center"
    android:hint="Enter your valid e-mail"
    android:inputType="textEmailAddress"
    android:textColor="@color/black"
    android:textColorHint="@color/purple_500"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/pass" />

<EditText
    android:id="@+id/number"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    android:gravity="center"
    android:hint="Enter your cell number"
    android:inputType="phone"
    android:textColor="@color/black"
    android:textColorHint="@color/purple_500"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintHorizontal_bias="0.0"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/email" />

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/submit"
    android:layout_width="445dp"
    android:layout_height="163dp"
    android:layout_centerInParent="true"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/number"
    android:layout_marginBottom="100dp"
    app:lottie_autoPlay="true"
    app:lottie_loop="true"
    app:lottie_rawRes="@raw/submit" />

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/trial"
    android:layout_width="match_parent"
    android:layout_height="40dp"
    android:layout_centerInParent="true"
    android:layout_marginLeft="30dp"
    android:layout_marginRight="30dp"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toBottomOf="@+id/submit"
    app:lottie_autoPlay="true"
    app:lottie_loop="true"
    app:lottie_rawRes="@raw/trial" />

<com.airbnb.lottie.LottieAnimationView
    android:id="@+id/signup_animation"
    android:layout_width="200dp"
    android:layout_height="200dp"
    android:layout_centerInParent="true"
    app:layout_constraintBottom_toTopOf="@+id/name"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"
    android:layout_marginTop="70dp"
    app:lottie_autoPlay="true"
    app:lottie_loop="true"
    app:lottie_rawRes="@raw/signup" />
</androidx.constraintlayout.widget.ConstraintLayout>

And how can I add the marquee effect on my text view? Can I make text view scrolling from right to left?


Solution

  • If TextView fill whole width you need to add these lines of code

    <TextView
        .
        .
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        />
    

    also do not forget to set selected for TexrtView as below

    textview.setSelected(true);
    

    and if TextView does not fill width the only thing is to call this method and pass TextView to it.

    Kotlin

        fun addMarquee(textView: TextView) {
        textView.viewTreeObserver.addOnGlobalLayoutListener(object :
            ViewTreeObserver.OnGlobalLayoutListener {
            override fun onGlobalLayout() {
                val pixels = textView.measuredWidth - 1
                val params = textView.layoutParams
                params.width = pixels
                textView.layoutParams = params
                textView.isSelected = true
                textView.ellipsize = TextUtils.TruncateAt.MARQUEE
                textView.isSingleLine = true
                textView.marqueeRepeatLimit = -1
                textView.viewTreeObserver.removeOnGlobalLayoutListener(this)
            }
        })
    }
    

    Java

    public void addMarquee(TextView textView) {
        textView.getViewTreeObserver().addOnGlobalLayoutListener(
                new ViewTreeObserver.OnGlobalLayoutListener() {
            @Override
            public void onGlobalLayout() {
    
                int pixels = textView.getMeasuredWidth() - 1;
                ViewGroup.LayoutParams params = textView.getLayoutParams();
                params.width = pixels;
                textView.setLayoutParams(params);
                textView.setSelected(true);
                textView.setEllipsize(TextUtils.TruncateAt.MARQUEE);
                textView.setSingleLine(true);
                textView.setMarqueeRepeatLimit(-1);
                textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
            }
        });
    }
    

    NOTE: this method only work when textView width is wrap_content.