-
Notifications
You must be signed in to change notification settings - Fork 85
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Common Layouts #3
Comments
What do you mean by "common UI or XMLs"? |
Like in standard practice, we can use the same layout (XML file) in different activity or fragments. Suppose, the activity_main.xml file can be used in, MainActivity.kt, ActivityOne.kt, ActivityTwo.kt or in FragmentOne.kt or FragmentTwo.kt or if we have multiple layouts in XML form we can use this in a layout like if the layout file in activity_main.xml and common layouts are, common_header.xml, content_main.xml and common_footer.xml So, we can perform the reusability. |
Another Question : Layout Or UI for Mobile Phones and Tablets. Describe the solution you'd like Describe alternatives you've considered Additional context Second Thing If you can guide us in this context that will be very helpful. Learning and implementing... |
Yes, the main functionality of Jetpack Compose is to use Reusable Composable functions, // CommonLayouts.kt
@Composable
fun Header(content: @Composable () -> Unit) {
Column(
modifier = Modifier
.fillMaxWidth()
.background(Color.Green)
.padding(horizontal = 8.dp)
) {
content();
}
}
@Composable
fun Footer(color: Color, message: String) {
Column(
modifier = Modifier
.fillMaxWidth()
.background(color)
.padding(horizontal = 8.dp)
) {
Box(contentAlignment = Alignment.Center) {
Text(text = message)
}
}
} By defining such methods, you can call them inside any other layout as per your wish. // MainActivity.kt
class MainActivity: AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentLayout {
Header {
Button(onClick = {/* TODO */}) {
Text(text = "Click me!")
}
}
/* More content */
Footer(Color.Red, "Jetpack Compose")
}
}
} Note: I have not tested this code, but I am sure the above code should work. If it doesn't, there maybe required a few minor tweeks. I hope you get the general idea. As per your question regarding the responsive design in Jetpack Compose, I haven't yet researched on that topic yet. It's a good question, I shall look around. |
Thank you for your suggestion. I will try and update. |
I am just wondering how we can use common UI or XMLs in jetpack compose. like we use in Android Activity or in Fragments.
Wanted to know about and tag as well.
The text was updated successfully, but these errors were encountered: