i have a button and edit text. i want to work differently on pressing button depending on the condition whether edittext have some text or not?
i tried the code given below but it's not working.
String baseurl = null;(global variable)
code under button's onclick listener
baseurl = edittext.getText().toString();
if(!baseurl.isEmpty()){
body...
}else if(baseurl.trim().length() == 0 || baseurl.isEmpty() || baseurl == null){
Intent i = new Intent(URLFetcher.this, MainActivity.class);
startActivity(i);
}
if condition is working but else if is not working app is crashing on 2nd condition.
Just use TextUtil library
String baseurl = edittext.getText().toString();
if(TextUtils.isEmpty(baseurl)
{
Intent i = new Intent(URLFetcher.this, MainActivity.class);
startActivity(i);
}
else
{
// body
}
Check this:
baseurl = edittext.getText().toString();
if(baseurl == null || baseurl.isEmpty() ){
Intent i = new Intent(URLFetcher.this, MainActivity.class);
startActivity(i);
}
else
{
body...
}
You do not need to use
else if(baseurl.trim().length() == 0 || baseurl.isEmpty() || baseurl ==
null)
just use else
and then add your intent code in else part
else{
Intent i = new Intent(URLFetcher.this, MainActivity.class);
startActivity(i);
}
Use the following code for button click listener
baseurl = edittext.getText().toString();
if(!baseurl.isEmpty()){
//Code for edittext has a url
}else{
// Code for edittext has nothing
Intent i = new Intent(URLFetcher.this, MainActivity.class);
startActivity(i);
}
You don't need to check again in else clause. If !baseurl.isEmpty() is false that means baseurl has a 0 length. baseurl will never be null as per your code.
Use equalsIgnoreCase() method of string:
baseurl = edittext.getText().toString();
if(!baseurl.equalsIgnoreCase("")){
body...
}else if(baseurl.trim().length() == 0 || baseurl.isEmpty() || baseurl == null){
Intent i = new Intent(URLFetcher.this, MainActivity.class);
startActivity(i);
}
initialize your baseUrl to an empty string instead of null
String baseurl = ""; (global variable)
and then check the conditions
if(!baseurl.isEmpty()){
body...
}else if(baseurl.trim().length() == 0 || baseurl.isEmpty() || baseurl == null){
Intent i = new Intent(URLFetcher.this, MainActivity.class);
startActivity(i);
}
Related
I am building an android dialer application, but when my input contains string of form *123# the number which gets dialed is *123.
How can I pass # also in the dialer application?
Below is the code:
public void onDial(View v) {
if (input.getText().length() <3) {
Toast.makeText(this, "Please Enter the Valid Number", Toast.LENGTH_SHORT).show();
} else {
Intent intent = new Intent(Intent.ACTION_CALL);
String hash = input.getText().toString();
if (hash.contains("#")) {
hash.replace("#", "%23");
}
intent.setData(Uri.parse("tel:" + hash));
if (ActivityCompat.checkSelfPermission(this,Manifest.permission.CALL_PHONE) == PackageManager.PERMISSION_GRANTED) {
startActivity(intent);
}else{
requestCallPermission();
}
}
}
You where close! Encoding will work, but the replace method returns a new string! :-)
if (hash.contains("#")) {
hash = hash.replace("#", "%23"); // Need to set the hash reference to the new string generated by replace()!
}
intent.setData(Uri.parse("tel:" + hash));
Refs:
http://zetcode.com/kotlin/strings/
How to use Uri.parse() with # at the end
I have constants set up for my Flavor names:
public static final String FLAVOR_NAME_PRO = "pro";
public static final String FLAVOR_NAME_FREE = "free";
Inside my Activity's onCreate() I assign the boolean mUsingProFlavor a value:
mUsingProFlavor = BuildConfig.FLAVOR.equals(FLAVOR_NAME_PRO);
In a clickListener created later on I have this:
if (mUsingProFlavor) {
customerId = getCustomerId();
Intent intent = new Intent(MainActivity.this, CustomerProfileActivity.class);
intent.putExtra("customerId", customerId);
startActivity(intent);
} else {
showProOnlyFeatureAlertDialog(MainActivity.this, mAlertDialog);
}
This is the only way for the user to access CustomerProfileActivity, and yet somehow I'm getting crash reports indicating that one of my Free users is crashing inside of CustomerProfileActivity.
Any idea how this could happen?
if (mUsingProFlavor) {
customerId = getCustomerId();
Intent intent = new Intent(MainActivity.this, CustomerProfileActivity.class);
intent.putExtra("customerId", customerId);
startActivity(intent);
} else {
showProOnlyFeatureAlertDialog(MainActivity.this, mAlertDialog);
}
Your -> if(mUsingProFlavor) is the issue here.
Since mUsingProFlavor is a String, you have to modify your if-check. Currently it is acting as boolean.
if (mUsingProFlavor.equals("pro")) {
customerId = getCustomerId();
Intent intent = new Intent(MainActivity.this, CustomerProfileActivity.class);
intent.putExtra("customerId", customerId);
startActivity(intent);
} else {
showProOnlyFeatureAlertDialog(MainActivity.this, mAlertDialog);
}
I am developing a code in which the app takes input as speech and does the specific tasks.
I have got the code from changing speech to text but I am not able to put a if condition to create calls or open maps
here is a piece of code after getting text
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Text.setText(text.get(0));
String check = text.get(0);
if(check.equals("call")){
System.out.println("yes calling");
}
}
break;
}
Here I am performing if condition if he says call it should print "yes calling" but its not printing. what should I do?
Okay you can do it.
case RESULT_SPEECH: {
if (resultCode == RESULT_OK && null != data) {
ArrayList<String> text = data
.getStringArrayListExtra(RecognizerIntent.EXTRA_RESULTS);
Text.setText(text.get(0));
String check = text.get(0);
if(check.equals("call")){
Intent intent = new Intent(package name for app);
try{
startActivity(intent);
}catch(ActivityNotFoundException e){
}
}
}
break;
}
I have JSON Object contacts which Looks like so {"email":"Mark#HookahStationBCS.com","phone":"+19796918899"}In my code I grab the phone key and am able to get it's value, but then when I print out the email value it says it's null, and in my code below it doesn't actually pass the termination case. Does anyone know why?
if(nextActivityObject.getJSONObject("contacts") != null){
Log.d("NEXT ACTIVITY OBJECT NOT EMPTY", "NOT EMPTY");
contacts = nextActivityObject.getJSONObject("contacts");
if(contacts.getString("phone") != null){
phone = contacts.getString("phone");
Log.d("PHONE PASSED", phone.toString());
intent.putExtra("phone",phone);
} else if(contacts.getString("email") != null){
email = contacts.getString("email");
Log.d("THIS IS THE EMAIL PASS", email.toString());
intent.putExtra("email", email);
}
}
I think your code should be something like this:
if (nextActivityObject.getJSONObject("contacts") != null){
Log.d("NEXT ACTIVITY OBJECT NOT EMPTY", "NOT EMPTY");
contacts = nextActivityObject.getJSONObject("contacts");
if(contacts.getString("phone") != null && contacts.getString("email") != null){
phone = contacts.getString("phone");
email = contacts.getString("email");
intent.putExtra("phone",phone);
intent.putExtra("email",email);
}
else if (contacts.getString("phone") != null){
phone= contacts.getString("phone");
intent.putExtra("phone", phone);
}
else if (contacts.getString("email") != null){
email = contacts.getString("email");
intent.putExtra("email", email);
}
}
You have an elseif condition.. You have to have 2 if conditions. I am pretty much saying what the previous poster said, just wording it differently.
Iam building Android application that checks the format of the received message
if it starts with $A its starts Activity A and send the content of the message to Activity A
if it starts with $B it starts Activity B and send the content of the message to Activity B
Please Any help
You can do it like this
if(messages.getMessageBody().contains("$A")) {
//Write your code here
}
else if(messages.getMessageBody().contains("$B")) {
//Write your code here
}
And to pass data to the next activity,do it like this,,
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
i.putExtra("STRING_I_NEED", strName);
Then, to retrieve the value try something like:
String newString;
if (savedInstanceState == null) {
extras = getIntent().getExtras();
if(extras == null) {
newString= null;
} else {
newString= extras.getString("STRING_I_NEED");
}
} else {
newString= (String) savedInstanceState.getSerializable("STRING_I_NEED");
}