Unable to get a reference for quick settings tile - java

I can set the state of the qs tile using the onClick() method but I would also like to toggle its state using a switch in my Main Activity. However, I can't change the state of the tile using a switch because getQsTile returns null outside the onClick() method.
public class AwesomeTileService extends TileService{
Tile tile;
public Activity activity;
public AwesomeTileService(Activity activity){
this.activity=activity;
Switch switch1 = (Switch)this.activity.findViewById(R.id.switch1);
switch1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
tile=getQsTile(); //tile is null
SwitchState(); //SwitchState does not work with onCheckedChanged
Log.v("Switch State=", ""+isChecked);
}
});
}
public AwesomeTileService(){
//zero arg constructor
}
#Override
public void onTileAdded() {
tile=getQsTile();
tile.setState(Tile.STATE_INACTIVE);
tile.updateTile();
}
#Override
public void onClick() {
tile=getQsTile(); //tile is not null
SwitchState(); //SwitchState method works with onClick
}
public void SwitchState(){
if(tile!=null){
if (tile.getState() == Tile.STATE_INACTIVE) {
tile.setState(Tile.STATE_ACTIVE);
}
else{
tile.setState(Tile.STATE_INACTIVE);
}
tile.updateTile();
}
}
}
I can simply remove the switch from the app but I'm curious to know what the issue is here and how I can overcome it.

If you want to update the tile programmatically from another activity, use sharedPreferences and store boolean value and write code in onStartListening method in tile service class to update tile. (onStartListening is called every time you swipe down the panel).

Related

Two different SwitchCompats in two different activities

I have 2 different SwithCompat in 2 different activity, what i want to achieve is, if i click the SwitchCompat in Activity A, the SwitchCompat in Acitivity B will be clicked as well. Vice Versa.
Can i achieve this with this code :
aSwitch = findViewById(R.id.switchs);
aSwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
//do something
}
}
});
first of all when you are pass bundle for open second activity pass with bundle
Intent mIntent = new Intent(this, Example.class);
Bundle mBundle = new Bundle();
mBundle.putString(key, value);
mIntent.putExtras(mBundle);
and get bundle in second activity
boolean value = getIntent().getExtras().getBoolean(key);
according to boolean value you can change switch on and off
You can define interface as a callback in activity one use to activity two like this :
public class ActivityOne{
private ICallback mICallback;
private SwitchCompat mSwitchButton;
public interface ICallback {
void getData(boolean state);
}
mSwitchButton.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)
{
mICallback.getData(isChecked);
}
});
}
And in TwoActivity:
public class TwoActivity implements OneActivity.ICallback{
#Override
public void getData(boolean state) {
//YOU CAN USE IT HERE IN SECOND ACTIVITY
}
}
Maybe you face to callback is null in first activity then you should pass context when you going to second activity from first activity.

How to disable a Button in ListView rows connected to ArrayAdapter?

My class ProductAdapter extends ArrayAdapter
on getView i'm inflating rows with 2 buttons in the each row for (+) and (-)
and set anonim OnClickListener for each button , like this :
viewHolder.removeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BigDecimal count = product.getCount().subtract(BigDecimal.ONE);
if (count.signum() < 0) count = BigDecimal.ZERO;
product.setCount(count);
viewHolder.countView.setText(formatValue(count, product.getUnit()));
mListener.onCardClick(v);
}
});
On activity i need to do some AsyncTask when i'm using integer value from each row.
The problem is when AsyncTask executing user still can change product adapter(Buttons are working).
I need to disable them while AsyncTask is working and then reenable after completing.
I was trying to disable ListView with no luck.
Also i was trying to override ArrayAdapter methods isAllEnadled and isEnabled also with no luck.
Interesting problem, you need a State and a place to save this, this State can be used to control the click behavior. You can save this either in the product itself or some other place like a list corresponding to that index
Something like
protected void onPreExecute(Void result) {
product.setFetching(true)
}
protected void onPostExecute(Void result) {
product.setFetching(false)
}
in onClick() you can check same and return like this
if (product.isFetching())
return;
You need to set a callback method for enable/disable buttons. You can use an interface for that.
pseudocode:
public interface ButtonsHandler {
void enableButtons();
void disableButtons();
}
Then you have to implement that interface in your viewHolder
public class CustomViewHolder extends RecyclerView.ViewHolder implements ButtonsHandler {
...
void enableButtons() {
yourButton1.setEnable(true);
yourButton2.setEnable(true);
}
void disableButtons() () {
yourButton1.setEnable(false);
yourButton2.setEnable(false);
}
...
}
Third, when you call the listener from the Holder to start the task, pass the object itself to manage buttons handling.
mListener.onCardClick(View v, ButtonsHandler buttonsHandler);
so the call will be:
mListener.onCardClick(v,viewHolder);
And finally, while your asynktask is working, you can call
buttonsHandler.disableButtons();
...
buttonsHandler.enableButtons();

Boolean value not being set upon onClickListener

I have two classes. I am trying to change a boolean value depending on if the user clicks a checkbox. Then, depending on whether that value is true/false (whether the checkbox was checked or not), I want to display specific text in a textview. Here is the MainActivity class:
public class BoiseActivity extends ActionBarActivity {
CheckBox cb;
public boolean isTrue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_boise);
cb = (CheckBox) findViewById(R.id.boiseCheckBox);
cb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
isTrue = true;
}
});
public boolean isTrue() {
if (isTrue == true) {
return true;
}
return false;
}
Here is the new Activity that is opened once the user presses the "submit" button:
public class BoiseResults extends BoiseActivity {
TextView TV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_boise_results);
TV = (TextView) findViewById(R.id.textView1);
if (isTrue() == true) {
TV.setText("checkbox checked");
}
}
The boolean value is never changed in the BoiseActivity class onCreate-> onClick method. Can anyone see what is wrong here? Thanks
Apart from what has been said on you picking up a book on OOP (come on guys, everybody started somewhere):
The isTrue instance variable of your BoiseActivity will be present in it's child class BoiseResults because it (BoiseResults) inherits isTrue from BoiseActivity, but it's value will not be set and therefore interpreted als false, as Vikram pointed out.
What to do to straighten this out:
use onCheckedChangeListener for CheckBox
pass the boolean ('if the checkbox has been ticked') to your second Activity via the Intent you are starting it with, links on how to do this have been given in comments
ask yourself if BoiseResults really needs to subclass BoiseActivity (I do not think it does, it is just another Activity)
get rid of all the boolean mayhem you created with the isTrue() method returning the value of your isTrue variable
Rename BoiseResults to BoiseResultsActivity for clarity
Your onCreate{...} method for BoiseActivity doesn't seem to be a closed method.
You don't need the isTrue() method as it is really only returning the value of isTrue and isTrue is already a public instance variable. Regardless of that detail, you should look into (Explicit) Intents for passing data between activities: http://www.vogella.com/tutorials/AndroidIntent/article.html
Consider the use of the OnCheckedChangeListener;
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// DO SOMETHING HERE WHEN CHECKED/UNCHECKED
}
});
You can then check whether it is checked or not using the checkBox.isChecked() method.
It's also best for you to avoid performing unnecessary checks on a boolean value like;
public boolean isTrue() {
if (isTrue == true) {
return true;
}
return false;
}
The following would achieve the same;
public boolean isTrue(){
return isTrue;
}
With regards to your BoiseResults Activity, take a look at Intents if you are going to be changing from the BoiseActivity Activity to the BoiseResults Activity from your submit and need to pass data to BoiseResults.

libgdx: InputAdapter does not work when resumed from browser

I have a Screen implementation with InputMultiplexer which is initialized in the show() method. The InputMultiplexer is initialized with InputAdapter and the Stage object.
The InputAdapter object listens for the back button.
class MyInputAdapter extends InputAdapter {
#Override
public boolean keyDown(int keycode) {
if (keycode == Keys.BACK) {
// do someting
return true;
}
return false;
}
}
class MyScreen implements Screen {
#Override
public void show() {
initInputProcessors();
}
private void initInputProcessors() {
if (backButtonInputProcessor != null) {
initInputMultiplexer();
Gdx.input.setCatchBackKey(true);
Gdx.input.setInputProcessor(inputMiltiplexer);
} else {
Gdx.input.setCatchBackKey(false);
Gdx.input.setInputProcessor(stage);
}
}
private void initInputMultiplexer() {
if (inputMiltiplexer == null) {
inputMiltiplexer = new InputMultiplexer();
inputMiltiplexer.addProcessor(backButtonInputProcessor);
inputMiltiplexer.addProcessor(stage);
}
}
}
All works fine, and the back button reacts without any problem.
The problem occurs, in the following scenario. I use admob. So when clicking an ad banner, this brings you to browser. When you are back from browser to the app, the back button is not intercepted and the application just exits.
I also tried calling the InitInputProcessors method inside the resume() method, same result.
The answer to my question on LibGDX forum has solved it. Following is the solution by skunktrader:
Try adding this to your android MainActivity
#Override
public void onResume() {
super.onResume();
theView.requestFocus();
theView.requestFocusFromTouch();
}
Where theView is the value returned from initializeForView().
Try to set your InputProcessor as null in hide() method. Like this:
#Override
public void hide() {
Gdx.input.setInputProcessor(null);
}

Android - toggleButton delay with own OnClickListener

I have problem with unwanted delay after click on toggleButton using own OnClickListener.
I make my listener by this advice on stackoverflow, like below:
public class ToggleButtonOnClickListener implements OnClickListener{
private String _name;
public ToggleButtonOnClickListener(String name) {
_name = name;
}
#Override
public void onClick(View v) {
Log.i("toggle button clicked",_name);
}
}
and using this:
toggle.setOnClickListener(new ToggleButtonOnClickListener(device.GetName()));
But it not fire onClick method after first click, but the next one.
And because I have group of toggleButtons is this very unhappy, when I click on first, and onClick method fire after click again or even after click to second (or any) from the group.
The OnCheckChangeListener behaves the same.
Please refer developer's example.
You can implement something like below:
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Enable vibrate
} else {
// Disable vibrate
}
}
After looking for errors and testing other options, I found that delay caused not Listener, but the log statement.
So, the code above working well except for
Log.i("toggle button clicked",_name);
and an alternative without needs have own class(parametrs) using OnCheckedChangeListener is:
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(buttonView.getContext(), "test", Toast.LENGTH_LONG).show();
}
});
I don't know why this Log do, but I used them only for debug, so problem solved!

Categories

Resources