1

I have one button view in my layout.xml like below

<Button
                            android:id="@+id/btn_follow"
                            android:layout_width="140dp"
                            android:layout_height="40dp"
                            android:layout_marginTop="15dp"
                            android:layout_marginBottom="15dp"
                            android:background="@{viewModel.isMyAccount == 1 ? @drawable/bg_grey_corner_5 : (viewModel.isMyAccount == 2 ? @drawable/bg_gradient : @drawable/bg_strock_corner_5)}"
                            android:fontFamily="@font/popins_light"
                            android:letterSpacing="0.05"
                            android:onClick="@{()->viewModel.setOnItemClick(1)}"
                            android:text='@{viewModel.isMyAccount == 1 ? "Unfollow" : (viewModel.isMyAccount == 2 ? "Follow" : "Edit Profile")}'
                            android:textAllCaps="false"
                            android:textColor="@color/light_white"
                            android:textSize="13sp"
                            android:textStyle="bold"
                            tools:text="Follow">

                        </Button>

It have used model condition for display text label in button like below

android:text='@{viewModel.isMyAccount == 1 ? "Unfollow" : (viewModel.isMyAccount == 2 ? "Follow" : "Edit Profile")}'

but I am trying to support multiple language in my app and so I want use string resource there along with java model condition, Let me know if someone have idea about do it, I am new and learning android yet. Thanks!

2 Answers 2

1

You can use:

android:text="@{condition ? @string/follow: @string/unfollow}
Sign up to request clarification or add additional context in comments.

3 Comments

so it will be like @{viewModel.isMyAccount == 1 ? context.getString(R.string.follow) ?
yes, if it doesn't work, try this android:text="@{condition ? @string/follow: @string/unfollow}"
@string/follow is working fine! edit your answer and let me accept it
0

Instead of hard coding the follow and unfollow, retrieve them from string resources using then you can add additional string files for all the languages your app will support. So the system will default to English for example at all times but if the app is being used in other countries for example France and there is Fr resource then the system will grab that file and display the contents in French. But remember it's your responsibility to add French translation for all the strings used in your app.

1 Comment

You are saying that I should display text using fragment instead of xml ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.