Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android CustomAttribute in MotionLayout to change Text in TextView

Anyone can help me with the correct way to change TextView's text in MotionLayout... this is what I doing.

I'm testing the MotionLayout on a Simple App... I reach the part on the Motion tutorials about CustomAttributes

With them you can change the BackgroundColor of a View, also the textColor using customColorValue

In this case you can see it works very well changing this values in the start and end scene:

        <CustomAttribute
            motion:attributeName="backgroundColor"
            motion:customColorValue="#004A6D" />

        <CustomAttribute
            motion:attributeName="textColor"
            motion:customColorValue="#000000" />

enter image description here

Also I note that there is a customStringValue so I think I can change the TextView text to "BEFORE" -> "AFTER". But when I try to set this with CustomAttribute the app crash.

In the start scene:

<CustomAttribute
            motion:attributeName="Text"
            motion:customStringValue="BEFORE" />

And in the final scene:

        <CustomAttribute
            motion:attributeName="Text"
            motion:customStringValue="AFTER" />

Outside MotionScene the textView text is TEST:

  • When I set the CustomAttribute only for the end scene... the text change from initial value TEST to the end AFTER value... so it partially works but never return to the initial state.
  • This happend also when there is not initial text setted on the TextView. It works partially.

enter image description here

So... anyone can help me with the correct way to change TextView's text in MotionLayout.

like image 854
Oscar Méndez Avatar asked Nov 10 '18 05:11

Oscar Méndez


People also ask

What is motionlayout in Android Studio?

MotionLayout is a layout type that helps you manage motion and widget animation in your app. MotionLayout is a subclass of ConstraintLayout and builds upon its rich layout capabilities. As part of the ConstraintLayout library, MotionLayout is available as a support library and is backwards-compatible to API level 14. Figure 1.

How to make textview animation with fadingtextview in Android?

You will be using a 3rd party library called FadingTextView to help you make this smooth TextView animation. 1- Open up Android Studio and open any project that you have in your computer. Create new Android Studio project or open existing project. ( Large preview) 2- Open up build.gradle (module:app) and add the library in the dependencies.

Is it possible to change the text color of a motionlayout?

I'm testing the MotionLayout on a Simple App... I reach the part on the Motion tutorials about CustomAttributes With them you can change the BackgroundColor of a View, also the textColor using customColorValue In this case you can see it works very well changing this values in the start and end scene:

What is edittext in Android?

Android – EditText on text change EditText is used to read input from user. A listener could be attached to the EditText to execute an action whenever the text is changed in the EditText View.


3 Answers

you can do it programmatically using a TransitionListener like this:

motionLayout.setTransitionListener(object : MotionLayout.TransitionListener {
    override fun onTransitionTrigger(p0: MotionLayout?, p1: Int, p2: Boolean, p3: Float) {
        // ADD YOUR CODE HERE
    }

    override fun onTransitionStarted(p0: MotionLayout?, p1: Int, p2: Int) {
        // ADD YOUR CODE HERE
    }

    override fun onTransitionChange(p0: MotionLayout?, p1: Int, p2: Int, p3: Float) {
        textView.text = if(p3==0f) "before" else "after"
    }

    override fun onTransitionCompleted(p0: MotionLayout?, p1: Int) {

    }
})
like image 61
SwaiX Avatar answered Sep 21 '22 01:09

SwaiX


Easy way of doing this is

 <ConstraintSet android:id="@+id/start">
    <Constraint
        android:id="@id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
      >

        <CustomAttribute
            app:attributeName="textColor"
            app:customColorValue="#1D1D1D" />
    </Constraint>
</ConstraintSet>

    <Constraint
        android:id="@id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">

        <CustomAttribute
            app:attributeName="textColor"
            app:customColorValue="#FFFFFF" />
    </Constraint>
</ConstraintSet>

Change motion to app

like image 21
Amirhossein Rashidi Avatar answered Sep 23 '22 01:09

Amirhossein Rashidi


I test and like this

enter image description here

//constraintSetStart
<ConstraintSet...>
<Constraint
  android:id="@+id/button">
    <CustomAttribute
       motion:attributeName="Text"
       motion:customStringValue="CLOSE"/>
</Constraint>
</ConstraintSet>

//constraintSetEnd
<ConstrainSet..>
<Constraint
   android:id="@+id/button">
     <CustomAttribute
       motion:attributeName="Text"
       motion:customStringValue="OPEN"/>
 </Constraint>
</ConstraintSet>
like image 41
Andi Tenroaji Ahmad Avatar answered Sep 21 '22 01:09

Andi Tenroaji Ahmad