Change compose View orientation - android

I use this code to change composable View orientation but it doesn't work and I don't need to change app orientation
val configuration = Configuration()
configuration.orientation = Configuration.ORIENTATION_LANDSCAPE
CompositionLocalProvider(LocalConfiguration provides configuration) {
Screen()
}

You can change the screen orientation using this:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startActivity(intent)
setContent {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE)
Text("I'm a cat person")
}
}
}

Related

How to start another activity in function? Kotlin

class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val navigate = Intent(this,Activity2::class.java)
startActivity(navigate)
}
}
}
fun switchActivity(){
val navigate = Intent(this,Activity2::class.java)
startActivity(navigate)
}
I want to start an activity from a function and not from the main activity..
I can start an activity from main class but in function, the code doesnt work..
Please help.. I'm new to kotlin android programming..
You can also try like this
class MainActivity : AppCompatActivity() {
#SuppressLint("MissingInflatedId")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
nextActivity(this, MainActivity2())
}
private fun nextActivity(context: Context, activity: Activity) {
startActivity(Intent(context, activity::class.java))
}
}
Just use it
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
switchActivity()
}
}
fun switchActivity(){
val navigate = Intent(this#MainActivity, Activity2::class.java)
startActivity(navigate)
}
Actually i don't get your question clearly. But i'll try to answer your question based on my understanding of yours.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
switchActivity()
// if you want to pass the class through the paramater,
// can try this
// TODO: Another Way
// switchActivityCustom(Activity2::class.java)
}
}
fun switchActivity(){
val navigate = Intent(this, Activity2::class.java)
startActivity(navigate)
}
// TODO: Another Function
// fun switchActivityCustom(destination: Class<*>,){
// val navigate = Intent(this, destination)
// startActivity(navigate)
// }
}
And the last one is, put your function inside the class (in this case: MainActivity Class)
If your function is declared at the top level, without a reference to this, you'd need to pass an instance of Activity to your function:
fun switchActivity(activity: Activity) {
val navigate = Intent(activity, Activity2::class.java)
activity.startActivity(navigate)
}

Show different content (layouts) depending on available width/height in Android Compose

I have an Activity and MyApp composable function. In the function I need to show either list with details or just list screen depending on the available width. How to determine available width for the content I want to show using Jetpack Compose? What is a good practice for this using Compose?
class MyActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
ProjectTheme {
MyApp()
}
}
}
}
#Composable
fun MyApp() {
val isLarge: Boolean = ...// how to determine whether the available width is large enough?
if (isLarge) {
ListScreenWithDetails()
} else {
ListScreen()
}
}
#Composable
fun ProjectTheme(darkTheme: Boolean = isSystemInDarkTheme(), content: #Composable () -> Unit) {
// ... project theme
}
You can use :
val configuration = LocalConfiguration.current
Then:
val isLargeWidth = configuration.screenWidthDp > 840
For a generic solution, consider using the Window Size Classes, see this and this for reference.
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
val windowSizeClass = calculateWindowSizeClass(this)
MyApp(windowSizeClass)
}
}

Check if first time using the app during launch

I'm trying to implement a first time using screen (like any other app when you have to fill some options before using the app for the first time).
I can't go to another Jetpack compose on an main activity on-create state because it check that every recomposition, and take me to the navigation path (I'd like to check the datastore entry once during launch), this what I already try, not seem to be working:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launch {
val onBoardingStatus = dataStoreManager.onBoard.first()
setContent {
val navController = rememberNavController()
OnBoardingNavHost(navController)
navController.navigate(if (onBoardingStatus) "on_boarding" else "main") {
launchSingleTop = true
popUpTo(0)
}
}
}
}
it is possible to check that only once (in application class for example and not in oncreate?)
please advice,
thanks in advance
You have to use LaunchedEffect for this, you can do something like this
enum class OnboardState {
Loading,
NoOnboarded,
Onboarded,
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
var onboardingState by remember {
mutableStateOf(OnboardState.Loading)
}
LaunchedEffect(Unit) {
onboardingState = getOnboardingState()
}
when (onboardingState) {
OnboardState.Loading -> showSpinner()
OnboardState.NoOnboarded -> LaunchedEffect(onboardingState) {
navigateToOnboarding()
}
OnboardState.Onboarded -> showContent()
}
}
}

How to add a common button in all activity?

Can we add a common button on all activity without add code in xml file.
we need to add some code in application class and button show in all activity of app.
First create a BaseActivity. Then add this code to onCreate of that:
class BaseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_nav_host)
//the layout of baseActivity
LinearLayout layout =(LinearLayout) findViewById (R.id.base_activity_layout);
//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(
new LayoutParams (LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT
));
btnTag.setText("Button");
btnTag.setId(some_random_id);
//add button to the layout
layout.addView(btnTag);
}
}
Then let other activities which you want to create being inherited from BaseActivity.
class FirstActivity : BaseActivity() {
//
//
}
class SecondActivity : BaseActivity() {
//
//
}
class ThirdActivity : BaseActivity() {
//
//
}
class ButtonActivity: Activity() {
protected var btnCommon: Button
override fun onCreate(saved: Bundle?) {
var layout = findViewById(R.id.layout) as ConstraintLayout
btnCommon = Button(this)
// set Button properties
layout.addView(button)
}
}
How to use
class MyActivity: ButtonActivity() {
onCreate() {
this.btnCommon.setOnClickListener...... // now you can use.
}
}
I saw in your comments that you specifically want to do this solely from your Application class. It is possible to add a callback that is called during lifecycle methods of every Activity, so you could add a button there. Every one of your Activities will have to have a root view in its layout of the same type and with the same ID for this to work.
class MyApplication: Application() {
override fun onCreate() {
super.onCreate()
registerActivityLifecycleCallbacks(object: ActivityLifecycleCallbacks {
override fun onActivityCreated(activity: Activity, savedInstanceState: Bundle?) {
val rootView = activity.findViewById<ConstraintLayout>(R.id.myRootView)
val layoutParams = ConstraintLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
).apply {
// Set up the appropriate layout constraints here
}
val button = Button(/* ... */)
rootView.addView(button, layoutParams)
}
override fun onActivityStarted(activity: Activity) = Unit
override fun onActivityResumed(activity: Activity) = Unit
override fun onActivityPaused(activity: Activity) = Unit
override fun onActivityStopped(activity: Activity) = Unit
override fun onActivitySaveInstanceState(activity: Activity, outState: Bundle) = Unit
override fun onActivityDestroyed(activity: Activity) = Unit
})
}
}
Personally, I think this is messy, and I would do it in a top level function that you call from onCreate() in each of your Activities. Or better yet, use a single Activity in your app and use Fragments for the different screens.

Activity content transition doesn't work

I want to start Activity with content transition. But the content transition doesn't work. Why and how to fix it?
This is my code:
class PolishStartActivity : AbsActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
initContentTransition()
setContentView(R.layout.activity_polish_start)
}
private fun initContentTransition() {
window.apply {
requestFeature(Window.FEATURE_CONTENT_TRANSITIONS)
enterTransition = TransitionSet().apply {
addTransition(Slide(Gravity.TOP).addTarget(R.id.tvBooksTitle).addTarget(R.id.tvBooksDescription))
addTransition(Fade().addTarget(R.id.cvSearchBox))
// addTransition(Fade()) only this transition works fine
duration = 1000L
}
}
}
}
P.S. I start my Activity with ActivityOptions.
After many attempts and experiments, I found the fix for this problem.
On my root ViewGroup (in my case it is ConstraintLayout) add this line:
android:transitionGroup="false"
Yes! Now you can set targets for transitions and it will be work!
Put initContentTransition() before super.onCreate();
class PolishStartActivity : AbsActivity()
{
override fun onCreate(savedInstanceState: Bundle?)
{
initContentTransition()
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_polish_start)
}
..................
..................
}

Categories

Resources