Skip to content
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

Open
dpnkrg opened this issue May 7, 2021 · 5 comments
Open

Common Layouts #3

dpnkrg opened this issue May 7, 2021 · 5 comments

Comments

@dpnkrg
Copy link

dpnkrg commented May 7, 2021

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.

@cybercoder-naj
Copy link

What do you mean by "common UI or XMLs"?

@dpnkrg
Copy link
Author

dpnkrg commented May 13, 2021

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, I can use those layouts in multiple XML files and use their ids in Activity or Fragments.
in XML file we can create a LinearLayout and in layout, we can include the
via include tag
common_header
content_main
common_footer

So, we can perform the reusability.
My question is that how we can use this in jetpack-compose. Because mainly we are creating UI via Code.

@dpnkrg
Copy link
Author

dpnkrg commented May 13, 2021

Another Question :

Layout Or UI for Mobile Phones and Tablets.
I am just wondering about, how we can achieve or implement layouts for Mobile and Tablets. For example, In the older implementation, we can, implements these res folder layouts for different screens, for the land folder for landscape, and for tablet layout-600dp, layout-720dp.

Describe the solution you'd like
How we can achieve this. In terms of UI and Behaviour of app for Mobile and Tablet. Because in general, the layout for tablet and mobile is different in terms of sizing, detailing and UX.

Describe alternatives you've considered
Please check the Gmail app on Mobile and Tablet.

Additional context
I am also attaching the screens shots for your reference.

Mobile Phone:

Tablet :

Second Thing
As of now if we are using, XML for layouts we can use same layout for different activities and fragments. I am just wondering how we can achieve in jetpack compose.

If you can guide us in this context that will be very helpful.

Learning and implementing...

@cybercoder-naj
Copy link

cybercoder-naj commented May 13, 2021

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, I can use those layouts in multiple XML files and use their ids in Activity or Fragments.
in XML file we can create a LinearLayout and in layout, we can include the
via include tag
common_header
content_main
common_footer

So, we can perform the reusability.
My question is that how we can use this in jetpack-compose. Because mainly we are creating UI via Code.

Yes, the main functionality of Jetpack Compose is to use Reusable Composable functions,
For example,

// 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.

@dpnkrg
Copy link
Author

dpnkrg commented May 13, 2021

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, I can use those layouts in multiple XML files and use their ids in Activity or Fragments.
in XML file we can create a LinearLayout and in layout, we can include the
via include tag
common_header
content_main
common_footer
So, we can perform the reusability.
My question is that how we can use this in jetpack-compose. Because mainly we are creating UI via Code.

Yes, the main functionality of Jetpack Compose is to use Reusable Composable functions,
For example,

// 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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants