Sorry I am a bit new to stack overflow but hopefully the question is understandable!
I talked to a staff memeber/community member who showed me Intent to go from 1 activity(in my case GlobeActivity where my animation is stored) to another activity( MainActivity where username/password is stored). But the animation does not show up, instead it cuts directly to MainActivity.
Anyone got some suggestions to why and how to make it so that when animation is finished, it re-directs/ transitions to MainActivity without use of buttons?
Here is my code so far:
GlobeActivity( my SecondActivity):
package com.example.testerino2022
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.TextView
class GlobeActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_globe)
supportActionBar?.hide()
val textView = findViewById<TextView>(R.id.textGlobeScreen)
textView.animate().translationX(1050F).setDuration(1000).setStartDelay(2500)
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
}
MainActivity(has default code in it );
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
}
You can use animation listener
textView.animate().translationX(1050F).setDuration(1000).setStartDelay(2500).setListener(
object: Animator.AnimatorListener{
override fun onAnimationStart(p0: Animator?) {
TODO("Not yet implemented")
}
override fun onAnimationEnd(p0: Animator?) {
val intent = Intent(this, MainActivity::class.java)
startActivity(intent)
finish()
}
override fun onAnimationCancel(p0: Animator?) {
TODO("Not yet implemented")
}
override fun onAnimationRepeat(p0: Animator?) {
TODO("Not yet implemented")
}
}
)
You can do this easily with the ViewPropertyAnimator stuff you're already using - just add withEndAction:
textView.animate()
.translationX(1050F)
.setDuration(1000)
.setStartDelay(2500)
.withEndAction {
startActivity(Intent(context, MainActivity::class.java))
finish()
}
textView.animate()
.translationX(1050F)
.setDuration(1000)
.setStartDelay(2500)
.withEndAction {
startActivity(Intent(context, MainActivity::class.java))
finish()
}
Have you changed the launcher activity from MainActivity to GlobeActivity in the app's manifest?
You should listen to the view or element that is being animated, assuming an ImageView. You can do this with the ViewPropertyAnimator.
ImageView imageview = findViewById(R.id.imageView);
imageView.animate()
.translationX(1050F)
.setDuration(2000)
.setStartDelay(1000)
.withEndAction {
startActivity(Intent(context, MainActivity::class.java))
finish()
}
Explanation
translationX(float value) - This method will cause the View's translationX property to be animated to the specified value
setDuration(long duration) - Sets the duration for the underlying animator that animates the requested properties.
setStartDelay(long startDelay) - Sets the startDelay for the underlying animator that animates the requested properties.
withEndAction(Runnable runnable) - Specifies an action to take place when the next animation ends.
For more information on this, check this out : -> ViewPropertyAnimator
Related
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)
}
I haven't found anything over the past day that shows how to do this action, everything I've seen is with a basic button of which I am unable to replicate for use with an image button. using setOnClickListener does not seem to work at all though the only cases I found of using them were 5+ years old.
Is there a Storyboard equivalent of linking activities in Android Studio?
Here is an example I found but 7 years old.
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myButton =
findViewById<View>(R.id.live) as ImageButton
myButton.setOnClickListener(object : OnClickListener() {
// When the button is pressed/clicked, it will run the code below
fun onClick() {
// Intent is what you use to start another activity
val intent = Intent(this, LiveActivity::class.java)
startActivity(intent)
}
})
}
}
gives the following error:
Object is not abstract and does not implement abstract member public abstract fun onClick(p0: View!): Unit defined in android.view.View.OnClickListener
The problem is that you haven't included the View parameter to your onClick override. The signature of OnClickListener.onClick includes a View (the View that was clicked) as its parameter, so onClick() (with no parameters) doesn't match that signature.
You could either add it explicitly (in which case you also need to refer to the Activity's this explicitly with ActivityName.this, as this refers to the OnClickListener otherwise):
myButton.setOnClickListener(object : View.OnClickListener {
// When the button is pressed/clicked, it will run the code below
override fun onClick(view: View) {
// Replace ActivityName with the name of your Activity that this is in
val intent = Intent(ActivityName.this, LiveActivity::class.java)
startActivity(intent)
}
})
or use Kotlin's SAM conversions to add it implicitly (I'd do this approach):
// When the button is pressed/clicked, it will run the code below
myButton.setOnClickListener { // there's an implicit view parameter as "it"
// Intent is what you use to start another activity
val intent = Intent(this, LiveActivity::class.java)
startActivity(intent)
}
Fix
Change from:
myButton.setOnClickListener(object : OnClickListener { })
to
myButton.setOnClickListener(object : View.OnClickListener { })
so method will be:
override fun onClick(v: View?) {
// Do something
}
instead yours:
fun onClick() {
}
Full code:
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val myButton = findViewById<ImageButton>(R.id.live)
myButton.setOnClickListener(object : View.OnClickListener {
// When the button is pressed/clicked, it will run the code below
override fun onClick(v: View?) {
// Intent is what you use to start another activity
val intent = Intent(this, LiveActivity::class.java)
startActivity(intent)
}
})
}
}
I know the concept of "Interfaces" but I've hard time to understand how to use them in android development.
Let's say I created an interface to decide if to show progress bar or not -
interface ProgressBarInterface {
fun showProgressBar()
fun hideProgressBar()
}
And I implement this inside BaseActivity/MainActivity in single Activity app:
class BaseActivity : AppCompatActivity() , ProgressBarInterface {
private val TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun showProgressBar() {
}
override fun hideProgressBar() {
}
}
And inside my other activity I've a button, that when I click on it, I want to trigger showProgressBar in the base activity:
button.setOnClickListener {
//Show progress bar
}
How can I interact with the interface to trigger the function inside base activity?
Since you already are implementing the interface in your BaseActivity, you can then just add what you need to do inside the interface methods, and then call them up in any point in your activity, if what you are looking for is to extend this BaseActiviy into more activities you will need to make this BaseActivity abstract then you can extend in each activity this BaseClass and just use the interface methods
abstract class BaseActivity : AppCompatActivity() , ProgressBarInterface {
private val TAG = "MainActivity"
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
override fun showProgressBar() {
progressBar.visibility = View.VISIBLE
}
override fun hideProgressBar() {
progressBar.visibility = View.GONE
}
}
and then in your Activities you can extend from BaseActivity() and just use your interface methods as you have defined in that BaseActivity() to prevent coding them again, you can do
class FirstActivity : BaseActivity() {
...
button.setOnClickListener {
showProgressBar()
}
An easier way to show and hide the views? Use extension functions
fun View.show() {
this.visibility = View.VISIBLE
}
fun View.hide() {
this.visibility = View.GONE
}
you can define that extensions in any class, for example ViewUtils.kt and then just call
button.setOnClickListener {
progressBar.show()
}
or
button.setOnClickListener {
progressBar.hide()
}
I am updating a view in activity's onCreate method which is working fine using kotlin extension as stated below.
Activity's onCreate
import kotlinx.android.synthetic.main.activity_otpverification.*
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_otpverification)
tvContactNumber.text = getString(R.string.dual_string_value_placeholder)
}
Then at a click of a button I am showing a custom dialog for performing some action. When the dialog is dismissed I update the same textView in activity with the data sent from dialog, but the view tvContact is throwing null exception.
Activity's onClick
override fun onClick(p0: View?) {
when (p0?.id) {
R.id.ivEdit -> {
object : ChangeNumberDialog(this) {
override fun onSubmitClicked(number: String) {
tvContactNumber.text =number
}
}.show()
}
}
}
onSubmitClicked is an abstract method in the dialog which is triggered when the dialog is dismissed.
Error from logcat :
java.lang.IllegalStateException: tvContactNumber must not be null
at com.beat.em.ui.activities.OTPVerificationActivity$onClick$1.onSubmitClicked
(OTPVerificationActivity.kt:211)
onCreate and onClick methods from the ChangeNumberDialog:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val view = layoutInflater.inflate(R.layout.dialog_change_number, null, false)
setContentView(view)
setCanceledOnTouchOutside(false)
setCancelable(true)
tvSubmit.setOnClickListener(this)
}
override fun onClick(view: View) {
when (view.id) {
R.id.tvSubmit -> {
onSubmitClicked(etNumber.text.toString().trim())
dismiss()
}
}
}
I have just started using kotlin extension and not able to understand the cause. Help appreciated.
The variable you are trying to access is in another scope, try to add explicit scope to the view i.e.
this#YourActivity.tvContactNumber.text = number
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)
}
..................
..................
}