How to execute two if conditions in android - android

I want to execute two if conditions either first if condition or second if condition based on my requirement.
Now below code if it executing second if condition everytime
switch (specilaity_name) {
case "Cardiac":
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_heart);
specialization.setTextColor(Color.WHITE);
break;
case "Urology":
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_urology__1___1_);
specialization.setTextColor(Color.WHITE);
break;
case "Gynaecology":
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_group_2105);
specialization.setTextColor(Color.WHITE);
break;
}

Well. You're using nested if else in the else part of outer if for each block of code.
If only code in second structure is working, then that's the only condition which holds true during execution of your App.

if(specilaity_name.equalsIgnoreCase("Cardiac")){
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_heart);
specialization.setTextColor(Color.WHITE);
}else if(specilaity_name.equalsIgnoreCase("Urology")) {
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_white);
speclialistimage.setImageResource(R.drawable.ic_urology__1_);
specialization.setTextColor(Color.rgb(73,179,206));
}else {
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_white);
speclialistimage.setImageResource(R.drawable.ic_group_2105);
specialization.setTextColor(Color.rgb(73, 179, 206));
}

String specialityName = specilaity_name.toUpperCase();
switch (specialityName) {
case "CARDIAC":
linearspeclist.setBackgroundResource(R.drawable.rectangle_border_theme);
speclialistimage.setImageResource(R.drawable.ic_heart);
specialization.setTextColor(Color.WHITE);
break;
case "UROLOGY":
// add code for urology
break;
case "GYNAECOLOGY":
// add code for Gynaecology
break;
default:
break;
}
}

Related

Android Firebase Phone Auth crashed when clicked button of empty edittext

With no problem, connected my app to firebase and I can test realtime database and other features, but when it comes to Phone Authentication, I am having problems. The problem is when Edittext is left empty and Button is clicked the app gets crashed. Don't know which code should be responsible for this problem.
Please help me to define it.
If you have carefully read the documentation of phone auth in firebase you will have below code:
private boolean validatePhoneNumber() {
String phoneNumber = mPhoneNumberField.getText().toString();
if (TextUtils.isEmpty(phoneNumber)) {
mPhoneNumberField.setError("Invalid phone number.");
return false;
}
return true;
Then either with if-else or switch-case
You should call this method
NOTE: onCLick method comes after btn.setOnClickListener depending upon your button id.
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button_start_verification:
if (!validatePhoneNumber()) {
return;
}
startPhoneNumberVerification(mPhoneNumberField.getText().toString());
break;
case R.id.button_verify_phone:
String code = mVerificationField.getText().toString();
if (TextUtils.isEmpty(code)) {
mVerificationField.setError("Cannot be empty.");
return;
}
verifyPhoneNumberWithCode(mVerificationId, code);
break;
case R.id.button_resend:
resendVerificationCode(mPhoneNumberField.getText().toString(), mResendToken);
break;
case R.id.sign_out_button:
signOut();
break;
}
}
Due to the codes which were not provided by you, I have written some references based on my own app. Please consider changing those and probably you wont get further crashes.

Capturing Multitouch in Android

For my games I allways use singletouch so I allways keep track of the Pointer in an onTouch() Method like this:
-get the pointer Id at (0) with event.getPointerId(0); when pId==0, where pId is the pointer Id
-there will be no computation of other pointers when the pId!=-1(standard value I assign when the prime pointer is lifted up again.) and if the current pointer index is not the same as the pointer indes of the pointer Id I have saved in pId
like so:
switch(e.getAction())
{
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_DOWN:
if(pID!=-1)break;
...
pID=e.getPointerId(0);
...
break;
case MotionEvent.ACTION_MOVE:
if(pID==-1||e.getActionIndex()!=e.findPointerIndex(pID))break;
...
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
if(pID==-1||e.getActionIndex()!=e.findPointerIndex(pID))break;
...
pID=-1;
...
break;
}
But now I stumbled across a code that I wrote 2 weeks ago where I do it other way, because I wanted to react to Multitouch(maximum two touch pointers allowed for zooming):
switch(e.getAction())
{
case MotionEvent.ACTION_POINTER_DOWN:
case MotionEvent.ACTION_DOWN:
if(tP1.getPointerIndex()==-1&&tP2.getPointerIndex()==-1)
{
tP1.setPointerIndex(currentPointerIndex);
mode=Mode.SINGLE;
}
else if(tP1.getPointerIndex()!=-1&&tP2.getPointerIndex()==-1&&!isDragging)
{
tP2.setPointerIndex(currentPointerIndex);
mode=Mode.MULTI;
}
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.X=e.getX();
tP1.Y=e.getY();
tP1.oldX=tP1.X;
tP1.oldY=tP1.Y;
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.X=e.getX();
tP2.Y=e.getY();
tP2.oldX=tP2.X;
tP2.oldY=tP2.Y;
}
else break;
...
break;
case MotionEvent.ACTION_MOVE:
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.X=e.getX();
tP1.Y=e.getY();
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.X=e.getX();
tP2.Y=e.getY();
}
else break;
...
break;
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_UP:
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.X=e.getX();
tP1.Y=e.getY();
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.X=e.getX();
tP2.Y=e.getY();
}
else break;
if(mode==Mode.SINGLE)
{
mode=Mode.NONE;
tP1.resetPointerIndex();
}
else if(mode==Mode.MULTI)
{
mode=Mode.SINGLE;
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
{
tP1.resetPointerIndex();
tP1.setPointerIndex(tP2.getPointerIndex());
tP1.X=tP2.X;
tP1.Y=tP2.Y;
tP1.oldX=tP2.oldX;
tP1.oldY=tP2.oldY;
tP2.resetPointerIndex();
}
else if(e.findPointerIndex(tP2.getPointerIndex())==currentPointerIndex)
{
tP2.resetPointerIndex();
}
}
break;
}
tP1 and tP2 ar class objects who keep track of the last x and y coordinates and of the pointer index. So they represent the two pointers. I allways swap the second pointer on the first pointer if the first pointer is raised up and the second is still on. Now it is really embarrassing because I wrote this code for a colleague of mine for a few bucks.
So I jsut keep track of the indices of the pointers but they are sometimes changing. I didnt noticed it because everything was working fine. But now as I came accross it I noticed this. What should I do. Do I need to change it or do you see no problem to let this code be as it is?
I am just assuming that it can go wrong because I dont track the pointer Id but only the pointer Index.
Also I never experience it when testing that for example the primary pointer when first touched get swapped to another index but 0 when another touchpointer comes down.
I feel so dumb that I didnt notice it, so now I need a good advice how to behave pls help...
It is really strange that is code worked because I do this:
if(e.findPointerIndex(tP1.getPointerIndex())==currentPointerIndex)
So Android not only assigns the index of the first pointer with 0 but also the pointerId with 0. This code is a mess i think

android how to change from if-else to switch case

I have an application about pmt function. However there are so many conditions that need to be handled. Somehow the app will not work with having more than 12 if-else. I want to use switch case, but i still not really understand how to use switch case(been 1 and half month since my 1st try using eclipse).Any example will be highly appreciated.
here is my example code:
if(String1.toString().equals("condition1")){
//do something
if(String2.toString().equals("condition1.1")&& String3.toString().equals("condition1.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition1.##")&& String3.toString().equals("condition1.##")){
//do something else
}
}
else if(String1.toString().equals("condition2")){
//do something
if(String2.toString().equals("condition2.1")&& String3.toString().equals("condition2.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition2.##")&& String3.toString().equals("condition2.##")){
//do something else
}
}
if(String1.toString().equals("condition3")){
//do something
if(String2.toString().equals("condition3.1")&& String3.toString().equals("condition3.2")){
//do something else
}
.
.
.
.
.
if(String2.toString().equals("condition3.##")&& String3.toString().equals("condition3.##")){
//do something else
}
}
and still keep going....to handle all possibilities .I am wondering, How to do this in switch case . Or a better implementation if we have 3 times 3 conditions. For example a,b,c(suppose these three conditions can only be used once) and d,e,f and g,h,i then condition 1 is a,d,g ; condition 2 is a,d,h condition 3 is a,d,i ; condition 4 a,e,g........on so on
Note:Suppose that the API version is 8-11 (old android)
thanks
The answer is dependent on your target version of android. From KitKat and upwards (API Level 19+), Java 7's switch (String) is available. I'd also strongly suggest trying to group the subcases (condition n.x) into different methods. It just gets very unwieldly quickly, otherwise:
switch (String1.toString) {
case "condition1":
handleCase1(String2, String3);
break;
case "condition2":
handleCase2(String2, String3);
break;
}
If that still results in too complex code, you can try a lookup table together with a command pattern:
class ConditionKey {
final String String1;
final String String2;
final String String3;
public int hashCode(); // hash strings
public boolean equals(); // compare strings
}
interface ConditionCommand {
// use whatever arguments the operation needs, you can also
// add fields and initialize in the constructor
void perform(final ConditionKey key, /* [...] */);
}
Map<ConditionKey, ConditionCommand> actionMap = new HashMap<>();
actionMap.put(
new ConditionKey("condition1", "condition1.1", "condition1.2"),
new ConditionCommand() {
void perform(final ConditionKey key) {
// perform actions that need to be done
}
}
);
And then instead of the if-else or switch-case:
[...]
ConditionKey key = new ConditionKey(string1, string2, string3);
// get the action from the map
ConditionCommand command = actionMap.get(key);
// perform the command
command.perform(key);
since java 1.7 switch on string is supported.
you could annidate two switch:
switch(String1) {
case "condition1": {
switch(String2) {
case "condition1.1":
break;
// ... other cases
default:
break;
}
}
break;
// ... other cases
default break;
}

OnVideoChatChangeState() usage in Quickblox

I am using quickblox api for 1 to 1 videochat but I dont know the usage OnVideoChatChangeState() of OnQBVideoChatListener() class and with what changes the event is invoked. I have modified the code but the video doesnt start the click functions but doesn't go to:
` public void onVideoChatStateChange(CallState state, VideoChatConfig receivedVideoChatConfig) {
videoChatConfig = receivedVideoChatConfig;
isCanceledVideoCall = false;
Toast.makeText(getApplicationContext(), "switch", Toast.LENGTH_LONG).show();
switch (state)
{
case ON_CALLING:
Toast.makeText(getApplicationContext(), "After this the showCallDialog() will be called.", Toast.LENGTH_LONG).show();
showCallDialog();
break;
case ON_ACCEPT_BY_USER:
progressDialog.dismiss();
startVideoChatActivity();
break;
case ON_REJECTED_BY_USER:
progressDialog.dismiss();
break;
case ON_DID_NOT_ANSWERED:
progressDialog.dismiss();
break;
case ON_CANCELED_CALL:
isCanceledVideoCall = true;
videoChatConfig = null;
break;
case ON_START_CONNECTING:
progressDialog.dismiss();
startVideoChatActivity();
break;
default:
break;
}
}
};
`
and the showCallDialog(); method is not called this shows the events doesn't occur here.
So I want to know can the event occurs so that the methods are called.
This has been fixed. Master branch is updated. Please try download and use the sample once again.

Swichcase not working in android?

I am stuck in swich case. Please check code below
Log.e("##################### pos",""+posSel_lay);
String ff=Integer.toString(posSel_lay);
//var ffs=Integer.toString(posSel_lay);
Log.e("##################### pos",""+ff);
if(ff.equals("0")){
Log.e("###################Lagan","");
}else if(ff.equals("1")){
Log.e("###################MBBS","");
}else if(ff.equals("2")){
Log.e("###################JODHA","");
}else if(ff.equals("3")){
Log.e("###################ZINDAGI","");
}
I got log only this line
##################### pos 2
& its not going in swich case why i dont know, can you help me please?
All the answers are good.
but you should at least put any message into message param of Log. otherwise you will not able to see this in logs
Log.e("###################ZINDAGI","some message.");
By default a switch case works WITH integers, why don't you try something like the following:
Log.e("##################### pos",""+posSel_lay);
switch (posSel_lay){
case 0:
Log.e("###################Lagan","");
break;
case 1:
Log.e("###################MBBS","");
break;
case 2:
Log.e("###################JODHA","");
break;
case 3:
Log.e("###################ZINDAGI","");
break;
default:
break;
}
Now instead of converting to a string, and using an if-else chain, you use a select statement, which IMO is much cleaner.
Log.e("##################### pos", "" + posSel_lay );
switch( posSel_lay ) {
case 0:
Log.e("###################Lagan","");
break;
case 1:
Log.e("###################MBBS","");
break;
case 2:
Log.e("###################JODHA","");
break;
case 3:
Log.e("###################ZINDAGI","");
break;
}
I can't really understand your question , but here is SwitchCase
switch(posSel_lay){
case 0:
Log.e("###################Lagan","");
break;
case 1:
Log.e("###################MBBS","");
break;
case 2:
Log.e("###################JODHA","");
break;
case 3:
Log.e("###################ZINDAGI","");
break;
}
I found solution. I get this posSel_lay from bundle which is passed by other activity.Previously i written switch code in on button click so now i changed flow,
When i am getting value of posSel_lay from bundle that time only i written code of switch
& there i am making some boolean values true or false which is declared locally. And when user
clicks on button that time i used boolean variables for checking. Then its done.
Thanks for your reply.

Categories

Resources