android programming
Bill amount -----------------
Percent ------------------(using seek bar)
Tip
Total (Bill amount + percent)
Split bill (use Spinner)(no way, one way , two way, three way)
per person ---(use if and else to give manual amount per person)
you initialised apply button as seekbar,this is the only mistake you did.after correcting the statements the activity calss is as follows
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.SeekBar;
import android.widget.TextView;
public class TipCalculatorActivity extends AppCompatActivity {
private EditText et_bill_amount;
private TextView tv_tip,tv_total,tv_percent;
private SeekBar seekBar;
private Button applyButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize edittext, seekbar, textViews and apply button.
et_bill_amount=(EditText)findViewById(R.id.billAmountEditText);
tv_total=(TextView)findViewById(R.id.totalTextView);
tv_tip=(TextView)findViewById(R.id.tipTextView);
tv_percent=(TextView)findViewById(R.id.percentTextView);
seekBar=(SeekBar)findViewById(R.id.percentSeekBar);
applyButton=(Button)findViewById(R.id.applyButton);
applyButton.setOnClickListener(new View.OnClickListener()
{
@Override
public void onClick(View view) {
calculateAndDisplay();
}
});
}
private void calculateAndDisplay() {
//Get seekbar progress value
int percent=seekBar.getProgress();
//Set percent value to percentTextView
tv_percent.setText(String.valueOf(percent)+"%");
//Extract entered bill amount and store in billAmount.
float
billAmount=Float.valueOf(et_bill_amount.getText().toString());
//Calculate tip and total.
float tip=billAmount*((float)percent/100);
float total=billAmount+tip;
//Set tip and total values respectively
tv_tip.setText("$"+String.valueOf(tip));
tv_total.setText("$"+String.valueOf(total));
}
}
Get Answers For Free
Most questions answered within 1 hours.