set textview from antoher activity in android - java

I'm having a problem with my app.
So, I'm trying to save some data into another activity, from mainactivity inputs. And I think I found the error but can't fix it.
I believe my app is searching for textview reference in wrong activity, in main. The problem is that I want to change another activity's textview. Is there anybody that could explain for me how to do that or give me some advice here?
public void saveEvent(View saveView){
Saved save=new Saved();
save.SaveNow(day,tvResultBefore.getText().toString(), tvResultAfter.getText().toString());
// Save class
public class Saved extends MainActivity {
TextView tvMonthResult;
TextView tvResultBef;
TextView tvResultAft;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_save);
tvMonthResult=findViewById(R.id.tvMonthResult);
tvResultBef=findViewById(R.id.tvResultBef);
tvResultAft=findViewById(R.id.tvResultAft);
}
public void SaveNow(String day, String resBef, String resAft){
tvMonthResult.setText(month);
tvResultBef.setText(resBef);
tvResultAft.setText(resAft);
}

get values from MainActivity like this ::
MainActivity.class
String data = editText.getText().toString();
Intent intent=new Intent(context,AnotherActivity.class);
intent.putExtra("DATA",data);
startActivity(intent);
Now, in AnotherActivty get value and set it to textview :
AnotherActivity.class
if(getIntent!=null)
String data = getIntent().getStringExtra("DATA");
if(data!=null)
textView.setText(data);
it solved your problem.

Related

The value of QR Code Scanner cannot show on new activity

I am trying to make a simple QR Code scanner and run well if the result shows in the MainActivity. But when I'm trying to generate new Activity as a result, it can't show. Would you like to help me with this problem? Thank you.
Here is the result of the scanner code in the scanner.java:
#Override
public void handleResult(Result rawResult) {
startActivity(new Intent(getApplicationContext(),ResultActivity.class));
ResultActivity.scanText2.setText(rawResult.getText());
onBackPressed();
}
And here is the code of my new Activity for showing the result:
public class ResultActivity extends AppCompatActivity {
public static TextView scanText2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_patient);
scanText2 = (TextView) findViewById(R.id.scanText2);
}
}
There are a lot of issues with this:
You are launching PatientActivity, not ResultActivity in handleResult()
You are trying to set the value of a TextView in another Activity with this code:
ResultActivity.scanText2.setText(rawResult.getText());
This is an absolute no-no. Pass the data to ResultActivity as an Intent extra when you launch it. You cannot access the views of another Activity like that.
You expect that ResultActivity.scanText2 will contain a reference to a TextView, but at this point it will only contain null. The reason is that it takes some time for ResultActivity to actually start and call its onCreate(). You have not accounted for this timing in your code. To solve this problem, just see (2) above.
Also, you should have gotten a bunch of crashes with useful messages in your logcat. In the future please look there to solve problems and/or post the messages with stacktrace in your Stackoverflow questions.

onSavedInstanceState doesn't save my Instance

Hope you're all doing fine in these tough times.
I have 2 activities, A and B.
A has a button that enables an intent to go from A to B. B itself has a Dialog window whom's cancel button enables an Intent to go from B to A.
I'd like to save some data from A (2 EditTexts' content) so when I come back to A I don't have to retype those EditTexts.
So after looking up the documentation and a bit of StackOverflow, I decided to go with the onSavedInstance method but I'm wondering whether the intent transition destroys the Activity and hence the savedInstance as well...
Here's my simplified code - Activity A :
public class A extends AppCompatActivity {
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_match);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
if (savedInstanceState != null) {
EditText firstPlayer = findViewById(R.id.firstPlayerName);
EditText secondPlayer = findViewById(R.id.secondPlayerName);
String firstPlayerName = savedInstanceState.getString("firstPlayerName");
String secondPlayerName = savedInstanceState.getString("secondPlayerName");
firstPlayer.setText(firstPlayerName);
secondPlayer.setText(secondPlayerName);
}
}
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
EditText firstPlayer = findViewById(R.id.firstPlayerName);
EditText secondPlayer = findViewById(R.id.secondPlayerName);
String firstPlayerName = String.valueOf(firstPlayer.getText());
String secondPlayerName = String.valueOf(secondPlayer.getText());
savedInstanceState.putString("firstPlayerName", firstPlayerName);
savedInstanceState.putString("secondPlayerName", secondPlayerName);
}
}
When I debugged it, I did notice it passed thru onSavedInstance and it did save the values, but once in onCreate, the savedInstanceState is null.
Any advice?
Thanks in advance & take care people.
Fares.
You note that the "cancel button enables an Intent to go from B to A.". This sounds like you're creating a new instance of Activity A (so your activity stack goes A, B, A), and the instance state is specific to the individual instance.
Instead, you should return to A by simply calling finish() on B. This will return to the original instance of A, which will have your data.

How can I let another class know that a button was clicked (Android Studio)

Below is the code which I'm using to return a number which should be 1 when the button is clicked. However when I try to get that number from another class, it always stays 0.
As you might recognize, I tried to change the number in the onClickListener and returned it below.
I also tried to use the onPause command so that it will return the number onPause but it still doesn't work.
public class MainActivity extends Activity {
public int number;
Button btnAngled;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
btnAngled = (Button) findViewById(R.id.btnAngled);
final Intent intent = new Intent(this, angledForeheadActivity.class);
btnAngled.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
number = 1;
startActivity(intent);
}
});
}
#Override
protected void onPause() {
super.onPause();
}
public int getNumber() {
return number;
}
}
I try to get the code in another class with:
MainActivity a = new MainActivity();
int number = a.getNumber();
Sorry for the noob question..
declare the variable as static variable. Then you can simply obtain the result you want since there is only one copy of that variable. If you want to pass the value using intent, you can call putExtra() of intent to carry information to another activity.
Intent reference page
What you actually want is getting the number from another Class. Don't mix the job with button click together. You should setup the concept of model to store data and seperate UI and data, UI just change/get the data.
I suggest you either of the two ways
Store the number in some global model, then you can get the number from another Class.
User Android Broadcast to transfer the data
Use static variable in Activity is not a good idea, it may cause memroy leak, though it can solve your problem.

Using buttons on android - newbie

I am a newbie to android development, trying to get buttons working. every time i use this code below, the error message "unfortunately the app has stopped". but when i remove the code the app runs but obviously the buttons do nothing. here is the code ive tried
public class MyActivity extends Activity {
/**
* Called when the activity is first created.
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button1 = (Button) findViewById(R.id.ExerciseButton);
button1.setOnClickListener (new View.OnClickListener(){
public void onClick(View v) {
setContentView(R.layout.exercises);
}
});
}
}
anybody able to help me out there? thanks
Don't try to load another View in the current activity. Navigate to a new ExercisesActivity.
Use:
public void onClick(View v) {
Intent intent = new Intent(ExercisesActivity.this, WcActivity.class);
startActivity(intent);
}
You can't call setContentView anymore after the view has loaded (which it obviously has to receive button clicks). Use a different approach, like showing and hiding views or using a ViewFlipper (see Calling setContentView() multiple times), using fragments (see Fragments) or starting a new activity.
Well, from your code, I see a couple of things:
I am usually familiar to using the onClickListener of the Button class if I want to use it for a button. Makes sense, doesn't it?
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Do stuff here
}
Second thing:
Start a new Activity (if that is what you want) by using an Intent:
Intent myIntent = new Intent(this, exercises.class);
startActivity(myIntent);
You CAN absolutaly call setContentView on any event, even after the view has loaded
I tried your code in a demo project and it is working fine. So, i think the error will be some where in your layout.(Let me know more if you need more help on this)

TextView setText from another class

My problem is that i've a tabhost with two tabs.
The first is a listview and the second only have two textviews.
What i want is when i click on an item in the listview on tab one, the array position id (int) should be sent to tab/class two, where there is an array who fetches a text by the position id that was sent.
The switching of tabs is done, but i fail everytime i try to send the position-id.
This is how i send it:
TAB ONE:
//This function is called when i click a listitem
public void setDetails(String Text, long arr_id){
Detail detail = new Detail();
detail.setDetail(); // right now i don't send the parameters because it wont even work with or without it.
}
TAB TWO:
TextView descr;
TextView title;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail);
descr = (TextView)findViewById(R.id.desc);
title = (TextView)findViewById(R.id.title);
}
public void setDetailText(String Text){
descr.setText(Text);
}
public void setDetailTitle(String Text){
title.setText(Text);
}
public void setDetail(){
this.setDetailTitle("example text");
this.setDetailText("example text2");
}
I want to set the text on tab two BEFORE it switch to tab two.
This works if i use SetDetail() and setDetailTitle() in the same tab/class, but not in another.
I've googled my ass off, please help me
i do this in my code using getParent() or getActivity() methods in my TabActivity and inner Activitys, cause if we use a TabActivity (wich subclass the ActivityGroup class) we can obtain the 'TextActivity' using ActivityManager and obtain the activity instance, so, here we can call the setDetail() method, and this will execute before the Activity is showed,
in your ListActivity do something like this
((MyTextActivity)((MyTabActivity)getParent()).getLocalActivityManager().getActivity(ACTIVITY_ID)).setDetail();
this works only if in you start the childs activity within your TabActivity with:
intent = new Intent(ACTIVITY_ID).setClass(this, MyMapActivity.class);
getLocalActivityManager().startActivity(ACTIVITY_ID, intent);

Categories