Progress bar Visibility - android

I have a very simple scenario: I have to make my progress bar invisible at the starting, but on the button click, have to make it visible so that the tasks which I am running in the background will be done and till then my progress bar will run.
I am using a very simple way. I have put the progress bar in XML, then simply at the onCreate method of the activity, first making it invisible by mProgress.setVisibility(4) and then when I am clicking my button trying to make this visible again.
But unfortunately its not working! Anyone please reply why its not doing this.
mProgress = (ProgressBar) findViewById(R.id.progressBar);
mProgress.setVisibility(4);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
strpatientid = txtpatientid.getText().toString();
if (strpatientid.length() == 0) {
Toast.makeText(getApplicationContext(),
"Enter the Patient ID",
Toast.LENGTH_LONG).show();
return;
}
else {
mProgress.setEnabled(false);
mProgress.setVisibility(View.VISIBLE);
setProgressBarVisibility(true);
}
}

try it like this:
mProgress=(ProgressBar) findViewById(R.id.progressBar);
mProgress.setVisibility(View.INVISIBLE);
btnSubmit.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
strpatientid = txtpatientid.getText().toString();
if (TextUtils.isEmpty(strpatientid)) {
Toast.makeText(getApplicationContext(),"Enter the Patient ID",
Toast.LENGTH_LONG).show();
mProgress.setVisibility(View.INVISIBLE);
return;
}
else{
//mProgress.setEnabled(false); //you dont need this
mProgress.setVisibility(View.VISIBLE);
setProgressBarVisibility(true);
}
}

Related

Android Animation not work when i call visibility

The animation does not work after I set visibility to Invisible, I tried clear animation but not work. I have a button when I click the button it opens a linear layout with animation when I press back button I set the linear layout visibility to invisible again I click the button linear layout appear but no animation please help me.
l1 = (LinearLayout) findViewById(R.id.lnrlgn);
l2 = (LinearLayout) findViewById(R.id.lnrlgn1);
l2.setVisibility(View.INVISIBLE);
Animation uptodown = AnimationUtils.loadAnimation(this,R.anim.uptodown);
viewcrrd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
l2.setAnimation(downtoup);
l2.clearanimation(); // is it right ?
l2.setVisibility(View.VISIBLE);
}
});
public void onBackPressed() {
// super.onBackPressed();
if (back_pressed + TIME_DELAY > System.currentTimeMillis()) {
// super.onBackPressed();
Exitdlg alert = new Exitdlg();
alert.showDialog(LoginActivity.this, "Are You Sure ");
l2.clearAnimation();
} else {
l2.clearAnimation();
l2.setVisibility(View.INVISIBLE);
}
back_pressed = System.currentTimeMillis();
}
Use startAnimation instead of setAnimation
viewcrrd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
l2.setVisibility(View.VISIBLE);
l2.clearanimation();
l2.startAnimation(downtoup);
}
});

Disable click after clicking the textview

I have a TextView and I put a OnClickListener on this TextView. I use this action to load custom view onto a LinearLayout.
But when I click on this TextView twice, custom view is repeating on the LinearLayout. I clear all custom views on this LinearLayout before I load new custom views on to this LinearLaout.
This is my OnClickListener on TextView,
TextView rejectedTitleTextView = (TextView) findViewById(R.id.roster_menu_rejected_title);
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
rejectedTitleTextView.setBackgroundColor(getResources().getColor(R.color.acceptedPurpleColour));
newTitleTextView.setBackgroundColor(getResources().getColor(R.color.defaultBlack));
acceptedTitleTextView.setBackgroundColor(getResources().getColor(R.color.defaultBlack));
locationLinearLayout.removeAllViews();
rosterBottomLayout.setVisibility(View.GONE);
Log.d("CHECK_ACTION"," REJECTED_TEXT_VIEW ");
InternetConnectivity internetConnectivity = new InternetConnectivity();
final boolean isConnectedToInternet = internetConnectivity.isConnectedToInternet(context);
if(isConnectedToInternet==true) {
try {
Thread.sleep(1300);
} catch (Exception e) {
e.printStackTrace();
}
getDataFromServer("REJECTED");
}else{
Snackbar.make(mainView, "No Internet Connection", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
}
});
getDataFromServer("REJECTED");
is the method which I used to load custom view onto this LinearLayout.
How can I prevent this issue ?
Have any ideas ?
Inside onclickListener put
rejectedTitleTextView.setClickable(false);
and once finish your functionality make it as true because u need to click for next time .
rejectedTitleTextView.setClickable(true);
Inside setOnclickListener try below code:-
textView.setClickable(false);
Try this
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mSpinner.setEnabled(false);
mSpinner.postDelayed(new Runnable() { #Override public
void run() {
mSpinner.setEnabled(true); }
}
// do your stuff here
});
You can maintain boolean value like this
boolean isClick=false;
rejectedTitleTextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!isClick)
{
//do your Stuff on onCLick
isClick=true;
}else
{
//leave it blank if you do not want to do anything second time
}
}
});

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

Android: TextView visibility changes

I have started with android programming and I am trying to do:
Let the user enter their login credential.
If the user login credential is valid let them login.
If login credential is invalid then give them an error message saying that the user name or password entered is invalid.
For the last part, the problem is that I have a TextView with default hidden visibility. I want to make it visible for a few seconds and make it disappear.
This post and this post have helped but they are essentially trying to do the opposite.
My code is given below:
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Check the validity of the user name.
if (valid) {
// Let the user log in.
} else {
// Make TextView visible for a few seconds.
}
}
});
How do I go about this?
In your case first you need to show TextView then make it INVISIBLE .
Use textView.postDelayed method as:
if(valid){ //let the user login }
else
{
// make TextView visible here
textView.setVisibility(View.VISIBLE);
//use postDelayed to hide TextView
textView.postDelayed(new Runnable() {
public void run() {
textView.setVisibility(View.INVISIBLE);
}
}, 3000);
//how to make the textview visible for a few seconds
}
To show any information or error message you can use small pop-up message for some time. Following the code and image for toast.
//display in short period of time
Toast.makeText(getApplicationContext(), "msg msg", Toast.LENGTH_SHORT).show();
You can use toast.
Context context = getApplicationContext();
CharSequence text = "Hello toast!";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Toasts in android
try this
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
mTextView.setVisibility(View.VISIBLE);
v.postDelayed(new Runnable() {
#Override
public void run() {
mTextView.setVisibility(View.GONE);
}
}, 2000);
}
});
you can achieve this with the help of timertask,as your textview is invisible first make it visible and then with the help of timertask make it invisible again
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//check the validity of the user name
if(valid)
{
//let the user login
}
else
{
txt.setVisibility(View.VISIBLE);
Timer t = new Timer(false);
t.schedule(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
txt.setVisibility(View.INVISIBLE);
}
});
}
}, 5000);
}
});
but i would suggest if you want user confirmation show error in alertdialog or a toast to show error would be fine
You can do this by using handler.
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//check the validity of the user name
if(valid)
{
//let the user login
}
else
{
//how to make the textview visible for a few seconds
new Handler().postDelayed(new Runnable()
{
#Override
public void run()
{
mTextView.setVisibility(View.VISIBLE);
}
}, 1000/* 1sec delay */);
}
});

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.

Categories

Resources