I have a base activity where I set content layout by creating a layout dynamically. setContent method looks like this
lateinit var topView: TextView
override fun setContentView(layoutResID: Int) {
val screenRootView = RelativeLayout(this)
screenRootView.setLayoutParams(RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT))
// Create your top view here
topView = TextView(this)
topView.setLayoutParams(RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT))
topView.gravity = Gravity.CENTER_HORIZONTAL
topView.setTextColor(resources.getColor(R.color.white))
var dimen = resources.getDimension(R.dimen.size_30dp)
if (AppHelper.isHomeScreen) {
topView.setPadding(10, dimen.toInt(), 10, 15)
} else {
topView.setPadding(10, 15, 10, 15)
}
topView.setBackgroundColor(resources.getColor(R.color.green))
val inflater = this.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val screenView = inflater.inflate(layoutResID, null)
screenRootView.addView(screenView)
screenRootView.addView(topView)
super.setContentView(screenRootView)
}
From an activity, I sent a layout ID. xml of the layout is as following
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:background="@color/screen_background_normal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="@color/white"
android:orientation="horizontal"
android:id="@+id/toolbar">
<ImageView
android:id="@+id/iv_back_navigation"
android:layout_width="@dimen/size_30dp"
android:layout_height="@dimen/size_30dp"
android:layout_gravity="center"
app:srcCompat="@drawable/ic_arrow_left" />
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="@dimen/size_30dp"
android:layout_weight="1"
android:gravity="center"
android:text="Title of Toolbar"
android:textColor="@color/text_blue"
android:textSize="@dimen/text_size_20sp" />
</LinearLayout>
<android.support.constraint.ConstraintLayout
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="@id/toolbar"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@color/text_blue"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Hello World" />
</android.support.constraint.ConstraintLayout>
</android.support.constraint.ConstraintLayout>
TextView of the setContent() method will show me network status. I've check network status like this way
fun drawLayoutfromNetworkStatus() {
var pos = 0
if (!isNetworkAvailable()) {
checkStatus(false)
//Add again check for full connected
val handler = Handler()
val r = object : Runnable {
override fun run() {
if (isNetworkAvailable()) {
handler.removeCallbacksAndMessages(null)
checkStatus(true)
} else {
pos++
checkStatus(false)
if (pos == 10) {
pos = 0
handler.removeCallbacksAndMessages(null)
} else {
handler.postDelayed(this, 1000)
}
}
}
}
handler.removeCallbacksAndMessages(null)
handler.postDelayed(r, 1000)
} else {
checkStatus(true)
}
}
private fun checkStatus(isAvailable: Boolean) {
if (isAvailable) {
if (topView != null) {
topView.text = getResourceText(R.string.connected)
topView.setBackgroundColor(resources.getColor(R.color.green))
topView.startAnimation(AnimationUtils.loadAnimation(this, R.anim.fade_out))
Handler().postDelayed({
topView.visibility = View.GONE
}, 1000)
}
} else {
if (topView != null) {
topView.clearAnimation()
topView.visibility = View.VISIBLE
topView.text = getResourceText(R.string.waiting_for_network)
topView.setBackgroundColor(resources.getColor(R.color.orange))
}
}
}
After running the application textView on the top which I set dynamically, is taking extra padding on the top of it.
What would be the problem and how can I solve this?
UPDATE: I've already used it on another activities. It works fine for layout of those activities but not for this layout..