checkbox not giving right state - java

I have one checkbox in my app which performes enabling\disabling bluetooth .. the problem is if I turn on\off from android setting(outside this app) then that checkbox is not updating like say if in app checkbox is checked means bluetooth is enable so now if I disable bluetooth from toggle or from system->settings and then go to my app then that checkbox is still checked ... how to get right state for that box ..(using fragment)
final BluetoothAdapter myBTadapter;
final Integer REQ_BT_ENABLE=1;
CheckBox enable;
myBTadapter=BluetoothAdapter.getDefaultAdapter();
enable=(CheckBox)vv.findViewById(R.id.cboxEnable);
enable.setOnCheckedChangeListener(new OnCheckedChangeListener(){
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked){
if(buttonView.isChecked()){
if(myBTadapter==null){
Toast.makeText(getActivity().getApplicationContext(),
"Device doesn't support Bluetooth",
Toast.LENGTH_LONG).show();
}
else{
if(!myBTadapter.isEnabled()){
Intent enableBtIntent = new Intent(BluetoothAdapter.);
startActivity(enableBtIntent);
}
}
}
else{
Toast.makeText(getActivity().getApplicationContext(),
"Disabling Bluetooth!!", Toast.LENGTH_LONG).show();
myBTadapter.disable();
}
}
});

Try this inside onResume() method:
if(myBTadapter.isEnabled()) {
enable.setChecked(true);
} else {
enable.setChecked(false);
}

Related

"Toast" is showing error. i run the code without it and it worked just fine but i need it in order to see my button working or not

I am working in a Bluetooth chat app and I just finished the interface in order to test my buttons I need to use Toast and it is showing error. I run the code without it and it runs fine
#Override
public boolean onOptionsItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()){
case R.id.menu_search_devices:
Toast.makeText(context, "clicked searched devices", Toast.LENGTH_SHORT).show();
return true;
case R.id.menu_enable_bluetooth:
Toast.makeText(context, "clicked Enable Bluetooth", Toast.LENGTH_SHORT).show();**here the problem**
return true;
default:
return super.onOptionsItemSelected(item);
}

Event of multiple checkboxes using onCheckedChanged

I have a problem with login checkboxes
First, I`m going to make 3 checkboxes and created change listener for each
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private CheckBox main_id_saver;
private CheckBox main_pw_saver;
private CheckBox main_auto_login;
and allocate ID of wigets for each too in onCreate(xml file is skipped)
main_id_saver = (CheckBox) findViewById(R.id.main_id_saver);
main_pw_saver = (CheckBox) findViewById(R.id.main_pw_saver);
main_auto_login = (CheckBox) findViewById(R.id.main_auto_login);
main_id_saver.setOnCheckedChangeListener(this);
main_pw_saver.setOnCheckedChangeListener(this);
main_auto_login.setOnCheckedChangeListener(this);
and I want to set a click event for checkboxes which satisfies below
If ID, PW checkboxes is checked, then auto login checkboxes are checked automatically
cannot check PW checkbox only
If auto login is checked, then ID, PW are checked automatically
If any checkbox is clicked in status of all checkboxes are checked, then all checkboxes are unchecked
I made some tries and below is that
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (main_auto_login.isChecked()){
if(!main_id_saver.isChecked() || !main_pw_saver.isChecked()) {
main_id_saver.setChecked(true);
main_pw_saver.setChecked(true);
return;
} else if (main_id_saver.isChecked()&&main_pw_saver.isChecked()&&main_auto_login.isChecked()){
main_auto_login.setChecked(false);
main_id_saver.setChecked(false);
main_pw_saver.setChecked(false);
return;
}
} else if(main_pw_saver.isChecked()){
if(main_id_saver.isChecked()){
main_auto_login.setChecked(true);
return;
} else if(!main_id_saver.isChecked()){
Toast.makeText(MainActivity.this, "CANNOT REMEMBER PASSWORD BUT FOR ID",Toast.LENGTH_SHORT).show();
main_pw_saver.setChecked(false);
return;
}
}
return;
}
but it doesn`t work as I have thought
what is the best code for this problem?
In my arrogant opinion, making onChangedLinster event separately is the best answer but I cannot make it...
chk.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.checkbox1:
if(isChecked)
//Do something
else
//Do something
break;
case R.id.checkBox2:
if(isChecked)
//Do something
else
//Do something
break;
}
}
});

The audio volume is zero?

I have an application with a mute button. I have been stuck on this for 4 and a half hours now, so I am plain desperate. I am trying to have the volume mute when user clicks mute and unmute when user clicks it again. This works fine, except the audio isn't playing. For some reason, Stream_Music is 0?
public void mute(View view) {
mutebutton = (ImageButton) findViewById(R.id.mutebutton);
if ((variableForMute.x % 2) != 0) { //If it's odd UNMUTE
Log.v(TAG, "Use this volume to unmute " +userVolumeOnStart.userVolume +"");
Toast.makeText(Main_Menu.this, "UNMUTED", Toast.LENGTH_SHORT).show();
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, userVolumeOnStart.userVolume, 0);
variableForMute.x++;
} else { //If its even MUTE
userVolumeOnStart.userVolume = mAudioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
Log.v(TAG, "Right Before Mute " +userVolumeOnStart.userVolume +"");
mAudioManager.setStreamVolume(AudioManager.STREAM_MUSIC, 0, 0);
Toast.makeText(Main_Menu.this, "MUTED", Toast.LENGTH_SHORT).show();
variableForMute.x++;
}
}
Here is the beginning where I declare the variables and objects:
ImageButton leftcenter;
ImageButton mutebutton;
final String TAG = "userVolume";
private AudioManager mAudioManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main__menu);
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
leftcenter = (ImageButton) findViewById(R.id.startGame);
}
The thing is, as log outputs, I am getting the following:
I should have the volume change to what my volume was before I started my app. I want to be able to have the user unmute the app to the same volume that the user was originally on. But for some reason it is always 0!? Why is this happening? My phone volume is at full?!
Please help,
I have spent way too much time on this-I appreciate all feedback:
positive, negative, neutral, whatever.
I found the issue. I have changed STRAM_MUSIC to STREAM_RING, and the variable userVolumeOnStart.userVolume changes successfully!
btn_mute.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
// TODO Auto-generated method stub
AudioManager audio = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
if(isChecked) {
btn_mute.setBackgroundResource(R.drawable.mute_bt);
audio.setStreamMute(AudioManager.STREAM_MUSIC, true);
} else {
btn_mute.setBackgroundResource(R.drawable.volume_bt);
audio.setStreamMute(AudioManager.STREAM_MUSIC, false);
}
}
});

Android saving state of checkbox if exit app

I have 5 checkboxs and when they are clicked they set a boolean in an abstract class to true or false using a onCheckedChangeListener. Example:
checkbox.setOnCheckedChangeListener(new OnCheckedChangeListener(){
#Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked)
{
if(isChecked)
{
CheckboxData.checked = true;
}
else if(!isChecked)
{
CheckboxData.checked = false;
}
}
});
However when I leave the app and return the checkboxes are not clicked anymore, yet the boolean values are still true. How can I make the app remember that the check boxes are clicked.? Should I just check the boolean values in the main activity onCreate and set the checkboxs to checked or not then or is there a better/faster way to make the app remember the checkboxs states?
Just added this in the main activities onCreate method:
if(CheckboxData.checked)
{
CheckBox checkbox = (CheckBox) findViewById(R.id.checkbox);
checkbox.setChecked(true);
}
But it does nothing
when Activity get destroyed check the checkbox state if true , and add the checkbox state in shared Preference
#Override
public void onDestroy() {
if(CheckboxData.checked){
SharedPreferences prefs = getSharedPreferences("private preference", Context.MODE_PRIVATE);
prefs.edit()
.putBoolean("CheckboxData",true)
.commit();
}
super. onDestroy();
}
And when Activity is launched back, In onCreate check for the checkbox state in preference and if true enable the checkbox
SharedPreferences prefs = getSharedPreferences("private preference", Context.MODE_PRIVATE);
boolean isChecked = prefs.getBoolean("CheckboxData",false);
if(isChecked)
checkBoxView.setChecked(true)

How do I disable the Home button & Back button?

I want way to disable the Home Button & Back Button when a checkbox is checked in my application. I am on version 4.2.2. Here is my code, it does not work, the application stops when the box gets checked:
public void HardButtonOnClick(View v)
{
boolean checked1 = ((CheckBox) v).isChecked();
if(checked1)
{
SQLiteDatabase db;
db = openOrCreateDatabase("Saftey.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
ContentValues values = new ContentValues();
values.put("hardBtn", "YES");
db.update("Setting", values, "id = ?", new String[] { "1" });
Toast.makeText(this, "Hard Button Locked", Toast.LENGTH_LONG).show();
//SharedPreferences pref = getSharedPreferences("pref",0);
//SharedPreferences.Editor edit = pref.edit();
//edit.putString("hard","yes");
//edit.commit();
/* String Lock="yes" ;
Bundle bundle = new Bundle();
bundle.putString("key", Lock);
Intent a = new Intent(Change_setting.this, ChildMode.class);
a.putExtras(bundle);
startActivity(a);*/
super.onAttachedToWindow();
this.getWindow().setType(WindowManager.LayoutParams.TYPE_KEYGUARD);
isLock = true;
}
else
{
SQLiteDatabase db;
db = openOrCreateDatabase("Saftey.db", SQLiteDatabase.CREATE_IF_NECESSARY, null);
db.setVersion(1);
db.setLocale(Locale.getDefault());
db.setLockingEnabled(true);
ContentValues values = new ContentValues();
values.put("hardBtn", "NO");
db.update("Setting", values, "id = ?", new String[] { "1" });
//SharedPreferences pref = getSharedPreferences("pref",0);
//SharedPreferences.Editor edit = pref.edit();
//edit.putString("hard","no");
//edit.commit();
Toast.makeText(this, "Hard Button Un-Locked", Toast.LENGTH_LONG).show();
isLock = false;
}
}
How can I make this work? I do not want to hide the buttons, I only want them to not respond to clicks when the checkbox is checked.
You can override the back key onBackPressed() and let it do nothing, but you can't override the home button but you can add some code to onStop() which is called upon home key press.
here is a piece of code to control backpress
#Override
public void onBackPressed()
{
if ( checked1 ) // if checked do nothing , remeber tomove checked1 to be a class member
return;
super.onBackPressed();
}
#Override
public void onBackPressed() {
}
With this you override back button. Just do nothing in it.
This you can use if you do something special when u press back:
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
int vfBackId = viewFlipper.getDisplayedChild();
if ((keyCode == KeyEvent.KEYCODE_BACK)) {
// Do some stuff
}
return false;
}
But you cannot override the home button. It's not provided from the Android system to handle the home button.
you cant disable home button in your application but you can disable back button with override
onBackPressed() and do nothing in that
You cannot disable home button.
There is no way to intercept the home button on Android, unless you make your app the home screen. This is for security reasons, so that malicious apps cannot take over your device by overriding all the buttons that can exit.
Home button is one sure short way to navigate to home screen.
If you want to handle the HOME button, implement a home screen.
Not able disable Home button on specific android devices
//Back key event detect
#Override
public boolean onKeyDown(int keyCode, KeyEvent event)
{
// Quit if back is pressed
if (keyCode == KeyEvent.KEYCODE_BACK)
{
moveTaskToBack(true);
return true;
}
return super.onKeyDown(keyCode, event);
}

Categories

Resources