I am creating one parent Activity(Class) and then want to extends this class to another Activity(Class). I have some controls in all the Activities(Classes) so I decide to use Abstract class so that I need not write some common code in all the classes.I created below classes and one of it is abstract class.When I am calling my Welcomepage Activity this will display me a screen with all common controls(Radio buttons in my case).In Abstract class I had set checkedChangedListener listener and in onCheckedChanged() method I am creating a toast but It is not displaying.I am confused in this case.What is the reason to not displaying a toast?.
My Activity(Class) Welcomepage_pillReminder which extends CustomTabsActivity
public class Welcomepage_pillReminder extends CustomTabsActivity
#Override
public void mappingWidgets() {
super.mappingWidgets();
}
#Override
public void addCheckChangedListner() {
super.addCheckChangedListner();
}
CustomActivity
public abstract class CustomTabsActivity extends Activity {
protected RadioButton radHome;
public void mappingWidgets(){
radHome = (RadioButton)findViewById(R.id.radHome);
}
public void addCheckChangedListner() {
radHome.setOnCheckedChangeListener(onCheckedChangeListener);
}
OnCheckedChangeListener onCheckedChangeListener = new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked) {
if(buttonView.getText().equals("Home")) {
Toast.makeText(getApplicationContext(), "Home", 2000).show();
}
}
}
};
}
You can do it like this,
Your CustomActivity.java
public class CustomActivity extends Activity implements OnClickListener{
public void initLayout(Button button){
button.setOnClickListener(this);
}
public void simple_method(){
System.out.println("test in CustomActivity");
}
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Button Clicked in Custom Activity", Toast.LENGTH_LONG).show();
}
}
Activity that extends CustomActivity
CustomClassDemoActivity.java
public class CustomClassDemoActivity extends CustomActivity{
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.myTextView);
button.setText("This is a Custom Activity Example");
super.initLayout(button);
simple_method();
}
public void simple_method(){
super.simple_method();
System.out.println("test in mainClass");
}
#Override
public void onClick(View v) {
super.onClick(v);
Toast.makeText(getApplicationContext(), "Button Clicked in Main Activity", Toast.LENGTH_LONG).show();
}
}
For your purpose the using of an abstract class dosent make sense.
An abstract class only declares its methods and the concrete class can implement the method then depending on its purpose. It would make sense if you plan to write various classes which extend your activity and each of them would have another implementation purpose for mappingWidgets() and addCheckChangedListner().
In your case a simple class which will be extended would do the job.
Furthermore I see, that you use the #Override annotation in your sub class. With this annotation you override your methods.
At first, please remove the #Override in your methods of the sub class. I guess they will work then.
Maybe also reconsider when usin abstract classes and / or #Override.
I forgot to call function mappingWidgets() and addCheckChangedListner() after calling this two method my code works fine.Thanks all for help me.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.welcome_pillminder);
mappingWidgets();
addCheckChangedListner();
}
Related
I have been looking for my question on Google and inside the forum but I cannot an answer so far. I am using android studio to code an app in Java
The thing is : inside a class, I have overrided the OnClick() method because I have to implement a lot of views.
Now I want to call a function just before OnClick() is called.
What I mean is, for instance, if the user taps on a button, before OnClick() is called I want one of my methods to be called.
Does anyone know how to do this ?
Thank you in advance
I want to call a function just before OnClick() is called
You can use a logic inside onClick() like,
if(userhaspermission())//your method to check if the user has permission
{
//your onclick operation code
}
Make your checking permission method with return type as boolean
Implement your custom OnClickListener as follows
public abstract class MyOnClickListener implements OnClickListener {
#Override
public void onClick(View v) {
//Do common action
if(condidtionSatisfied){
performClick(v);
}
}
public abstract void performClick(View v);
}
Set onClickListener to any component as follows:
button.setOnClickListener(new MyOnClickListener() {
#Override
public void performClick(View v) {
//Execute post click action
}
});
This will ensure your common code will be called for all the views and it would be much cleaner approach.
Override the onClick() method and write your first logic which you want.
Write your own Listener like Sagar said
This logic is quite similar to Sagar's Answers.
Write abstract class as given below
public abstract class OnClick implements View.OnClickListener {
public void beforeClick(View v) {
}
#Override
public void onClick(View v) {
beforeClick(v);
performClick(v);
afterClick(v);
}
public void performClick(View v) {
}
public void afterClick(View v) {
}
}
NOTE : See in above code it implements View.OnClickListener, so this logic will works for views which extends View super class. If you want above logic for object's which needs Dialog interface onClick then you need to modify it for DialogInterface.OnClickListener.
So you can use above logic as below
//Let say you need it for button with id button
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClick() {
#Override
public void beforeClick(View v) {
Toast.makeText(MainActivity.this, "Before", Toast.LENGTH_SHORT).show();
}
#Override
public void performClick(View v) {
Toast.makeText(MainActivity.this, "Click", Toast.LENGTH_SHORT).show();
}
#Override
public void afterClick(View v) {
Toast.makeText(MainActivity.this, "After", Toast.LENGTH_SHORT).show();
}
});
I tried to use a bottom sheet which use a layout of an activity, not dialog.
[Here is my bottom sheet][1]
But I can't access "Pay Now" button. I tried to use click listener in the same activity, but nothing happens. How can I listen this button and where?
Activity of Bottom Sheet
public class SheetActivity extends AppCompatActivity {
Button sheet_button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sheet);
sheet_button = (Button)findViewById(R.id.sheet_button);
//
sheet_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getApplicationContext(), "Done!", Toast.LENGTH_SHORT).show();
sheet_button.setText("deneme");
}
});
}}
This is generally pretty simple.
If your bottom sheet is a DialogFragment or BottomSheetDialogFrament attach a listener into the onAttach(Context context) method like so:
define the interface in the Fragment:
interface CheckoutButtonListener {
void onClick(/*provide whatever arguments you need to back to parent*/);
}
usage:
// Define the member variable
private CheckoutButtonListener mCheckoutListener;
#Override
public void onAttach(Context context){
super.onAttach(context);
try{
mCheckoutListener = (CheckoutButtonListener) context;
}catch(ClassCastException){
// Handle the error silently or rethrow so usage is expected
}
}
Then when you attach the listener to the button:
mButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
mCheckoutListener.onClick(); // call the interface method
}
});
Finally, implement the interface in whatever Activity is showing the sheet:
class MyActivity extends AppCompatActivity implements CheckoutButtonListener {
//.. other code
// interface method
#Override
public void onClick(){
// do whatever you need to do
}
}
If you are not using a re-usable bottom sheet (i.e as a DialogFragment) then you should include this code in each place you display it, however better to modularize.
Good Luck and Happy Coding!
My ParentActivity.java is like this
public class ParentActivity extends Activity{
public void childOnlyMethod(){
Log.d(TAG,"child only method triggered in parent activity");
}
public void startChildActivtityButton(){
startActivity(new Intent(this, ChildActivity.class));
}
public void childOnlyMethodButton(){
childOnlyMethod();
}
}
And my ChildActivity.java is like this
public class ChildActivity extends ParentActivity{
#Override
public void childOnlyMethod(){
Log.d(TAG,"child only method triggered in child activity");
}
}
The problem is when I press childOnlyMethodButton, childOnlyMethod() in both parent and child activity gets invoked I want it to be invoked on child only how can I achieve that?
#Override annotation does nothing, it is only used to tell the compiler and IDE that this method overrides its super class. Non-static methods are associated to objects, not classes. Overriding means completely replace the method in its super class. So if you invoke childOnlyMethod on ChildActivity, only child version will be invoked.
I'm guessing you were actually clicking on the parent activity instance. I don't really get the point why you want to invoke a child method on parent reference. If you can post the real code, I can give you more precise answer.
However, you can try the following code. This example will only invoke child version childOnlyMethod on ChildActivity instance. But it will still invoke parent's childOnlyMethod if you click the button on ParentActivity.
public class ParentActivity extends Activity implements View.OnClickListener {
private static final String TAG = "ParentActivity";
private Button mStartChileButton;
private Button mButton;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStartChileButton = (Button) findViewById(R.id.startChileButton);
mButton = (Button) findViewById(R.id.button);
mStartChileButton.setOnClickListener(this);
mButton.setOnClickListener(this);
}
public void onClick(View v) {
switch(v.getId()) {
case R.id.R.id.startChileButton:
startActivity(new Intent(this, ChildActivity.class));
break;
case R.id.R.id.button:
default:
childOnlyMethod();
break;
}
}
public void childOnlyMethod() {
Log.d(TAG, "Called from ParentActivity");
}
}
public class ChildActivity extends ParentActivity {
private static final String TAG = "ChildActivity";
#Override
public void childOnlyMethod() {
Log.d(TAG, "Called from ChildActivity");
}
}
If you are extending a parent class, all the methods in the parent class are passed down somewhat to the child class, if you don't want the method in the parent class to be implemented, first start by removing that #Override annotation in the child class above the method. Then use this snipped below, all it does is make the Method in the parent class either private or protected so that it can't be accessed in the child class.
public class ParentActivity extends Activity {
private void childOnlyMethod(){
Log.d(TAG,"child only method triggered in parent activity");
}
//OR
protected void childOnlyMethod(){
Log.d(TAG,"child only method triggered in parent activity");
}
}
Can i define an activity in parent class of all activities that that can open new activity like this method that working:
public class ActivityBase extends Activity{
public <T extends Activity,U extends Activity> void openActivity()
{
Intent myIntent = new Intent(T.this, U.class);
T.this.startActivity(myIntent);
}
}
public class ActivityChield extends ActivityBase{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_warning_unregistered_shipping);
// Set widgets reference
btnOpenActivity = (Button)findViewById(R.id.btn_wus_select_violation);
// Set widgets event listener
setListeners();
}
private void setListeners()
{
btnOpenActivity.setOnClickListener( new View.OnClickListener()
{
#Override
public void onClick(View view)
{
openActivity<ActivityChield , OtherActivity>();
}
});
}
}
This code is not working . Please help me how can i define a method that can open all activities with one method.
I don't think this way is a perfect solution. Better is to write the calling code when you need.
By the way here is a solution for your question
public void openActivity(Class<?> calledActivity) {
Intent myIntent = new Intent(this, calledActivity);
this.startActivity(myIntent);
}
And you can call it as
openActivity(OtherActivity.class);
Is View.OnClickListener() a function or interface? When we try to set a onclicklistener() method in android, we use new View.OnClickListener() and it bugs me there cuz as far as I know,
we don't need to initialize an object of class containing static method inorder to use those methods. Why we do this?
When we use implements inorder to implement an interface, we don't call the static methods of the interface.
So can some one tell me why do we do:
new View.OnClickListener(), for using onclick() method?
Why do we use () with View.OnClickListener if it is an interface?
Thanks for your reply..
I'm not sure I understand what you are writing about static methods. View.OnClickListener is an interface: http://developer.android.com/reference/android/view/View.OnClickListener.html
To set a click listener on a view, you pass an instance implementing the OnClickListerner interface: http://developer.android.com/reference/android/view/View.html#setOnClickListener(android.view.View.OnClickListener)
The most common way to do this in android is to define an anonymous inner class (http://docs.oracle.com/javase/tutorial/java/javaOO/innerclasses.html) that implements OnClickListener like
myView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle view click here
}
});
The code above both defines an anonymous inner class and creates an instance of it. It is equivalent to first defining a class implementing View.OnClickListener (if defined in the same class)
class MyOnClickListener implements View.OnClickListener {
#Override
public void onClick(View v) {
// Handle view click here
}
}
And later using this
MyOnClickListener listener = new MyOnClickListener();
myView.setOnClickListener(listener);
Sample Code,
Internally it works something like this,
public class MyView{
public stinterface MyInterface{
public void myOnClick(View view);
}
}
public class MyButton{
View view;
public void setOnClicker(MyInterface onClicker) {
onClicker.myOnClick(view);
}
}
public class MyExample{
public void method(){
MyButton myButton = new MyButton();
myButton.setOnClicker(new MyInterface() {
#Override
public void myOnClick(View view) {
}
});
}
}