Android how to handle multiple switches - android

So I'm rather new to the android development and quickly ran into a problem, I have a view where I want to enable / disable both the bluetooth and wifi of the device, though both controlled over a seperate switch.
So here is what my view is like:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_connect"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="nl.neverdull.payleven.safeport.Connect">
<Switch
android:text="Bluetooth"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:id="#+id/bluetooth_switch"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
tools:text="bluetooth"
android:contentDescription="Status of bluetooth"
android:textOn="#string/bluetooth_on"
android:textOff="#string/bluetooth_off" />
<Switch
android:text="Wifi"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="25dp"
android:id="#+id/wifi_switch"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
tools:text="wifi" />
</RelativeLayout>
Now I already have it working for the bluetooth, which I do as follow
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_connect);
blueToothStatus();
wifiStatus();
}
public void blueToothStatus() {
Switch s = (Switch) findViewById(R.id.bluetooth_switch);
if (s != null) {
s.setOnCheckedChangeListener(this);
}
}
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Toast.makeText(this, "Bluetooth " + (isChecked ? "enabled" : "disabled"),
Toast.LENGTH_SHORT).show();
if(isChecked) {
mBluetoothAdapter.enable();
} else {
mBluetoothAdapter.disable();
}
}
Now when I try to do the same for the wifi it will ofcourse end up using the same onCheckedChanged function, so what my ideal solution would be it how to have the wifi use it's own onCheckedChanged function?
As of now I have basically the exact same code for the WIFI and Bluetooth, but it won't work since both end up going to the same function, which makes my wifi switch change the bluetooth status.
public void wifiStatus() {
Switch s = (Switch) findViewById(R.id.wifi_switch);
s.setOnCheckedChangeListener(this);//This goes to my onCheckedChanged
}

public void wifiStatus() {
Switch s = (Switch) findViewById(R.id.wifi_switch);
s.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// do what you need to do
}
});
}

Related

Android How to hide the phone number programmatically

How can I hide or show my phone number during a call without going into the settings?
you can implement this by using a check box :
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cb_Show" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/text"
/>
_
CheckBox cbShow = (CheckBox) findViewById(R.id.cb_Show);
TextView text=findViewById(R.id.text);
text.setText("some text to hide");
cbShow.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked)
text.setVisibility(VIEW.INVISIBLE);
else
text.setVisibility(VIEW.VISIBLE);
}
});
or you can use a toggleButton instead example
good luck

Why is RadioGroup.check() and RadioButton.setChecked() failing?

I have a RadioGroup with 2 RadioButtons. When clicked, some condition is validated, and, based on the outcome, the click should happen, otherwise revert.
However, it seems I can't set the check programmatically.
Here is my code:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Toast.makeText(this, "onCheckedChanged", Toast.LENGTH_SHORT).show();
switch (buttonView.getId()){
case R.id.radio_adresse:
if(radioAdresse.isChecked()){
getEditTextPlz().setEnabled(true);
getEditTextOrt().setEnabled(true);
getEditTextStrasse().setEnabled(true);
} else {
getEditTextPlz().setEnabled(false);
getEditTextOrt().setEnabled(false);
getEditTextStrasse().setEnabled(false);
}
break;
case R.id.radio_aktueller_standort:
if(isLocationAvailable()){
Toast.makeText(this, "Location available", Toast.LENGTH_SHORT).show();
if(radioAktuellerStandort.isChecked()){
getEditTextPlz().setEnabled(false);
getEditTextOrt().setEnabled(false);
getEditTextStrasse().setEnabled(false);
} else {
getEditTextPlz().setEnabled(true);
getEditTextOrt().setEnabled(true);
getEditTextStrasse().setEnabled(true);
}
} else {
if(isChecked){
} else {
}
Toast.makeText(this, "Location NOT available", Toast.LENGTH_SHORT).show();
searchModeRadioGroup.clearCheck();
searchModeRadioGroup.check(R.id.radio_adresse);
radioAdresse.setSelected(true);
radioAktuellerStandort.setSelected(false);
}
break;
}
}
Here is the XML:
<RadioGroup android:layout_width="fill_parent"
android:id="#+id/search_mode_radio_group"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_column="0"
android:layout_span="2">
<RadioButton android:id="#+id/radio_adresse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/adresse"
android:checked="true"/>
<RadioButton android:id="#+id/radio_aktueller_standort"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/aktueller_standort"
android:checked="false"
android:layout_gravity="right" />
</RadioGroup>
As you can see at the end of my event handler, I tried several methods, but none works...
Any suggestions?
I used the onClick event instead of onCheckedChanged event, and it worked.

How to define an "On/Off" Check box in android

I want to design a layout having a checkbox similar to the image below
How do I achieve this.
I tried the following but it's not the result I want :
<CheckBox
android:id="#+id/chkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="On"
android:textColor="#000"
android:textSize="17sp" />
You want a Switch:
<Switch
android:id="#+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="Play with the Switch" />
From the Android documentation:
A Switch is a two-state toggle switch widget that can select between two options
Switches are functionally similar to check boxes, but look different. A full layout example can be found here.
As Der Golem pointed out, you could also use the older Toggle Button. Here is a comparison of the two:
Switch
Toggle Button
*First of all its not a checkbox its a switch in android
**credit to mysamplecode.com for code since i couldnt post the link
as example:
<Switch
android:id="#+id/mySwitch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:text="Play with the Switch" />
<TextView
android:id="#+id/switchStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/mySwitch"
android:layout_marginTop="22dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Main:
public class MainActivity extends Activity {
private TextView switchStatus;
private Switch mySwitch;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
switchStatus = (TextView) findViewById(R.id.switchStatus);
mySwitch = (Switch) findViewById(R.id.mySwitch);
//set the switch to ON
mySwitch.setChecked(true);
//attach a listener to check for changes in state
mySwitch.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
if(isChecked){
switchStatus.setText("Switch is currently ON");
}else{
switchStatus.setText("Switch is currently OFF");
}
}
});
//check the current state before we display the screen
if(mySwitch.isChecked()){
switchStatus.setText("Switch is currently ON");
}
else {
switchStatus.setText("Switch is currently OFF");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

Android ImageButton - How to change the image when button is clicked?

If the button was clicked I want to turn on the wifi and also change the Image of ImageButton, but I do not know how to change it
I have also tried this from how to change the image of a button with every click? but is isn't working:
boolean isPressed=false
button.setOnClickListener(buttonListener);
OnClickListener buttonListener= new OnClickListener() {
#Override
public void onClick(View v) {
if(isPressed){
button.setBackgroundResource(R.drawable.icon1);
}else{
button.setBackgroundResource(R.drawable.icon2);
}
isPressed=!isPressed;
}};
when I write the code above, android studio shows this:
Cannot resolve symbol setOnClickListener
I have also created a button_wifi_selector xml, which it looks like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<item android:drawable="#drawable/wifi_on"
android:state_pressed="true" />
<item android:drawable="#drawable/ic_launcher"
android:state_focused="true" />
<item android:drawable="#drawable/wifi" />
</selector>
and in my activity i have this
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton_wifi"
android:layout_below="#+id/toggleButton_wifi"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:onClick="turnOffWifiDemo"
android:src="#drawable/button_wifi_selector" />
but it isn't doing, what I want
Can somebody pls help me?
Thanks
EDIT: it works with the first code. I just had to remove the onClick from ImageButton in the xml
BUT: he is changing the picture the second time, when I start the app. After that he changes it every time
this code work for me; test it
final ImageButton btnTest =(ImageButton) findViewById(R.id.btnexctract);
btnTest.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
btnTest.setSelected(!btnextra.isPressed());
if (btnTest.isPressed()) {
btnextra.setImageResource(R.drawable.yourImage);
}
else {
btnTest.setImageResource(R.drawable.yourImage2);
}
}
});
This is how you have to do it
Selector xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_selected="true"
android:drawable="#drawable/info_icon_solid_with_shadow" />
<item
android:drawable="#drawable/info_icon_outline_with_shadow" />
</selector>
And then in java:
//assign the image in code (or you can do this in your layout xml)
imageButton.setImageDrawable(getBaseContext().getResources().getDrawable(R.drawable....));
//set the click listener
imageButton.setOnClickListener(new OnClickListener() {
public void onClick(View button) {
//Set the button's appearance
button.setSelected(!button.isSelected());
if (button.isSelected()) {
//Handle selected state change
}
else {
//Handle de-select state change
}
}
});
I have modified it,for on/off it may help you
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageButton
android:id="#+id/imageButton_wifi2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickEvent"
android:visibility="invisible"
android:src="#drawable/on" />
<ImageButton
android:id="#+id/imageButton_wifi1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="clickEvent"
android:src="#drawable/off" />
</FrameLayout>
like you need to create method with view parameter
{
c=false;
im1=(ImageButton)findViewById(R.id.imageButton_wifi1);
im2=(ImageButton)findViewById(R.id.imageButton_wifi2);
}
public void clickEvent(View v)
{
//Code to implement
if(c) {
im2.setVisibility(View.VISIBLE);
im1.setVisibility(View.INVISIBLE);
c=false;
}
else {
im1.setVisibility(View.VISIBLE);
im2.setVisibility(View.INVISIBLE);
c=true;
}
You can use togglebutton like this
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="74dp"
android:layout_height="26dp"
android:background="#drawable/toggle"
android:checked="false"
android:paddingRight="10dp"
android:text="ON"
android:textColor="#ffffff"
android:textSize="13sp"
android:textStyle="bold" />
And in java file
final ToggleButton toggleButton=(ToggleButton)findViewById(R.id.toggleButton1);
toggleButton.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
//
toggleButton.setBackgroundResource(R.drawable.icon1);
}else{
//
toggleButton.setBackgroundResource(R.drawable.icon2);
}
}
});
You can define a boolean flag and just change visibility of items.
Description: changeBack is a button. This code is for change images
Your onClick does not know which id is clicked?
private boolean bgcolor = false;
public void onClick(View view) {
switch(view.getId())
{
case R.id.changeBack:
bgcolor = !bgcolor;
if (bgcolor) {
imv.setVisibility(View.VISIBLE);
imv2.setVisibility(View.INVISIBLE);
}
else {
imv2.setVisibility(View.VISIBLE);
imv.setVisibility(View.INVISIBLE);
}
break;
}
}

How to do something when a checkbox change state?

This is my code:
<CheckBox
android:id="#+id/sprint_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sprint_game" />
<CheckBox
android:id="#+id/marathon_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/marathon" />
<CheckBox
android:id="#+id/never_ending_checkbox"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/never_ending" />
What i want to do is "detect" when one of these is checked and then set the other two to "disable", so the user can select only one at time.
I tried to use the ".setOnCheckedChangeListener", but i can't do that, can someone help me with some code?
Thanks a lot guys!
This is the way you are notified about checked changes:
CheckBox check = findViewById(R.id.sprint_checkbox);
check.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
//do stuff
}
});
You can also let your activity implements the OnCheckedChangeListener interface and then:
CheckBox check1 = findViewById(R.id.sprint_checkbox);
CheckBox check2 = findViewById(R.id.marathon_checkbox);
CheckBox check3 = findViewById(R.id.never_ending_checkbox);
check1.setOnCheckedChangeListener(this);
check2.setOnCheckedChangeListener(this);
check3.setOnCheckedChangeListener(this);
Overriding the interface method:
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch(buttonView.getId()){
case R.id.sprint_checkbox:
//do stuff
break;
case R.id.marathon_checkbox:
//do stuff
break;
case R.id.never_ending_checkbox:
//do stuff
break;
}
}
I believe a RadioButton would be more suitable for your aims.
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/radio_group1">
<RadioButton
android:id="#+id/sprint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rad_option1"
android:checked="true"
android:onClick="onRadioButtonClick"
/>
<RadioButton
android:id="#+id/marathon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rad_option2"
android:onClick="onRadioButtonClick"
/>
<RadioButton
android:id="#+id/neverending"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rad_option3"
android:onClick="onRadioButtonClick"
/>
</RadioGroup>
Then in your code:
public void onRadioButtonClick(View v) {
RadioButton button = (RadioButton) v;
// DO YOUR THING HERE DEPENDING ON WHICH RADIOBUTTON IS PICKED
}
In this case, you won't have to deal with disabling other options. The user will have only one option to pick by default.
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
if(isChecked){
switch(id){
case R.id.sprint:
//add to database
...
}
}
else{
switch(id){
case R.id.sprint:
//remove from database
...
}
}
}
This should allow you to with ID's. Hope it works.

Categories

Resources