Activity content transition doesn't work - android

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)
}
..................
..................
}

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)
}

How to get all ImageButton ID's on Android with Kotlin

I have ImageButtons
and i send a Toast from override fun onCreate(savedInstanceState: Bundle?)
when its clicked successful.
I want loop trough all ImageButtons to add the setOnClickListener to them.
this works:
works without a loop:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
findViewById<ImageButton>(R.id.r1col1).setOnClickListener {
toastContentDescription(it)
}
}
private fun toastContentDescription(it: View) {
val contentDescription = it.contentDescription
val myToast = Toast.makeText(applicationContext, contentDescription, Toast.LENGTH_SHORT)
myToast.show()
}
}
works not, not start anymore
I found a example for Android Java Buttons here: How to get all Buttons ID's in one time on Android
So i modified my code to following. But then the app not start anymore (gives me no errors).
i guess i have to get the id first and then the problem is solved.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
for (i in 0..4) {
val id: Int = resources.getIdentifier("R.id.r1col$i", "id", this.packageName)
findViewById<ImageButton>(id).setOnClickListener {
toastContentDescription(it)
}
}
}
private fun toastContentDescription(it: View) {
val contentDescription = it.contentDescription
val myToast = Toast.makeText(applicationContext, contentDescription, Toast.LENGTH_SHORT)
myToast.show()
}
}
Is there any way to set the setOnClickListener to all ImageButtons in a loop (etc.) Code?
Remove the "R.id." prefix from the string you’re passing.

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()
}
}
}

Android access view binding val outside onCreate activity

I have an activity that has a button. On the button click I want to update text in text view.
I want to use ViewBinding instead of the normal findViewById
This is how I created the val binding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater);
setContentView(binding.root)
binding.btnRoll.setOnClickListener {
rollDice()
}
}
Now in rollDice I want to update the text view but I'm not able to access binding which make sense because its scope is limited to onCreate() , so what is the best practice for this?
private fun rollDice() {
val random = Random().nextInt(6) + 1
binding.txt_random.setText("random")
}
You have two options.
1. Store in a property
Since the inflated content of Activity is fully bound to it's lifecycle, it's safe to keep the reference as a property
class MainActivity : AppCompatActivity() {
lateinit var binding: ActivityMainBinding
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityMainBinding.inflate(layoutInflater);
setContentView(binding.root)
binding.btnRoll.setOnClickListener {
rollDice()
}
}
private fun rollDice() {
val random = Random().nextInt(6) + 1
binding.txt_random.setText("random")
}
}
2. Pass the binding to the methods
That's what I usually do, it avoids creating a property where it's not really a necessity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityMainBinding.inflate(layoutInflater);
setContentView(binding.root)
binding.btnRoll.setOnClickListener {
rollDice(binding)
}
}
private fun rollDice(binding: ActivityMainBinding) {
val random = Random().nextInt(6) + 1
binding.txt_random.setText("random")
}
}
Both options are valid ways to make the binding visible to Activities methods.
Store the binding in an instance variable on the Activity. Then you have access to it from all the methods in the Activity.
As the question has accepted answer and it is already addressed but here is my approach to the viewBinding
class MainActivity : AppCompatActivity() {
private val binding by lazy{
ActivityMainBinding.inflate(layoutInflater)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(binding.root)
binding.btnRoll.setOnClickListener {
rollDice()
}
}
private fun rollDice() {
val random = Random().nextInt(6) + 1
binding.txt_random.setText("random")
}
}
I go with lazy initialization of binding so that way it is only intitialized if it is required.
More you can read about lazy initialization here
https://www.baeldung.com/kotlin/lazy-initialization

Expand and collapse a colapsingToolBar with a button in Kotlin

I want to expand and collapse a CollapsingToolBar in android studio
when i click a button.
I want it collapsed by dafault. Im doing something wrong and i dont know what, thanks for the help.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
//maybe this doesnt go here, im trying to make it collapse by default
var start = appbar.setExpanded(false)
button.setOnClickListener {
if(start == appbar.setExpanded(false)){
start = appbar.setExpanded(true)
}else{
start = appbar.setExpanded(false)
}
}
}
Try this:
class MainActivity : AppCompatActivity() {
private var isAppbarExpanded = false
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
button.setOnClickListener {
if(!isAppbarExpanded){
appbar.setExpanded(true)
isAppbarExpanded = true
}else{
appbar.setExpanded(false)
isAppbarExpanded = false
}
}
}

Categories

Resources