I have three activities A,B and C
In my B activity I have one imageview, when I get a String from activity A to B, the imageview should be visible.
When I get a String from activity C to B then the imageview should not be visible.
//From activity A
Intent iin= getIntent();
Bundle b = iin.getExtras();
//From Activity C
Intent i2=getIntent();
Bundle abcd=i2.getExtras();
if(b!=null)
{
String j =(String) b.get("arrowvisi");
Toast.makeText(getApplicationContext(), j, Toast.LENGTH_LONG).show();
if(j==b.get("arrowvisi"))
{
img_back.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Operational arrow visible", Toast.LENGTH_LONG).show();
}
else
{
if(abcd!=null)
{
String jst =(String) abcd.get("arrow_val");
if(jst==abcd.get("arrow_val"))
{
img_back.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "scan dispatch visble", Toast.LENGTH_LONG).show();
}
else
{
//img_back.setVisibility(View.GONE);
System.out.println("from scan dispatch");
Toast.makeText(getApplicationContext(), "scan dispatch not visible", Toast.LENGTH_LONG).show();
}
}
Toast.makeText(getApplicationContext(), "Operational not visible", Toast.LENGTH_LONG).show();
}
}
Replace
String j = (String) b.get("arrowvisi");
with
String j = b.getString("arrowvisi");
and
String jst = (String) abcd.get("arrow_val");
with
String jst = abcd.getString("arrow_val");
also string comparison should be like
j.equals(b.getString("arrowvisi")) // change again
and
jst.equals(abcd.getString("arrow_val")) // change again
So, the final ans should be something like
//From activity A
Intent iin = getIntent();
Bundle b = iin.getExtras();
//From Activity C
Intent i2 = getIntent();
Bundle abcd = i2.getExtras();
if(b != null){
String j = "String to check"; // Replace content inside "" with your string
Toast.makeText(getApplicationContext(), j, Toast.LENGTH_LONG).show();
if(j.equals(b.getString("arrowvisi"))){
img_back.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "Operational arrow visible", Toast.LENGTH_LONG).show();
}else{
if(abcd != null){
String jst = "String to check"; // Replace with string you want to check
if(jst.equals(abcd.getString("arrow_val"))){
img_back.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "scan dispatch visble", Toast.LENGTH_LONG).show();
}else{
//img_back.setVisibility(View.GONE);
System.out.println("from scan dispatch");
Toast.makeText(getApplicationContext(), "scan dispatch not visible", Toast.LENGTH_LONG).show();
}
}
Toast.makeText(getApplicationContext(), "Operational not visible", Toast.LENGTH_LONG).show();
}
}
Also note that, doing something like
String jst = abcd.getString("arrow_val"));
if(jst.equals(abcd.getString("arrow_val"))) // this will be always true
this, will result in if statement being always true.
2 options :
1) Remove if statement because always true.
2) change String jst = "Some string to compare" (change this) and now compare jst with getString("arrow_val") using jst.equals(abcd.getString("arrow_val")) in your if loop
change this
String j = (String) b.get("arrowvisi");
with
String j = b.getString("arrowvisi");
and
String jst = (String) abcd.get("arrow_val");
with
String jst = abcd.getString("arrow_val");
also put this in bracket()
b.get("arrowvisi")
abcd.get("arrow_val")
First of all,to compare strings in java,you need to use .equals
so change
if(j==b.get("arrowvisi"))
to
if(j.equals(b.getString("arrowvisi")))
and
if(jst==abcd.get("arrow_val"))
to
if(jst.equals(abcd.getString("arrow_val")))
And second,your condition always will be true,because you are comparing two equal values always,i.e. first getting value in jst and then comparing same value.
Edit : to resolve null pointer change
String j =(String) b.get("arrowvisi");
to
String j = b.getString("arrowvisi");
and
String jst =(String) abcd.get("arrow_val");
to
String jst =abcd.getString("arrow_val");
And post logcat exception.
Related
I'm a noob who's working on his first app. Imagine you're busy and not able to pick up some calls during the day. The app shows you a log of the calls you missed, and you can start calling them with a single button click. Not all at once - you start with the first missed number, automatically come back to the app when the call is finished, automatically dial the second number, and so on until the list is empty or you're done calling. This is my what my app looks like right now:
https://imgur.com/tke7SDx
I log missed calls and display them, and I have a "start calling" button that's supposed to start the loop. I'm not sure how to make it so that the onClick starts calling missed call no1, then missed call no2 etc and I haven't found much about it though my Google game isn't very strong yet. This is how I get the call details:
public String getCallDetails() {
StringBuffer sb = new StringBuffer();
// if
// (ActivityCompat.checkSelfPermission(getApplicationContext(),
// Manifest.permission.READ_CALL_LOG) !=
// PackageManager.PERMISSION_GRANTED) {
//
// return ;
// }
Cursor managedCursor = getApplicationContext().getContentResolver().query(CallLog.Calls.CONTENT_URI, null, null, null, CallLog.Calls.DATE + " DESC");
int number = managedCursor.getColumnIndex(CallLog.Calls.NUMBER);
int type = managedCursor.getColumnIndex(CallLog.Calls.TYPE);
int date = managedCursor.getColumnIndex(CallLog.Calls.DATE);
int duration = managedCursor.getColumnIndex(CallLog.Calls.DURATION);
sb.append("\n");
while (managedCursor.moveToNext()) {
String phNumber = managedCursor.getString(number);
String callType = managedCursor.getString(type);
String callDate = managedCursor.getString(date);
Date callDayTime = new Date(Long.valueOf(callDate));
String callDuration = managedCursor.getString(duration);
String dir = null;
int dircode = Integer.parseInt(callType);
switch (dircode) {
case CallLog.Calls.OUTGOING_TYPE:
dir = "OUTGOING";
break;
case CallLog.Calls.INCOMING_TYPE:
dir = "INCOMING";
break;
case CallLog.Calls.MISSED_TYPE:
dir = "MISSED";
break;
}
// Getting the current date and time using the date class
Date d = new Date();
if (dir == "MISSED") {
sb.append("\n Phone Number: " + phNumber + " \n Call Date: " + callDayTime + "\n");
sb.append(" ---------------------------------------------------\n \n");
}
}
managedCursor.close();
return sb.toString();
}
And this is my button onClick:
callBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
try {
// String phone = ????
// Using the ACTION.CALL intent, you're going straight to the first
// call
// Intent callIntent = new
// Intent(Intent.ACTION_CALL, Uri.fromParts("tel",
// phone, null));
// Check for permission, write yes/no etc. here
if (ActivityCompat.checkSelfPermission(getApplicationContext(), Manifest.permission.CALL_PHONE) !=
PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(MainActivity.this, new String[]{Manifest.permission.CALL_PHONE},
UNIQUE_REQUEST_CODE);
} else {
Toast.makeText(MainActivity.this, "Permission granted! Thank you!", Toast.LENGTH_SHORT).show();
}
startActivity(Intent.createChooser(callIntent, "callTitle"));
} catch (Exception e) {
Toast.makeText(getApplicationContext(), "Oh no, your call has failed!",
Toast.LENGTH_LONG).show();
e.printStackTrace();
}
}
});
I'm also trying to filter it so that only missed calls from the past two days are showing, but that's something for later. Just wondering what's a good way to call-loop through the missed calls right now.
Any pointers are welcome!!
Thank you!
You can use Phone State Listener to listen call states. Whenever call states returns to STATE_IDLE you can go for next calls.
Dont forget to stop listining state when you are done.
Hello i am building one app which will take google profile name and then it will be displayed in another activity as EditText.
ActivityGoogle
private void getProfileInformation() {
try {
if (Plus.PeopleApi.getCurrentPerson(mGoogleApiClient) != null) {
Person currentPerson = Plus.PeopleApi
.getCurrentPerson(mGoogleApiClient);
String personName = currentPerson.getDisplayName();
String personPhotoUrl = currentPerson.getImage().getUrl();
String personGooglePlusProfile = currentPerson.getUrl();
String email = Plus.AccountApi.getAccountName(mGoogleApiClient);
textView_name.setText(personName);
textView_email.setText(email);
// by default the profile url gives 50x50 px image only
// we can replace the value with whatever dimension we want by
// replacing sz=X
personPhotoUrl = personPhotoUrl.substring(0,
personPhotoUrl.length() - 2)
+ 400;
new LoadProfileImage(imageView_profile_image).execute(personPhotoUrl);
} else {
Toast.makeText(getApplicationContext(),
"Person information is null", Toast.LENGTH_LONG).show();
any idea how to take name from this and display it in EditText
ActivityUser
profilename = (EditText)findViewbyId(R.id.profilename);
(I posted it as a comment but I think it solved the issue, so I post it as answer and add some explanation)
Have a look at these links : 1 2 3. A Bundle is exactly what you need.
This code is taken from one of the links and it's what you need. It saves a value on the Bundle:
ActivityGoogle
Intent mIntent = new Intent(this, Example.class);
Bundle extras = mIntent.getExtras();
extras.putString(key, value); // <-- key is the same
Then, on your ActivityUser add the following (note that key variable must be the same in both Activity):
ActivityUser
String value = getIntent().getExtras().getString(key); // <-- as the key here
I'm beginner for Android, im having stringArray resource on string.xml file.. i need check whether the item name is correct then i hav to do something.. im entering the correct item name but it goes to else part.. may be the problem is in if(itemname=stringArray).. plz give me the solution ..
String[] drugs = getResources().getStringArray(R.array.Drugsinfo);
if(editext1.getText().equals(drugs))
{
Toast.makeText(getApplicationContext(), "some usefull info for you buddy ...", Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(MainActivity.this, Descriptionjava.class);
startActivity(myIntent);
}
else
{
Toast.makeText(getApplicationContext(), "Wrong pill name !! check out the list",Toast.LENGTH_LONG).show();
bt1.setEnabled(true);
}
}
Try this:
String[] bob = { "this", "is", "a", "really", "silly", "list" };
if (Arrays.asList(bob).contains("silly")) {
// true
}
"silly" is text of your EditText.
For getting text from edit text and compare according to your comment use some like:
if(yourEditText.getText().toString().trim().equalsIgnoreCase("silly"))
{
//do your work here
}
Thanks.
I haven't test it but you could try this.
String[] drugs = getResources().getStringArray(R.array.Drugsinfo);
for(int index = 0; index < drugs.length;index++) {
if(editext1.getText().toString().equals(drugs[index])) {
Toast.makeText(getApplicationContext(), "some usefull info for you buddy ...",Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(MainActivity.this, Descriptionjava.class);
startActivity(myIntent);
} else {
Toast.makeText(getApplicationContext(), "Wrong pill name !! check out the list",Toast.LENGTH_LONG).show();
bt1.setEnabled(true);
}
}
Not getting any errors im just stumped on this friggin if statement. I want the if statement to basically say if a check box is not checked and 3 EditTexts are empty then print a toast. Otherwise dont print the toast and continue to the next activity. First I turn the value of the checkbox into either t/f for true/false then execute my if statements as the following.
CheckBox noPtD1 = (CheckBox) findViewById(R.id.noPTD1);
String noPt_D1 = "f";
if (noPtD1.isChecked()) {
noPt_D1 = "t";
}
if (noPt_D1.equals("f") && day1_inst.equals("") || day1_uniform.equals("") ||
day1_location.equals(""))
{
Toast.makeText(getApplicationContext(), "Please Enter All Data Or Select the NO PT THIS DAY
checkbox!", Toast.LENGTH_LONG).show();
}
if(noPt_D1.equals("t") || !day1_inst.equals("") && !day1_uniform.equals("") &&
!day1_location.equals(""))
{
//PASS VARIABLES WITH INTENT
Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Week1Day2.class);
//PASS VARIABLES FOR DAY 1
intent.putExtra("noPt_D1", noPt_D1);
intent.putExtra("day1_inst", day1_inst);
intent.putExtra("day1_uniform", day1_uniform);
intent.putExtra("day1_location", day1_location);
intent.putExtra("d1hours", d1hours);
intent.putExtra("d1min", d1min);
intent.putExtra("d1x1", d1x1);
intent.putExtra("d1x2", d1x2);
intent.putExtra("d1x3", d1x3);
intent.putExtra("d1x4", d1x4);
startActivity(intent);
}
This is working except for then I check the checkbox and and start the next activity with a button the next activity pops up like it is supposed to but the toast still appears. What did I mess up?
I got it I posted my answer below. But to rephrase exactly what my intent was:
If the user DOES NOT check the checkbox and leaves all 3 inputtexts blank show the toast and dont continue to next activity.
If the user DOES check the checkbox then DONT show the toast and continue to next activity.
Use brackets inside if statement
if (noPt_D1.equals("f") && (day1_inst.equals("") || day1_uniform.equals("") ||
day1_location.equals("")))
{
}
if(noPt_D1.equals("t") || (!day1_inst.equals("") && !day1_uniform.equals("") &&
!day1_location.equals("")))
{
}
You are making minor mistake in condition PLUS you need to use if - else block instead of 2 if blocks
//surround all && conditions inside separate braces
if (noPt_D1.equals("f") && (day1_inst.equals("") || day1_uniform.equals("") ||
day1_location.equals("")))
{
//show toast
}
else if(noPt_D1.equals("t") || (!day1_inst.equals("") && !day1_uniform.equals("") &&
!day1_location.equals("")))
{
//start activity
}
Hope this helps.
also try this. and its better to avoid including special characters for ur class name
if ("f".equals(noPt_D1) && "".equals(day1_inst) || "".equals(day1_uniform) ||
"".equals(day1_location))
{
Toast.makeText(getApplicationContext(), "Please Enter All Data Or Select the NO PT THIS DAY
checkbox!", Toast.LENGTH_LONG).show();
}
if("t".equals(noPt_D1) || !"".equals(day1_inst) && !"".equals(day1_uniform) &&
!"".equals(day1_location))
{
//PASS VARIABLES WITH INTENT
Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Week1Day2.class);
//PASS VARIABLES FOR DAY 1
intent.putExtra("noPt_D1", noPt_D1);
intent.putExtra("day1_inst", day1_inst);
intent.putExtra("day1_uniform", day1_uniform);
intent.putExtra("day1_location", day1_location);
intent.putExtra("d1hours", d1hours);
intent.putExtra("d1min", d1min);
intent.putExtra("d1x1", d1x1);
intent.putExtra("d1x2", d1x2);
intent.putExtra("d1x3", d1x3);
intent.putExtra("d1x4", d1x4);
startActivity(intent);
}
Try to check EditText values via length() of String value. Secondly put && operator in if statement because you said in your question: if a check box is not checked and 3 EditTexts are empty then print a toast. Otherwise dont print the toast and continue to the next activity.
if (noPt_D1.equals("f") && day1_inst.trim().length() == 0 && day1_uniform.trim().length() == 0 &&
day1_location.trim().length() == 0)
{
Toast.makeText(getApplicationContext(), "Please Enter All Data", Toast.LENGTH_LONG).show();
}
else
{
Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Week1Day2.class);
intent.putExtra("noPt_D1", noPt_D1);
intent.putExtra("day1_inst", day1_inst);
intent.putExtra("day1_uniform", day1_uniform);
intent.putExtra("day1_location", day1_location);
intent.putExtra("d1hours", d1hours);
intent.putExtra("d1min", d1min);
intent.putExtra("d1x1", d1x1);
intent.putExtra("d1x2", d1x2);
intent.putExtra("d1x3", d1x3);
intent.putExtra("d1x4", d1x4);
startActivity(intent);
}
EDIT:
Make sure you should declare your Strings which I am assuming are:
String day1_inst = your_editText_inst.getText().toString();
String day1_uniform = your_editText_uniform.getText().toString();
String day1_location = your_editText_location.getText().toString();
String noPt_D1 = "f";
if(your_check_box.isChecked())
{
noPt_D1 = "t";
}
else
{
noPt_D1 = "f";
}
Try using an OnCheckedChangeListener Listener, like whats shown below :
CheckBox noPtD1 = (CheckBox) findViewById(R.id.noPTD1);
String noPt_D1 = "f";
noPtD1.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
noPt_D1 = "t";
} else {
noPt_D1 = "f";
}
}
});
Replace getApplicationContext() with this if you are in activity. getActivity() if you're in fragment.
Just figured it out thanks to M S Gadag =)
Basically I took the if statements and put the startActivity if statement on top. Then I added a variable to show that that statement had already been executed. Then I used that variable to start another if statement to decide whether or not to show the toast.
int showToast = 0;
if("t".equals(noPt_D1) || !"".equals(day1_inst) && !"".equals(day1_uniform) &&
!"".equals(day1_location))
{
//PASS VARIABLES WITH INTENT
Intent intent = new Intent (OneWeekPlan_Start_Btn.this, Week1Day2.class);
//PASS VARIABLES FOR DAY 1
intent.putExtra("noPt_D1", noPt_D1);
intent.putExtra("day1_inst", day1_inst);
intent.putExtra("day1_uniform", day1_uniform);
intent.putExtra("day1_location", day1_location);
intent.putExtra("d1hours", d1hours);
intent.putExtra("d1min", d1min);
intent.putExtra("d1x1", d1x1);
intent.putExtra("d1x2", d1x2);
intent.putExtra("d1x3", d1x3);
intent.putExtra("d1x4", d1x4);
startActivity(intent);
showToast = 1;
}
if ("f".equals(noPt_D1) && "".equals(day1_inst) || "".equals(day1_uniform) ||
"".equals(day1_location))
{
if (showToast !=1)
{
Toast.makeText(getApplicationContext(), "Please Enter All Data Or Select the NO PT THIS
DAY checkbox!", Toast.LENGTH_LONG).show();
}
}
Im new to programming, so bear with me. After following a couple of tutorials i made an SMS application, i want to do an specific task when a specific text is received, i do this by comparing the text received to a value(code) that i already declared. The problem is that this condition never ocurrs.
public class SmsReceiver extends BroadcastReceiver
{
#Override
public void onReceive(Context context, Intent intent)
{
//---get the SMS message passed in---
Bundle bundle = intent.getExtras();
SmsMessage[] msgs = null;
String str = "";
String code = "blue";
String conf = "Ok";
String inv = "Invalid";
if (bundle != null)
{
//---retrieve the SMS message received---
Object[] pdus = (Object[]) bundle.get("pdus");
msgs = new SmsMessage[pdus.length];
for (int i=0; i<msgs.length; i++){
msgs[i] = SmsMessage.createFromPdu((byte[])pdus[i]);
str = msgs[i].getMessageBody().toString();
//n = Integer.parseInt(str);
}
if (str == code)
Toast.makeText(context, conf, Toast.LENGTH_SHORT).show();
else
//str = Integer.toString(n);
//Toast.makeText(context, str, Toast.LENGTH_SHORT).show();
Toast.makeText(context, inv, Toast.LENGTH_SHORT).show();
}
}
}
When I send the message it always displays the inv variable (Invalid). I tried to change the "code" variable to an integer, and then parse the string so I could compare it; in this scenario it did work, but whenever I received a string message the application crashes. I think it should be a simple thing, but since i don't fully understand java i can't figure it out.
I hope you could provide me with some guidelines or suggestions about what could i do.
Thanks in advance.
When comparing two strings, you should be using:
if (str.equals(code))
Java does not override the equals operator for string equality testing like it does for concatenation.
The test str == code only evaluates to true if the two String variables both refer to the EXACT same object in memory.
String str1 = "hello";
String str2 = "world";
if (str1 == str2) // not applicable for strings, can be use for integers
if (str1.equals(str2)) // applicable for strings
if (str1.compareTo(str2) > 0) // applicable for strings