I confused with the issue
Why can NOT display the content when the TableLayout tag has more children (added programmatically) at runtime in Android studio?
as the title says.
I’m sure that
-
The project uses
test9.xml
as content view at startup of runtime. (See followingMainActivity.kt
) -
The initial screen of runtime is shown as
At startup of runtime
in Screenshot at runtime section.
3.There are NO compiler error and exception at runtime in this project.
-
After I clicked
ADD AN ITEM
button, the TableLayout with idexamination_tablelayout
(See followingtest9.xml
) has more one child than before. (See video in Demo section) -
I add one child into TableLayout with id
examination_tablelayout
through.addView
method. -
Before adding one child, I have fully set the property
layoutParams
for the child and its descendant in the child) through Brian Cooley’s Answer in the stackoverflow
I don’t know the root cause of the issue.
Appreciation
Any replies about this issue will be very appreciated.
Thank you very much, readers.
infos
Screenshot at runtime
At startup of runtime
After I click the “ADD AN ITEM” button
Demo
The demo of result is available at YT channel. (P.S. DON’T care about the background sound, that’s the music NOT relates to the project)
Demo of my project MyTable1 in Android Studio
MainActivity.kt
package com.example.mytable1
import android.icu.text.SimpleDateFormat
import android.os.Bundle
import android.util.Log
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import android.widget.EditText
import android.widget.TableLayout
import android.widget.TableRow
import android.widget.TextView
import androidx.activity.ComponentActivity
const val TAG = "MainActivity"
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.test1)
//setContentView(R.layout.test2)
//setContentView(R.layout.test3)
// setContentView(R.layout.test4)
//setContentView(R.layout.test5)
//setContentView(R.layout.test6)
//setContentView(R.layout.test7)
//setContentView(R.layout.test8)
setContentView(R.layout.test9)
//setContentView(R.layout.test10)
//setContentView(R.layout.test11)
//setContentView(R.layout.main);
var index = 0
val examination_tableLayout = findViewById<TableLayout>(R.id.examination_tablelayout)
findViewById<Button>(R.id.add_an_item_button)
.setOnClickListener {
val input_raw_date = findViewById<EditText>(R.id.examination_date).text.toString()
val input_item = findViewById<EditText>(R.id.examination_item).text.toString()
val input_value = findViewById<EditText>(R.id.examination_value).text.toString()
var new_item_flag = true
if(input_raw_date.length == 8 && isInteger(input_raw_date)){
//val input_parsed_date = simpleDateFormat.parse(input_raw_date)
findViewById<TextView>(R.id.invalid_date_message).visibility = View.INVISIBLE
}else{
findViewById<TextView>(R.id.invalid_date_message).visibility = View.VISIBLE
new_item_flag = false
}
if(input_item.isNotEmpty()) {
Log.d(TAG, "input_item:" + input_item)
findViewById<TextView>(R.id.invalid_item_message).visibility = View.INVISIBLE
}else{
findViewById<TextView>(R.id.invalid_item_message).visibility = View.VISIBLE
new_item_flag = false
}
if(input_value.isNotEmpty()) {
Log.d(TAG, "input_value:" + input_value)
findViewById<TextView>(R.id.invalid_value_message).visibility = View.INVISIBLE
}else{
findViewById<TextView>(R.id.invalid_value_message).visibility = View.VISIBLE
new_item_flag = false
}
if(new_item_flag){
Log.d(TAG,"new_item_flag == true is true")
val tablerow1 = TableRow(this)
val textView_date = TextView(this)
val textView_item = TextView(this)
val textView_value = TextView(this)
tablerow1.layoutParams =
ViewGroup.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,TableLayout.LayoutParams.WRAP_CONTENT)
tablerow1.tag = "tablerow[$index]"
index += 1
textView_date.text = input_raw_date
textView_item.text = input_item
textView_value.text = input_value
textView_date.layoutParams =
ViewGroup.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT)
textView_item.layoutParams =
ViewGroup.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT)
textView_value.layoutParams =
ViewGroup.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT)
tablerow1.addView(textView_date)
tablerow1.addView(textView_item)
tablerow1.addView(textView_value)
Log.d(TAG,"After event, tablerow1 tag has n children where n: "+tablerow1.childCount.toString())
examination_tableLayout.addView(tablerow1,TableLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.MATCH_PARENT))
Log.d(TAG,"success to add an tablerow1 tag programmatically into examination_tableLayout")
}else{
Log.d(TAG,"new_item_flag == true is false")
}
Log.d(TAG,"After event, examination_tableLayout tag has n children where n: "+examination_tableLayout.childCount.toString())
for(i in 0..examination_tableLayout.childCount-1){
val child = examination_tableLayout.getChildAt(i)
Log.d(TAG,"The "+i.toString()+"th child has tag: "+child.tag.toString())
}
}
}
}
fun isInteger(number : Any?) : Boolean{
val integerChars = '0'..'9'
if (number == null){
return false
}
val s1 = number.toString()
if (number is Number){
return true
}
for(i in s1.indices){
val c = s1[i]
if(c !in integerChars){
return false
}
}
return true
}
test9.xml
code (at code page)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
tools:ignore="Suspicious0dp">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TableRow>
<TextView
android:layout_column="1"
android:text="Date:"
android:padding="3dip" />
<EditText
android:id="@+id/examination_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="20200101"
android:inputType="date"
tools:ignore="TouchTargetSizeCheck" />
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="Item:"
android:padding="3dip" />
<EditText
android:id="@+id/examination_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="item"
android:inputType="text"
tools:ignore="TouchTargetSizeCheck" />
</TableRow>
<TableRow>
<TextView
android:layout_column="1"
android:text="value:"
android:padding="3dip" />
<EditText
android:id="@+id/examination_value"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="0"
android:inputType="number"
tools:ignore="TouchTargetSizeCheck" />
</TableRow>
<Button
android:id="@+id/add_an_item_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add an item" />
<TextView
android:id="@+id/invalid_date_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="invalid date. please enter a date whose format is yyyymmdd (e.g. 20010101)"
android:inputType="none"
android:textSize="12sp"
android:textColor="@android:color/system_error_light"
android:visibility="invisible"
tools:ignore="TextFields,TouchTargetSizeCheck" />
<TextView
android:id="@+id/invalid_item_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="invalid date. please enter an item whose is NOT empty (e.g. item1)"
android:inputType="none"
android:textSize="12sp"
android:textColor="@android:color/system_error_light"
android:visibility="invisible"
tools:ignore="TouchTargetSizeCheck" />
<TextView
android:id="@+id/invalid_value_message"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="invalid date. please enter a nonnegative integer as value(e.g. 1)"
android:inputType="none"
android:textSize="12sp"
android:textColor="@android:color/system_error_light"
android:visibility="invisible"
tools:ignore="TouchTargetSizeCheck" />
</TableLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
tools:ignore="Suspicious0dp">
<TableLayout
android:id="@+id/examination_tablelayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="0,1"
>
<TableRow android:layout_width="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Date"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Value" />
</TableRow>
</TableLayout>
</LinearLayout>
</LinearLayout>
test9.xml
UI design (at design page)
test9.xml
Absolute path of test9.xml
C:Users{USERNAME}AndroidStudioProjectsMyTable1appsrcmainreslayouttest9.xml
Project
The project (MyTable1.zip) is available at Github
MyTable1.zip
See
Brian Cooley’s Answer in stackoverflow
and see API reference in Android Docs for more understanding of the syntax.
ViewGroup
ViewGroup.LayoutParams
TableLayout