I have stored value in First screen and pass that value and set the value in Edit Text of Second screen. While i am back into first screen and change the value and pass again to second screen and set that value in edit text. Here, it shows previous value only doesn't replace that value.
EditText mobilenumber = (EditText) rootView.findViewById(R.id.mobilenumber);
mobilenumber.setText(mobile);
Above mobile is a variable and I stored the value from first screen and pass it to second screen to set that value in mobilenumber edit text.
1)if you are using activity(first screen and second screen). you can send value using intent.
intent.putExtra("mobile",mobile);
and access it in second screen using
getIntent().getExtras().getString("mobile","defaultvalue");
OR
2) Make mobile as global and static variable in first activity
and in second activity(screen)
in onResume()
setText(firstactivity.mobile);
As you said both the screen are fragment you can share a global variable in activity(if both fragment part of same activity) or
can communicate via passing value in bundle would be safest and
reliable way
Using the Bundle
From your first activity :
public void onCreate(Bundle b){
super.onCreate(b);
String number = "" + R.id.IDInYourFirstActivity;
b.putString("mobilenumber", number);
}
in your next activity
public void onCreate(Bundle bd){
super.onCreate(bd);
String mobileNo = bd.getString("mobilenumber");
EditText mobilenumber = (EditText) rootView.findViewById(R.id.mobilenumber);
mobilenumber.setText(mobileNo);
}
You can use a static variable in second screen. Then in first screen when you change the value then
Second.mobile=your value.
It will change the value accordingly.
Related
I have a string array and I have to display the string array in a edittext box showing one value at a time.
For Instance,
String str = new String{"abc", "def", "ghi"};
I have to show these values in a edittext box such that the value keep changes periodically as soon as a user opens this activity.
Please help me out, thanks in advance.
You can put this string array into parent activity of this activity. And wen you are starting intent of this activity(which contains edittext), put extra values i,e in your case array[i] and then start the activity.
starting intent from parent activity :
Intent i = new Intent(FirstScreen.this, SecondScreen.class);
i.putExtra("STRING_you_NEED", string[i]);
and then get it on your activity that contains editText !
Bundle extras = getIntent().getExtras();
edittext.setText(extras.getString("STRING_you_NEED"))
There are lots of ways to store an index to the string array and retrieve it whenever you want. Depending on the use case, you can try the following,
Use SharedPreferences and use a key-value pair to store the index and retrieve it when you want to show the string in the Activity(Persistent)
Extend your Application(or create a static class) and use a static int variable to store the index(Non-Persistent)
Let's say I have 10 buttons whose ID's are 1,2,...,10. I have an XML file called preferences.xml that contains a checkbox:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="applicationPreference" android:title="#string/config">
<CheckBoxPreference
android:key="checkbox" />
</PreferenceScreen>
My buttons all call the same function which calls for the intent to start the PreferenceActivity.
What I want to do is to call the same model with every button but save the values of the checkbox for each button. Currently, every time I click on a button, it launches the activity, but, for example, the value of my button 1 will be found in the value of my button 5.
Should I use SharedPreferences or something else?
I know it's possible but as I am still unfamiliar with many concepts, I just don't find it.
This is your problem: The PreferenceActivity automatically saves settings for you in the shared preferences of your application. For each setting, you define a key in your preferences.xml. In your case this is
android:key="checkbox"
so the value "checkbox" in your SP gets set to true, when you open your preferences with button 5. When you open the preferences with button 1 again, the PreferencesActivity looks up the SPs, sees that "checkbox" is true and therefore sets the checkbox to checked.
To avoid this issue, like Adrian said, you have to pass information to your PreferenceActivity about what button get clicked. In your PreferenceActivity you have to get a reference to your checkbox preference and add your passed button id to the key.
Preference checkboxPreference = findPreference("checkbox");
checkboxPreference.setKey(checkboxPreference.getKey() + buttonId);
Now there are 10 uniquely named boolean values ("checkbox1","checkbox2",..) saved in your SP for each of your buttons.
have fun
If I understood correctly, you have to tell the PreferenceActivity which button was press. You can achieve this by setting the button id as a parameter when starting the activity.
/*
* This is the common method used by all the buttons after the click
* was done in order to start the PreferenceActivity and pass
* the button id as a parameter
* #param sender - Button which was pressed and will start the
* PreferenceActivity
*/
private void startPreferenceActivity(Button sender)
{
// create the intent
Intent intent=new Intent(context, PreferenceActivity.class);
// add button id as a parameter
intent.putExtra("buttonID", sender.getId());
// start activity
startActivity(intent);
// or you can start it to way for a result
// startActivityForResult(intent, 0);
}
Now, in the PreferenceActivity you have to get the parameter which you have sent:
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// get the extras where the parametrs are stored
Bundle bundle = getIntent().getExtras();
if(bundle.getInt("buttonID") != null)
{
// get id as int with default value as 0
int id= intent.getInt("buttonID", 0);
}
if(bundle.getString("buttonID") != null)
{
// get id as string with default value as ""
string id= intent.getString("buttonID", "");
}
// other code here ...
}
Hope this will help you.
Okay, so i just created an activity that queries post titles from a database and post it on a view using an adapter.
I would like to implement the following:
Once I click the title, I should be taken to a separate page/activity that displays the content section of that title. Similar to a blog app that goes to a blog post when we click on the title.
I'm going through a block right now. How does one get this done?
In Intent you can use add extra e.g id from each:
First: and in activity you can change it Dynamically setContentView(xxx);
xxx: change depend on extra you add previously
Second: you can create standard layout and depend on extra you can fill
e.g id==1 ===> fill textview to "zzzz"
id==2 ===> fill textview to "mmmm"
... and so on
Well actually you're not dynamically creating an activity. The content of your second (detail) activity just depends on the user's input on the first activity.
As soon as the user click's on a title on your first activity, you create a new intent and append the post's to the intent's extras.
When you startup your second activity using this intent, you will be able to get the post's id from the extras and load the content from your database.
FirstActivity
Intent i = new Intent(FirstActivity.this, SecondActivity.class);
i.putExtra("POST_ID", <post_id>);
startActivity(i);
SecondActivity
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
Bundle bundle = getIntent().getExtras();
String postId;
if(bundle != null) {
// process your id.
postId = bundle.getString("POST_ID");
}
}
I am passing a value for username in one activity through XML. I want to recall the value in the next screen, like, 'Hello/Welcome, '. How is it possible using GetExtra and PutExtra?
Pls. find the below code, which I have tried for the same.
For storing the value of username in one activity:
Intent myActivity2 = new Intent(this,Activity1.class);
myActivity2.putExtra("nickname", R.id.mynickname3);
startActivity(myActivity2);
For recalling the value in other activity:
Bundle extras = getIntent().getExtras();
String val = extras.getString("nickname");
TextView tv=new TextView(this);
tv.setText(val);
If you want to reuse a username, use sharedPreference.
It is not a good idea to pass a userName to another activity then use that.Field like userName may need to use again and again.
In the first activity, you are passing in an integer value (R.id.mynickname3). Instead you need to give it a String. Use this instead:
myActivity2.putExtra("nickname", getString(R.id.mynickname3));
As a side note, you are trying to get a Bundle unnecessarily. Try this:
String val = getIntent().getStringExtra("nickname");
TextView tv = new TextView(this);
if (val != null)
{
tv.setText(val);
}
You're nearly right.
It is not enough to just pass the id of the EditText. You have to use below code -
((EditText)findViewById(R.id.mynickname3)).getText().toString();
int putExtra. Everything else should be fine.
UPDATE: Don't forget to set your TextView as contentView in OnCreate via:
setContentView(..)
Just a silly mistake in your code:
myActivity2.putExtra("nickname", R.id.mynickname3);
Where you have written ID of the EditText (nickname), instead it should be as a:
txtNickName = (EditText) findViewById (R.id.mynickname3);
myActivity2.putExtra("nickname", txtNickName.getText().toString());
i have a first screen in my app that the user enter his name in an editText.Then, when the user presses the button "ok",the app is going to a new activity.I would like to get the text from the first activity and move it to the second.For example,if the user fills the edittext with the name "kostas",when he goes to the second activity,to appear a textView writing "Hello kostas"..
i have tried to use putExtra, but i m thinking that i m doing it in a wrong way.In the first class i m using this
Button ok = (Button) findViewById(R.id.ok);
ok.setOnClickListener(new View.OnClickListener() {
public void onClick (View view) {
Intent newActivity = new Intent(view.getContext(),home.class);
newActivity.putExtra("NAME", name);
startActivity(newActivity);
}
});
in order to move the name into my next activity "home".but then i dont know how to get it there...
and then in my new "home" activity i m using this:
Bundle extras = getIntent().getExtras();
String Name = extras.getString("NAME");
First, in your onClick Handler I would extract the text from the editText box and place the text into the intent.
Second, to debug the problem I would enable LogCat viewing
Third, I would log the actual values being passed (in Act 1) and extracted (
in Act 2) using a call such as:
Log.d(TAG,name);
Hope that helps,
JAL