Android Notification Area Customization - java

I don't know whether this question get minus points, but I searched every where and my last resort is stackoverflow.
I need to add five buttons to notification area in horizontally. And each button I need to add even listener. I know it is possible to do with RemoteViews. But I never seen anyone adding event listener to each element.
These are the references if anyone need to refer.
Notifications Documentation
How to create a custom notification on android
SlidingDrawer API

You can add 5 anonymous listeners, or a single named listener.
Anonymous:
Button b1 = new Button(...);
b1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// first listener's code goes here
}
});
Button b2 = new Button(...);
b2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// second listener's code goes here
}
});
...
named is much the same, but contains a switch statement to differentiate what happens:
View.OnClickListener myListener = new View.OnClickListener() {
public void onClick(View v) {
String buttonTitle = ((Button)v).getText();
if ("title1".equals(buttonTitle)) {
// do things for the first button's click
} else if ("title2".equals(buttonTitle)) {
// do things for the second button's click
}
...
}
});
...

Related

Android - Cannot setClickable(false) a button after click

I have a button like a switch where I am trying to setClickable(false) after I click it so that only the first click will be handled
(additional clicks are ignored in the case of accidental double-clicks/multiple-clicks).
Here is a similar code:
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button.setClickable(false);
//do other things
}
});
Then eventually, I have a code somewhere where I will reset the clickable to true, depending on a state variable, so I can switch-off.
The problem is when I click the button very quickly, it seems the succeeding clicks are still handled.
Is there a delay to the effects of setClickable()?
Also, I have read about using setEnabled(false) instead, but I cannot use it in my case. I need the button to still be enabled but not clickable.
Judging from your comment you probably need something like this.
Boolean SWITCH_ON = false;
Button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!SWITCH_ON ){
SWITCH_ON = true;
}
}
});
Button.setOnLongClickListener(new View.OnLongClickListener() {
public boolean onLongClick(View v) {
if(SWITCH_ON ){
// do your task for long click here ...SWITCH_ON
}
return true;
}
});
You can use button.setEnabled(false); within your onClick method to disable the button.
Disabled buttons don't trigger the onClick method, and you can easily re-enable it with button.setEnabled(true); when needed.
You could add another variable named buttonEnabled or so and initialize it with true. Then in the onclick do this:
Button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Button.setClickable(false);
if(buttonEnabled) {
//do other things
}
buttonEnabled = false;
}
});
Note that you need to change the variable to if you want to reactivate it

How to create a util that can be referenced multiple times in different activities

I have successfully implemented a custom Dialog box that appears when the user tries to leave an activity via a back button or by using onBackPressed(). They can simply cancel the dialog box or continue, and leave the activity. This function has been implemented in multiple activities, however its making my code a lot longer than it needs to be. I wanted to know how to create a util that can be referenced in different activities, without the need for the chunk of code to copy pasted multiple times. Please note that I am retrieving the dialog title and description from string.xml
This is my code:
Dialog customDialog;
Button button_one, button_two;
TextView dialog_title, dialog_description;
customDialog = new Dialog(this);
//Back button will close app
#Override
public void onBackPressed() {
customDialog.setContentView(R.layout.custom_dialog_box);
dialog_title = customDialog.findViewById(R.id.dialog_title);
dialog_title.setText(getString(R.string.leaving_activity_warning_title));
dialog_description = customDialog.findViewById(R.id.dialog_description); dialog_description.setText(getString(R.string.leaving_activity_warning_description));
button_one = customDialog.findViewById(R.id.button_one);
button_one.setText(getString(R.string.cancel));
button_two = customDialog.findViewById(R.id.button_two);
button_two.setText(getString(R.string.leave_anyway));
button_one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
}
});
button_two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
finish();
overridePendingTransition(R.anim.slide_in_left, R.anim.slide_out_right);
}
});
Objects.requireNonNull(customDialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
customDialog.show();
}
UPDATE
Created a Java file called "DialogBoxMessage"
DialogBoxMessage Code:
class DialogBoxMessage {
private Dialog customDialog;
private TextView dialog_title, dialog_description;
private Button button_one, button_two;
//Custom Dialog Box Initialization
DialogBoxMessage(Button myButtonOne, TextView myDialogTitle, TextView myDialogDescription, Dialog myCustomDialog) {
customDialog = myCustomDialog;
button_one = myButtonOne;
button_two = myButtonOne;
dialog_title = myDialogTitle;
dialog_description = myDialogDescription;
}
void leaveActivity() {
customDialog.setContentView(R.layout.custom_dialog_box);
dialog_title = customDialog.findViewById(R.id.dialog_title);
dialog_title.setText(Resources.getSystem().getString(R.string.leaving_activity_warning_title));
dialog_description = customDialog.findViewById(R.id.dialog_description);
dialog_description.setText(Resources.getSystem().getString(R.string.leaving_activity_warning_description));
button_one = customDialog.findViewById(R.id.button_one);
button_one.setText(Resources.getSystem().getString(R.string.cancel));
button_two = customDialog.findViewById(R.id.button_two);
button_two.setText(Resources.getSystem().getString(R.string.leave_anyway));
button_one.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
}
});
button_two.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
customDialog.dismiss();
}
});
Objects.requireNonNull(customDialog.getWindow()).setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
customDialog.show();
}
}
I input the following code in another activity
Other activity code:
//Reusable exit dialog message
DialogBoxMessage dialogBoxMessage;
//Back button will close app
#Override
public void onBackPressed() {
dialogBoxMessage.leaveActivity();
finish();
}
But it doesn't seem to work, I think there are a lot of issues... please help :(
I assume customDialog is a seperate class you wrote - therefore i would suggest you put main information like contentview, title, message or type in the constructor when you initialize ur Dialog.
For your onClick Method I suggest you create an Interface to handle Button Clicks in your
customDialog class.
This could be implemented as a static method in a utilities class. The method would require 'this' as a parameter, which contains the activity context. The method should return the result of the button press. The activity can use this response to determine if finish() should be called or not.
UPDATE
I had suggested a simple static method, but you've gone down the object-oriented route. That's fine.
However, your constructor requires passing in several views, which wouldn't appear to achieve the code efficiency you are after.
Your constructor should just require the Activity context; everything else is encapsulated in your new class.
In each Activity's onBackPressed method you will need to create the object with
dialogBoxMessage = new DialogBoxMessage(this);
before you can call any of that object's methods.

Can a ToggleButton change the functionality of other buttons in the same Activity?

Summed up, can a ToggleButton change what other buttons in the activity do when toggled? If so, a more specific explanation of what I want to do is below:
Basically there are three buttons and a togglebutton. When the togglebutton is toggled, pressing any of the three buttons will take a picture and 'save it' for that button. When untoggled, pressing any of the three buttons simply displays their images. I think I can figure out the camera capture part, but I need some direction when it comes to the togglebutton.
Any help is appreciated and I can explain further if necessary.
What I would do is keep a couple flags for each state at the class level, like this:
public class MyClass {
private static final int STATE_SAVE = 0;
private static final int STATE_DISPLAY = 1;
private int currentState = STATE_DISPLAY;
// I made this default for the example,
// you should use what makes sense to your project.
}
Then, inside your toggle button, you can set the flag. Paraphrasing this code since I don't have an editor open:
toggleButton.setOnToggleListener(new OnToggleListener() {
#Override
public void onToggled(boolean toggled) {
if(toggled) {
currentState = STATE_SAVE;
} else {
currentState = STATE_DISPLAY;
}
});
Now, when the buttons are clicked, you can switch based on the state to do an action:
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(currentState == STATE_SAVE) {
// Save the image.
} else if (currentState == STATE_DISPLAY) {
// Display the image.
}
});
Create a boolean such as isToggleOn that is true or false depending on the ToggleButton. Then for each of your buttons, you can simply do:
Button button1 = (Button) findViewById(R.id.button1);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(isToggleOn){
//do one thing
} else {
//do other thing
}
}
});
Yes, you definitely can. Very rarely is anything impossible with code!
All you have to do is to change the listener of the three buttons when the togglebutton is pressed. You keep alternating between the listeners each time you toggle.
For your purpose, I'd suggest defining two sets of listeners - two for each of the three buttons and then keep changing between them.

Using a common dialog box, but calling different functions from diffrent activities in android

In my app i have to use same dialog box on all the activities but then on to the click of button on dialog box i need to perform different operations for different activities, i have kept a common code for dialog but then how to call different functions, here is my code:
final Dialog dialog = new Dialog(mContext,R.style.Theme_Levels);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom_alert);
TextView title = (TextView)dialog.findViewById(R.id.title);
title.setText("Network Error");
TextView msg = (TextView)dialog.findViewById(R.id.msg_txt);
msg.setText("The system is down, please check after some time ");
ImageView cancel = (ImageView)dialog.findViewById(R.id.cancel);
cancel.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
TextView continue_btn = (TextView)dialog.findViewById(R.id.continue_btn);
continue_btn.setBackgroundResource(R.drawable.feedback_button_purple);
continue_btn.setText("Retry");
continue_btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//TODO perform different operation depending upon from where this function has been called
dialog.dismiss();
}
});
dialog.show();
Create an interface, say DialogActivity, with one method "handlePositiveButton". Let all your Activities implement this interface. From the Dialog.onClick you do this:
DialogActivity activity = (DialogActivity) getActivity();
activity.handlePositiveButton();
Put the code you have specified in a function of a Utils file.
Then pass the positive button onclick listener in that function.
Refer the below code.
public static void showAlertDialog(OnClickListener listener) {
// enter your code here
continue_btn.setOnClickListener(listener);
// more code here
}

how to calculate the number of touches on a button in android

hi i am a new developer. i am trying to design an app. In my app i want to calculate the no of touches in a particular button. Is this can be calculated by onTouch process if yes can anyone give me an example or idea.
Try below code
First Create an Global variable
int numberOfClick = 0;
Now for your button try following code
clickButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
numberOfClick++;
}
}
now you can get the number of clicks by this variable
A click on a button is sent to the app via the onClick event. So if you have a Button:
Button myButton = (Button) findViewById(R.id.myButton);
myButton.setOnClickListener(myClickListener);
You can set up your onClickListener to do whatever you want when the button is clicked.
// Create an anonymous implementation of OnClickListener
private OnClickListener myClickListener = new OnClickListener() {
public void onClick(View v) {
// increment the counter on click
numberOfClicks++;
}
};

Categories

Resources