I have 2 actvities with a xml each but I don't know how to switch to the second page with a button.
Can someone send me the code for the MainActivitie.java please (for a button)
I currently use the findView and onClick
In your MainActivity use this code:
Intent intent = new Intent(MainActivity.this, Second.java);
startActivity(intent);
Please read about "Intents", I have added some links that will clear your concepts.
Previously asked question
Intent Types
Related
Is the function onAdDismissedFullScreenContent() working when the user closes the add or it works when the ad did not appear? I do not understand very well the meaning of dismissed in this context. The second question is, is it mandatory to put inside this onAdDismissedFullScreenContent function the following code? What is the intention of the following code? Could I put nothing on this function? Thanks in advance
onAdDismissedFullScreenContent(){
Intent intent = new Intent(getApplicationContext(), Activity.class);
startActivity(intent);
}
For the first question
Yes, this method onAdDismissedFullScreenContent() is user closes the full screen ad content.
And if the ad failed to show full screen content the method
onAdFailedToShowFullScreenContent(AdError adError) will be called.
You can check the public method summery for FullScreenContentCallback here
And for your second question,
No, it's not mandatory to set anything inside these functions.
and the code
Intent intent = new Intent(getApplicationContext(), ActivityWorkout.class);
startActivity(intent);
will open an activity called ActivityWorkout when the ad is closed.
I hope I answered your questions, Happy coding.
Intent intent = new Intent(currentActivity, home.class);
currentActivity.startActivity(intent);
For example, first activity of app is MainActiviy which is basically 2 text fields (login and password) with a button. Pressing a button tells the database these text fields' data so it can tell the program if the user is in database and password is correct.
What I'm used to is to create a bool flag field saved in SharedPreference so the program knows i already logged in my app recently so it can open my second activity, but it seems like wrong and weird solution.
So, how do I properly login into my app in theory?
Mikkel,
Your approach with storing data using shared preferences is perfect but I have some suggestions which can make this solution better and robust.
First of all, you should check the sharePreference value inside a splash screen. Which checks for the login flag and launch the LoginActivity if a user is not logged in and launch the HomeActivity if a user is logged in.
Second, I would suggest using Androidx DataStore, which is a better version of shared preference and it can be asynchronous API so you can use it safely on the UI thread.
Don't forget to close your main activity after user login using the below code.
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
If you need an example to handle this stuff better here is the github-sample which can be very helpful.
This is wrong:
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
finish();
startActivity(intent);
You should use "startActivity" before "finish()"
Correctly:
Intent intent = new Intent(this,MainActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
This question already has answers here:
How to make a phone call using intent in Android?
(21 answers)
Closed 7 years ago.
I'm building an app that lets you do various tasks and for this particular task. I want the user to be able to call someone. I've taken a look at many questions but all of them tell you to use intents and direct you to the phone app or instead they give you an answer where the users clicks a button and it calls the number assigned in the code.
I want the user to be able to type in a number in and EditText field and be able to hit the call button to call someone, how can I do something similar to this?
I'm not sure if this website is good for stuff like this as answers are hard to come by so I will keep looking around for answers, hopefully someone can answer my q and help me out a bit.
EDIT -
Super disappointed with all the notices and the downvote, really sucks however I appreciate the answers provided. But none helped lol.
So here is how I managed to do what I had asked. I create a onClick event and defined the two main objects with the objects and IDS above it (The EditText and the Button). In the onClick method I got the code to check and see if anything has been entered in the textfield (EditText), it detects with the following code -
(!TextUtils.isEmpty(txtPhone)) {
Uri uri = Uri.parse("tel:" + txtPhone);
If numbers have been typed in, it will run this intent and pull the string from the EditText object.
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
if nothing is in field it will show a toast.
Check below for my method. Hopefully someone in my position will benefit from this and wont have to get downvoted for no particular reason.
// Call button
ImageButton call = (ImageButton) findViewById(R.id.call_button);
final EditText txtPhoneNo = (EditText) findViewById(R.id.txtPhoneNo);
call.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
String txtPhone = txtPhoneNo.getText().toString();
if (!TextUtils.isEmpty(txtPhone)) {
Uri uri = Uri.parse("tel:" + txtPhone);
Intent intent = new Intent(Intent.ACTION_CALL, uri);
startActivity(intent);
} else {
Toast.makeText(getApplicationContext(),
"Please enter recipients number", Toast.LENGTH_LONG)
.show();
}
}
});
Looks like I will be using YouTube for stuff like this now, StackOverFlow is supposed to be about helping new users. Not downvoting their questions and repeatedly labelling their posts as duplicates and already answered.
You should check this : how to make phone call using intent in android? and the accepted answer.
I don't know whether you have searched or not, I found this solution as first link in my search and its pretty much what you want to do (i.e. Call Directly from your app).
Try this
handle this on your call button click
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setData(Uri.parse("tel:"+ editText.getText.toString()));
startActivity(callIntent);
add <uses-permission android:name="android.permission.CALL_PHONE" /> in Manifest
I am new to Android so apologies if I am asking something silly. I am trying to develop an alarm clock application - basically, it's my final project and I am trying to develop an alarm like there is in API level 2.3.3.
I have designed the list view that takes input through a dialog box like time. I have also coded it to set an alarm.
Now I want to save that alarm as an intent in the other class, and I don't have any idea how to save different alarms in the other activity. I have also checked for the desk-clock alarm code but I didn't get that too.
Please help me someone, I am stuck here for the code for more than a week. Please someone help me, I shall be thankful to you.
If you want to send an Intent from one Activity to another, and then retrieve information from inside the Intent, the best way is use the Bundle object inside the intent:
Let's supose you send the intent from Activity1 to Activity2...
In Activity 1:
Intent intent = new Intent(Activity1.class,Activity2.class);
//I use the String class name as a key value, but you can use whatever key
intent.putExtra(String.class.getCanonicalName(), myString);
startActivity(intent);
//Or this other method if you want to retrieve a result from Activity2
//startActivityForResult(intent,Activity2);
In Activity 2:
Intent intent = this.getIntent();
Bundle bundle = intent.getExtras();
String myString = bundle.getString(String.class.getCanonicalName());
OK,I'm new at this forum, so don't blame me for putting this in the wrong tags,not putting something in,eg.
I want to learn how to create a shortcut(I did that by Googling) and link it to an activity (In this case, com.android.mms.ui.ComposeMessageActivity)
I tried doing it, but it only showed me a toast saying "Application not installed" and I'm pretty sure it is.
It would be better if you can display a "complete action with another application" dialog.
If I assumed your question correctly, you mean a button or something within an activity that leads to another activity, that being -- "com.android.mms.ui.ComposeMessageActivity"
if your activity that you want to link to is in another application-- then
Intent intent = new Intent();
intent.setComponent(new ComponentName("com.mine", "com.android.mms.ui.ComposeMessageActivity"));
startActivity(intent);
if it is within the same application, then
Intent intent = new Intent(this, ComposeMessageActivity.class);
startActivity(intent);
//optional add this to your manifest to finish the current loading activity so
//as to not keep it in the activity stack
//<activity android:name="yourActivity" android:noHistory="true" ... />
EDIT If you mean a shortcut on a homescreen, then I would create a tiny application that only has one activity which uses the above method to link to a different application. Then I would drag that application to the home screen, and boom. If there's a better way, then please feel free to correct me