Modify layout when other activity is called - android

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)

Related

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

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

Android, accesing a textview from a surfaceview

I have an activity, this activity name is Game, it's xml file is composed by two views control, first of them it's a TextView called Texto and the second one is a SurfaceView created by me called Juego.
The Juego view has an onTouchListener event, and i want to send a text to the control called Texto everytime the user clicks on the control Juego.
I have all the "structure" created but i can't "communicate" from Juego to Texto, every thing i try i get an error.
Thx for help in advance,
Create a listener such as
onViewClick(String text). Create instance of this on your SurfaceView and in constructor get this listener as parameter. Now call this listener wherever you want to update the textView.
In you activity class, implement onViewClick listener and in method do:
public void onViewClick(String text){
TextTo.setText(text);
}

How can I setText() (TextView) when the TextView is in another layout?

My app has 2 layouts (main layout) and (preference (prefs) layout).
When the MainActivity loads, I set setContentView(R.layout.main); - main layout
I need to then set text for a TextView in the preference layout, but it never gets set.
LayoutInflater factory = getLayoutInflater();
View inflate = factory.inflate(R.layout.prefs, null);
TextView eSerial = (TextView) inflate.findViewById(R.id.editTextSerial);
mSerial = "Test";
eSerial.setText(mSerial);
The way I get to the preference page is with a menu and then the page loads up with no change to TextView
I have searched and not found an answer yet.
Please help.
Thank you.
When the menu kicks off your prefs activity, you can populate the view with the values. user1853479 points out one way of doing this, which is to add the values to an intent. Assuming you want to store these prefs for future runs, you can also set any that for that specific run and save them in your local store. Another method is to create a singleton to store your settings, load it when your app starts, modify and save as needed, and access it from any of your activities.
Short answer: You can't do this.
Long answer:
If you are launching the preference page yourself, you must be creating an Intent to do so. Call putExtra() to store your text inside that intent. In your PreferenceActivity, call getIntent().getStringExtra() to get the text, then put it in your TextView.

SetText() from another Activity

I have an EditText in my MainActivity. When the user clicks the NewFile activity the layout for this activity is transparent so the EditText can still be seen. I want to be able to update the EditText in the background from the NewFile activity. This is what I've tried, which results in a NullPointerException. I understand why this didn't work but what can I do instead to get the results I'm looking for.
NewFile.java
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.newfile);
newet = (EditTextLineNumbers) findViewById(R.id.ide);
newet.setText("Testing");
}
Your background EditText is not from the layout currently set for the user, thus you are getting the exception.
Regarding updating the UI, 2 things you might need.
Pass the handler of the background activity to the new one
Make the new one as a dialog rather than a transparent activity.
Use the handler to send notifications and update the UI accordingly
How did you think it will work..? How do you access the view which is not in your activity layout..? To say it is not possible...
You can get the EditText of your previous layout by using the layout inflatter service to your layout and from that your view
In the NewFile activity, have an edit text which overlays your transparent editTextView(may be have a transparent BG for this textview which might give illusion that its in background), now update this editTextView
and
once you go back, take data from this edittext(using setActivityForResult and those mechanisms) and fill in the previous activity.

How to refresh Activity

Actually in my app i have a button on an listView..now on click of that button i have done some changes..so when i move to previous activity after than this changes should appear on that activity..but in my case the changes occur but not appears after i exit from the activity where my listView Button is present..so how can i do that so that my changes occur immediately after i exit from my first activity..code i have wrritten:
code for ListView Button Onclick:
public boolean stopCycleStage(View v)
{
Button butStop=(Button) findViewById(R.id.butStop);
TextView setStopTxtViewTitle =(TextView)findViewById(R.id.setStopTxtViewTitle);
Date currentDate=new Date();
int iStopStartCount = CycleManager.getSingletonObject().getStopStartCount();
Date dtStopDate = currentDate;
CycleManager.getSingletonObject().setStopStartDate(dtStopDate, iStopStartCount);
Date dtStart = CycleManager.getSingletonObject().getStartDate();
if (dtStopDate.getTime() == dtStart.getTime())
CycleManager.getSingletonObject().removeHistoryDate(dtStart);
butStop.setBackgroundResource(R.drawable.settings_but_disabled);
setStopTxtViewTitle.setTextColor(Color.parseColor("#808080"));
return true;
}
In your first activity, you should refresh the view in the onResume function rather than just in the onStart or onCreate.
Refer to the activity documentation to see the lifecycle of an activity
PS: this is just a guess because you have not given enough code to show how you load the data in your 1st acitvity.
Id use onActivityResult() in the waiting activity, and in there I would redraw the elements that should have changed when you clicked your button in the child activity. In any case, onActivityResult() is the correct way to go unless you are not waiting for any data from child activity, then I'd use onResume().
See http://developer.android.com/reference/android/app/Activity.html#StartingActivities for information on what you should use in your particular situation (you didn't exactly give much information :))

Categories

Resources