A view has a Parent in Android ( LOL on Complication )
Hi Guys, I am going to start a whole new series of LOL on Complication. The main theme of this series, I will share with you my experiences or my old codes, which I wrote a long time ago. Or maybe I will find out in reading some code on Github. Or perhaps we will find out in pair programming, which we are writing complex due to the lack of knowledge of some API’s or concepts. So I will share with you the original code what we find out and how we improved. I hope that will be helpful for everyone. Second, this series will contain small and independent blog posts. It’s time to start first sharing of this series.
View Has A Parent in Android:
I find out in my career. A lot of time I wrote a lot of code which I can simplify very easily if I know the correctly my API’s. If we don’t know about the available API’s mostly below mentioned things, happen in your code.
- You always write a lot of code to achieve something.
- If you write a lot of code, you have more chances to create more bugs.
- If you have more code, you need to write more tests.
Today’s sharing is a method in which I want to hide a container if I have the value null or empty of its child.
Scenario:
In the real scenario, we have a pretty complex screen but to make this easy and more focused I took the small part which we resolved and achieve the reduction of the code.
We have a UI in which we have a nested Linear Layout, which contains a TextView and Button as shown above in image or shown as below in code.
<LinearLayout
...
android:id="@+id/productContainer">
<TextView
android:id="@+id/productNameTextView"
android:text="Product Name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="@+id/buyButton"
android:text="Buy"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Now, when I don’t have product Name we want to hide a whole container. So button and productName text view will hide. Second, we have this same UI component in different screens. So due to that, we have one common method, which will do this thing for us.
Now, here take some time and try to think about how you will implement ……..
First Approach ( I don’t like ):
fun renderProduct(producName: String, textView: TextView, container: ViewGroup) { if (producName != null) {
textView.text = producName
} else {
container.visibility = View.GONE
}
}
So how many people choose this implementation please write in comment +1 :P. OK, so how we can improve? I want to reduce the params of the method. Also, we know every child view in Android always knows about his parent.
Best Approach:
fun renderProduct(producName: String, textView: TextView) { if (producName != null) {
textView.text = producName
} else {
(textView.parent as ViewGroup).visibility = View.GONE
}
}
We removed the one param from the method how I did because I know the View API’s. In this case, I am 100% sure textView is always inside of my LinearLayout. But if you guys are using some third-party library so in that case I will recommend you first check parent is a VIewGroup or not. There is a possibility that maybe a parent is not a ViewGroup.
You can write this code more in a good way by using Kotlin power. But I feel my main intention is already conveyed.
Use cases or in Code Reviews:
If you are writing a code or you are doing a code review. Always try to remember below points to achieve this beautiful code.
- If you find out someone has a Container Id in some field or using directly Container Id in code. Do careful review maybe you find out what he is trying to achieve he can achieve by using view.parent.
- If you are going to write code and you feel you need a Container Id then ask again to your self. Maybe that is a proper use case of view.parent.
When should not use view.parent:
As a developer, we always write code for other developers. In this case, if you abuse this API that may create a problem for your fellow developers or maybe for yourself. So be careful. Like if you need to write a code something like below:
textView.parent as LinearLayout
Try to ask again to yourself. Because if someone change XML of this layout and replaced this LinearLayout to some other ViewGroup layout then maybe app crashed.
Conclusion:
Truly saying, writing code is easy but reducing a code with the same functionality is really tough. In this case, I removed a one param from the method without losing any functionality. Also, now I need to write less code for testing and I can achieve more code coverage with less code.
See you guys in the next post. Bye