I can't seem to use data that is in an EditText box.
I have one activity for entering a url and a submit button.
When the user clicks submit, a new activity opens as a WebView and should display the url that
the user typed in the EditText box.
This should be the simplest of tasks but I can't find how to do it anywhere...
You can write below code
Intent myIntent = new Intent(CurrentActivity.this, NextActivity.class);
myIntent.putExtra("Url", edittext.getText());
And you can fetch this string in NextActivity using
String url = this.getIntent().getExtras().getString("Url");
when you create the Intent to start the next activity, you will use Intent.putExtra(...) to bundle the url and set the action to VIEW.
see the examples here.
Related
can someone help me about this one? I'm trying to build a simple quiz app that uses button, checkbox and textview. I want the score of all questions appear in the last page (5 pages) but can't make it work. I want to get the score from the previous question and then send it to the result page.
Store them in SharedPreference and get the value in the last page and/or clear it depending on what you need to do.
You should be able to pass the score from one activity to another using bundle in your intent.
//From Activity A
//To pass data from one activity to another
Intent i=new Intent(context, MyTargetClass.class);
i.putExtra("somekey", scoreValue);
context.startActivity(i);
//To Activity B
//To retrieve data from an activity
Intent intent = getIntent();
String scoreValue = intent.getStringExtra("someKey");
I am trying to code a native app with social logins as you can see the below image.
Now I'm able to authenticate users from my FirstActivity and sending the authenticated users to next SecondActivity.
Here comes my issue.
I have a single logout button on my SecondActivity
Now how do I know which account user clicked and how do I make them logout ?
Any suggestions are most welcome.
In your first activity make a string variable and set it to either facebook or either google + depending upon which the user picks to login. Pass in this variable as an extra with the intent to start the second activity.
So in your first activity you need something like this:
Intent myIntent = new Intent(this, NewActivityClassName.class);
myIntent.putExtra("id","FirstKeyValue");
myIntent.putExtra("name","SecondKeyValue");
startActivity(myIntent);
In your second activity you need something like this to retrieve the value :
Intent intent = getIntent();
String id = intent.getStringExtra("id");
String name = intent.getStringExtra("name");
I am making an app in which, dialog pops up and user selects an option. based on this option, next activity opens up in which there is some text. There are lots of options for the user to choose from in the dialog. Every option is intended to have the same layout just the text is different. Do I need to make these many new activities or can I somehow change the string based on user's choice by adding some conditionals somewhere? Please help
You need not create separate activity for each user input, as the layout is same. You can achieve this by using a TextView and changing its text according to user input like this:
TextView textView = (TextView) findViewById(R.id.<YourTextViewID>);
textView.setText("SetTextAccordingToUserInput");
One Activity is sufficient. Set the text on selection event in dialogue.
You can use only one Activity with content and create this activity with parameter depend on user selection in dialog.
To open activity with parameter:
Intent intent = new Intent(FirstActivity.this, SecondActivity.class);
Bundle b = new Bundle();
b.putInt("yourParameter", 1);
intent.putExtras(b);
startActivity(intent);
Then to use it in your SecondActivity.class onCreate method
onCreate(Bundle instance)
{
// some stuff like setting layout ant etc.
Bundle b = getIntent().getExtras();
int value = b.getInt("yourParameter");
}
Here is what I want to do, I have an android login form with username and password, after the user enters his credentials and login, the next form should display on top of the page welcome,+username entered from the login page! can someone please help me??
I'm new to android development and don't know how to go about this. Thanks
You can use intent to pass username from one activity to another.
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("username","enteredusername");
startActivity(intent);
And in SecondActivity
String name = getIntent().getStringExtra("username");
Now use the name and display the welcome +name
http://developer.android.com/training/basics/firstapp/starting-activity.html
In order to send data from one activity to another, you need to use intents. So, in your login activity, you would launch your welcome activity like this:
Intent intent = new Intent(LoginActivity.this, WelcomeActivity.class);
intent.putextra("userName","value");
startActivity(intent);
Then, in your welcome activity, you need to retrieve the extra data, like so:
String data= getIntent().getStringExtra("userName");
There are a multitude of examples that can be found via Google, and many questions similar/identical to this have been answered previously on SO.
You can store the username in sqlite or sharedpreferences to get the username across all your application.
I have a long text that I want to show it page by page. For my first try, I created 10 activity and put some button that when user clicks On it, next activity shows.
I read somewhere that I can load all my text in one activity with using Intent. is it possible? How it is possible?
I don't want user scroll the page.
Thanks
Let's say you have your activity called MyTextActivity, then in your button click listener, you pass on the text you want into an intent to your activity:
Intent i = new Intent(this, MyTextActivity.class);
i.putExtra("myText", "this is the text you want to display in the next page");
startActivity(i);
Then in your MyTextActivity, you put the following in onCreate():
String textToDisplay = "";
Bundle extras = getIntent().getExtras();
if (extras != null){
textToDisplay = extras.getString("myText");
}
then you set your TextView text to this text.
Assuming you already have a TextView and a Button in your activity, when the Button is pressed you do not need to go to a new activity. Instead, you can change the content of your existing text view. Just use TextView.setText() to change it.
If the text is static,you can put it in a string resource and access it in all your activities. Otherwise, you can attach the string to the intent, using putExtra.
I think the answer is "don't do this". Android runs on a variety of devices, and you should handle the cases where it won't fit on the screen that matches the device you're using for testing. Consider tablets, for example.