In my project I have two activities or classes. In first activity I have a EditText and I want to get the text of it from second class.
In the first class I wrote this code but it seems has problem.
public String getTextMessage()
{
return textMessage.getText().toString();
}
because in second class when I want to get it, program crashes.
message = encode.getTextMessage();
What is your suggestion?
As told by sunil you have to firstly get string from edittextbox and through intent send it to another second activity. After start of second activity you have to get text from bundle.
code snippet is given below...
Activity A
Intent i = new Intent(this, Second.class);
i.putExtra("EXTRATEXT", editText.gettext().toString());
startActivity(i);
Activity B
Class Second extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String text = getIntent().getExtras().getString("EXTRATEXT");
}
Access the text by getText() from edit text and store it in an string. when you move to second activity sent string variable to second class via bundel. Extract the bundel in second class and use it.
You have to pass the value through intents
Related
Please how do I link a new activity from my main activity using a clickable text. I have set the text to clickable from my main.xml but I don't know how to call the new activity from my MainActivity.java class. I know I have to use this code "textView.setOnClickListener(new View.OnClickListener());" I found in a similar question, but I don't know how and where to place it on my MainActivity.java class so that it calls a the next activity I named display
Check out Intent. You use these to start new activities or services within your application.
You're correct in that you have to assign an OnClickListener interface to your text, after you made it clickable. In the interface's onClick() method you would need to do something like this.
For example:
#Override
public void onClick(View v) {
// Create the intent which will start your new activity.
Intent newActivityIntent = new Intent(MainActivity.this, NewActivity.class);
// Pass any info you need in the next activity in your
// intent object.
newActivityIntent.putExtra("aString", "some_string_value");
newActivityIntent.putExtra("anInteger", some_integer_value);
// Start the new activity.
startActivity(newActivityIntent);
}
In the next activity, you can retrieve the intent used to start it, so that you'll have access to the data you passed from the first activity, like so:
#Override
public void onCreate(Bundle savedInstanceState) {
// Get the intent that started this activity.
Intent startingIntent = getIntent();
// Retrieve the values.
String aString = startingIntent.getStringExtra("aString");
Integer anInteger = startingIntent.getIntExtra("anInteger", 0); // 2nd param is the default value, should "anInteger" not exist in the bundle.
// Use the values to your hearts content.
}
Hope that helps.
I'm learning Android and I didn't find an answer on the web. I want to save a string from an EditText on my MainActivity to restore it on my third Activity -> Activity3
I want this string to be displayed on a TextView of this Activity.
public class MainActivity extends Activity {
private EditText nomPrenom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
nomPrenom = (EditText) findViewById(R.id.nomPrenom);
...................}
in my activity_main.xml :
android:id="#+id/nomPrenom"
android:text="#string/nomPrenom"
and
public class Activity3 extends ListActivity {
private TextView yourName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_activity3);
yourName = (TextView) findViewById(R.id.nomPrenom);
yourName.setText("take the name of activity 1 here"); //<-- this line causes crash !!
..................}
here is activity3.xml
android:id="#+id/yourName"
android:text="#string/nomPrenom"
How can I do that ?
First of all your question is not so clear.. But as per given i think you need to fetch a data from edittext ( in main activity ) and then pass it to the third activity .
For This you need to use Intent to pass the data from one activity to other activity.
Example -
nomPrenom = (EditText) findViewById(R.id.nomPrenom);
nomPrenom .getText.toString();
Intent in = new Intent(MainActivity.this, Activity3 .class);
in.putExtra("value",nomPrenom .getText.toString()) ;
In Activity3 receive intent like this :-
String strValue = getIntent().getExtras().getString("value");
yourName = (TextView) findViewById(R.id.yourName);
yourName.setText(strValue);
Use Intent.putExtra("Your text here") and pass it to other activities.
or you can take the string from EditText and make the string Global, public and static and then use it in any other class.
You can do it with many ways ...
You can use shared preference so that you can access it from anywhere. But shared preference will retain your value after your application closed.
Another way pass your value with your activity intent when you are creating your activity like this: intent.putExtra' and get value into your activity viagetExtra`
Another way you can define your variable at global place consider create one class too hold such common variables and from there you can access those variable from anywhere.
All three ways can be changed according to your requirement.
In order to move from MainActivity to Activity3 write the following code
//get your edittext's text here
String text = nomPrenom.getText().toString();
//move to Activity3
Intent intent = new Intent(MainActivity.this, Activity3.class);
//pass the edit text value to Activity3
intent.putExtra("EditTextValue", text);
//start Activity3
startIntent(intent);
In Activity3, to get your edit text value do the following.
//get your edit text value here which is passed in MainActivity
String text = getIntent().getStringExtra("EditTextValue");
The variable "text" is the final output which you are looking for.
So, I'm basically fooling around with some kind of login form, and trying to say a customized hello with the user name on the upcoming activity. The code compiles with no problem but the app crashes as soon as I click on the login button, the login worked successfully before I tried to implement the customized hello, so the problem has to be somewhere in the following code.
Here is where I call the activity:
Intent k = new Intent(this, MainActivity.class);
//Sends login name to activity k
k.putExtra("loginName", login.getText().toString());
//login is the EditText variable name for the login text field
startActivity(k);
Here is where I retrieve the extra data and try to use it as described:
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class ProfileActivity extends Activity {
TextView helloString;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
Bundle extras = getIntent().getExtras();
//Getting the hello text string
helloString = (TextView)findViewById(R.id.textHello);
String loginName = extras.getString("loginName");
helloString.setText("¡Hello, " + loginName + "!");
}
}
It somehow avoids the crash if I put in comment these two lines:
String loginName = extras.getString("loginName");
helloString.setText("¡Hello, " + loginName + "!");
Still, I can't be sure if the problem is really there or not, I thought it could have something to do with the type of data sent from the first activity not matching with the type being retrieved on the second, but still got no clue after trying some stuff around that.
Thanks in advance.
Editing:
I actually found out that I may have something to do with the fact that I am calling to mainActivity.class while the text is being shown in an activity called profileActivity.class, the problem is, profileActivity is being shown as a tab inside the mainActivity so I don't really know how should I approach that.
Editing 2:
So I solved it finally myself, for anyone interested I just sent the data to the MainActivity.class
Intent k = new Intent(this, MainActivity.class);
//Sends login name to activity k
k.putExtra("loginName", login.getText().toString());
startActivity(k);
And, inside the Main Activity, when calling the ProfileActivity to set it up as a tab:
//Profile tab
intent = new Intent(this, ProfileActivity.class);
Bundle extras = getIntent().getExtras();
intent.putExtra("loginName", extras.getString("loginName"));
spec = mTabHost.newTabSpec("home")
.setIndicator("Home", res.getDrawable(R.drawable.profile_icon))
.setContent(intent);
mTabHost.addTab(spec);
Problem solved, thanks for the help everyone anyways.
Your intent is refering to MainACtivity.class and you are trying to fetch the extras in ProfileActivity. Try changing the MainActivity.class to ProfileActivity.class if that is what your flow is.Please cross check your activities flow.
Hope it helps.
Intent k = new Intent(this, ProfileActivity.class);
//Sends login name to activity k
k.putExtra("loginName", login.getText().toString());
//login is the EditText variable name for the login text field
startActivity(k);
Since you stated that you want the data sent to ProfileActivity but need to open MainActivity, you can follow one of the 4 approaches.
NOTE: you should be checking when you use getExtra, that the returned value isn't null, and deal with it accordingly.
send the data first to the MainActivity, and then when you launch ProfileActivity from MainActivity, send the data that was previously passed in.
Put the data in a public static field in ProfileActivity. Public static variables can lead to issues, so I would recommend against this.
Save the data to a sharedPreferences file and read it when you need it in ProfileActivity.
SubClass Application, and you can set / access many variables there. The below was copied from Are static fields in Activity classes guaranteed to outlive a create/destroy cycle?:
public class MyApplication extends Application{
private String thing = null;
public String getThing(){
return thing;
}
public void setThing( String thing ){
this.thing = thing;
}
}
public class MyActivity extends Activity {
private MyApplication app;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app = ((MyApplication)getApplication());
String thing = app.getThing();
}
}
On MainActivity:
String user = this.getIntent().getStringExtra("loginName");
And when you create the tabhost on MainActivity pass the string the same way:
Intent intent = new Intent().setClass(this, ProfileActivity.class);
intent.putExtra("loginName", user);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Initialize a TabSpec for each tab and add it to the TabHost
spec = mTabHost.newTabSpec("profile").setIndicator("Profile").setContent(intent);
mTabHost.addTab(spec);
Something like this.
But if you need the user name across the entire application use a Helper Class to store the user name.
I want to transfer a String value from one class to another class in same package. The second class is called using intent in onclick of a menu item. I used the code
Intent i = new Intent(TouristGuideActivity.this, PointOfInterest.class);
i.putExtra("videoId", videoId);
startActivity(i);
in first class and then in second class,
String address=getIntent().getExtras().getString("videoId");`
But when I click on the menu item, I get a force close. If I remove that put Extra part, it works ine. But in that case I can't send the string. Please help!
Intent intent=new Intent(this,secondclass.class);
intent.putExtra("videoId",videoId);
startActivity(intent);
and in your second class try 2 get these items by doing
Bundle extras = getIntent().getExtras();
if(extras!=null){
String videoId=extras.getString("videoId");
}
just after u define your class i hope dis would help u out 2 get the values on other page
Make sure that you are getting the same type, for example, if you put a String, get a String.
Then try with:
getIntent().getStringExtra("videoId");
I have two activities A and B, and from activity A, I click on a button that opens a dialog box which contains a form consisting of two edit text fields and a button(the button in the dialog box is used to start activity B). So, my question is: how do I pass a string from activity B to activity A, but without closing the dialog box(the string will be used to fill one of the two edit text fields).
You need to create a class to store the variable. In ActivityB use set the value of the variable, the created class stores it and in ActivityA get the value of the variable.
Create a class: GlobalVars.java. In this class put this:
public class GlobalVars extends Application {
private static String var2;
public static String getVar() {
return var2;
}
public static void setVar(String var) {
var2 = var;
}
}
In ActivityB put this line in to the appropriate place:
String something;
GlobalVars.setVar(something);
In ActivityA put this line in to the appropriate place:
String getsomething = GlobalVars.getVar();
And that's it!
It sound like you want to keep dialogbox when activity B returns result. If such case then you can open dialog box onActivityResult:
Activity A
Click on button open dialogbox
start Activity B
return result to Activity A
onActivityResult will call
open dialog box again
Note: Activity A must not be SingleTask, SingleInstance, SingleTop.
Perhaps try using sharedpreferences !?
You can use the broadcast system to send an Intent containing data to another activity.
Search google or stackoverflow there are a lot of tutorials and examples of how to achieve this.
as i understand you want activity a to get notified and fill a field based on some action in the dialog.
what i am suggesting is one way of doing this. the other answers provide also different solutions to the same problem. also you can register an interface with the creation of your dialog which will be called from inside the dialog and do something in the first activity.
I think you need to use Bundle and static global variable and onActivityResult(). If you want to edit client with previous client to new client . Suppose you have "ClientList" Activity and "EditClient" Activity
Write into "EditClient" Activity
Bundle extras = getIntent().getExtras();
if (extras != null)
{
String name = extras.getString(ClientList.KEY_Client);//ClientList.KEY_Client is global static variable of "ClientList" Activity.
if (name != null)
{
nameText.setText(name);//"nameText" is a EditText object represent EditText view
}
}