I am not expert in programming. I have implemented acceleration sensor in Android program.
I have used textview to display acceleration data for x, y & z axis.
Now using this sensor data I want to plot a real time graph. So is it good idea to put the textview in an array list & than plot graph?? Or some one has a better solution.
How to add textview's data into arraylist?
How to plot graph of this real time sensor data??
This is my program.
public class HelloAndroid extends Activity implements SensorEventListener {
private SensorManager sensorManager;
TextView xCoor; // declare X axis object
TextView yCoor; // declare Y axis object
TextView zCoor; // declare Z axis object
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
xCoor=(TextView)findViewById(R.id.xcoor); // create X axis object
yCoor=(TextView)findViewById(R.id.ycoor); // create Y axis object
zCoor=(TextView)findViewById(R.id.zcoor); // create Z axis object
sensorManager=(SensorManager)getSystemService(SENSOR_SERVICE);
// add listener. The listener will be HelloAndroid (this) class
sensorManager.registerListener(this,
sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER),
SensorManager.SENSOR_DELAY_NORMAL);
}
public void onAccuracyChanged(Sensor sensor,int accuracy){
}
public void onSensorChanged(SensorEvent event){
if(event.sensor.getType()==Sensor.TYPE_ACCELEROMETER){
// assign directions
float x=event.values[0];
float y=event.values[1];
float z=event.values[2];
xCoor.setText("X: "+x);
yCoor.setText("Y: "+y);
zCoor.setText("Z: "+z);
}
}
}
You can watch for Changes in your TextView and run your code eg. :
editText.addTextChangedListener(new TextWatcher() {
public void afterTextChanged(Editable s) {
}
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
String[] array = new String[] {TextView.getText().toString() , TextView1.getText().toString()
//Correct me if iam Wrong ... }
}
});
If you just want to put TextView into arrayList
List<TextView> textViews= new ArrayList<TextView>();
textViews.add(TextView1);
textViews.add(TextView2);
textViews.add(TextView3);
Related
so i have been trying to make this BMI calculator in android studio, where your BMI will instantly get calculated when you drag Progress-bar to any value .
To execute this, what i did was-
inside the the Bundle class,
i directly declared 2 variable and assigned one of them to get the data from the Height progress bar and another to get the data from weight progress bar.
and then i wrote the typical code for the calculation and the text setting.
And it did not work
For myself being a very new to this, i really cant find my mistake here.
so as a result what i got was- a still BMI which was the result for the initial value of the progress-bar that i have set for the height and weight.
i am getting a feeling that i made a very silly mistake somewhere that i still can not notice.
Would you be kind enough to point that out?
the Java code that i have used is below, please check it-
The code for the BMI is at the end. I feel the problem is lying there.
here is the screenshot of the app-
screenshot2
screenshot1
here is my code--
public class MainActivity extends AppCompatActivity {
TextView mheighttext,mweighttext2,mbmitext,mfunnymsg;
SeekBar mbar1, mbar2;
RelativeLayout mweightlayout, mlayout3;
ImageView mgincbtn1,mgincbtn2,mgdecbtn1,mgdecbtn2;
String mbmi, bmitext;
int wt= 45;
String wtwt="45";
int ht= 158;
String htht="158";
float rslt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mheighttext=findViewById(R.id.heighttext);
mweighttext2=findViewById(R.id.weighttext2);
mbar1= findViewById(R.id.bar1);
mbar2=findViewById(R.id.bar2);
mgincbtn1=findViewById(R.id.gincbtn1);
mgincbtn2=findViewById(R.id.gincbtn2);
mgdecbtn1=findViewById(R.id.gdecbtn1);
mgdecbtn2=findViewById(R.id.gdecbtn2);
mbmitext=findViewById(R.id.bmitext1);
mfunnymsg=findViewById(R.id.funnymsg);
mbar1.setMax(246);
mbar1.setProgress(160);
mbar1.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
ht=progress;
htht=String.valueOf(ht);
mheighttext.setText(htht);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mbar2.setMax(244);
mbar2.setProgress(50);
mbar2.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
wt=progress;
wtwt=String.valueOf(wt);
mweighttext2.setText(wtwt);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
}
});
mgincbtn1.setOnClickListener(v -> {
ht=ht+1;
if (ht>=0 && ht<247) {
mbar1.setProgress(ht);
htht = String.valueOf(ht);
mheighttext.setText(htht);
}
});
mgdecbtn1.setOnClickListener(v -> {
ht=ht-1;
if (ht>=0 && ht<247) {
mbar1.setProgress(ht);
htht = String.valueOf(ht);
mheighttext.setText(htht);
}
});
mgincbtn2.setOnClickListener(view -> {
wt=wt+1;
if (wt>=0 && wt<244) {
mbar2.setProgress(wt);
wtwt = String.valueOf(wt);
mweighttext2.setText(wtwt);
}
});
mgdecbtn2.setOnClickListener(v -> {
wt=wt-1;
if (wt>=0 && wt<244) {
mbar2.setProgress(wt);
wtwt = String.valueOf(wt);
mweighttext2.setText(wtwt);
}
});
int htt = mbar1.getProgress();
int wtt = mbar2.getProgress();
float httt=htt/100;
rslt= wtt/(httt*httt);
mbmi=Float.toString(rslt);
mbmitext.setText(mbmi);
}
}
}
You would want to put the snippet of code that calculates the BMI (perhaps the last 4 lines of code) in both onProgressChanged() to instantly calculate BMI as height/weight gets changed. Since you're setting global variables for both height and weight, you'll get current values for each of them.
You can also make a function for calculating the BMI like this:
private float calculateBMI(int height, int weight) {
float ht = height / 100;
float result = weight / (ht * ht);
return result;
}
and have this function call in both onProgressChanged() like so (for the weight for example):
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
wt = progress;
mweighttext2.setText(String.valueOf(getBMI(ht, progress)));
}
Move this your code...
int htt = mbar1.getProgress();
int wtt = mbar2.getProgress();
float httt=htt/100;
rslt= wtt/(httt*httt);
mbmi=Float.toString(rslt);
mbmitext.setText(mbmi);
to a method say
public void computeBMI(){
int htt = mbar1.getProgress();
int wtt = mbar2.getProgress();
float httt=htt/100;
rslt= wtt/(httt*httt);
mbmi=Float.toString(rslt);
mbmitext.setText(mbmi);
}
Then add a button in your UI say mCompute. In your oncreate method you can then implement that buttons onClickListener to call computeBMI() above.
That is:
yourButton.setOnclickListener((v)->{
computeBMI();
});
ShadowSpan.java
public class ShadowSpan extends ReplacementSpan {
public ShadowSpan(int color, PointF offset){
}
#Override
public int getSize(#NonNull Paint paint, CharSequence text, int start, int end, #Nullable Paint.FontMetricsInt fm) {
return (int)paint.measureText(text,start,end);
}
#Override
public void draw(#NonNull Canvas canvas, CharSequence text, int start, int end, float x, int top, int y, int bottom, #NonNull Paint paint) {
canvas.drawText(text,start,end,x,y,paint);
}}
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String tx = "sssss3333";
SpannableString sp = new SpannableString(tx);
ShadowSpan ss = new ShadowSpan(Color.GRAY,new PointF(8,8));
AbsoluteSizeSpan as = new AbsoluteSizeSpan(100);
sp.setSpan(ss,0,tx.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
sp.setSpan(as,0,tx.length(), Spanned.SPAN_INCLUSIVE_EXCLUSIVE);
TextView tv = new TextView(this);
tv.setText(sp, TextView.BufferType.SPANNABLE);
tv.measure(0,0);
int h = tv.getMeasuredHeight();
int w = tv.getMeasuredWidth();
FrameLayout fl = (FrameLayout)getWindow().getDecorView().findViewById(android.R.id.content);
fl.addView(tv);
}}
The above is all my code, very simple, but there is a very strange problem.
if I use ReplacementSpan, and if the debug is a real machine, then getMeasuredHeight will only return 0 or the same value, if the debug is virtual Device, then getMeasuredHeight will return the correct value.
However, whether it is a real machine or a virtual device, the value returned by getMeasuredWidth is correct.
This is the build environment:
compileSdkVersion 27
minSdkVersion 15
targetSdkVersion 27
the real machine is sangsumg Galaxy Note 9
Update:
I have some new discoveries, even on virtual devices, if you use API27, there will be problems, API28 is no problem, the real machine API is 28
By looking at the TextLine.java source code, because ReplacementSpan is inherited from MetricAffectingSpan, and Text is processed in the process of processing MetricAffectingSpan and then dealing with CharactorStyle, and when MetricAffectingSpan is ReplacementSpan, then directly draw and continue, the following CharactorStyle is not processed at all
I´m not very good at programming, but I need to build an App where you can type in Datapoints and after clicking a button the datapoint should be added to a graph.
I started with a bar graph and it works, but for some reason the labels are in doubles, even if I type in 1 the bar is between 0.8 an 1.2 on the xaxis and the first 3 datapoints I type in doesn't show up.
Please excuse my bad english (and my bad programming).
public class BalkenActivity extends AppCompatActivity implements View.OnClickListener{
GraphView bargraph;
BarGraphSeries<DataPoint> series;
double xval = 1;
double yval;
TextView texty;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_balken);
Button button1 = findViewById(R.id.addbtn);
button1.setOnClickListener(this);
texty = findViewById(R.id.yvalue);
bargraph = findViewById(R.id.bargraph);
series = new BarGraphSeries<>(getDataPoint());
}
private DataPoint[] getDataPoint() {
DataPoint[] dp = new DataPoint[]
{
new DataPoint(0,0),
};
return dp;
}
public void onClick (View v) {
yval = new Double(texty.getText().toString()).doubleValue();
series.appendData(new DataPoint(xval++,yval),true,100);
bargraph.addSeries(series);
bargraph.getViewport().setScalable(true);
bargraph.getViewport().setMinX(0);
series.setSpacing(50);
series.setDrawValuesOnTop(true);
series.setValuesOnTopColor(Color.BLACK);
}
}
Without either a DefaultLabelFormatter or a GridLabelFormatter, GraphView will automatically format the graphs labels so that is why you are seeing double values.
For AndroidStudio, I am creating a simple conversion app that allows you to convert kilometres to miles and vice versa. I am able to convert it by clicking a button, but how would you do it in real time? For example, as I am typing the number, it converts it right away in a different textbox.
This is the code for my onClick method that I created for my button:
public void onClick(View vw){
EditText value = (EditText)findViewById(R.id.editText);
double total = Double.parseDouble(value.getText().toString()) * 0.621371192;
TextView obj = (TextView)findViewById(R.id.milesDisplay);
obj.setText(Double.toString(total));
obj.setVisibility(View.VISIBLE);
}
You can do this if you're using an EditText:
mEditText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// Every time you hit a number, capture the number and convert it
}
#Override
public void afterTextChanged(Editable s) {
}
});
inside onTextChanged a listener is trigged every time you hit the keyboard. Force it to only accept numbers.
I'm trying to get a character count of an EditText (numberRoom). When user would insert 8 characters button should switch from state Disabled and color 0xBBFFFFFF to state Enabled and color 0xFFFFFFFF.
I've tried few method and I think the best one I've found is that one below. However button has state Enabled and color 0xFFFFFFFF even when input is empty. What's wrong there?
public class Join_room_screen extends Activity {
EditText numberRoom;
Button goToRoom;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.joinroom);
numberRoom = (EditText) findViewById(R.id.roomNumber);
goToRoom = (Button) findViewById(R.id.goToRoom);
TextWatcher watcher = new LocalTextWatcher();
goToRoom.addTextChangedListener(watcher);
updateButtonState();
}
void updateButtonState() {
boolean enabled = checkEditText(numberRoom);
goToRoom.setBackgroundColor(0xFFFFFFFF);
goToRoom.setEnabled(enabled);
}
private boolean checkEditText(EditText edit) {
return ((edit.getText().toString()).length() == 8 );
}
private class LocalTextWatcher implements TextWatcher {
public void afterTextChanged(Editable s) {
updateButtonState();
}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
}
}
However in properties I've
In this function the enabled variable is never used so the background colour and enabled states are always set.
void updateButtonState() {
boolean enabled = checkEditText(numberRoom);
goToRoom.setBackgroundColor(0xFFFFFFFF);
goToRoom.setEnabled(enabled);
}
I would replace with something like
void updateButtonState() {
boolean enabled = checkEditText(numberRoom);
if (enabled) {
goToRoom.setBackgroundColor(0xFFFFFFFF);
goToRoom.setEnabled(enabled);
} else {
//change them back to disabled state
}
}
You have one problem in updateButtonState(), it always sets one color to your button. I see, you have already solved that.
The other problem is that you set TextChangeListener not to an EditText, but somewhy to a Button.
The EditText should be watched.
numberRoom.addTextChangedListener(watcher);
instead of
goToRoom.addTextChangedListener(watcher);