I am having problem in the View during notifyDataSetChanged in recyclerView. I implemented pull torefresh logic. So I refresh my RecyclerView and call notifyDataSetChanged. I create a linear layout dynamically and add it to the parent view. The Dynamic linear layout has images. It displays correctly when it loads initially. During pull to refresh, the images loads but disappears immediately. I couldnot understand what the problem would be?
Part of BindViewHolder
if(tempAvailable == true && heartAvailable == true && lungAvailable == true) {
for(i in 1..3) {
if(i == 1) {
val df = DecimalFormat("#.##");
var childLinearLayout:LinearLayout? = createChildView(BitmapFactory.decodeResource(context.getResources(), R.drawable.temperature), df.format(coreBodyTemp).toString(), context.getString(R.string.temperatureStr))
parentLinearLayout.addView(childLinearLayout)
//childLinearLayout = null
} else if(i == 2) {
var childLinearLayout:LinearLayout? = createChildView(BitmapFactory.decodeResource(context.getResources(), R.drawable.heart), heartRate.toString(), context.getString(R.string.heartRate))
parentLinearLayout.addView(childLinearLayout)
//childLinearLayout = null
} else if(i == 3) {
var childLinearLayout:LinearLayout? = createChildView(BitmapFactory.decodeResource(context.getResources(), R.drawable.lung), "", "")
parentLinearLayout.addView(childLinearLayout)
// childLinearLayout = null
}
}
} else if (tempAvailable) {
val df = DecimalFormat("#.##");
var childLinearLayout:LinearLayout? = createChildView(BitmapFactory.decodeResource(context.getResources(), R.drawable.temperature), df.format(coreBodyTemp).toString()+" \u00B0F", context.getString(R.string.temperatureStr))
parentLinearLayout.addView(childLinearLayout)
//childLinearLayout = null
} else if (heartAvailable) {
var childLinearLayout:LinearLayout? = createChildView(BitmapFactory.decodeResource(context.getResources(), R.drawable.heart), heartRate.toString()+" bpm", context.getString(R.string.heartRate))
parentLinearLayout.addView(childLinearLayout)
// childLinearLayout = null
} else if (lungAvailable) {
var childLinearLayout:LinearLayout? = createChildView(BitmapFactory.decodeResource(context.getResources(), R.drawable.lung), "", "")
parentLinearLayout.addView(childLinearLayout)
//childLinearLayout = null
}
The method which creates linear layout dynamically
fun createChildView(icon: Bitmap,readingVal: String, readingTag: String) : LinearLayout {
val lp = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT)
lp.weight = 1f
val parent: LinearLayout = LinearLayout(context)
parent.setOrientation(LinearLayout.HORIZONTAL)
parent.setLayoutParams(lp)
val imageView = ImageView(context)
val layoutParams = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT)
layoutParams.weight = 0.4f
layoutParams.height = 60
layoutParams.width = 60
layoutParams.leftMargin = 10
layoutParams.rightMargin = 10
layoutParams.topMargin = 10
layoutParams.bottomMargin =10
imageView.setLayoutParams(layoutParams)
imageView.setImageBitmap(icon)
parent.addView(imageView)
val readingLayout = LinearLayout(context)
readingLayout.setOrientation(LinearLayout.VERTICAL)
val lp1 = LinearLayout.LayoutParams(0, ViewGroup.LayoutParams.WRAP_CONTENT)
lp1.weight = 0.6f
lp1.topMargin = 10
readingLayout.setLayoutParams(lp)
val displayValue = TextView(context)
val layoutParams1 = LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, 0)
layoutParams1.weight = 0.7f
layoutParams1.topMargin = 24
layoutParams1.bottomMargin = 4
displayValue.setLayoutParams(layoutParams1)
displayValue.setText(readingVal)
displayValue.setTextSize(12f)
displayValue.setTextColor(Color.BLACK)
readingLayout.addView(displayValue)
val displayTag = TextView(context)
layoutParams1.weight = 0.3f
layoutParams1.topMargin = 0
displayTag.setLayoutParams(layoutParams1)
displayTag.setText(readingTag)
displayTag.setTextSize(8f)
displayTag.setTextColor(Color.BLACK)
displayTag.setAlpha(0.6f)
readingLayout.addView(displayTag)
parent.addView(readingLayout)
return parent
}
UPDATE:
I fixed this issue by putting setHasStableIds(true) in Recycler view constructor
From Android documentation,
setHasStableIds
void setHasStableIds (boolean hasStableIds)
Indicates whether each item in the data set can be represented with a
unique identifier of type Long.
You must set setHasStableIds(true) for your recycle view
Related
private fun setQuestion() {
val mainImg = findViewById<ImageView>(R.id.movieImgView)
val movieNameText = findViewById<TextView>(R.id.movieName)
val movieYearText = findViewById<TextView>(R.id.movieYear)
val movieRatingText = findViewById<TextView>(R.id.movieRatingText)
val seriesOrNoText = findViewById<TextView>(R.id.seriesOrNoText)
val btnNext = findViewById<Button>(R.id.btnNext)
movieList = Constants.getActionMovies()
val movie: ActionMovies? = movieList!![currentPosition - 1]
val displayMetrics = DisplayMetrics()
windowManager.defaultDisplay.getMetrics(displayMetrics)
val height = displayMetrics.heightPixels.toFloat()
val width = displayMetrics.widthPixels.toFloat()
mainImg.setImageResource(movie!!.moviePic)
movieNameText.text = movie.movieName
movieNameText.scaleX = width
movieNameText.scaleY = height
movieYearText.text = "(${movie.date.toString()})"
movieRatingText.text = movie.rating
if (movie.series) {
seriesOrNoText.text = "Yes"
} else {
seriesOrNoText.text = "No"
}
if (currentPosition == movieList!!.size) {
btnNext.text = "Return to \n Main Menu"
btnNext.setOnClickListener {
finish()
}
}
}
}
I'm trying to set the width and height of my MovieNameText, however it seems to have no effect at all, any help? I've defined the text size in the xml file to 30sp, I've tried setText and scaleX and textSize
To change the size of the text
movieNameText.textSize = 30f // or whatever value you would like to use
Aslong as your Textview is constrained and layout width and height is set to 'wrap content' it should scale with your app.
I am using MP chart in my project. I am using multiple dataset bar chart. I ma facing following issues:
I want to provide some space between legend and x-axis. Right now it is sticked with the axis. I check the documentation but didn't found any method for setting margin between axis and legend. (Attached screenshot - Highlighted as Issue 1)
The chart is showing an empty group in the beginning, which I want to remove.(Attached screenshot - Highlighted as Issue 2)
Below is my code:
tco_chart?.description?.isEnabled = false
tco_chart?.setPinchZoom(false)
tco_chart?.setDrawBarShadow(false)
val leftYAxis = tco_chart?.axisLeft
leftYAxis?.setDrawGridLines(false)
leftYAxis?.axisMinimum = 0f
tco_chart?.setDrawGridBackground(false)
val mv = MyMarkerView(this, R.layout.custom_marker_view)
mv.setChartView(tco_chart) // For bounds control
tco_chart.setMarker(mv)
val legend = tco_chart?.legend
legend?.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM)
legend?.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT)
legend?.setOrientation(Legend.LegendOrientation.HORIZONTAL)
legend?.setYOffset(0f)
legend?.setXOffset(10f)
legend?.setYEntrySpace(0f)
legend?.setTextSize(5f)
val axisLabels = ArrayList<String>()
axisLabels.add("Year 1")
axisLabels.add("Year 2")
axisLabels.add("Year 3")
axisLabels.add("Year 4")
axisLabels.add("Year 5")
val xAxis = tco_chart?.xAxis
xAxis?.granularity = 1f
xAxis?.position = XAxis.XAxisPosition.BOTTOM
xAxis?.setCenterAxisLabels(true)
xAxis?.setDrawLabels(true)
xAxis?.valueFormatter = IndexAxisValueFormatter(axisLabels)
val insuranceCostEntry = ArrayList<BarEntry>()
val insuranceCostDataModel = tcoResultModel.insurance_cost
if (insuranceCostDataModel != null) {
insuranceCostEntry.add(BarEntry(0f, insuranceCostDataModel.first_year.toFloat()))
insuranceCostEntry.add(BarEntry(1f, insuranceCostDataModel.second_year.toFloat()))
insuranceCostEntry.add(BarEntry(2f, insuranceCostDataModel.third_year.toFloat()))
insuranceCostEntry.add(BarEntry(3f, insuranceCostDataModel.fourth_year.toFloat()))
insuranceCostEntry.add(BarEntry(4f, insuranceCostDataModel.fifth_year.toFloat()))
}
val fuelCostEntry = ArrayList<BarEntry>()
val fuelCostDataModel = tcoResultModel.fuel_cost
if (fuelCostDataModel != null) {
fuelCostEntry.add(BarEntry(0f, fuelCostDataModel.first_year.toFloat()))
fuelCostEntry.add(BarEntry(1f, fuelCostDataModel.second_year.toFloat()))
fuelCostEntry.add(BarEntry(2f, fuelCostDataModel.third_year.toFloat()))
fuelCostEntry.add(BarEntry(3f, fuelCostDataModel.fourth_year.toFloat()))
fuelCostEntry.add(BarEntry(4f, fuelCostDataModel.fifth_year.toFloat()))
}
val serviceCostEntry = ArrayList<BarEntry>()
val serviceCostDataModel = tcoResultModel.service_cost
if (serviceCostDataModel != null) {
serviceCostEntry.add(BarEntry(0f, serviceCostDataModel.first_year.toFloat()))
serviceCostEntry.add(BarEntry(1f, serviceCostDataModel.second_year.toFloat()))
serviceCostEntry.add(BarEntry(2f, serviceCostDataModel.third_year.toFloat()))
serviceCostEntry.add(BarEntry(3f, serviceCostDataModel.fourth_year.toFloat()))
serviceCostEntry.add(BarEntry(4f, serviceCostDataModel.fifth_year.toFloat()))
}
val tyreChangeCostEntry = ArrayList<BarEntry>()
val tyreChangeCostDataModel = tcoResultModel.tyre_change_cost
if (tyreChangeCostDataModel != null) {
tyreChangeCostEntry.add(BarEntry(0f, tyreChangeCostDataModel.first_year.toFloat()))
tyreChangeCostEntry.add(BarEntry(1f, tyreChangeCostDataModel.second_year.toFloat()))
tyreChangeCostEntry.add(BarEntry(2f, tyreChangeCostDataModel.third_year.toFloat()))
tyreChangeCostEntry.add(BarEntry(3f, tyreChangeCostDataModel.fourth_year.toFloat()))
tyreChangeCostEntry.add(BarEntry(4f, tyreChangeCostDataModel.fifth_year.toFloat()))
}
val depreciationCostEntry = ArrayList<BarEntry>()
val depriciationCostDataModel = tcoResultModel.depreciation_cost
if (depriciationCostDataModel != null) {
depreciationCostEntry.add(BarEntry(0f, depriciationCostDataModel.first_year.toFloat()))
depreciationCostEntry.add(BarEntry(1f, depriciationCostDataModel.second_year.toFloat()))
depreciationCostEntry.add(BarEntry(2f, depriciationCostDataModel.third_year.toFloat()))
depreciationCostEntry.add(BarEntry(3f, depriciationCostDataModel.fourth_year.toFloat()))
depreciationCostEntry.add(BarEntry(4f, depriciationCostDataModel.fifth_year.toFloat()))
}
val year1Set: BarDataSet
val year2Set: BarDataSet
val year3Set: BarDataSet
val year4Set: BarDataSet
val year5Set: BarDataSet
var data = tco_chart?.data
if (data != null) {
if (data.dataSetCount > 0) {
year1Set = data.getDataSetByIndex(0) as BarDataSet
year1Set.values = insuranceCostEntry
year2Set = data.getDataSetByIndex(1) as BarDataSet
year2Set.values = fuelCostEntry
year3Set = data.getDataSetByIndex(2) as BarDataSet
year3Set.values = tyreChangeCostEntry
year4Set = data.getDataSetByIndex(3) as BarDataSet
year4Set.values = serviceCostEntry
year5Set = data.getDataSetByIndex(4) as BarDataSet
year5Set.values = depreciationCostEntry
data.notifyDataChanged()
tco_chart?.notifyDataSetChanged()
}
} else {
year1Set = BarDataSet(insuranceCostEntry, "Insurance Cost")
year1Set.color = Color.rgb(232, 128, 44)
year2Set = BarDataSet(fuelCostEntry, "Fuel Cost")
year2Set.color = Color.rgb(232, 199, 44)
year3Set = BarDataSet(tyreChangeCostEntry, "Tyre Change Cost")
year3Set.color = Color.rgb(10, 149, 221)
year4Set = BarDataSet(serviceCostEntry, "Service Cost")
year4Set.color = Color.rgb(47, 194, 144)
year5Set = BarDataSet(depreciationCostEntry, "Depriciation Cost")
year5Set.color = Color.rgb(105, 123, 192)
data = BarData(year1Set, year2Set, year3Set, year4Set, year5Set)
data.setValueFormatter(IndexAxisValueFormatter())
tco_chart?.data = data
}
val rightYAxis = tco_chart?.axisRight
rightYAxis?.isEnabled = false
tco_chart?.getBarData()?.setBarWidth(0.1f)
tco_chart?.xAxis?.axisMaximum = (0 + data.getGroupWidth(0.4f, 0.02f) * 5)
tco_chart?.groupBars(0f, 0.4f, 0.02f)
tco_chart?.invalidate()
I had the same issue, you can fix it with use some additional padding for chart.
Use this:
chart.setExtraOffsets(5f,5f,5f,15f)
Hope it helps.
You Give Offset to Legend Like ..
val legend = chart.legend
legend.xOffset = 10f
I'm currently using a Table Layout and programatically adding rows and buttons. However, when I add the views a lot of them go off-screen. Is there a way to programatically set the size to the portion of the screen.
I have decent experience with Android, but new to Kotlin.
Here is where I add the views
private fun setupTable () {
for(i in 0 until this.rowSize) {
val row = TableRow(context)
row.layoutParams
row.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
for(j in 0 until this.columnSize) {
val button = Button(context)
button.apply {
layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT)
text = "R $i C $j"
}
row.addView(button)
}
wordLayout?.addView(row)
}
}
Here is the picture for reference. Here I want a 10x10 table and to fit all the buttons inside the TableLayout.
Try the following code:
package net.androidly.androidlylayouts
class MainActivity : AppCompatActivity() {
val ROWS = 10
val COLUMNS = 5
val tableLayout by lazy { TableLayout(this) }
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
textView.text = "ROWS : $ROWS COLUMNS: $COLUMNS"
val lp = TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)
tableLayout.apply {
layoutParams = lp
isShrinkAllColumns = true
}
createTable(ROWS, COLUMNS)
}
fun createTable(rows: Int, cols: Int) {
for (i in 0 until rows) {
val row = TableRow(this)
row.layoutParams = ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT)
for (j in 0 until cols) {
val button = Button(this)
button.apply {
layoutParams = TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,
TableRow.LayoutParams.WRAP_CONTENT)
text = "R $i C $j"
}
row.addView(button)
}
tableLayout.addView(row)
}
linearLayout.addView(tableLayout)
}
}
Hope this helps.
I am trying to subclass ConstraintLayout to build a dynamic menu that I can pass in items and it will build a View with sub-views set up. I seem to be struggling with, in Kotlin, laying out things horizontally
I've tried a few things, I've tried laying the items out and constraining them to each other, with the first and last items constrained to the parent. and I've tried using a ConstraintSet for the multiple chain
This is how I initialize it.
val menuLayout = ManageableMenu(tc,listOf(
ManageableMenu.ManageableMenuItem("Item 1"),
ManageableMenu.ManageableMenuItem("Item 2"),
ManageableMenu.ManageableMenuItem("Item 3"),
ManageableMenu.ManageableMenuItem("Item 4")
))
menuLayout.listener = this
val menuLayoutConstraint =menuLayout .layoutParams as ConstraintLayout.LayoutParams
menuLayoutConstraint.topToBottom = actorName.id
menuLayoutConstraint.startToStart = thisLayout.id
menuLayoutConstraint.endToEnd = thisLayout.id
thisLayout.addView(menuLayout)
I tried...
class ManageableMenu(context: Context, val items: List<ManageableMenuItem>): ConstraintLayout(context) {
var textSize: Int = 17
var textColor: Int = Color.WHITE
var activeFragmentTitle: String = items[0].title
var listener: ManageableMenuChangeListener? = null
init {
this.id = View.generateViewId()
this.layoutParams =
ConstraintLayout.LayoutParams(convertToDP(100, this.context), ConstraintLayout.LayoutParams.WRAP_CONTENT)
val baseConstraint = ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
)
this.setBackgroundColor(Color.BLACK)
//Build Dividers
val dividers: MutableList<TextView> = mutableListOf()
if (items.size > 1) {
repeat(items.size - 1) {
val newView = TextView(this.context)
newView.id = View.generateViewId()
newView.text = "|"
newView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize + 8.toFloat())
newView.setTextColor(textColor)
dividers.add(newView)
}
}
val constraintSet = ConstraintSet()
constraintSet.clone(this)
val dividerIds: IntArray = IntArray(dividers.size)
for ((index, divider) in dividers.withIndex()) {
dividerIds.set(index, divider.id)
}
constraintSet.createHorizontalChainRtl(
dividerIds[0],
ConstraintSet.START,
this.id,
ConstraintSet.END,
dividerIds,
null,
ConstraintSet.CHAIN_SPREAD
)
constraintSet.applyTo(this)
for (divider in dividers) {
this.addView(divider)
}
}
data class ManageableMenuItem(val title: String) {
var id: Int? = null
}
interface ManageableMenuChangeListener {
fun onMenuItemChanged(var1: String)
}
}
This puts a stack of all "|" created on top of each other, all the way to the left of the view. (If I can attach pictures I will)
I also tried...
class ManageableMenu(context: Context, val items: List<ManageableMenuItem>): ConstraintLayout(context) {
var textSize: Int = 17
var textColor: Int = Color.WHITE
var activeFragmentTitle: String = items[0].title
var listener: ManageableMenuChangeListener? = null
init {
this.id = View.generateViewId()
this.layoutParams =
ConstraintLayout.LayoutParams(convertToDP(100, this.context), ConstraintLayout.LayoutParams.WRAP_CONTENT)
val baseConstraint = ConstraintLayout.LayoutParams(
ConstraintLayout.LayoutParams.WRAP_CONTENT,
ConstraintLayout.LayoutParams.WRAP_CONTENT
)
this.setBackgroundColor(Color.BLACK)
//Build Dividers
val dividers: MutableList<TextView> = mutableListOf()
if (items.size > 1) {
repeat(items.size - 1) {
val newView = TextView(this.context)
newView.id = View.generateViewId()
newView.text = "|"
newView.setTextSize(TypedValue.COMPLEX_UNIT_SP, textSize + 8.toFloat())
newView.setTextColor(textColor)
dividers.add(newView)
}
}
for ((index, divider )in dividers.withIndex()) {
val constraint = ConstraintLayout.LayoutParams(baseConstraint)
constraint.topToTop = this.id
constraint.bottomToBottom = this.id
if (index == 0){
constraint.leftToLeft = this.id
constraint.rightToLeft = dividers[index+1].id
} else if (index == dividers.lastIndex) {
constraint.rightToRight = this.id
constraint.leftToRight = dividers[index-1].id
} else {
constraint.rightToLeft = dividers[index+1].id
constraint.leftToRight = dividers[index-1].id
}
divider.layoutParams = constraint
this.addView(divider)
}
}
data class ManageableMenuItem(val title: String) {
var id: Int? = null
}
interface ManageableMenuChangeListener {
fun onMenuItemChanged(var1: String)
}
}
This aligns a stack of "|" centered in the view. What am I doing wrong?! Any help appreciated! Thanks.
The arguments to createHorizontalChainRtl according to the documentation are
public void createHorizontalChainRtl (int startId,
int startSide,
int endId,
int endSide,
int[] chainIds,
float[] weights,
int style)
You specify the following:
constraintSet.createHorizontalChainRtl(
dividerIds[0],
ConstraintSet.START,
this.id,
ConstraintSet.END,
dividerIds,
null,
ConstraintSet.CHAIN_SPREAD
)
Try changing dividerIds[0] (startId) and this.id (endId) to ConstraintSet.PARENT_ID to form the chain.
I'm trying to use the latest version of the FontAwesome typeface for a TextView in a LinearLayout that I created programmatically, but instead a box is shown containing a cross. I tried both FontManager.getTypeface(activity!!.baseContext, FontManager.FONTAWESOME) and FontManager.getTypeface(activity!!.applicationContext, FontManager.FONTAWESOME) but neither of these worked. I'm even using the latest .otf file provided by FontAwesone. I really don't understand what I've done wrong, because I followed all the instructions on the FontAwesome website. Is there a problem because the layout is created dynamically or something else?
FontManager object
object FontManager {
private const val ROOT = "fonts/"
internal const val FONTAWESOME = ROOT + "fontawesome5-free-regular.otf"
internal fun getTypeface(context: Context, font: String): Typeface {
return Typeface.createFromAsset(context.assets, font)
}
}
Fragment class
class MyFragment : androidx.fragment.app.Fragment() {
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val mainRelativeLayout = RelativeLayout(activity)
// Layout Parameters for relative layout items
val rlpCVPrimary = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
val rlpTVPrimaryIcon = RelativeLayout.LayoutParams(LinearLayout.LayoutParams
.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
val rlpTVPrimaryText = RelativeLayout.LayoutParams(LinearLayout.LayoutParams
.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
val rlpTVTitle = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
val rlpCVSecondary = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
val rlpTVSecondaryIcon = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
val rlpTVSecondaryText = RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT)
// Accessing FontAwesome font
val iconFont = FontManager.getTypeface(activity!!.applicationContext, FontManager.FONTAWESOME)
//
val r = context!!.resources
val fourDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4f, r.displayMetrics).toInt()
val tenDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 10f, r.displayMetrics).toInt()
val thirtyDp = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 30f, r.displayMetrics).toInt()
// Creating RelativeLayouts for root layout
val cvRLPrimary = RelativeLayout(context)
val cvRLSecondary = RelativeLayout(context)
// Creating CardViews
val cvPrimary = context?.let { CardView(it) }
when {
cvPrimary != null -> {
cvPrimary.radius = fourDp.toFloat()
cvPrimary.setContentPadding(tenDp,tenDp,tenDp,tenDp)
cvPrimary.useCompatPadding = true
cvPrimary.cardElevation = fourDp.toFloat()
cvPrimary.layoutParams = rlpCVPrimary
}
}
rlpCVPrimary.setMargins(0, 0, 0, thirtyDp)
val cvSecondary = context?.let { CardView(it) }
when {
cvSecondary != null -> {
cvSecondary.radius = fourDp.toFloat()
cvSecondary.setContentPadding(tenDp,tenDp,tenDp,tenDp)
cvSecondary.useCompatPadding = true
cvSecondary.cardElevation = fourDp.toFloat()
cvSecondary.layoutParams = rlpCVSecondary
}
}
rlpCVSecondary.setMargins(0, 0, 0, thirtyDp)
/*
* Creating Primary CardView items
*/
// 'tvPrimaryIcon' TextView
val tvPrimaryIcon = TextView(context)
tvPrimaryIcon.text = getString(R.string.fa_icon_sign_in)
tvPrimaryIcon.typeface = iconFont
tvPrimaryIcon.layoutParams = rlpCVPrimary
tvPrimaryIcon.setMargins(0, 0, tenDp, 0)
TextViewCompat.setTextAppearance(tvPrimaryIcon, android.R.style.TextAppearance_Medium)
// 'tvPrimaryText' TextView
val tvPrimaryText = TextView(context)
tvPrimaryText.text = getString(R.string
.primary_title)
TextViewCompat.setTextAppearance(tvPrimaryText, android.R.style.TextAppearance_Medium)
// 'tvTitle' TextView
val tvTitle = TextView(context)
tvTitle.text = getString(R.string.hello_world)
tvTitle.gravity = Gravity.CENTER
TextViewCompat.setTextAppearance(tvTitle, android.R.style.TextAppearance_Medium)
rlpTVTitle.setMargins(0, tenDp, 0, tenDp)
/*
* Creating Secondary CardView items
*/
// 'tvSecondaryIcon' TextView
val tvIconExit = TextView(context)
tvIconExit.text = getString(R.string.fa_icon_sign_out)
tvIconExit.typeface = iconFont
TextViewCompat.setTextAppearance(tvIconExit, android.R.style.TextAppearance_Medium)
rlpTVIconExit.setMargins(0, 0, tenDp, 0)
// 'tvSecondaryText' TextView
val tvSecondaryText = TextView(context)
tvSecondaryText.text = getString(R.string.secondary_title)
TextViewCompat.setTextAppearance(tvSecondaryText, android.R.style.TextAppearance_Medium)
// Set IDs for text views
mainRelativeLayout.id = View.generateViewId()
cvPrimary!!.id = View.generateViewId()
tvPrimaryIcon.id = View.generateViewId()
tvPrimaryText.id = View.generateViewId()
tvTitle.id = View.generateViewId()
cvSecondary!!.id = View.generateViewId()
tvSecondaryIcon.id = View.generateViewId()
tvSecondaryText.id = View.generateViewId()
// Set Layout Parameters for text views
tvPrimaryIcon.layoutParams = rlpTVPrimaryIcon
tvPrimaryText.layoutParams = rlpTVPrimaryText
tvTitle.layoutParams = rlpTVTitle
tvSecondaryIcon.layoutParams = rlpTVSecondaryIcon
tvSecondaryText.layoutParams = rlpTVSecondaryText
// Set RelativeLayout rules for views
rlpTVPrimaryText.addRule(RelativeLayout.END_OF, tvPrimaryIcon.id)
rlpTVTitle.addRule(RelativeLayout.BELOW, cvPrimary.id)
rlpCVSecondary.addRule(RelativeLayout.BELOW, tvTitle.id)
rlpTVSecondaryText.addRule(RelativeLayout.END_OF, tvSecondaryIcon.id)
// Adding items to root layout
mainRelativeLayout.addView(cvPrimary)
cvPrimary.addView(cvRLPrimary)
cvRLPrimary.addView(tvPrimaryIcon)
cvRLPrimary.addView(tvPrimaryText)
mainRelativeLayout.addView(tvTitle)
mainRelativeLayout.addView(cvSecondary)
cvSecondary.addView(cvRLSecondary)
cvRLSecondary.addView(tvSecondaryIcon)
cvRLSecondary.addView(tvSecondaryText)
return mainRelativeLayout
}
}