How to open another activity with a pre-written EditText? - android

I have an activity which consists of an EditText. The problem is I want to open this activity with a pre-written EditText.
Here is the example:
I want when I open this activity from the MainActivity, the text of EditText will always be set to "Tùng".

very very simple just put it in your XML
<EditText
android:id="#+id/edt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tùng"/>
or programattically you can set it oncreate of activity as below
edt.setText("Tùng");

Use editText.setText("Text you want to show"); in your activity's onCreate/onStart/onResume method (depending on the expected behavior). Also make sure you first have a reference to your editText by calling findViewById(R.id.yourEditTextId) before calling setText to this element.

As far as I understand you need to transfer data from one activity to antoher. You should use Intents:
Intent intent = new Intent(MainActivity.this, YourOtherActivity.class);
intent.putExtra("TUNG_ID", "Tung");
startActivity(intent);
And on the other activity. In onCreate method:
String tungString = getIntent().getStringExtra("TUNG_ID");
Log.d("ApplicationTag", tungString); //it's gonna print "Tung"
EditText et = findViewById(R.id.youredittext); //find your edittext to write text
et.setText(tungString); //This will populate received string into edittext

Related

How do I pass data from an EditText in one Activity to a TextView in another activity without switching to the second activity?

I want to send a string variable that is entered into an EditText on ActivityOne to pass the data and be displayed as a TextView on ActivityThree. However, I am having problems as the only solutions to do this that I can find cause the activity to switch to ActivityThree while doing this. I want to avoid this or maybe even send the data to ActivityThree and switch to ActivityTwo all on the click of a button. Any help or redirection to a current solution would be greatly appreciated.
If you want to send data from ActivityOne to ActivityThree by avoiding ActivityTwo then save that data in a static variable then use that variable in ActivityThree to set TextView data.
You can simply do this using SharedPreferences.
Setting EditText values in Preference from ActivityOne:
// EditText
EditText editText = (EditText) findViewById(R.id.editText);
SharedPreferences.Editor editor = getSharedPreferences("Your_Preference_Name", MODE_PRIVATE).edit();
editor.putString("KEY_VALUE", editText.getText().toString());
editor.commit();
In ActivityThree, retrieve value from Preference:
SharedPreferences prefs = getSharedPreferences("Your_Preference_Name", MODE_PRIVATE);
String editTextValue = prefs.getString("KEY_VALUE", null);
// TextView
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText(editTextValue);
Hope this will help~
Please use a global variable in Application class and set it's value in ActivityOne and read the same value from ActivityThree.Global variables are available to entire project Activities.
The best way is using input extra. Do this in Activity one
Intent i = new Intent(ActivityOne.this, ActivityThree.class);
i.putExtra("label", "label_text");
startActivity(i);
Then receive the string in Activity three as such:
EditText input = //intialize it in OnCreate
Intent intent = getIntent();
String data = intent.getExtras().getString("label");
input.setText(data);
There are so many ways to do this.. But it heavily depends on what you plan to do with data and your situation..
If you want to display the data in activity three than you can make data persist and later show it when activity three is created or resumed, now:
1- If you want to display the data in activity three and you want the value to persist only in current session you can use a Global Variable or even Static one, if you define the desired value as a static variable inside your activity three than you can easily access it and use it without any need for the activity to be even created:
public ActivityThree extends Activity {
public static String myValue;
2- If you want to display the data in activity three and you want data to persist even if the app is closed you can use SharedPreferences as described here:
https://developer.android.com/training/basics/data-storage/shared-preferences.html
3- If you want to run a background task in activity three on value getting determined you can use LocalBroadcastManager:
https://www.intertech.com/Blog/using-localbroadcastmanager-in-service-to-activity-communications/

custom listview empty edittext and checkbox

I have a custom array adapter which has a imageview, 2 textviews an edittext and a checkbox. I need to get the text from my textviews and my edittext then send that data to another activitya textview when the checkbox is checked.
How can I set up a method to check if the edittext is empty as I need the checkbox only to trigger when there is a value in the edittext.
How can I get the data sent through to another activity.
Thanks
Take your EditText variable and call getText() on that variable to check if it is empty.
E.g.
if (myEditText.getText().toString() == "") {
// trigger checkbox
}
To pass data from one activity to another you use intents. See here: How do I pass data between Activities in Android application?

Modify layout when other activity is called

I want to refresh a textview when next activity is called:
TextView tv = (TextView)v.findViewById(R.id.text_status);
if(tv != null){
tv.setText(getResources().getString(R.string.up_to_date));
tv.setTextColor(Color.BLACK);
}
v.setBackgroundColor(Color.TRANSPARENT);
but I want to refresh the layout once the next activity is called. Actually i can se how text view changes after the next activity shows up. What I want to do is to modifiy textview once the other activity is called and see the new text in textview when I click back button.
Is there any way to do that?
You can achieve this by using Activity's onactivityresult Method. Pass your new text through bundles. Get that intent in onactivityresult Method, and set that text to Your textview. See below links for more clarifications : -
http://developer.android.com/training/basics/intents/result.html
http://developer.android.com/reference/android/app/Activity.html#onActivityResult(int, int, android.content.Intent)

how to make a button redirect to another xml page

I'm making a button in xml, like this:
<Button
android:id="#+id/buttondp"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/thisisstringtext" />
and I want it to direct it to another page coded in xml. Can anyone help me out?
Make another activity and use
setContentView(R.layout.your_other_layout);
inside of it.
Then in the onClickListener for your button put this:
Intent i = new Intent(YourActivity.this, YourOtherActivity.class);
startActivity(i);
You can add the onclick listener to your button, so: android:onclick="method_in_your_activity".
In your activity added the method (method_in_your_activity) and add startActivity(NewActivity).
If you dynamically want to change the Content of your activity you can always call setContentView(my_layout) and change the content. However; its best practice to use another Activity.
You can use different activities for different layout. But if you want to use same activity for different layout then you should go for ViewFlipper . You can also get some animation when switching from one view to another. Tutorial regarding the same can be found here.
using this code in java file you will click button to redirect next page
public void onClick(View v)
{
Intent ia=new Intent(getApplicationContext(),second.class);
startActivity(ia);
}
In your button XML, add :
android:onCLick="myRedirectFunction"
In your MyMainActivity.java, add function named myRedirectFunction and inside that function :
Intent homepage = new Intent(MyMainActivity.this, MySubActivity.class);
startActivity(homepage);

Android Activity

I want to send the Button View to another activity, eg.
Button bttn=(Button )findViewById(R.id.bttn)
startActivity(new Intent(this,Account.class))
by using bundles we can send values from 1 Activity to another activity,same way how i can send view to another activity..
You shouldn't send views between activities. Instead, create a separate button in the second activity and send only the necessary information.
It's simple..
Create a method like this in original activity:
ex:
public Button getButton(){
return bttn;
}
and declare buttonview as static.
By declaring it static you can access this view in another activity.
Call this method from another activity and get it and use it.

Categories

Resources