I'm new to android and Java, so I'm having problems with on click method. Here's i have 2 activities main and sub having different layouts. I want to know is there any method to implement on click action on button view which is not from main activity layout I can able show button view of sub activity layout but its on click action does not works.
Asked
Active
Viewed 61 times
2 Answers
0
What I normally like to do is set the onClickListener programmatically
myButton.setOnClickListener(v -> handleMyButtonClick());
Since you are new to Java you may not be familiar with lambdas. So your code could also look more like is.
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
// maybe call handleMyButtonClick();
}
};
The onClickListener has to be set after the button has been initialized but before you actually click it.

JonR85
- 700
- 4
- 12
-
Thanks for respond, I have a custom volume seekbar not button , Basically i need a code in which my seekbar works from any activity layout i have declared (xml layout) regardless of declaring repeatedly in all the activities. – user9330612 Nov 17 '21 at 06:15
0
You need to create object of your Activity, By using this Object you are able to use activity method.
For more detail; visit this link. How to use method from another Class?
Use this to call your Button's OnClick Action.

Bhoomika Patel
- 1,895
- 1
- 13
- 30
-
Hi, i do not want to initialize the button in second activity class. Is thier any way that in second layout ,buttons itself find its activity class where they belongs. Or can i create custom buttons ,like found in some xml layout like
– user9330612 Nov 20 '21 at 08:26