Skip to content

Instantly share code, notes, and snippets.

@ajaypro
Last active February 12, 2020 05:39
Show Gist options
  • Save ajaypro/d07db922231474af87f4f10f68765a9e to your computer and use it in GitHub Desktop.
Save ajaypro/d07db922231474af87f4f10f68765a9e to your computer and use it in GitHub Desktop.
Displaying static and dynamic data in a view with a simple technique
UseCase: If we want to show in a textview "Model : BMW"
model - static data
BMW - dynamic data, If your doing a n/w call and in your model you have a model attribute for vehcile object, below is one of approach.
-------
define this in your strings.xml
<string name="display_model">Model %s</string>
* In case you have a data object vehicle as a livedata then you do something like this in your Viewmodel
* displayModel is a variable that will be used by data binding to show in your layout
val displayModel = Transformations.map(vehicle) {
app.applicationContext.getString(when(it.isCarLuxury){
true -> R.string.display_luxury
false -> R.string.display_normal
}, it.model)
}
* In your layout you define your databinding variable and in your textview call like this
<data>
<variable
name="detailViewModel"
type="com.android.vechile.detail.DetailViewModel" />
</data>
<TextView
android:id="@+id/moel_value_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{detailViewModel.displayModel}"
tools:text="Bentley" />
UI TextView
Model BMW
------------------------------
for integers
<string name="display_price">$%,.0f</string>
if you want to display price as $5,00,000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment