Android code for checking visibility of edit text - android

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

Related

Toast when fields are Empty in Application [duplicate]

This question already has answers here:
How do I check if my EditText fields are empty? [closed]
(30 answers)
Closed 4 years ago.
I want to display toast when fields are empty.
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);
}
});
Try this :
When the fields are empty, apply this check :-
if (edittext1.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(),"Edit text 1 can not be empty",Toast.LENGTH_LONG).show();
}
else if (edittext2.getText().toString().isEmpty()) {
Toast.makeText(getApplicationContext(),"Edit text 2 can not be empty",Toast.LENGTH_LONG).show();
}
else{
//success code here
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);
}
Before doing that
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener)
Try to check if data is empty as follow
if (edittext1.getText().toString().isEmpty() || edittext2.getText().toString().isEmpty()) {
Toast.makeText(this,"fill the required field",Toast.LENGTH_LONG).show();
} else {
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener)
}
Instead of toast, "setError" to edit texts. It would be better
editText.setError("This field can not be blank");
Try this,
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(edittext1.getText().toString().length()<=0){
Toast.makeText(this, "email is empty", Toast.LENGTH_SHORT).show();
}else if (edittext2.getText().toString().length()<=0){
Toast.makeText(this, "password is empty", Toast.LENGTH_SHORT).show();
}else{
aut.signInWithEmailAndPassword(edittext1.getText().toString(), edittext2.getText().toString()).addOnCompleteListener(MainActivity.this, _aut_sign_in_listener);
}
}
});

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();
}
});
}

Toggle Button Not Working Properly on Bluetooth Adapter

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();
}

How to prevent custom dialog from popping twice in asynctask onPostExecute?

I'm using a custom dialog in onpostexecute method in AsyncTask, it is being popped twice. When the user clicks on a button the dialog has to be closed, this seems to work fine.
Can someone shed some light on why it is being called twice ?
#Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
if (pDialog != null) {
pDialog.dismiss();
}
try {
if (responseFromServer.contains("x")) {
// Pop up to create password
final Dialog dialog = new Dialog(getActivity());
dialog.setContentView(R.layout.dialog_password);
dialog.setTitle("Title...");
dialog.setCancelable(false);
final TextView etpassword = (TextView) dialog.findViewById(R.id.etpassword_dialog);
final Button btnpassword = (Button) dialog
.findViewById(R.id.btnsavepassword_dialog);
btnpassword.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (etpassword.getText().toString().length() == 0) {
Toast.makeText(getActivity(), "Enter password", Toast.LENGTH_SHORT)
.show();
} else if (etpassword.getText().toString().length() < 6) {
Toast.makeText(getActivity(),
"Password should contain minimmum 6 characters",
Toast.LENGTH_SHORT).show();
} else {
dialog.dismiss();
}
}
});
if (!dialog.isShowing()) {
dialog.show();
}
}
else {
Toast.makeText(getActivity(), "Unexpected error occurred. Please try again",
Toast.LENGTH_SHORT).show();
}
} catch (Exception e) {
Log.v("Main FRagment FB async::::::", e.getMessage());
}
}
You can write following condition before display a custom dialog,
if ( !dialog.isShowing() )
{
dialog.show();
}
your code is correct there is no problem in code. Check button click. i think you are calling twice execute() method of AsyncTask.
Can you post the calling code like how are you calling AsyncTask fron button click.

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