Toggle Button Not Working Properly on Bluetooth Adapter - android

Hi Sir/Ma'am,
I'm trying to Enable/Disable Bluetooth on Toggle Button and also there is an additional functionality which ask for password on enable/disable. Toggle Button is taking default state of Bluetooth at launch(Means if Bluetooth is Enable, then toggle will be set on and if Bluetooth is Disable, toggle will be off).
Also, its working fine on when we enter correct password. But, the main problem is that When I enter wrong password, Toggle is not behave as expected.
What I mean, Suppose Bluetooth is On and I am trying to disable it by my app. When I click on Toggle, it ask me to enter password. Now, If i enter wrong password, Bluetooth doesn't turn off, but toggle button changed its state to turn Off.
Here is What I tried so far:-
setContentView(R.layout.activity_main);
mb = BluetoothAdapter.getDefaultAdapter();
tb1 = (ToggleButton)findViewById(R.id.appToggleBtn);
tb1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Check();
}
});
}
public void Check()
{
final Dialog dialog = new Dialog(MainActivity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
dialog.setCancelable(true);
dialog.show();
ImageView saveBtn=(ImageView)dialog.findViewById(R.id.confirmdialogBtn);
final EditText password=(EditText)dialog.findViewById(R.id.confirmdialogEditText);
saveBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
if(password.getText().toString().equals("asd"))
{
if(mb.isEnabled())
{
mb.disable();
tb1.setChecked(mb.isEnabled());
}
else
{
mb.enable();
tb1.setChecked(mb.isEnabled());
}
}
else
{
Toast.makeText(getApplicationContext(), "Wrong", 1000).show();
if(mb.isEnabled())
{
tb1.setChecked(mb.isEnabled());
}
else
{
tb1.setChecked(mb.isEnabled());
}
}
dialog.dismiss();
}
});
}
Also, for Toggle Button, I used:-
android:background="#drawable/toggle_selector"
and in toggle_selector.xml
<item android:drawable="#drawable/toggle_on" android:state_checked="true"/>
<item android:drawable="#drawable/toggle_off" android:state_checked="false"/>
Please Please Help me into this. I am badly stuck at this point.
Thanks in advance.

try this.
if(password.getText().toString().equals("asd"))
{
if(mb.isEnabled())
{
mb.disable();
tb1.setChecked(mb.isEnabled());
}
else
{
mb.enable();
tb1.setChecked(mb.isEnabled());
}
}
else
{
Toast.makeText(getApplicationContext(), "Wrong", 1000).show();
}

Related

android toggle button state always true

Been trying to use a ToggleButton to act as a bookmark kind of thing in my application. I am using this for the first time. I have declared my toggle button under onCreateView() as below:
bmark = (ToggleButton) v.findViewById(R.id.bmark);
bmark.setChecked(false);
I am trying to just toggle the state and show a Toast message! I tried the below:
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
bmark.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean status;
if (bmark.isChecked()) status = true;
else status = false;
Log.w("Bmark status",String.valueOf(status));
if (status) {
bmark.setChecked(false);
Log.w("Bmark after true",String.valueOf(bmark.isChecked()));
Toast.makeText(getActivity(), "Bookmark removed!", Toast.LENGTH_SHORT).show();
} else {
bmark.setChecked(true);
Log.w("Bmark after false",String.valueOf(bmark.isChecked()));
Toast.makeText(getActivity(), "Post Bookmarked..!", Toast.LENGTH_SHORT).show();
}
}
});
Every time I press the button, the status is initially read "true", although I've set it to "false". After I call setChecked(false), it also becomes false. But when I click it again, it again reads "true" and instead of "false"
I dont know why its happening like this. I just want to toggle it every time I click it. Pls help me out! Thanks in advance :)
Change code to:
if (bmark.isChecked()){
status = true;
Toast.makeText(getActivity(), "Post Bookmarked..!",Toast.LENGTH_SHORT).show();
}
else {
status = false;
Toast.makeText(getActivity(), "Bookmark removed!", Toast.LENGTH_SHORT).show();
}
Toggle changes self checked status, You done that again so status was changed two times.
The problem is you're inverting the state of your button by the setChecked() calls in onClick().
Use an OnCheckedChangeListener instead of an OnClickListener, so you don't have to bother with keeping track of the status:
bmark.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
// checked
Toast.makeText(getActivity(), "Post Bookmarked!", Toast.LENGTH_SHORT).show();
} else {
// not checked
Toast.makeText(getActivity(), "Bookmark removed!", Toast.LENGTH_SHORT).show();
}
}
});
You seem to be switching the button back to what it was previously in your statements. If you stop changing the button's state in the onClickListener, it should work just fine.
private boolean bmarkStatus = false;
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
bmark.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
bmarkStatus = bmark.isChecked();
if (bmark.isChecked()) Toast.makeText(getActivity(), "Bookmark removed!", Toast.LENGTH_SHORT).show();
else Toast.makeText(getActivity(), "Bookmark added!", Toast.LENGTH_SHORT).show();
}
});
}

Android code for checking visibility of edit text

Hi i have a small problem. I have spinner when i select "yes" edit text 1 and edit text 2 will display when i select "NO" edit text will disappear its working fine for me. But when i press on button for validating the edit text logcat as "AUDIO_OUTPUT_FLAG_FAST denied by client". this is the code.
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(edittext1.getText().toString().length()==0)
{
Toast.makeText(getApplicationContext(), "Please Enter Key NO(FX)", Toast.LENGTH_SHORT).show();
}
else if(sfpchangeddata.contains("SFP Changed"))
{
Toast.makeText(getApplicationContext(), "Please Select SFP changed or Not", Toast.LENGTH_SHORT).show();
}
else if(edittext2.getVisibility()==View.VISIBLE)
{
if(edittext2.getText().toString().length()!=0)
{
}
else
{
Toast.makeText(getApplicationContext(), "Please Enter Siga SFP serial No", Toast.LENGTH_SHORT).show();
}
}
else if(edittext3.getVisibility()==View.VISIBLE)
{
if(edittext3.getText().toString().length()!=0)
{
}
else
{
Toast.makeText(getApplicationContext(), "Please Enter Old Siga SFP serial No", Toast.LENGTH_SHORT).show();
}
}
}
});
isShown() method returns boolean value, so you can use this in your if loop.
if(edittext.isShown())
{
//Set the code here if the edittext is visible.
}
else
{
//Here the code which will run if ediitext is invisible.
}
Hope this will help you.
You can check the visibility for an editText by using isShown() on your ediText

Android dialog dismisses on second click only. Why?

Why doesn't dialog dismiss on the first click (but shows Toast) ?
On the second click it dismisses (Toast is shown again).
private void networkDialog(){
final Dialog dialog = new Dialog(EnterActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.custom_dialog);
Button nobutton = (Button) dialog.findViewById(R.id.dialogButLeft);
nobutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), "DIALOG", Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
Try this way .Let me inform .I hope it will help you.
private void networkDialog()
{
final Dialog dialog = new Dialog(EnterActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.custom_dialog);
Button nobutton = (Button) dialog.findViewById(R.id.dialogButLeft);
nobutton.setOnClickListener(this);
dialog.show();
}
Then Use onClick switch Statement
public void onClick(View view)
{
switch (view.getId())
{
case R.id.dialogButLeft:
Toast.makeText(getApplicationContext(), "DIALOG", Toast.LENGTH_LONG).show();
dialog.dismiss();
break;
}
}
A bit late but a colleague had the same issue and referred to this, are you absolutely sure that you're not creating two dialogs by calling networkDialog() twice?
Add some unique text to the dialog that will be visible to you when it's displayed like System.currentTimeMillis(), that way you can see if it's called twice because the text is different.
Or add logging / run in debug
Make your Button also final like this:
private void networkDialog(){
final Dialog dialog = new Dialog(EnterActivity.this, android.R.style.Theme_Translucent_NoTitleBar);
dialog.setContentView(R.layout.custom_dialog);
final Button nobutton = (Button) dialog.findViewById(R.id.dialogButLeft);
nobutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
Toast.makeText(getApplicationContext(), "DIALOG", Toast.LENGTH_LONG).show();
}
});
dialog.show();
}
it's working for me in my app like this:
// Initialize variables
final Dialog passwordDialog = new Dialog(BPMActivity.this,R.style.CustomDialogStyle);
passwordDialog.setContentView(R.layout.password_view);
final Button btnCancel=(Button) passwordDialog.findViewById(R.id.btn_cancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
passwordDialog.dismiss();
}
});
passwordDialog.show();
I don't know if this is still relevant to the OP. But I've been banging my head against the wall for quite some time trying to figure this one out. It appears to happen in later (5-6+) Android versions and did not occur on a 4.4.2 device which I have. The solution I've found is to setFocusableInTouchMode of the Button to false:
button.setFocusableInTouchMode(false)
This answer gave me the idea:
I have to click the button twice for it to work

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
}
}

Focus removed while typing in edit text

I have an EditText
serialText = (EditText) findViewById(R.id.pinText);
Now onclick of somebutton, I should get the focus to edit text to write something. But when I click inside of edit text to write, focus is getting removed, i.e., whatever i type does not appear in the edit text
This is the code to do that,
public void method(View v) {
arrowButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
serialNumber = serialText.getText().toString();
if (serialNumber.equals("")) {
} else {
}
} }...
Here if I don't type anything it comes to if loop without any problem, but samething is not happening for else part.
Can you please help me?
This is my full code
public void onClick(View v) {
if (v.getId() == R.id.pinBtn) {
arrowButton = (Button) findViewById(R.id.arrowBtn);
serialText = (EditText) findViewById(R.id.pinText);
serialText.setVisibility(View.VISIBLE);
arrowButton.setVisibility(View.VISIBLE);
//serialNumber = serialText.getText().toString();
arrowButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
serialNumber = serialText.getText().toString();
if (serialNumber.equals("")) {
Toast.makeText(getApplicationContext(),
"Please enter the serial number",
Toast.LENGTH_SHORT).show();
}
else {
isRegularSerialNumber(serialNumber);
String encodedserialNumber = Base64.encodeToString(
serialNumber.getBytes(), Base64.DEFAULT);
receiveSerialData(encodedserialNumber);
if (serialResponse.equals("Validated Successfully!")) {
showAudio(v);
} else {
Toast.makeText(getApplicationContext(),
"Invalid Serial Number", Toast.LENGTH_SHORT)
.show();
}
}
}
});
}
}
Thanks
I solved the problem finally, the problem was when we keep an edit text in the listview and when try to write inside that, keyboard pushes the listview up. So we need to add the property android:windowSoftInputMode="adjustPan" to the listview or to the activity containing the list view in the manifest file.
Instead of using serialNumber.equals(""), try replacing it with:
serialNumber.length() == 0
or
serialNumber.matches("")
The code you posted is onClick() of arrowButton, you should post onClick() of serialText

Categories

Resources