Skip to content
Advertisement

Use String Resource in layout xml along with Java

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!

Advertisement

Answer

You can use:

android:text="@{condition ? @string/follow: @string/unfollow}
User contributions licensed under: CC BY-SA
5 People found this is helpful
Advertisement