Reputation: 41
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.
Upvotes: 1
Views: 452
Reputation: 93699
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.
Upvotes: 1
Reputation: 132
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.
}
}
Upvotes: 1
Reputation: 319
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() {
//
//
}
Upvotes: 1