Refresh Icon in Android View on Variable - android

I got a Button in my Main Activity which can get two background images.
I got a variable in a class, which is boolean and got the state the button should have.
In the onCreate() I got following:
if(HelperFunctions.itsRinging == true){
stopButton.setBackgroundResource(R.drawable.klingelt);
}else {
stopButton.setBackgroundResource(R.drawable.normal);
}
If the App is closed, a push message comes in, I open the app, the icon got the "klingelt" background. Like it should!
After clicking it, it got the "normal" background. Like it should!
But when the app is in front, the icons not changes.
How can i refresh the background even if the app is in front?
The button itself is in a standard onClick Method:
stopButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
SoundManager.stop();
//set Background normal
if(HelperFunctions.itsRinging = true) {
stopButton.setBackgroundResource(R.drawable.normal);
HelperFunctions.itsRinging = false;
}
I hope its clear, what I want to archieve here :)
So the icon should change its background in "live-mode" when the app is on in front and visible.

Related

How to check if button has been clicked

I have a bunch of dynamic buttons which I am setting an onClickListeners as they are produced, as well as tagging them with IDs.
Not sure if this a simple one which I have just spent too much time staring at but this is the problem.
If a user clicks a button, it changes colour this is simple and has been achieved by:
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (counter == 0) {
button.setBackgroundColor(Color.parseColor("#FF4DCBBF"));
Toast.makeText(getActivity(), "User Has Been Marked As Present",Toast.LENGTH_LONG).show();
//change boolean value
userPresent = true;
counter++;
} else {
button.setBackgroundColor(Color.parseColor("#FFFFFF"));
Toast.makeText(getActivity(), "User Has Been Marked As Absent",Toast.LENGTH_LONG).show();
//change boolean value
userPresent = false;
counter = 0;
}
}
});
If the user clicks it again, it will change back to the previous colour - but...
If the user clicks one of the other dynamic buttons that hasn't been previously clicked, the counter is thrown out.
I need to know if the button has been clicked and if not, should mark the user as present.
Currently, If on one button I click it and mark the user as present, and then move onto the next button, I will have to click it once (which marks the user as absent due to the counter) then press it again to mark the user as present.
I need the counter to treat each button individually, any ideas how this could be achieved?
Once the user has been marked present,maybe disable the onClick listener for that button since you wouldn't need it anymore?
I don't mean to sound condescending but I'm having trouble understanding what you're trying to achieve, but if each button is supposed to hold different information about a user, why not make a custom button that does just that? Make a class called customButton in your package and paste the following code there:
import android.content.Context;
import android.widget.Button;
public class customButton extends Button {
boolean haveIBeenClicked; //false by default
public customButton(Context context) {
super(context);
}
public void toggleHaveIBeenClicked(){
haveIBeenClicked=!haveIBeenClicked;
updateBackgroundColor();
}
void updateBackgroundColor(){
if (haveIBeenClicked){
this.setBackgroundColor(Color.parseColor("#FF4DCBBF"));
}
else{
this.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
}
}
then, inside the onClick method (in the activity whose snippet you've shown earlier) you can just call
((customButton)v).toggleHaveIBeenClicked();
...after having created a customButton object and setting an on click listener on it.
Please let me know if this achieves what you desired. If you have trouble running this code, make sure to let me know if the comments and we'll work it out

Android Studio changing background color from fragment not working?

I am trying to change the background color of my whole application through a settings page, I have searched for a long time and so far cannot find a solution.
So, inside the fragment I have a button, and it acts as a toggle. When clicking the background color changes as it should, from white to grey interchangeably.
Here is the logic for the button:
Button changeBGButton = (Button) view.findViewById(R.id.btn_change_bg);
changeBGButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((MainActivity)getActivity()).bgWhite == true)
{
//White is true, set to grey
((MainActivity)getActivity()).bgWhite = false;
((MainActivity)getActivity()).bgGrey = true;
Toast.makeText(getActivity(), "Set is white to false, grey to true.", Toast.LENGTH_SHORT).show();
}
else if (((MainActivity)getActivity()).bgWhite == false && ((MainActivity)getActivity()).bgGrey == true)
{
//White is true, set to grey
((MainActivity)getActivity()).bgWhite = true;
((MainActivity)getActivity()).bgGrey = false;
Toast.makeText(getActivity(), "Set is white to true, grey to false.", Toast.LENGTH_SHORT).show();
}
}
});
The main activity, inside OnCreateView override method is responsible for changing the background color, and it does. Upon clicking the button it changes.
But...
When pressing the back button to return to the previous screen in the backstack the background becomes white, if I then go to the settings page again it is grey, and again changes to white on pressing back.
It is acting as if only the fragment has the background changed, but the layout contentLayout only exists on the mainLayout not the fragment.
I suspect during the calling of the Fragment Lifecycle Methods the background is being reset, But I do not know how to make it persist as no instance is saved on the back button pressed.
Anyway. Here is the logic that changes the background, including the overriding method:
#Override
public View onCreateView(View parent, String name, Context context, AttributeSet attrs)
{
if (contentMain != null) //Only do this if we have a hope at not crashing the application/
if (bgWhite == true && bgGrey == false)
{
contentMain.setBackgroundColor(Color.parseColor("#FFFFFF"));
}
else if (bgGrey == true && bgWhite == false)
{
contentMain.setBackgroundColor(Color.parseColor("#888888"));
}
return super.onCreateView(parent, name, context, attrs);
}
I have tried to do it inside of the OnBackstackChangeListener too but the same result is experienced. Please help me out.
Cheers.
The problem is a bit ambiguous, but by what i think the problem is mainly because the color you are setting is from the onCreateView() method of your fragment, if the contentMain layout in resident on the activity which holds the fragments then you can create a method on that activity that change the color from the activity rather than the onCreateView of the fragment.
Thus you button click will look like this::
(MainActivity)getActivity().changeBgColor(bgGrey);
so the changes that you make here are done through the activity which controls all the fragments rather than the fragment itself.
Hope that help, good luck.

Listening for clicks on a disabled AlertDialog-Button

I want to listen for clicks on the positive button of an AlertDialog, which I have disabled by calling button.setEnabled(false);.
How should I do this? If this is not possible, is there a known workaround?
PS. The reason I want to do this, is that I want to show a toast when somebody hits the button, saying "You need to do this before you can continue".
This is not a way to listen for clicks on a disabled button. This is a workaround.
I liked the result I got by changing the color of the button, making it look like it's disabled.
What you want to do:
// Instantiate positive button
final Button posButton = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
// Save the original button's background
final Drawable bg = posButton.getBackground();
// Set button's looks based on boolean
if (buttonDisabled) {
posButton.setTextColor(getResources().getColor(R.color.disabledButtonColor, null));
// R.color.disabledButtonColor == #DBDBDB, which is pretty close to
// the color a disabled button gets.
posButton.setBackgroundColor(Color.TRANSPARENT);
// Color.TRANSPARENT makes sure all effects the button usually shows disappear.
} else {
posButton.setTextColor(getResources().getColor(R.color.colorPrimaryDark, null));
// R.color.colorPrimaryDark is the color that gets used all around my app.
// It was the closest to the original for me.
posButton.setBackground(bg);
// bg is the background we got from the original button before.
// Setting it here also re-instates the effects the button should have.
}
Now, don't forget to catch your buttons actions whenever it's "disabled"
public void onClick(View v) {
if (buttonDisabled) {
// Button is clicked while it's disabled
} else {
// Button is clicked while it's enabled, like normal
}
}
That should do, have fun with it.

Get informed which button in popup window has been clicked

Im trying to implement a feature of my small android app.
There is a image button, and a popup window will appear when I click it. According to which button in that popup window user click, that image button should change its image accordingly.
Like when i click 1 in the popup window, I should inform the button to update image to 1.
Can anybody tell me how to do this?
You can make use of the following method
public void setTabFor(Button btn) {
for (Button button : btnArray) {
if (button == btn) {
if (button == button_one) {
Utils.setTabButton(R.drawable.left_selected, button_one);
} else if (button == button_two) {
Utils.setTabButton(R.drawable.middle_selected, button_two);
} else if (button == button_three) {
Utils.setTabButton(R.drawable.middle_selected, button_three);
} else {
Utils.setTabButton(R.drawable.right_selected, button_four);
}
}
Where setTabButton in Utils class is used to set backgrounddrawable:
public static void setTabButton(int drawable, Button... btn) {
for (Button button : btn) {
button.setBackgroundResource(drawable);
}
}
You should assign an ID to each of the buttons in popup window. Then you have to implement a listener that will notify (and pass the ID to) your object (responsible for the image button) that a button was clicked and your object will update the image button according to the received ID.
Try this,
First of all,you have to assign unique Id for all image button that you used in your application.then which button you clicked with help of Ids u can display buttons as per your choice.
I hope it will help you and solve your problem very soon.

changing visibility of text android

Hi i have two textViews that i initially set its visibility to gone then animate in and become visible. now i want to make the invisible again but for some reason they're still showing on screen does anyone no why?
in my onCreate() i make the view gone
register = (TextView)findViewById(R.id.register);
register.setVisibility(View.GONE);
forgotpassword = (TextView)findViewById(R.id.forgotpw);
forgotpassword.setVisibility(View.GONE);
then later on i make it visible
public void run()
{
animations();
loginForm.setVisibility(View.VISIBLE);
register.setVisibility(View.VISIBLE);
forgotpassword.setVisibility(View.VISIBLE);
}
and then when a user presses a button i want the text views to become invisible so that they retain their layout but they stay visible on screen
signInBtn = (Button) findViewById(R.id.signin);
signInBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
signInProcess();
}
});
public void signInProcess() {
register.setVisibility(View.INVISIBLE);
forgotpassword.setVisibility(View.INVISIBLE);
setuploader.setVisibility(View.VISIBLE);
}
In Android when you animate something, It's just drawn somewhere else. The actual element is not moved. So when you animate signInBtn it's drawn somewhere else, but the actual button is not moved from the original position. So when you click the button the click handler is not called.
To avoid this set fillAfter = True in your animation so the button will actually get moved at the end of your animation.
Also, after animating a view in Android make sure you call View.clearAnimation() before trying to change its visibility.

Categories

Resources