Reputation: 544
Let's say I have two layouts activity_home_1
and activity_home_2
for my HomeActivity
.
I want to be able to switch between them like I used to do in Java:
int layout = getResources().getIdentifier("activity_home_" + getHomeScreenNumber(),"layout",getPackageName());
setContentView(layout);
And later reference elements of my layout using findViewById();
Of course, all the elements in those two layouts are named the same, they are just different looking, so this is working perfectly in Java.
On the other side in Kotlin, I'm avoiding findViewById()
and I'm using Kotlin Android Extensions, and it's working great with including one layout like this:
import kotlinx.android.synthetic.main.activity_home_1.*
The trouble is when I want to switch between them. Just simply adding another import for the other layout it's causing an error because it doesn't know which layout to look at (cause they both have the same fields).
How can I switch between two layouts when using Kotlin Android Extensions?
Upvotes: 1
Views: 507
Reputation: 544
The answer was simple and I wasn't paying enough attention. It was to include the desired layout with:
setContentView(..)
For example, if you include
import kotlinx.android.synthetic.main.activity_home_1.*
and in your OnCreate()
set the
setContentView(R.layout.activity_home_3)
Now, your activity_home_3 layout will be shown and their elements will be referenced!
No need for findViewById()
Upvotes: 1