First thing; I'm pretty much brand new to all this.
I've built a basic calculator but I lose the data when switching orientation (such as typing the number 9 then it going blank when switched to landscape).
I've already looked and found this:
How to use onSavedInstanceState example please
, but I'm unsure how to implement it into my code.
public class MainActivity extends AppCompatActivity {
private TextView _screen;
private String display = "";
private String currentOperator = "";
private String result = "";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
_screen = (TextView)findViewById(R.id.textView);
_screen.setText(display);
}
private void updateScreen(){
_screen.setText(display);
}
Can anyone help? An example using my piece would be perfect. Thanks
Related
i am doing project on ecg monitoring android app so i need to get real time data from ubidots, i manage to get latest variable value when i run app but not the all new values which will be updated continuously.So, i need help to get real time data in my app as a result my app should get data when it is updated in ubidots variable .
here is my code for getting variable:
public class MainActivity extends Activity {
private TextView ecgLevel;
String variableValue;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ecgLevel = (TextView) findViewById(R.id.ecg);
ApiUbidots getApi = new ApiUbidots();
getApi.execute();
}
class ApiUbidots extends AsyncTask<Integer, Void, Value[]> {
private final String API_KEY = “";
private final String VARIABLE_ID = "”;
#Override
protected Value[] doInBackground(Integer... params) {
ApiClient apiClient = new ApiClient(API_KEY);
Variable ecgLevel = apiClient.getVariable(VARIABLE_ID);
Value[] variableValues = ecgLevel.getValues();
return variableValues;
}
#Override
protected void onPostExecute(Value[] variableValues) {
double varValue = variableValues[0].getValue();
variableValue = (String.valueOf(varValue));
ecgLevel.setText(variableValue);
}
}
}```
Use Retrofit for hit the API after you get latest variable
How do i set the text of a TextView to a value of an integer from another class.
the main activity where i want the text set below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
TextView counterText = (TextView) findViewById(R.id.counter);
counterText.setText();
}
the class below
public class Ship implements Serializable {
private static int counter = 0;
public int getCounter()
{
return counter;
}
}
For a starter this should help
Ship s=new Ship();
int i=s.getCounter();
String value=String.valueOf(i);
counterText.setText(value);
Use String.valueOf to show Integer in TextView,as:
counterText.setText(String.valueOf(new Ship().getCounter()));
As far as i know you cannot access the UI component of 2nd activity while being in first activity. So the best way is to pass the value through putExtra method of intents and then set it to textview in the onCreate of 2nd activity by getting it. Here is link which shows this method: http://mobileorchard.com/android-app-development-using-intents-to-pass-data-and-return-results-between-activities/
I'm implementing a timer inside of a foreach/for loop in android. There is a TextField or TextView (I'm agnostic as to which to use) that is populated every 30ms with the next string from a string array:
String[] faces = retrieve_pattern().split("\"");
I need help implementing this.
EDIT:
Boris, your example works, but it prints out all the strings, but I need to show just one string, and in "some time" delete it and show the following one until I reach the end. Sort of a text animation in TextField.
I've tried TimerTask, but couldn't make it work though.
I think the most appropriate way to do this is using a Handler:
public class MyActivity extends Activity {
private String [] mSplits;
private int mSplitIndex;
private Handler updater = new Handler() {
#Override
public void handleMessage(Message msg) {
TextView textView = (TextView)MyActivity.this.findViewById(R.id.text_view);
String text = mSplits[mSplitIndex++];
textView.setText(text);
if (mSplitIndex < mSplits.length) {
sendEmptyMessageDelayed(0, 30);
}
}
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_split);
updater.sendEmptyMessageDelayed(0, 0));
}
}
I am writing by heart so if you encounter any problems write back.
Assume that i have a activity class named MainActivity.java. But this activity has about 3000 lines code for example.
I want to seperate code parts of this file to an external java file(class) named NecessaryThings.java. But if i run my project on emulator it stops itself after this operation.
Is there a way to seperate some lines of this activity?
I wrote mini example for better..
Also what do you think about;
Using this method is beneficial or harmful in terms of performance?
This is my MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//I want to call these lines from NecessaryThings.java
TextView genderResult = (TextView) findViewById(R.id.genderText);
genderResult.setText("Cinsiyet:");
TextView calorieResult = (TextView) findViewById(R.id.remainCalorie);
String getGenderSTR = getIntent().getStringExtra("GENDER");
genderResult.setText(getGenderSTR);
String calorieResultSTR = getIntent().getStringExtra("CALORIECHOOSED");
calorieResult.setText(calorieResultSTR);
/*
.....
.....
*/
}
Aftet I take above code, then I want to store it in NecessaryThings.java
like this..
//All necessary imports here. There is no problem about those.
public class NecessaryThings extends Activity {
public void myPersonalMethod() {
TextView genderResult = (TextView) findViewById(R.id.genderText);
genderResult.setText("Cinsiyet:");
TextView calorieResult = (TextView) findViewById(R.id.remainCalorie);
String getGenderSTR = getIntent().getStringExtra("GENDER");
genderResult.setText(getGenderSTR);
String calorieResultSTR = getIntent().getStringExtra("CALORIECHOOSED");
calorieResult.setText(calorieResultSTR);
}
}
If I rearrange my MainActivity.java It will be like this...
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
NecessaryThings showMyMethod = new NecassaryThings();
showMyMethod.myPersonalMethod();
/*
the rest of the codes...
*/
}
But it is not working if I seperate code. Why and How can I do it?
public class MainActivity extends NecessaryThings {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
myPersonalMethod();
}
NecessaryThings extends Activity so MainActivity no long needs to extend from Activity, it should extend from NecessaryThings. One thing I need to point out is that super.onCreate(savedInstanceState); will call the onCreate(); from NecessaryThings. Since my myPersonalMethod(); is from super class, you can just call it.
All activities are regular Java classes and you can - of course, have many non-UI classes like Application, you can have helpers etc. Looking into your question, I would like to tell you that the Activity doesn't have user defined constructor and can be created only indirectly by calling startActivity method, but in other aspects it is a common Java class.
Hence, what you'll have to do is, let your NecessaryThings.java be a normal class, to which you can pass the context from your MainActivity and do all that is required.
Hope this helps.
I need get te value from checkbox and show in other activity and display in textview.
This is the code FormSupervisar.
public class FormSupervisar extends Activity {
CheckBox Si;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.formsupervisar);
Si=(CheckBox)findViewById(R.id.chBoxSi);
}
public void btnEnviar(View view){
if (Si.isChecked()){
Intent i = new Intent(this,FormBotonSi.class);
i.putExtra("Si",Si.getText().toString().trim());
startActivity(i);
}
}
The activity when i call the string and show the String in a TextView
public class FormBotonSi extends Activity {
private String ,Si ;
private TextView tvTipoInspeccion;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.formbotonsi);
Bundle bn1 = getIntent().getExtras();
Si = bn1.getString(Si);
tvTipoInspeccion.setText(Si.toString());
}
}
Try it this way.....
public class FormBotonSi extends Activity {
......
......
String Si = getIntent().getExtras().getString("Si"); // its "Si" instead of Si
}
One more thing... While you code in Java you must follow the Camel case. Field and Methods always starts with small letter whereas Class, Interface, Enums, Constructors starts with Capital letters.
use
Si = bn1.getString("Si");
instead of
Si = bn1.getString(Si);
for getting value in second activity because you are setting "Si" as key in first activity(in FormSupervisar)
OR
You can initialize Si String as:
private String Si="Si";
Si = bn1.getString(Si);