Combining Urdu String through Data Binding

Today, I had a case in my app where there was an EditText and TextView. The goal was that when EditText is changed, then TextView value will be updated instantly. So TextView value will be like this:

    textView = "SOME URDU STRING 1" + editTextValue + "ANOTHER URDU STRING"

First, I added a String value in my strings.xml file which was like this:

    <resources>
        <string name="app_name">AppName</string>
        <string name="sarbarah_str">آپ کا گھر کے سربراہ %s سے کیا رشتہ ہے؟</string>        // My String
    </resources>

Please note the %s in the string. That’s where magic happens. Then in my layout, I had EditText and TextView like this:

<?xml version="1.0" encoding="utf-8"?>
<layout 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">

    <data>
        <import type="com.wajahatkarim3.myapp.R"/>
    </data>

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".DemoActivity">


        <EditText
            android:id="@+id/editText"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:onTextChanged='@{(text, start, before, count) -> textView.setText(String.format(context.getString(R.string.sarbarah_str, text)))}' />

        <TextView
            android:id="@+id/textView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginStart="8dp"
            android:layout_marginTop="8dp"
            android:text="@string/sarbarah_str"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/editText" />

    </android.support.constraint.ConstraintLayout>
</layout>

So, the main line is:

    android:onTextChanged='@{(text, start, before, count) -> textView.setText(String.format(context.getString(R.string.sarbarah_str, text)))}'

So, whatever text is entered in EditText, that is added in place of %s in the string, and that value is set to the TextView.

And that’s it.


If you liked this article, you can read my new articles below:


Wajahat Karim is Pakistan’s first Google Developer Expert in Android. As an experience Android developer, he deeply cares about it and keeps writing and speaking about it. He has written two worldwide 300+ pages books on Android development with more than 100 articles around the internet either on his website or his Medium publications. He is also a passionate contributor in open source and has created many Android libraries used by thousands of developers in their apps worldwide. As active public speaker, he spends lots of time giving talks in conferences and motivating people on Android development. You can find Wajahat most active on Twitter @WajahatKarim where he regularly shares all the good stuff about Android and community building.

Author's picture

Wajahat Karim

🔥 Google Dev Expert (GDE) in Android .
📱 Android Dev. 💻 Open Source Contributor . 📝 Technical Writer . 🎤 Public Speaker

Senior Android Developer

Karachi, Pakistan