I would like to set the size and color of the text at random during onCreate method
Here is my code:
private TextView start;
private boolean isClicked;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (TextView) findViewById(R.id.tvStart);
isclick = false;
Random r = new Random();
while (isclick = false)
{
start.setTextSize(r.nextInt(50));
start.setTextColor(Color.rgb(r.nextInt(256), r.nextInt(256),
r.nextInt(256)));
}
}
This code of mine doesn't work.
During onCreate I want the text size and color continuously and randomly changing.
It works for me :( let me know is there is a problem )
private boolean isclick;
Handler handler ;
private TextView start;
private boolean isClicked;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
start = (TextView) findViewById(R.id.tvStart);
handler = new Handler();
isclick = false;
new Thread(new Runnable() {
#Override
public void run() {
while (isclick == false)
{
handler.post(new Runnable() {
#Override
public void run() {
Random r = new Random();
start.setTextSize(r.nextInt(50));
start.setTextColor(Color.rgb(r.nextInt(256), r.nextInt(256),
r.nextInt(256)));
}
});
Log.w("DEBUG","Text View value : "+ start.getText().toString());
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}).start();
}
you need to use a handler sins your are modifying UI widgets
this article may help you .
Here is some code that works on a TextView called x
//create random value between 0 and 70
int random = (int)Math.ceil(Math.random()*70);
x.setTextSize((float)random);
int red = (int)Math.ceil(Math.random()*255);
int green = (int)Math.ceil(Math.random()*255);
int blue = (int)Math.ceil(Math.random()*255);
Color randomcolor = new Color();
if (red < 16){
hexred = "0" + Integer.toHexString(red);
}else {
hexred = Integer.toHexString(red);
}
if (green < 16){
hexgreen = "0" + Integer.toHexString(green);
}else {
hexgreen = Integer.toHexString(green);
}
if (blue < 16){
hexblue = "0" + Integer.toHexString(blue);
}else {
hexblue = Integer.toHexString(blue);
}
String color = "#" + hexred + hexgreen + hexblue;
x.setTextColor(randomcolor.parseColor(color));
Continuasly changing is not recommended however. If you want to make it continuasly change color, chance is that the update of the color and size are to slow that nothing is displayed. Also it might happen that the entire XML layout is not loaded because of the calculations on the xml.
if you want your text randomly changing continously, use thread.
put your "while" action. inside run method, and give some delay on it.
I think its because of a missing = in the isclick = false if clause. It should be isclick==false.
Related
I am making an app which consists of an activity and a service. By pressing a button the service is started, it collects data in the background from a sensor and classifies it and outputs a string. I want to display the string in a textView. Right now I can see in the log that the variable is updated 2 times every second, but when I try and update the textView from the service class nothing is happening unless I press the button, whenever I press the button, the string is displayed in the textView.
What is the easiest solution here? I tried to make the textView static and it still can't update it. Can you make it so that the view is updated automatically every second? Can I add a listener somehow? Since I am not very experienced I would like an easy solution that does not have to be a "good" one.
Here is my code
Activity:
public class CollectorActivity extends Activity {
private enum State {
IDLE, COLLECTING, TRAINING, CLASSIFYING
};
private final String[] mLabels = { Globals.CLASS_LABEL_STANDING,
Globals.CLASS_LABEL_WALKING, Globals.CLASS_LABEL_RUNNING,
Globals.CLASS_LABEL_OTHER };
private RadioGroup radioGroup;
private final RadioButton[] radioBtns = new RadioButton[4];
private Intent mServiceIntent;
private File mFeatureFile;
public static TextView mCurrentLabel;
private State mState;
private Button btnDelete;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
radioGroup = (RadioGroup) findViewById(R.id.radioGroupLabels);
radioBtns[0] = (RadioButton) findViewById(R.id.radioStanding);
radioBtns[1] = (RadioButton) findViewById(R.id.radioWalking);
radioBtns[2] = (RadioButton) findViewById(R.id.radioRunning);
radioBtns[3] = (RadioButton) findViewById(R.id.radioOther);
btnDelete = (Button) findViewById(R.id.btnDeleteData);
mCurrentLabel = (TextView) findViewById(R.id.textView);
mState = State.IDLE;
mFeatureFile = new File(getExternalFilesDir(null),
Globals.FEATURE_FILE_NAME);
mServiceIntent = new Intent(this, SensorsService.class);
}
public void onCollectClicked(View view) {
if (mState == State.IDLE) {
mState = State.COLLECTING;
((Button) view).setText(R.string.ui_collector_button_stop_title);
btnDelete.setEnabled(false);
radioBtns[0].setEnabled(false);
radioBtns[1].setEnabled(false);
radioBtns[2].setEnabled(false);
radioBtns[3].setEnabled(false);
int acvitivtyId = radioGroup.indexOfChild(findViewById(radioGroup
.getCheckedRadioButtonId()));
String label = mLabels[acvitivtyId];
Bundle extras = new Bundle();
extras.putString(Globals.CLASS_LABEL_KEY, label);
mServiceIntent.putExtras(extras);
startService(mServiceIntent);
} else if (mState == State.COLLECTING) {
mState = State.IDLE;
((Button) view).setText(R.string.ui_collector_button_start_title);
btnDelete.setEnabled(true);
radioBtns[0].setEnabled(true);
radioBtns[1].setEnabled(true);
radioBtns[2].setEnabled(true);
radioBtns[3].setEnabled(true);
stopService(mServiceIntent);
((NotificationManager) getSystemService(NOTIFICATION_SERVICE)).cancelAll();
}
}
public void onDeleteDataClicked(View view) {
if (Environment.MEDIA_MOUNTED.equals(Environment
.getExternalStorageState())) {
if (mFeatureFile.exists()) {
mFeatureFile.delete();
}
Toast.makeText(getApplicationContext(),
R.string.ui_collector_toast_file_deleted,
Toast.LENGTH_SHORT).show();
}
}
#Override
public void onBackPressed() {
if (mState == State.TRAINING) {
return;
} else if (mState == State.COLLECTING || mState == State.CLASSIFYING) {
stopService(mServiceIntent);
((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
.cancel(Globals.NOTIFICATION_ID);
}
super.onBackPressed();
}
#Override
public void onDestroy() {
// Stop the service and the notification.
// Need to check whether the mSensorService is null or not.
if (mState == State.TRAINING) {
return;
} else if (mState == State.COLLECTING || mState == State.CLASSIFYING) {
stopService(mServiceIntent);
((NotificationManager) getSystemService(NOTIFICATION_SERVICE))
.cancelAll();
}
finish();
super.onDestroy();
}
And this is the "doInBackground" method in my service class. The line "CollectorActivity.mCurrentLabel.setText(classification);" is the problem. I want this to update the textView continously.
public class OnSensorChangedTask extends AsyncTask<Void, Void, Void> {
#Override
protected Void doInBackground(Void... arg0) {
Instance inst = new DenseInstance(mFeatLen);
inst.setDataset(mDataset);
Instance inst2 = new DenseInstance(65);
int blockSize = 0;
FFT fft = new FFT(Globals.ACCELEROMETER_BLOCK_CAPACITY);
double[] accBlock = new double[Globals.ACCELEROMETER_BLOCK_CAPACITY];
double[] re = accBlock;
double[] im = new double[Globals.ACCELEROMETER_BLOCK_CAPACITY];
double max = Double.MIN_VALUE;
while (true) {
try {
// need to check if the AsyncTask is cancelled or not in the while loop
if (isCancelled () == true)
{
return null;
}
// Dumping buffer
accBlock[blockSize++] = mAccBuffer.take().doubleValue();
if (blockSize == Globals.ACCELEROMETER_BLOCK_CAPACITY) {
blockSize = 0;
testList = new ArrayList<Double>();
// time = System.currentTimeMillis();
max = .0;
for (double val : accBlock) {
if (max < val) {
max = val;
}
}
fft.fft(re, im);
for (int i = 0; i < re.length; i++) {
double mag = Math.sqrt(re[i] * re[i] + im[i]
* im[i]);
inst.setValue(i, mag);
testList.add(i,mag);
im[i] = .0; // Clear the field
}
// Append max after frequency component
inst.setValue(Globals.ACCELEROMETER_BLOCK_CAPACITY, max);
inst2.setValue(Globals.ACCELEROMETER_BLOCK_CAPACITY, max);
testList.add(max);
classificationIndex = WekaClassifier.classify(testList.toArray());
classification = testLabel.get((int) classificationIndex);
CollectorActivity.mCurrentLabel.setText(classification);
inst.setValue(mClassAttribute, mLabel);
mDataset.add(inst);
Log.i("new instance", mDataset.size() + "");
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
In doInBackground(Void... arg0) change CollectorActivity.mCurrentLabel.setText(classification); to publishProgress(classification); then change second argument from Void to String: public class OnSensorChangedTask extends AsyncTask<Void, Srting, Void> and add onProgressUpdate().
Finally your code should looks like:
public class OnSensorChangedTask extends AsyncTask<Void, Srting, Void> {
#Override
protected Void doInBackground(Void... arg0) {
//...
publishProgress(classification);
//...
}
#Override
protected Void onProgressUpdate(String... classification) {
CollectorActivity.mCurrentLabel.setText(classification[0]);
}
I am practising on a simple Android Game where a round button is randomly placed on the screen when the user taps on it..
it works fine but i want to speedify the process of placing the button so that the game gets harder for user...
here is the Code I'm using -
public class GameWindow extends Activity implements View.OnClickListener {
static int score;
private Timer t;
private int TimeCounter = 29;
private boolean canMove = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setVolumeControlStream(AudioManager.STREAM_MUSIC);
////Remove title screen for activty.
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_game_window);
moveButton();
endonTimeOver();
}
public void endonTimeOver(){
////Activity timer for 60 seconds.
final TextView timer = (TextView) findViewById(R.id.seconds);
t = new Timer();
t.scheduleAtFixedRate(new TimerTask() {
////Set string to timer.
#Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable() {
public void run() {
timer.setText(String.valueOf(TimeCounter)); // you can set it to a textView to show it to the user to see the time passing while he is writing.
TimeCounter = TimeCounter - 1;
}
});
}
}, 1000, 1000); // 1000 means start from 1 sec, and the second 1000 is do the loop each 1 sec.
new Timer().schedule(new TimerTask(){
public void run() {
GameWindow.this.runOnUiThread(new Runnable() {
public void run() {
startActivity(new Intent(GameWindow.this, Finished.class));
}
});
}
}, 30000);
}
////Move button.
private void moveButton()
{
if(!canMove){ return; }
runOnUiThread(
new Runnable()
{
#Override
public void run()
{
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Button button = (Button)findViewById(R.id.button);
Random r = new Random();
int startX = width/2;
int startY = height/2;
if(score==0){
button.setX(startX);
button.setY(startY);
}
else {
int x = r.nextInt(width - 210);
int y = r.nextInt(height - 200);
button.setX(x);
button.setY(y);
}
}
}
);
}
////Display score
public void displayScore(int score) {
TextView scoreView = (TextView) findViewById(R.id.score);
scoreView.setText(String.valueOf(score));
}
#Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(this, R.raw.buttonsound);
mp.setOnCompletionListener(new OnCompletionListener() {
#Override
public void onCompletion(MediaPlayer mp) {
// TODO Auto-generated method stub
mp.release();
}
});
mp.start();
score = score + 1;
displayScore(score);
switch (v.getId()) {
case (R.id.button): {
moveButton();
}
}
}
public static int getScore(){
return score;
}}
Use global variables for values that don't change:
findViewById is slow
Creating new Random every time is not necessary
getting the window parameter every time is not necessary either
You seem to be starting a new thread and telling it to runonui , this might be slowing you down , try this :
private void moveButton()
{
if(!canMove){ return; }
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
int height = size.y;
Button button = (Button)findViewById(R.id.button);
Random r = new Random();
int startX = width/2;
int startY = height/2;
if(score==0){
button.setX(startX);
button.setY(startY);
}
else {
int x = r.nextInt(width - 210);
int y = r.nextInt(height - 200);
button.setX(x);
button.setY(y);
}
}
the computation itself doesn't seem to heavy so no need for another thread , you can do it on the main thread , if you're calling from oncreate that means you're already on main thread , this might give you the answer if i understood the question , try it
I have this code:
public void setText(String s){
TextView tv= (TextView)HomeActivity.tf.getView().findViewById(R.id.textViewFragment);
char c;
for(int i=0; i< s.length(); i++){
c= s.charAt(i);
tv.append(String.valueOf(c));
try{
Thread.sleep(100);
}catch(Exception e){}
}
}
The problem is that i cant get the TextView to display the letters one-by-one. After the loop is completed and everything is executed, thats when the text shows up.
I want to have it show up letter by letter, to give an animation effect to the TextView.
This code works,
public void setText(final String s)
{
TextView tv= (TextView)HomeActivity.tf.getView().findViewById(R.id.textViewFragment);
final int[] i = new int[1];
i[0] = 0;
final int length = s.length();
final Handler handler = new Handler()
{
#Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
char c= s.charAt(i[0]);
Log.d("Strange",""+c);
tv.append(String.valueOf(c));
i[0]++;
}
};
final Timer timer = new Timer();
TimerTask taskEverySplitSecond = new TimerTask() {
#Override
public void run() {
handler.sendEmptyMessage(0);
if (i[0] == length - 1) {
timer.cancel();
}
}
};
timer.schedule(taskEverySplitSecond, 1, 500);
}
Just in case someone's still looking for a better solution (with animating letters), try out Fade-In TextView.
This TextView library inherits its properties directly from the native TextView class, which means that all the native TextView methods are supported. There are practically no limitations including multiline support. It also has some of its own methods and attributes which offer full control over the View.
More simple
Thread thread = new Thread() {
int i;
#Override
public void run() {
try {
for (i = 0; i < text.length(); i++) { // use your variable text.leght()
Thread.sleep(1000);
runOnUiThread(new Runnable() {
#Override
public void run() {
textView.setText(text.substring(0, i));
}
});
}
} catch (InterruptedException e) {
}
}
};
thread.start();
I am trying to write a step counter for android. It currently includes four buttons in one activity:
a button to start recording accelerometer data which is stored in an arraylist. The arraylist takes a type Trace, which is a class I created to hold the data of one sensor change. I also have a stop button, and buttons to read or write the data from text file.
This program keeps giving me a NullPointerException on the arraylist and I can't figure out why. Any help would be greatly appreciated!
EDIT: Sorry if the indentation is off or the code is unclear, this is a school assignment and I'm on a strict deadline so I have to rush to make the code usable before I can worry about readability or efficiency.
EDIT 2: I no longer get any exceptions, however I still cannot read/write properly. I was able to write to file one successfully, and then somehow it stops functioning.
package com.myApp.playpool;
//imports
public class MainActivityBAK extends Activity implements SensorEventListener{
//global fields (traces is instantiated here as new arraylist)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
acceleration=(TextView)findViewById(R.id.acceleration);
startButton = (Button) findViewById(R.id.startButton);
stopButton = (Button) findViewById(R.id.stopButton);
readButton = (Button) findViewById(R.id.readButton);
writeButton = (Button) findViewById(R.id.writeButton);
sm = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
accelerometer = sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_GAME);
dataFilePath = getString(R.string.data_file_path);
acceleration.setText("Current file: " + dataFilePath);
lastCheck = System.currentTimeMillis();
defineButtons();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onSensorChanged(SensorEvent event1) {
if (running && ((System.currentTimeMillis() - lastCheck) > 1000)) {
acceleration.setText("X: "+event1.values[0]+"\nY: "+event1.values[1]+"\nZ: "+event1.values[2]);
traces.add(new Trace(System.currentTimeMillis(), event1.values[0], event1.values[1], event1.values[2]));
lastCheck = System.currentTimeMillis();
}
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
}
public void defineButtons() { //defines onClick methods for the buttons
startButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
traces = new ArrayList<Trace>();
running = true;
}
});
stopButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
traces = new ArrayList<Trace>();
running = false;
}
});
readButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
scan = new Scanner(new File(dataFilePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
while (scan.hasNext()) {
String str = scan.nextLine();
String [] strings = str.split(";");
double time = Double.parseDouble(strings[0]);
float x = Float.parseFloat(strings[0]), y = Float.parseFloat(strings[1]), z = Float.parseFloat(strings [2]);
traces.add(new Trace(time, x, y, z));
}
Toast.makeText(getBaseContext(),
("Done reading to SD file: '" + dataFilePath + "'"),
Toast.LENGTH_SHORT).show();
scan.close();
}
});
writeButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
try {
File file = new File(dataFilePath);
print = new PrintWriter(file);
file.createNewFile();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
for (int i = 0; i < traces.size(); i++) {
double time = traces.get(i).time;
float x = traces.get(i).x, y = traces.get(i).y, z = traces.get(i).z;
print.println(time + ";" + x + ";" + y + ";" + z + ";");
}
print.close();
Toast.makeText(getBaseContext(),
("Done writing to SD file: '" + dataFilePath + "'"),
Toast.LENGTH_SHORT).show();
}
});
}
}
It seems to be crashing in the for loop in the write method:
for (int i = 0; i < traces.size(); i++) {
You are only initializing the traces variable if your buttons start or stop are clicked.
If you click the "write" button before that, then the reference is still null.
It looks perhaps like traces has not been initialized in either onSensorChanged() or in your readButton.setOnClickListener, although it's hard to tell without the full source code or stack trace. If traces is a "// global field" then you should probably initialize it where it's declared, like:
protected ArrayList<Trace> traces = new ArrayList<Trace>();
EDIT for 2nd Question
My guess is that using the File.createNewFile() method won't work if the file already exists. Maybe try a different approach, like this sample:
Writer writer = null;
try {
OutputStream out = mContext.openFileOutput(dataFileName, Context.MODE_PRIVATE);
writer = new OutputStreamWriter(out);
for (int i = 0; i < traces.size(); i++) {
double time = traces.get(i).time;
float x = traces.get(i).x, y = traces.get(i).y, z = traces.get(i).z;
writer.write(time + ";" + x + ";" + y + ";" + z + ";");
}
}
finally {
if (writer != null) {
writer.close();
}
}
I'm newbie in java. So the program should take the user entered text and if there is "a" it gonna play a-sound, if there is "b" it gonna play b-sound.And it must play this sounds one by one even if there are multiple "a" or "b". Here is my code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button bStart = (Button) findViewById(R.id.bStart);
final EditText etStart = (EditText) findViewById(R.id.etStart);
final EditText etFinish = (EditText) findViewById(R.id.etFinish);
final char[] arr = etStart.getText().toString().toCharArray();
final MediaPlayer as = MediaPlayer.create(R2d2Activity.this, R.raw.as);
final MediaPlayer bs = MediaPlayer.create(R2d2Activity.this, R.raw.bs);
final SoundPool sp;
final int a;
final int b;
final int t;
sp = new SoundPool(2, AudioManager.STREAM_MUSIC, 0);
a = sp.load(this, R.raw.as, 1);
b = sp.load(this, R.raw.bs, 1);
final String value = etStart.getText().toString();
final Thread timer = new Thread(new Runnable() {
#Override
public void run() {
try {
Thread.sleep(1300);
// Do some stuff
} catch (Exception e) {
e.getLocalizedMessage();
}
}
});
bStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int n = 0;
String value;
String first;
value = etStart.getText().toString();
// TODO Auto-generated method stub
//actual code
for (int i=0; i<value.length(); i++){
first = etStart.getText().toString().substring(i, i+1);
if(first.contentEquals("a")){
as.start();
as.setOnCompletionListener(new OnCompletionListener(){
public void onCompletion(MediaPlayer arg0) {
//when finished
}
});
}else{
}
if(first.contentEquals("b")){
bs.start();
}else{
}
}
}
});
The problem is that it starts playing audio files all at one time. I tried to add some OnCompletionListener, but I don't know what to do with it. Help me please.
What you can do is
//define a variable to be used as index.
int audioindex = 0;
//Extract the files id into an array
int[] audioFileIds=new int[]{R.raw.as,R.raw.bs};
Then in your MediaPlayer onCompletionListener put something like following.
then in your OnCompletionListener.
mp.setOnCompletionListener(new OnCompletionListener(){
// #Override
public void onCompletion(MediaPlayer player) {
// File has ended, play the next one.
FunctionPlayFile(audioFileIds[audioindex]);
audioindex+=1; //increment the index to get the next audiofile
}
});