Logic error AndroidStudio - java

package com.karanvir.tip;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import static com.karanvir.tip.R.drawable.p;
public class MainActivity extends AppCompatActivity {
public EditText people;
public EditText amount;
public EditText percentage;
public EditText finalAmount;
public String finalAmountz;
public RatingBar rating;
public TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
people=(EditText) findViewById(R.id.editText);
amount=(EditText) findViewById(R.id.editText2);
percentage=(EditText) findViewById(R.id.editText3);
finalAmount=(EditText) findViewById(R.id.editText6);
rating=(RatingBar) findViewById(R.id.ratingBar);
//ratingBar.setRating(3);
txt=(TextView) findViewById(R.id.textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void calc(View view){
try{
double pep= Double.parseDouble(people.getText().toString());
double a=Double.parseDouble(amount.getText().toString());
double perc=Double.parseDouble(percentage.getText().toString());
double percc=Double.parseDouble(percentage.getText().toString());
if(pep>1||pep==1){
if(perc>1.0) {
double aa=a;
perc = perc / 100;
a = (a * perc);
double newamount=a+aa;
double placeholder = newamount / pep;
double rounder = Math.round(placeholder);
finalAmountz = "$" + String.valueOf(rounder);
finalAmount.setText(finalAmountz.toString());
if (perc>0.12) {
rating.setRating(5);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
else if (perc>0.10&&perc<0.12||perc==0.10) {
rating.setRating(4);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
else if (perc>0.7&&perc<0.10||perc==0.07) {
rating.setRating(3);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
else if (perc>0.05&&perc<0.07||perc==0.05) {
rating.setRating(2);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
if (perc<0.05) {
rating.setRating(1);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
}
}
else if (pep<1||pep==1){
Toast.makeText(MainActivity.this, "Please Enter 1 person minimum", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}
Please don't rip me apart here. But I'm new to android development. I'm making a tip calculator app right now, everything works the way I want it to. However, my star Calculation is off. it keeps showing 1 star? Please look at lines 72-117. Can someone help me? Sorry for the noob question, I'm still learning.
package com.karanvir.tip;
import android.graphics.Color;
import android.graphics.PorterDuff;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.RatingBar;
import android.widget.TextView;
import android.widget.Toast;
import static com.karanvir.tip.R.drawable.p;
public class MainActivity extends AppCompatActivity {
public EditText people;
public EditText amount;
public EditText percentage;
public EditText finalAmount;
public String finalAmountz;
public RatingBar rating;
public TextView txt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
people=(EditText) findViewById(R.id.editText);
amount=(EditText) findViewById(R.id.editText2);
percentage=(EditText) findViewById(R.id.editText3);
finalAmount=(EditText) findViewById(R.id.editText6);
rating=(RatingBar) findViewById(R.id.ratingBar);
//ratingBar.setRating(3);
txt=(TextView) findViewById(R.id.textView);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void calc(View view){
try{
double pep= Double.parseDouble(people.getText().toString());
double a=Double.parseDouble(amount.getText().toString());
double perc=Double.parseDouble(percentage.getText().toString());
if(pep>1||pep==1){
if(perc>1.0) {
double aa=a;
perc = perc / 100;
a = (a * perc);
double newamount=a+aa;
double placeholder = newamount / pep;
double rounder = Math.round(placeholder);
finalAmountz = "$" + String.valueOf(rounder);
finalAmount.setText(finalAmountz.toString());
if (perc>12) {
rating.setRating(5);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
if (perc>10&&perc<12||perc==10) {
rating.setRating(4);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
if (perc>7&&perc<10||perc==7) {
rating.setRating(3);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
if (perc>5&&perc<7||perc==5) {
rating.setRating(2);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
if (perc<5) {
rating.setRating(1);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
}
else if(perc<=1.0){
double aa=a;
a = (a * perc);
double newamount=a+aa;
double placeholder = newamount / pep;
double rounder = Math.round(placeholder);
finalAmountz = "$" + String.valueOf(rounder);
finalAmount.setText(finalAmountz.toString());
finalAmountz = "$" + String.valueOf(rounder);
finalAmount.setText(finalAmountz.toString());
if (perc>12)
rating.setRating(5);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
if (perc>10&&perc<12||perc==10)
rating.setRating(4);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
if (perc>7&&perc<10||perc==7)
rating.setRating(3);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
if (perc>5&&perc<7||perc==5)
rating.setRating(2);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
if (perc<5)
rating.setRating(1);
rating.setVisibility(View.VISIBLE);
txt.setVisibility(View.VISIBLE);
}
}
else if (pep>1){
Toast.makeText(MainActivity.this, "Please Enter 1 person minamum", Toast.LENGTH_SHORT).show();
}
}catch (Exception e){
Toast.makeText(MainActivity.this, e.toString(), Toast.LENGTH_SHORT).show();
}
}
}

You are dividing perc by 100
if(pep>1||pep==1){
if(perc>1.0) {
double aa=a;
perc = perc / 100;
....
Unless the value you input is superior to 500 then its value will always be inferior to 5 (your last condition) and therefore always give 1 star

Related

Android Studio error: class, interface, enum expected

Starting with line 34 I am getting error: class, interface, enum expected. This is a simple percent calculator app as my introduction to android studio. I have tried fix all all the formatting suggestion I read for other people getting the same error but I had no luck. I would appreciate any suggestions or ideas on how to fix that or also if you have any basic app ideas I would love to hear them.
package com.example.bheue.percenttutorial;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.EditText;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView totalTextView;
EditText percentageText;
EditText numberText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
};
TextView totalTextView = (TextView) findViewById(R.id.totalText);
percentageText = (EditText) findViewById(R.id.percentageText);
numberText = (EditText) findViewById(R.id.numberText);
Button calcButton = (Button) findViewById(R.id.percentText);
calcButton.setOnClickListener(new view.OnClickListener())
#Override
public void onClick(view view){
float percentage = Float.parseFloat(percentageText.getText().toString());
float dec = percentage / 100;
float total = dec * Float.parseFloat(numberText.getText().toString());
totalTextView.setText(Float.toString(total));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
This is what your code should be:
package com.example.bheue.percenttutorial;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.uplication.hamza.helpinghands.R;
public class MainActivity extends AppCompatActivity {
TextView totalTextView;
EditText percentageText;
EditText numberText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final TextView totalTextView = (TextView) findViewById(R.id.totalText);
percentageText = (EditText) findViewById(R.id.percentageText);
numberText = (EditText) findViewById(R.id.numberText);
Button calcButton = (Button) findViewById(R.id.percentText);
calcButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
float percentage = Float.parseFloat(percentageText.getText().toString());
float dec = percentage / 100;
float total = dec * Float.parseFloat(numberText.getText().toString());
totalTextView.setText(Float.toString(total));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Did you copy this code from somewhere and had problems in pasting?

Android Text input always give me NaN value

I can not figure out why this code output always show NaN. Is there anybody who can help me figure out where I did mistake?
package com.example.user.solar_calculator;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.content.Context;
import android.content.Intent;
import android.widget.Button;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.Toast;
public class efficiency extends Activity {
Button button;
EditText efficiency;
EditText vol;
String e,v;
double e1,wat,v1, AC_Load_in_ampere_hour_calculation,dc;
double load,load_vol,Total_Daily_load,lol;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_efficiency);
initControls();
addListenerOnButton();
}
public void initControls() {
efficiency=(EditText)findViewById(R.id.editText1);
vol=(EditText)findViewById(R.id.editText2);
button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
try {
e1=Double.parseDouble(efficiency.getText().toString()); // Make use of autoboxing. It's also easier to read.
} catch (NumberFormatException e) {
e1=1;
}
try {
v1=Double.parseDouble(vol.getText().toString());// Make use of autoboxing. It's also easier to read.
} catch (NumberFormatException e) {
v1=1;
}
Bundle extras = getIntent().getExtras();
if(extras !=null) {
String value = extras.getString("FOUR");
load=Double.parseDouble(value.toString());
}
wat=load_vol*(1+.1);
AC_Load_in_ampere_hour_calculation = (load)/( v1* (e1/100));
Total_Daily_load=AC_Load_in_ampere_hour_calculation+dc;
Intent i = new Intent(efficiency.this, calculation.class);
//Create the bundle
Bundle bundle = new Bundle();
//Add your data to bundle
bundle.putString("seven", String.valueOf(AC_Load_in_ampere_hour_calculation));
bundle.putString("eight", String.valueOf(Total_Daily_load));
bundle.putString("nine", String.valueOf(dc));
//Add the bundle to the intent
i.putExtras(bundle);
//Fire that second activity
startActivity(i);
}
});
}
public void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.button);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I think the line who cause problem is AC_Load_in_ampere_hour_calculation = (load)/( v1* (e1/100));
Because if v1 = 0 it will be impossible.

Android Studio Java code

I'm trying to replace a part of the text after clicking the bottom but I can't make it work. When I click the bottom without entering a name the message should say "Hello Username", and if I enter a name the "Username" should be replace by the name. Here is the code so far. Thanks
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.*;
public class HelloAndroid extends AppCompatActivity implements OnClickListener {
private EditText name;
private TextView display;
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hello_android);
name = (EditText) findViewById(R.id.name);
display = (TextView) findViewById(R.id.display);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_hello_android, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onClick(View view) {
if (button == view) {
String message = "Hello, Username " + name.getText().toString() + "!";
if (button == view) {
String username = "Hello " + name.getText().toString() + "";
Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
toast.show();
display.setText(message);
}
}
}
}
You are enable to do this because may be you are not getting click here . Here is my code change it .
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class HelloAndroid extends AppCompatActivity implements View.OnClickListener {
private EditText name;
private TextView display;
private Button button;
String message ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dummy);
name = (EditText) findViewById(R.id.name);
display = (TextView) findViewById(R.id.display);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button :
if (name.getText().toString().isEmpty())
{
message = "Hello username" ;
}
else {
message = "Hello "+name.getText().toString() ;
}
Toast toast = Toast.makeText(this, message, Toast.LENGTH_SHORT);
toast.show();
display.setText(message);
break;
}
}
}

Shared element transition with picasso not working even after implementing callback

The image moves perfectly the first time into its place i.e detail activity image view and also returns perfectly back to the main activity but when I click the same image next time the transition animation moves the image to an incorrect (too high) offset in the detail activity and once the animation completes the image will appear to "warp" into the correct position.
Here is my DetailActivity.java file:
package com.akshitjain.popularmovies;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.ViewTreeObserver;
import android.widget.ImageView;
import android.widget.TextView;
import com.squareup.picasso.Callback;
import com.squareup.picasso.Picasso;
public class DetailActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
postponeEnterTransition();
}
ImageView backdropImageView = (ImageView) findViewById(R.id.detail_image_view);
final ImageView posterImageView = (ImageView) findViewById(R.id.detail_poster);
TextView overviewTextView = (TextView) findViewById(R.id.overview_text_view);
TextView releaseDateTextView = (TextView) findViewById(R.id.release_date);
TextView userRatingTextView = (TextView) findViewById(R.id.user_rating);
TextView genreTextView = (TextView) findViewById(R.id.genre);
genreTextView.setText("");
Genre genreObject = new Genre();
String genreName;
Intent intent = getIntent();
if (intent != null && intent.hasExtra(Constants.MOVIE_OBJECT_PARCELABLE_EXTRA)) {
Movies movies = intent.getParcelableExtra(Constants.MOVIE_OBJECT_PARCELABLE_EXTRA);
int[] genre;
genre = movies.genre;
for (int i = 0; i < genre.length; ++i) {
genreName = genreObject.getGenreName(genre[i]);
genreTextView.append(genreName);
if (i < genre.length - 1) {
genreTextView.append(", ");
}
}
setTitle(movies.originalTitle);
Picasso.with(getApplicationContext())
.load((Constants.IMAGE_BASE_URL + Constants.POSTER_SIZE_LARGE).trim() + movies.backdropPath)
.into(backdropImageView);
Picasso.with(getApplicationContext())
.load((Constants.IMAGE_BASE_URL + Constants.POSTER_SIZE_SMALL).trim() + movies.posterPath)
.into(posterImageView, new Callback() {
#Override
public void onSuccess() {
posterImageView.getViewTreeObserver().addOnPreDrawListener(
new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
posterImageView.getViewTreeObserver().removeOnPreDrawListener(this);
startPostponedEnterTransition();
}
return true;
}
}
);
}
#Override
public void onError() {
}
}
);
overviewTextView.setText(movies.overview);
releaseDateTextView.setText(movies.releaseDate);
userRatingTextView.setText(movies.userRating);
}
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_detail, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
switch (id) {
case android.R.id.home:
supportFinishAfterTransition();
return true;
case R.id.action_settings:
return true;
}
return super.onOptionsItemSelected(item);
}
}
Try this.
posterImageView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Picasso.with(getApplicationContext())
.load((Constants.IMAGE_BASE_URL + Constants.POSTER_SIZE_SMALL).trim() + movies.posterPath)
.into(posterImageView, new Callback() {
#Override
public void onSuccess() {
startPostponedEnterTransition();
}
#Override
public void onError() {
}
});
}
}

Trouble updating listview data

I have an application that populates a ListView with user input data from another Activity ; however, whenever I enter that data into the second Activity then return to the ListView activity, I see it populated with the values I entered. But, if I return to the second activity again, enter the values, and go back to the ListView activity, I see the new values updated but the old ones have disappeared.
Now, I don't know if I'm not updating the listview correctly or if I need to use SQLite databases to store this data or shared preferences in order to update the listview to new data while containing the previous information.
Thanks for any help.
ListActivity.java
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.personalproject.peter.timerapp.TestingForAlarmData.TestAlarm;
import java.util.ArrayList;
import java.util.List;
public class ListOfAlarms extends ActionBarActivity {
private static final int RESULT = 1000;
List<TestAlarm> alarms = new ArrayList<>();
String title;
int totalTime;
ListView listOfAlarms;
ArrayAdapter<TestAlarm> alarmArrayAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_of_alarms);
final TextView emptyViewForList = (TextView) findViewById(R.id.emptyTextViewForList);
listOfAlarms = (ListView) findViewById(R.id.listView);
alarmArrayAdapter = new ArrayAdapter<>(this,android.R.layout.simple_list_item_1, alarms);
listOfAlarms.setAdapter(alarmArrayAdapter);
// if(listOfAlarms.getCount() <= 0){
// emptyViewForList.setText("No Alarms Currently Available");
// listOfAlarms.setEmptyView(emptyViewForList);
// }
listOfAlarms.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
alarms.get(position);
Intent clockDownActivity = new Intent(ListOfAlarms.this, CountDownAct.class);
clockDownActivity.putExtra("Title", title);
clockDownActivity.putExtra("totalTime", totalTime);
startActivity(clockDownActivity);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_list_of_alarms, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void goToFillOut(View view) {
Intent goingToFillOut = new Intent(this, Test.class);
startActivityForResult(goingToFillOut, RESULT);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if(requestCode == RESULT && resultCode == RESULT_OK){
title = data.getStringExtra("title");
totalTime = data.getIntExtra("totalTime", 0);
alarms.add(new TestAlarm(title, totalTime));
alarmArrayAdapter.notifyDataSetChanged();
}
}
}
SecondActivity
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class Test extends ActionBarActivity {
private static final String LOGTAG = "Test.class";
private static final long timeInterval = 1000;
private Button complete;
private EditText titleEditText;
private EditText hourEditText;
private EditText minuteEditText;
private EditText secondEditText;
public static int hour;
public static int minute;
public static int second;
public static String title;
public int actualTimeFiniliazedInMilliSeconds;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
titleEditText = (EditText) findViewById(R.id.titleEditText);
hourEditText = (EditText) findViewById(R.id.hourEditText);
minuteEditText = (EditText) findViewById(R.id.minuteEditText);
secondEditText = (EditText) findViewById(R.id.secondEditText);
complete = (Button) findViewById(R.id.completeButton);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void saveTimer(View view) {
if(titleEditText.getText().toString().isEmpty() || hourEditText.getText().toString().isEmpty()
|| minuteEditText.getText().toString().isEmpty() || secondEditText.getText().toString().isEmpty()) {
Toast.makeText(this, "Oops you forgot one", Toast.LENGTH_LONG).show();
return;
}
// complete.setVisibility(View.GONE);
title = titleEditText.getText().toString();
hour = Integer.parseInt(hourEditText.getText().toString().trim());
minute = Integer.parseInt(minuteEditText.getText().toString().trim());
second = Integer.parseInt(secondEditText.getText().toString().trim());
hour *= 3600000;
minute *= 60000;
second *= 1000;
actualTimeFiniliazedInMilliSeconds = hour + minute + second;
Intent intent = new Intent(Test.this, ListOfAlarms.class);
intent.putExtra("title", title);
intent.putExtra("totalTime", actualTimeFiniliazedInMilliSeconds);
setResult(RESULT_OK, intent);
finish();
}
}
Alarm.java
public class TestAlarm {
public String title;
public int totalTime;
public TestAlarm (String title, int totalTime) {
this.title = title;
this.totalTime = totalTime;
}
#Override
public String toString() {
return title;
}
}

Categories