Change State of ToggleButton programmatically - android

I have placed a togglebutton with id toggleButton
and I am using the code below to check if it's on or off
public void toggleClick(View view){
boolean on = ((ToggleButton) view).isChecked();
if(on){
}else{
}
}
Everything works fine.. I am trying to change the state of the toggle button, I tried a lot of codes through internet but none changes the state of the toggle to off.
I tried this:
ToggleButton toggleButtons;
toggleButtons = (ToggleButton) findViewById(R.id.toggleButton); //In onCreate
toggleButtons.setChecked(false);
any ideas please?

Use setChecked(boolean checked) by example:
toggleButtons.setChecked(true)

I ended up using Toggle feature using ImageButton. The code below works fine.
*
private ImageButton ib;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//......
ib = (ImageButton) rootView.findViewById(R.id.imagefavouriteButton);
if (checkFavorite()) {
ib.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.fav_on));
} else {
ib.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.fav_off));
}
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (checkFavorite()) {
remFromFav();
} else {
addFavorites();
}
}
});
//.....
}
private boolean checkFavorite(){// code to check in db}
public void addFavorites() {
ib.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.fav_on));
Toast.makeText(getActivity(), "Added ...", Toast.LENGTH_SHORT).show();
}
public void remFromFav() {
ib.setImageDrawable(ContextCompat.getDrawable(getContext(), R.drawable.fav_off));
Toast.makeText(getActivity(), "Removed ...", Toast.LENGTH_SHORT).show();
}
Hope this helps you.

Related

setVisible not always in operation

I have LinearLayout and a Button, in layout I have a SeekBar and when click button I show or hide LinearLayout, I used View.GONE and View.Visible to hide and show.
It work in many devices but when I test it in Note Edge or cool pad It does not work.
What is happening here?
rlFont = (RelativeLayout) rootView.findViewById(R.id.rlFont);
ivFont.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ivFont.getDrawable().getConstantState().equals(getActivity().getResources().getDrawable(R.drawable.top_a001).getConstantState())) {
ivFont.setImageResource(R.drawable.top_a002);
rlFont.setVisibility(View.VISIBLE);
rlFont.requestLayout();
} else {
ivFont.setImageResource(R.drawable.top_a001);
rlFont.setVisibility(View.GONE);
rlFont.requestLayout();
}
}
});
This can solve your problem, with this is possible to toggle your button.
boolean isClicked = false;
rlFont = (RelativeLayout) rootView.findViewById(R.id.rlFont);
ivFont.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!isClicked) {
ivFont.setImageResource(R.drawable.top_a002);
rlFont.setVisibility(View.VISIBLE);
rlFont.requestLayout();
isClicked = true;
} else {
ivFont.setImageResource(R.drawable.top_a001);
rlFont.setVisibility(View.GONE);
rlFont.requestLayout();
isClicked = false;
}
}
});

Android "Toggle off all buttons"

I am making an app that toggles off and on a switch. I have 4 switches in total and at the bottom I would like to have a button that will toggle them all off at the same time - like an override switch. I'm trying to follow the same format as when creating the 4 switches, but I can't wrap my head around how it would be. I already tried looking through stackOverFlow and I can't find anything on it, maybe I just don't know the key words.
Switch toggleapp1 = (Switch) findViewById(R.id.app1);
toggleapp1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
toggleapp1(true);
Toast.makeText(getApplicationContext(), "[Application1] Enabled!", Toast.LENGTH_LONG).show();
} else {
toggleapp1(false);
Toast.makeText(getApplicationContext(), "[Application1] Disabled!", Toast.LENGTH_LONG).show();
}
}
});
Is how one of the switches look. toggleapp1 is switched with 2,3,4.
public boolean toggleapp1(boolean status) {
if (status == true) {
return true;
}
else if (status == false) {
return false;
}
return status;
}
I have 4 switches in total and at the bottom I would like to have a button that will toggle them all off at the same time - like an override switch.
If i understood the problem you have something like:
toggleapp1 = (Switch) findViewById(R.id.app1);
toggleapp2 = (Switch) findViewById(R.id.app2);
toggleapp3 = (Switch) findViewById(R.id.app3);
toggleapp4 = (Switch) findViewById(R.id.app4);
And you want to disable all of them togheter.
You can create a method that do this:
private toggleOffSwitches(boolean state) {
toggleapp1.setChecked(state);
toggleapp2.setChecked(state);
toggleapp3.setChecked(state);
toggleapp4.setChecked(state);
}
And call it in the OnClickListener of the button:
Button yourButton = (Button) findViewById(R.id.yourButton);
yourButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
toggleOffSwitches(false);
}
});
Remember to declare your Switches as field class variables in order to use them in the toggleOffSwitches method!
UPDATE
For example:
public class MainActivity extends Activity {
private Switch toggleapp1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
....
toggleapp1 = (Switch) findViewById(R.id.app1);
....
}

How to get Click Event of futuresimple library's FloatActionButton?

I am using this library this library.I used the code described in its description on github. looks like this I have event for the menu buttons, not its main Button. i want to get its main button's click event for make rest layout blur when its clicked/expended. even want to hide or set its default state when user click or touch somewhere else on screen.
FloatingActionButton actionC = new FloatingActionButton(getBaseContext());
actionC.setIcon(R.drawable.notes);
actionC.setTitle("Add note");
actionC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//
}
});
FloatingActionsMenu floatingActionsMenu = (FloatingActionsMenu)findViewById(R.id.multiple_actions);
floatingActionsMenu.addButton(actionC);
// ((FloatingActionsMenu) findViewById(R.id.multiple_actions)).addButton(actionC);
final FloatingActionButton actionA = (FloatingActionButton) findViewById(R.id.action_a);
actionA.setIcon(R.drawable.event);
actionA.setTitle("Make life Event");
actionA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//
}
});
and the answer is .
floatingActionsMenu.setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
#Override
public void onMenuExpanded() {
Toast.makeText(Launcher.this, "Fdf", Toast.LENGTH_LONG).show();
}
#Override
public void onMenuCollapsed() {
}
});

Why doesn't the isEnabled for the ToggleButton work in Android?

I just defined a ToggleButton variable, found the view by the id, set activated to false, and set a OnClickListener. In the onClick method, I checked if it was enabled in a if statement. If it was enabled, then it should have logged it, but I checked in Console and LogCat, and it didn't display anything. Here's my code:
tb = (ToggleButton) findViewById(R.id.onoff);
tb.setActivated(false);
tb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(tb.isEnabled()){
Log.d("", "activated");
}else if(!tb.isEnabled()){
Log.d("", "deactivated");
}
}
});
I don't know how to do the 'code inside text' thing (like onClick should have a gray box over it).
public boolean isEnabled ()
This method is inherited from base class view, which mostly defines whether it is user intractable or not.
You will need to use isChecked() method to determine whether ToggleButton is on or off.
Update your code as:
tb = (ToggleButton) findViewById(R.id.onoff);
tb.setActivated(false);
tb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(((ToggleButton)v).isChecked()){
Log.d("", "activated");
}else{
Log.d("", "deactivated");
}
}
});
or second way
in your xml code
<ToggleButton
android:id="#+id/togglebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="Vibrate on"
android:textOff="Vibrate off"
android:onClick="onToggleClicked"/>
in your activity
public void onToggleClicked(View view) {
// Is the toggle on?
boolean on = ((ToggleButton) view).isChecked();
if (on) {
// Enable vibrate
} else {
// Disable vibrate
}
}

on click long implementation when onClick is already in XML?

I have actually read several answers to this but they are so different than the simple way I am implementing click responses that I am wondering if there is a way to add something simple to what I am doing to create an onLongClick responas.
Basically, all my XML code is written with statements like this:
android:onClick="onSync"
Then my Java has:
public void onSync(View v) {
...
Toast toast3=Toast.makeText(this, "Sync was pressed",Toast.LENGTH_SHORT);
toast3.show();
}
What I would like to do is have a different function that is called when the button gets a long press. Right now, a long press causes the same action as a short press.
Specifically, I would like to know how to interface to a routine such as this:
public void onSyncLong(View v) {
...
Toast toast3=Toast.makeText(this, "Long Sync was pressed",Toast.LENGTH_SHORT);
toast3.show();
}
I would certainly appreciate any help on this problem. It would be great if the reply told me what to do in the XML and in the Jave. Thanks so much.
----------------------------UPDATE------------------------
Here is my onCreate code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.start_meters);
textLL = (TextView)findViewById(R.id.textLL);
textTimer = (TextView)findViewById(R.id.textTimer);
textTimeToLine = (TextView)findViewById(R.id.textTimeToLine);
Button button = (Button) findViewById(R.id.button_sync);
button.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
}
And here is the button XML segment
<Button
android:id="#+id/buttonSync"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Gun/Sync"
android:onClick="onSync"
android:textSize="#dimen/font_small"
android:background="#drawable/round_button"
android:padding="3sp"
android:longClickable="true"/>
------------Final Update----------------
Here is the working code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
setContentView(R.layout.start_meters);
textLL = (TextView)findViewById(R.id.textLL);
textTimer = (TextView)findViewById(R.id.textTimer);
textTimeToLine = (TextView)findViewById(R.id.textTimeToLine);
Button button = (Button) findViewById(R.id.buttonSync);
button.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
StartLine2.startTime = pTime + 1000*60*5;
return true;
}
});
}
You can't do this via XML. Instead, use:
Button button = (Button) findViewById(R.id.<your_id>);
button.setOnLongClickListener(new OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
return true;
}
});
Make sure this code comes after setContentView() has been called.
Also, make sure that the android:longClickable property is set to true.
In your XML, the ID is set to buttonSync, while in the Java code you're using button_sync. This is the reason for your NullPointerException, as you don't have a button called button_sync.
public class GameScoreFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Log.v("TTT", "GameScoreFragment.OnCreateView()");
View viewScore = inflater.inflate(R.layout.gamescorelayout, container, false);
// set onLongClick listeners for both buttons
// when player long presses any of the two buttons, scores are reset
Button tempButton = (Button) viewScore.findViewById(R.id.id_button_pone_score);
tempButton.setOnLongClickListener( mLongListener );
tempButton = (Button) viewScore.findViewById(R.id.id_button_ptwo_score);
tempButton.setOnLongClickListener( mLongListener );
return viewScore;
}
// define a variable mLongListener to hold the listener code
// and then use mLongListener to set the listener
// if we don't define the variable, then we will have to write the listener code at two places (once for each button)
private View.OnLongClickListener mLongListener = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View pView) {
//reset player scores
GameFragment tempGameFragment = (GameFragment) getFragmentManager().findFragmentById(R.id.id_gamefragment);
if (tempGameFragment != null)
tempGameFragment.resetPlayersScore(false);
return true;
}
};
}

Categories

Resources