Reputation: 791
I have a custom textView, which placed inside of LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools">
<TextView
android:id="@+id/timerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="2"
android:ellipsize="end"
tools:text="Only on 22 of July, 2019"
android:textColor="@color/white" />
</LinearLayout>
And CustomTimerView with some logic
class CustomTimerView(context: Context, attributeSet: AttributeSet) : LinearLayout(context, attributeSet) {
private var textView: TextView
init {
LinearLayout.inflate(context, R.layout.customTimer_layout, this)
textView = findViewById(R.id.timerView)
}
fun setDates(ob: Promo) {
val startDate = getStartDate(ob)
val endDate = getEndDate(ob)
if (startDate.get(Calendar.MONTH) != endDate.get(Calendar.MONTH)) {
textView.text = getInDifferentMonths(context!!, ob)
} else {
if (startDate.get(Calendar.DAY_OF_MONTH) == endDate.get(Calendar.DAY_OF_MONTH)) {
textView.text = getOnlyOneDay(context!!, ob)
} else {
textView.text = getInOneMonth(context!!, ob)
}
}
}
}
Obviosly, LinearLayout is useless. But how can I remove this without moving my textView data to code and to child in other layouts?
Upvotes: 1
Views: 48
Reputation: 686
You can substitute the linear layout with the textView:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="2"
android:ellipsize="end"
tools:text="Only on 22 of July, 2019"
android:textColor="@color/white"
/>
About the class, I don't know if I understood correctly, but I'll give it a guess:
class CustomTimerView(context: Context, attributeSet: AttributeSet) : TextView(context, attributeSet) {
init {
TextView.inflate(context, R.layout.custom_timer_layout, null)
}
fun setDates(ob: Promo) {
val startDate = getStartDate(ob)
val endDate = getEndDate(ob)
if (startDate.get(Calendar.MONTH) != endDate.get(Calendar.MONTH)) {
this.text = getInDifferentMonths(context!!, ob)
} else {
if (startDate.get(Calendar.DAY_OF_MONTH) == endDate.get(Calendar.DAY_OF_MONTH)) {
this.text = getOnlyOneDay(context!!, ob)
} else {
this.text = getInOneMonth(context!!, ob)
}
}
}
}
Upvotes: 2