Thanks for all suggestion and thoughts i finally achieved it by yours suggestions and it was a simple logic work to solve the problem. I want to share it if any one want to create custom Health Bar , but it is not suitable for 100 HP because then you have to write 100 IF statements,
Idea was to create simple custom Healthbar and decrease it by pressing button clicks.
This link also help me a lot.
HealthBar
Code
private TextView Message,result;
private Button play;
private float maxHP = 10;
private float currentHP = maxHP;
//private float percentHP = (float) (currentHP/maxHP);
private int user_shield;
private float healthBar;
private ImageView shieldusb1,shieldusb2,shieldusb3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_level_one);
Initialize();
}
private void Initialize(){
user_shield = R.raw.shield_blue;
shieldusb2 = (ImageView) findViewById(R.id.shield_b2);
shieldusb1 = (ImageView) findViewById(R.id.shield_b1);
shieldusb3 = (ImageView) findViewById(R.id.shield_b3);
result = (TextView) findViewById(R.id.result);
play = (Button) findViewById(R.id.playButton);
play.setOnClickListener(new OnClickListener(){
public void onClick(View arg0){
play();
}
});
}
public void play(){
{
healthBar=0;
currentHP = (float) (currentHP - 0.5);
//currentHP -= 1;
if(currentHP <= 0) //if the player died
{
currentHP = 0; //set his HP to zero, (just in case)
Message.setText("You died! :P"); //add any extra death-code here
//update the healthBar
result.setText ("WIN POINTS");
}
updateHealthBar();
}
}
private void updateHealthBar() {
// percentHP = currentHP / maxHP;
healthBar = currentHP;
if(healthBar<9.6){
shieldusb1.setVisibility(View.GONE);
}
{
if (healthBar<=9){
shieldusb2.setVisibility(View.GONE);
}
if (healthBar<=8.6){
shieldusb3.setVisibility(View.GONE);
}
}
}
My guess, is you need to remove the else if and make it just if because if you execute first statement where <= 1, else if will never get executed.
private void updateHealthBar() {
percentHP = currentHP / maxHP; / calculating points
healthBar = percentHP;
if(healthBar<=1){ // Hiding first Image View
shieldusb1.setVisibility(View.GONE);
}
if (healthBar<=0.9){
shieldusb2.setVisibility(View.GONE); // How to Hide this Image View after button click
}
}
Your healthBar is of int type, so checking against 0.9 is the same as checking against 1.
Also, in your conditions, you're first checking:
if (healthBar <= 1)
If this condition is true, you're hiding shieldusb1, and you will never reach second condition because you have used else. What you can do is to remove else in front of second condition, but as said above, your healthBar is int, so the second condition is basically the same as the first. Change healthBar to double.
You need to adjust these conditions
if(healthBar<=1)
...
else if (healthBar<=0.9)
Even if healthBar<= 0.9 evaluates true, healthBar<=1 is also true so it will always enter the first if
You check if healthBar is <= 1, and after if is <= 0.9... But this case never verify, cause 0.9 is < 1, so it always choose the first option!
Change your first if for check values between 1 and 0.9, and it will work!
It's only logical problem. Change if/else if block for if/if.
if(healthBar<=1)
shieldusb1.setVisibility(View.GONE);
if (healthBar<=0.9)
shieldusb2.setVisibility(View.GONE);
Related
I wanted to make an app that would get the test score from the text field (id etxt1) and on a click of a button it would show the grade in the other text field (id etxt2).
marks 100-91 grade A.
marks 90-81 grade B.
marks 80-71 grade C.
and so on.
and how to use the ">=" thing.
Here's my code:
Button bt1;
EditText etxt1;
EditText etxt2;
char grade = 0;
int score;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activitywhatsyourgrade);
bt1 = (Button) findViewById(R.id.button1);
etxt1 = (EditText) findViewById(R.id.testscore);
final int score = etxt1.getTextAlignment();
etxt2 = (EditText) findViewById(R.id.editText2);
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (score == 90){
etxt2.setText("A1");
}
else if (score ==80){
etxt2.setText("A2");
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_activitywhatsyourgrade, menu);
return true;
}
There are a couple things wrong.
final int score = etxt1.getTextAlignment(); is incorrect because:
You define it outside of the button listener, and as final, so it never changes depending on input.
getTextAlignment() is not the function you want to call.
Here's how to fix it:
Get rid of that line of code all together. We'll replace that in the button listener.
We'll use getText() method to get the text from the EditText. It won't be returned as a String, but instead an Editable, so we'll use the toString() method on that to use it as a String.
Once we have the String representation of the score, we'll parse that to an integer to check which range of grade it's between.
This code is just for your button listener. Just replace it with this. It's self explanatory, so I won't give any more explanation for it.
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String strScore = etxt1.getText().toString();
int score = Integer.parseInt(strScore);
if (score >= 91) {
etxt2.setText("A");
}
else if (score >= 81) {
etxt2.setText("B");
}
else if (score >= 81) {
etxt2.setText("B");
}
else if (score >= 71) {
etxt2.setText("C");
}
else if (score >= 61) {
etxt2.setText("D");
}
else {
etxt2.setText("F");
}
}
});
This next section is just extra, you don't have to read this part if you're not completely understanding the code so far:
You may run into a problem where if the field is left blank when the button is pressed, your app will crash. This is because you're trying to parse an empty string (which is "") to a numerical value, which obviously cannot be done. The same thing will happen if the field is just a negative sign or decimal point.
To fix that, you can just wrap the part of your code that is parsing the String to an integer in a try-catch block. This will catch an exception that will be thrown for the problem stated above. Like this:
Replace this line:
int score = Integer.parseInt(strScore);
With this:
int score = 0;
try {
score = Integer.parseInt(strScore);
} catch (NumberFormatException nfe) {
// This means NFE was thrown, so the field text cannot be parsed
// to a numerical value. Just leave score = 0 as it was initialized
}
Or you can use an if-statement to test if the input is valid (this solution is worse because it only catches three cases when there could possibly be more, depending on the keyboard restrictions):
int score = 0;
// if the input is not blank, a negative sign or a decimal point
if (!(strScore.equals("") || strScore.equals("-") || strScore.equals(".")) {
score = Integer.parseInt(strScore);
}
String a = etxt1.getText().toString();
etxt2.setText(a);
I would like to add an amendment to Mike Yaworski's answer
While this code is correct that he gave...
#Override
public void onClick(View v) {
String strScore = etxt1.getText().toString();
int score = Integer.parseInt(strScore);
...
... there is a fundamental flaw in that getText() can and will return null if the field is left blank. That is because it is not returning a String - it is returning an Editable; hence why you have to call toString() on it.
Here is my correction to it. Note, I had to get the text from 2 items in the GUI, so I separated the logic into a private method that returned the given String
// Returns a String from and Editable. Deals with the null value
private String extractStringNumber(Editable text) {
if (text == null) return "0"; // Change this to fit you needs
return text.toString();
}
#Override
public void onClick(View v) {
String strScore = extractStringNumber(etxt1.getText());
int score = Integer.parseInt(strScore); // This is now guaranteed not to fail as long as strScore can be represented as a number.
I need to save 5 best times (an integer) for a game. The game is a reaction time game.
I'm assuming that the easiest way to do this would be to store it in a text file but I really have no idea how to go about doing this.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
startButton = (Button) findViewById(R.id.btnStart);
textTimer = (TextView) findViewById(R.id.textTimer);
myHandler = new Handler();
startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
startTime = SystemClock.uptimeMillis();
myHandler.postDelayed(updateTimerMethod, 0);
MainActivity.this.textTimer.setVisibility(0);
}
});
pauseButton = (Button) findViewById(R.id.btnPause);
pauseButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
timeSwap += timeInMillies;
myHandler.removeCallbacks(updateTimerMethod);
MainActivity.this.textTimer.setVisibility(0);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private Runnable updateTimerMethod = new Runnable() {
#Override
public void run() {
timeInMillies = SystemClock.uptimeMillis() - startTime;
finalTime = timeSwap + timeInMillies;
int seconds = (int) (finalTime / 1000);
int minutes = seconds / 60;
seconds = seconds % 60;
int milliseconds = (int) (finalTime % 1000);
textTimer.setText("" + minutes + ":" + String.format("%02d", seconds) + ":" + String.format("%03d", milliseconds));
myHandler.postDelayed(this, 0);
}
};
that is the code i have for now. all i want to happen is that when i press the pause button, it will go to the next screen and display the time. and also there will be a button, let's say "records". if it is pressed, it will display the 5 best times.
Instead of using a file to save scores, you can use SharedPreferences putInt() and getInt() methods to save int values.
The following sample is a possible (very lazy) implementation of it;
protected final static String PREFS_NAME = "PREF_GAME_SCORE";
public static int GetBestScore(Context context, int no)
{
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
return settings.getInt("Score" + String.valueOf(no), 0); // # 0 is the default value if no data is found
}
public static void SetBestScore(Context context, int no, int score)
{
SharedPreferences settings = context.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("Score" + String.valueOf(no), score);
editor.commit();
}
You can use SetBestScore(conxtext, 1, 666); to set the best score #number:1 and to get the best score, use; GetBestScore(context, 1);
The values saved using SharedPreferences will remain until the application is removed from the device or Application Data is cleared manually using:
Settings -> Applications -> Manage applications -> (choose your app)
-> Clear data
Building off of what Zefnus said, here is a method to send a score in and check if it is in the top lowest scores. If it is, it sets the TEMP_SPOT and then updates accordingly.
You could set any number of top scores too. This could easily be modified to got the other way too, IE, check for high scores.
public static void CheckAndSetBestScore(Context context, int score) {
int TOTAL_TOP_SCORES = 5;
int TEMP_SPOT = -1;
SharedPreferences settings = context
.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
for (int i = 1; i <= TOTAL_TOP_SCORES; ++i) {
if (TEMP_SPOT == -1) {
if (score <= settings.getInt("Score" + String.valueOf(i), 1000)) {
TEMP_SPOT = i;
}
}
}
if (TEMP_SPOT != -1) {
for (int i = TOTAL_TOP_SCORES; i >= TEMP_SPOT; --i) {
if (i != TEMP_SPOT) {
editor.putInt("Score" + String.valueOf(i), settings.getInt("Score" + String.valueOf(i - 1), 1000));
}
else {
editor.putInt("Score" + String.valueOf(i), score);
}
}
}
}
There are three ways to do it
1. Flat FIle
2. Shared Preferences
3. SQL
Personally i would recommend you to go for SQL, since any of the operation that you want to add in the future makes lesser work.If in case of sharedpreferences you need a whole new set of code to handle a new operation.so better go with sqlite.
Whenever a game is ended take the score and insert it to the table. When you want to display the top 5 results. Do a query with Score in Ascending order and limit to 5 rows. Bingo you can get the result,which you wanted.
Hope it helps.
Take a look at this link for how to create and use an SQL database from android.
http://chrisrisner.com/31-Days-of-Android--Day-24%E2%80%93Using-SQLite-Databases
I'm attempting to work on a simple first Android app, and while eventually it will be a shake-to-activate app, right now it still works with the click of a button because I need to be able to test it on the emulator I have.
The end goal for the app is to, when the device is shaken, choose one random number out of three (each corresponds to a Character from a show) and read that number to fade in an image of that character. Directly after, it fades in a random quote from an array of quotes from that specific character.
Currently, I'd gotten the app working to the point where, when I clicked the button, the app chose a character, and a quote from the array for that character, and was able to fade the quote in. In trying to implement the image fading in, I've done something that makes the app crash when I try to run it.
I'm sure it'll be some stupid mistake, but it would be fantastic if it could be found.
The project has four class files: MainActivity.java, DoctorWho.java, Nine.java, Ten.java, and Eleven.java. Nine, Ten, and Eleven are all nearly identical, just using different quotes.
In trying to add the image fade-in, I added it to DoctorWho.java, here:
public class DoctorWho {
private Nine mNine = new Nine();
private Ten mTen = new Ten();
private Eleven mEleven = new Eleven();
private ImageView mImageView1;
private ImageView mImageView2;
private ImageView mImageView3;
int randomNumber = 0;
public String getDoctorQuote() {
String quote = "";
// Choose a Random number out of three values
Random randomGenerator = new Random();
int randomNumber = randomGenerator.nextInt(3);
// Use that value to choose which of the Doctors to get a quote from
if (randomNumber == 0) {
// Quote from Nine
quote = mNine.getQuote();
}
else if (randomNumber == 1) {
// Quote from Ten
quote = mTen.getQuote();
}
else if (randomNumber == 2) {
// Quote from Eleven
quote = mEleven.getQuote();
}
else {
quote = "Error";
}
return quote;
}
public void animateDoctor() {
if (randomNumber == 0) {
animateNinthDoctor();
}
else if (randomNumber == 1) {
animateTenthDoctor();
}
else if (randomNumber == 2) {
animateEleventhDoctor();
}
}
private void animateNinthDoctor() {
AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
fadeInAnimation.setDuration(1500);
fadeInAnimation.setFillAfter(true);
mImageView1.setAnimation(fadeInAnimation);
}
private void animateTenthDoctor() {
AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
fadeInAnimation.setDuration(1500);
fadeInAnimation.setFillAfter(true);
mImageView2.setAnimation(fadeInAnimation);
}
private void animateEleventhDoctor() {
AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
fadeInAnimation.setDuration(1500);
fadeInAnimation.setFillAfter(true);
mImageView3.setAnimation(fadeInAnimation);
}
In my MainActivity.java file, I try to run the animation on-click here:
#Override
public void onClick(View arg0) {
String quote = mDoctorWho.getDoctorQuote();
mDoctorWho.animateDoctor();
// mQuoteLabel is the TextView where the quotes are displayed
mQuoteLabel.setText(quote);
animateQuoteIn();
}
The app still starts up fine, and no errors are shown before I run it on the emulator. When I click on the button that should run this sequence, the app crashes. There are no errors shown in the console, and I wasn't able to find a specific line in the LogCat view - I may be looking in the wrong place though. Let me know if updating the post with more code would be helpful in finding what is causing the crash.
Try changing you DoctorWho class as following:
Also have you initialized mImageView1,mImageView2,mImageView3 inside DoctorWho class???
public class DoctorWho {
private Nine mNine = new Nine();
private Ten mTen = new Ten();
private Eleven mEleven = new Eleven();
private ImageView mImageView1;
private ImageView mImageView2;
private ImageView mImageView3;
int randomNumber = 0;
public String getDoctorQuote() {
String quote = "";
// Choose a Random number out of three values
Random randomGenerator = new Random();
randomNumber = randomGenerator.nextInt(3);//<--- don't create new randonNumber
// Use that value to choose which of the Doctors to get a quote from
if (randomNumber == 0) {
// Quote from Nine
quote = mNine.getQuote();
}
else if (randomNumber == 1) {
// Quote from Ten
quote = mTen.getQuote();
}
else if (randomNumber == 2) {
// Quote from Eleven
quote = mEleven.getQuote();
}
else {
quote = "Error";
}
return quote;
}
public void animateDoctor() {
ImageView imageView=null;
if (randomNumber == 0) {
imageView=mImageView1;
}
else if (randomNumber == 1) {
imageView=mImageView2;
}
else if (randomNumber == 2) {
imageView=mImageView3;
}
AlphaAnimation fadeInAnimation = new AlphaAnimation(0, 1);
fadeInAnimation.setDuration(1500);
fadeInAnimation.setFillAfter(true);
imageView.setAnimation(fadeInAnimation);
}
}
I am trying to create an application that returns a score based on user input.
for example if the user has 1000 posts on a specific site it would return 1. i would end it at 10000.
1000 = 1
2000 = 2 etc.
here is what i have so far and thanks. this site is awesome.
for now i just have each entry adding. value1+value2 etc.
public class DataIn extends Activity {
EditText editPostCount;
EditText editThanksCount;
EditText editRomCount;
EditText editThemeCount;
EditText editKernelCount;
EditText editTutorialCount;
EditText editYearsJoined;
Button mButton;
TextView results;
Button mButton1;
#Override
public void onCreate (Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.data_in);
android.app.ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
editPostCount = (EditText)findViewById(R.id.editPostCount);
editThanksCount = (EditText)findViewById(R.id.editThanksCount);
editRomCount = (EditText)findViewById(R.id.editRomThreads);
results = (TextView)findViewById(R.id.results);
editThemeCount = (EditText)findViewById(R.id.editThemeCount);
editKernelCount = (EditText)findViewById(R.id.editKernelCount);
editTutorialCount = (EditText)findViewById(R.id.editTutorialCount);
editYearsJoined = (EditText)findViewById(R.id.editYearsJoined);
mButton = (Button)findViewById(R.id.results_button);
mButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//When the button is clicked, call the calucate method.
calculate();
}
});
private void calculate() {
try {
Double value1 = Double.parseDouble(editPostCount.getText().toString());
Double value2 = Double.parseDouble(editThanksCount.getText().toString());
Double value3 = Double.parseDouble(editRomCount.getText().toString());
Double value4 = Double.parseDouble(editKernelCount.getText().toString());
Double value5 = Double.parseDouble(editThemeCount.getText().toString());
Double value6 = Double.parseDouble(editYearsJoined.getText().toString());
Double value7 = Double.parseDouble(editTutorialCount.getText().toString());
//do the calculation
Double calculatedValue = (value1+value2+value3+value4+value5+value6+value7);
//set the value to the textView, to display on screen.
results.setText(calculatedValue.toString());
} catch (NumberFormatException e) {
// EditText EtPotential does not contain a valid double
}
mButton1 = (Button)findViewById(R.id.clear_button);
mButton1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
editPostCount.setText("");
editThanksCount.setText("");
editRomCount.setText("");
editThemeCount.setText("");
editKernelCount.setText("");
editTutorialCount.setText("");
editYearsJoined.setText("");
results.setText("");}
});
} }
You can get the score for every value using a simple division, that is cut to an integer.
In this example I also defined one constant to determine for each different value a specific score factor.
private static final int TOTALCOUNT_SCOREFACTOR = 1000;
int totalCountScore = totalCount / TOTALCOUNT_SCOREFACTOR;
I suggest you not to use doubles, generally int is enough.
I also suggest you to use an array of values, instead of defining all of them separately. In that way, you can easily add or remove values in future.
I hope I am not misunderstanding your question, but if you want the score to add 1 point for every 1000 posts, you simply get the number of posts and divide by 1000. for example:
//value1 is the post count
int calculatedvalue = value1/1000;
So if the number of posts(value1) is 3500, calculatedvalue would be 3.(the remainder is cut off during division)
Here's the code:
public class MainActivity extends Activity implements OnTouchListener {
RelativeLayout rl;
int i, j = 0;
final int imageArray[] = { R.drawable.w1, R.drawable.w2, R.drawable.w3 };
int image;
final int imageCount = 3;
ImageView back, save, next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_layout);
final int imageArray[] = { R.drawable.w1, R.drawable.w2, R.drawable.w3 };
image = imageArray[0];
rl = (RelativeLayout) findViewById(R.id.rlBackground);
back = (ImageView) findViewById(R.id.bBack);
save = (ImageView) findViewById(R.id.bSave);
next = (ImageView) findViewById(R.id.bNext);
back.setOnTouchListener(this);
save.setOnTouchListener(this);
next.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.bBack:
if (j == 0) {
j = imageCount;
}
image = imageArray[j - 1];
rl.setBackgroundResource(image);
j = j - 1;
break;
case R.id.bSave:
BitmapFactory.Options opts = new BitmapFactory.Options();
opts.inSampleSize = 2;
Bitmap bm = BitmapFactory.decodeResource(getResources(), image,
opts);
// savePicture(bm, "image_name.jpg");
SaveImage savefile = new SaveImage();
savefile.SaveImagee(this, bm);
Toast.makeText(getApplicationContext(),
"Image saved on your gallery!", Toast.LENGTH_LONG).show();
break;
case R.id.bNext:
if (j != imageCount) {
rl.setBackgroundResource(imageArray[j]);
image = imageArray[j];
j = j + 1;
} else {
j = 0;
rl.setBackgroundResource(imageArray[j]);
image = imageArray[j];
j = j + 1;
}
break;
}
return false;
}
}
The Problem: If I click on save button it works on the first click. If I click next button, I need to click it twice to trigger the function, but after that, if I continue to click next button, it works with one click. But when I switch to button back, it needs to be clicked two times and then only one time and if I switch back to button next, the same happens - two times, one time..
I guess it has something to do with focus.
If I change my ImageView to ImageButton, it triggers the function twice, If I add an if statemt(event.getAction() == MotionEvent.ACTION_DOWN) then again I have to click the button two times.. I want the button to work with one click all the time. I don't understand why this is happening, because save button works with one click all the time..
EDIT: If I change
image = imageArray[j];
to
image = imageArray[2];
then the button works at first with one click, still, I can't get it.
I just realized that I set ImageCount = 3, but in arrays it start counting from 0, I programmed that value at the start of this app and I tought it can't be wrong.
You should really use an OnClickListener,
but maybe it is because you aren't telling the system you have handled the touch event. Try returning true when you deal with the touch event and false otherwise. For example:
switch (v.getId()) {
case R.id.bBack:
if (j == 0) {
j = imageCount;
}
image = imageArray[j - 1];
rl.setBackgroundResource(image);
j = j - 1;
return true;
default:
return false;