Crashing whenever any buttons are clicked - java

The app executed just fine, and the interface loads just fine as well, but when I try to press any of the buttons, nothing that should happen happens. The point is fairly simple: press the '+' button, the quantity increases, and the price immediately updates to match said quantity, vice versa for the '-' button. I don't know what I've done wrong, but any button interaction with the app crashes it.
Here's my Java:
package com.t99sdevelopment.mobile.eyy;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.TextView;
import java.text.NumberFormat;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public int quantityValue = 1;
public void increaseQuantity (View view) {
quantityValue = quantityValue + 1;
updateQuantityValue(view);
}
public void decreaseQuantity (View view) {
quantityValue = quantityValue - 1;
updateQuantityValue(view);
}
public void updateQuantityValue(View view) {
updateQuantity(quantityValue);
updatePrice(quantityValue);
}
private void updateQuantity(int number) {
TextView quantity = (TextView) findViewById(
R.id.quantityValue);
quantity.setText(number);
}
private void updatePrice(int number) {
TextView price = (TextView) findViewById(R.id.priceValue);
price.setText(NumberFormat.getCurrencyInstance().format(number * 5));
}
}
Here's my XML:
<TextView
android:text="Quantity"
android:id="#+id/quantityText"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:autoText="false"
android:paddingBottom="20dp" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center_horizontal">
<TextView
android:text="0"
android:id="#+id/quantityValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="16dp"
android:paddingBottom="16dp" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="-"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/quantityValue"
android:layout_toStartOf="#+id/quantityValue"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:onClick="decreaseQuantity" />
<Button
android:layout_width="48dp"
android:layout_height="48dp"
android:text="+"
android:id="#+id/button2"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/quantityValue"
android:layout_toEndOf="#+id/quantityValue"
android:layout_marginLeft="15dp"
android:layout_marginStart="15dp"
android:onClick="increaseQuantity" />
</RelativeLayout>
<TextView
android:text="Price"
android:id="#+id/priceText"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#000000"
android:autoText="false"
android:paddingTop="20dp" />
<TextView
android:text="$0"
android:id="#+id/priceValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="20dp"
android:paddingBottom="20dp" />
<Button
android:text="Order"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
The formatting of the actual code is just fine, but it got screwy when I pasted in into stack, so I know that's not the problem.

Change you updateQuantity and updatePrice method like :
private void updateQuantity(int number) {
TextView quantity = (TextView) findViewById(
R.id.quantityValue);
quantity.setText(String.valueOf(number));
}
private void updatePrice(int number) {
TextView price = (TextView) findViewById(R.id.priceValue);
price.setText(String.valueOf(NumberFormat.getCurrencyInstance().format(number * 5)));
}
}

Related

Issue with RadioButton with RadioGroup in Android using a toggle feature/isChecked and cleaning code up

Hi I been trying for a while and going through lots of theories and stack answers. Though I just can't figure out how to take the code below for a simple trivia app, using a RadioButton in a RadioGroup, then add one to score when checked. The issue is if you keep pressing the correct answer the score just increments. I would like to apply some type of toggle if the answer is changed the points adjust accordingly.
I would like to also clean this code up so it's not so repetitive. Using a new class or method. Though I can't figure out how to implement it.
Main
package com.example.samuel.trivaapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
//Score keeper.
private int score = 0;
private int correct = 0;
private int wrong = 0;
// determines if the selected was right or wrong, then used to add up the score.
boolean question1 = false;
boolean question2 = false;
boolean question3 = false;
// private boolean question3 = false;
// private boolean question4 = false;
// private boolean question5 = false;
// private boolean question6 = false;
// private boolean question7 = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup rGroup1 = (RadioGroup) findViewById(R.id.first_question_group);
RadioButton checkedRadioButton1 = (RadioButton) rGroup1.findViewById(rGroup1.getCheckedRadioButtonId());
rGroup1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (R.id.correct_answer1 == checkedId) {
question1 = true;
} else {
question1 = false;
}
}
});
RadioGroup rGroup2 = (RadioGroup) findViewById(R.id.second_question_group);
RadioButton checkedRadioButton2
= (RadioButton) rGroup2.findViewById(rGroup2.getCheckedRadioButtonId());
rGroup2.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (R.id.correct_answer2 == checkedId) {
question2 = true;
} else {
question2 = false;
}
}
});
RadioGroup rGroup3 = (RadioGroup) findViewById(R.id.third_question_group);
RadioButton checkedRadioButton3
= (RadioButton) rGroup3.findViewById(rGroup3.getCheckedRadioButtonId());
rGroup3.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup group, int checkedId) {
if (R.id.correct_answer3 == checkedId) {
question3 = true;
} else {
question3 = false;
}
}
});
}
//used to diplay the players previous score on screen.
public void displayScore() {
TextView scoreView = (TextView) findViewById(R.id.score_keeper);
score = correct + wrong;
scoreView.setText(Integer.toString(correct));
}
//used to submit results with button and tally score.
public void submitResults(View view){
question1();
question2();
question3();
displayScore();
score= 0;
correct = 0;
wrong = 0;
}
//selector for radioGroup1 check if id matches correct answer.
//submit results calls this to find out if the answer was correct for question 1 to use for tally.
public void question1() {
if(question1) {
correct++;
} else {
wrong--;
}
}
//submit results calls this to find out if the answer was correct for question 2 to use for tally.
public void question2() {
if(question2) {
correct++;
} else {
wrong--;
}
}
//submit results calls this to find out if the answer was correct for question 3 to use for tally.
public void question3() {
if(question3) {
correct++;
} else {
wrong--;
}
}
}
XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.samuel.trivaapp.MainActivity">
<TextView
android:id="#+id/header_logo"
android:layout_centerInParent="true"
android:layout_alignParentTop="true"
android:layout_margin="16dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<EditText
android:id="#+id/player_name"
android:layout_below="#+id/header_logo"
android:inputType="textCapWords"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/enter_player_name"
android:layout_margin="8dp"/>
<LinearLayout
android:id="#+id/score_board"
android:layout_below="#+id/player_name"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Score:"
android:textStyle="bold"
android:layout_marginLeft="8dp"
/>
<TextView
android:id="#+id/score_keeper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_marginLeft="8dp"/>
</LinearLayout>
<!--questions scroll view.-->
<ScrollView
android:id="#+id/question_scroll_view"
android:layout_margin="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/score_board">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp">
<RadioGroup
android:id="#+id/first_question_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Is this question 1? "/>
<RadioButton
android:id="#+id/correct_answer1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 4" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="#+id/test2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/test"
android:layout_marginBottom="16dp">
<RadioGroup
android:id="#+id/second_question_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Is this question 2? "/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 1" />
<RadioButton
android:id="#+id/correct_answer2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 2" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 4" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:id="#+id/test3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/test2"
android:layout_marginBottom="16dp">
<RadioGroup
android:id="#+id/third_question_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Is this question 3? "/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 1" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 2" />
<RadioButton
android:id="#+id/correct_answer3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 3" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Answer 4" />
</RadioGroup>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="submit"
android:onClick="submitResults"
android:layout_below="#id/test3"
android:layout_centerInParent="true"/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Okay I believe I figured out what I wanted to do. If anyone else has this issue or a similar problem, of using multiple RadioGroups I hope this helps.
https://github.com/raregamer/TrivaApp1/tree/trivia2/app/src/main/java/com/example/samuel/trivaapp

Animation Problems in Android

I've got few problems in using animation in android studios:
I'm trying to translate certain images at an angle. Like in the image below I want to move that attachment pin like icons to all the ends of the corner. How can I do this?
I was trying to scale up the image of the gear image in the center every time its tapped and scale down when tapped again. However, the image starts losing its quality. How can I prevent the quality from getting deteriorated?
Here's the code:
MainActivity.java
package com.rootonelabs.vicky.pulse;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.LinearInterpolator;
import android.widget.Button;
import android.widget.ImageView;
import com.gigamole.library.PulseView;
public class MainActivity extends AppCompatActivity {
PulseView pulseView;
Button btnStart, btnStop;
ImageView gear;
ImageView icon1,icon2,icon3,icon4,icon5,icon6;
int chkClick = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pulseView = (PulseView)findViewById(R.id.pv);
btnStart = (Button)findViewById(R.id.btnStart);
btnStop = (Button)findViewById(R.id.btnStop);
btnStart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pulseView.startPulse();
}
});
btnStop.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
pulseView.finishPulse();
}
});
gear = (ImageView)findViewById(R.id.gear);
gear.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
gear.animate().scaleX(1.5f).scaleY(1.5f).setDuration(500);
animateDiagonalPan();
icon1 = findViewById(R.id.icon1);
icon1.animate().translationXBy(120).translationYBy(120).setDuration(500);
if(chkClick==0)
{
pulseView.animate().alpha(0).setDuration(500);
gear.animate().scaleX(1.5f).scaleY(1.5f).setDuration(500);
pulseView.finishPulse();
chkClick = 1;
}else{
pulseView.animate().alpha(1f).setDuration(500);
gear.animate().scaleX(1f).scaleY(1f).setDuration(500);
pulseView.startPulse();
chkClick = 0;
}
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f39c12"
tools:context="com.rootonelabs.vicky.pulse.MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:weightSum="2"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnStart"
android:layout_weight="1"
android:text="START"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btnStop"
android:layout_weight="1"
android:text="STOP"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
<ImageView
android:id="#+id/gear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/men3"/>
<ImageView
android:id="#+id/icon1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_video"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_archive"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_attachment_black_24dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_video"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_action_archive"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_attachment_black_24dp"/>
<com.gigamole.library.PulseView
android:id="#+id/pv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
app:pv_alpha="70"
app:pv_color="#FFF"
app:pv_icon="#drawable/men2"
app:pv_icon_width="100dp"
app:pv_icon_height="100dp"
app:pv_interpolator="#android:anim/linear_interpolator"
app:pv_measure="height"
app:pv_spawn_period="1000" />
</RelativeLayout>

Summing totals to an edit text output and preventing negatives

I have been stuck, regrettably, in a simple math problem.
I am creating a coffee application, as seen below, that takes the "Quantity" and multiplies it by the price to create a sub-total for each drink drink. It will then take all sub-totals from the drinks and add them together for the output at the bottom which would be updated automatically.
I have worked on the code to add or subtract from the 0 (I still can't figure out a way to prevent it from going below 0, if anyone has an idea, help is appreciated).
I have it so that the first 2 buttons (in pink) work right now for Espresso and Macchiato.
Here is my Java file
package com.example.cofeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class DrinkMenu extends Activity {
EditText quantity, quantity2, total;
Button button, plus1, minus1, plus2, minus2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();
// area for the espresso
plus1 = (Button) findViewById(R.id.button6);
minus1 = (Button) findViewById(R.id.button7);
quantity = (EditText) findViewById(R.id.editText2);
// area for the macchiato
plus2 = (Button) findViewById(R.id.button8);
minus2 = (Button) findViewById(R.id.button9);
quantity2 = (EditText) findViewById(R.id.editText4);
//subtotal for espresso
//subtotal for macchiato
total = (EditText) findViewById(R.id.editText9);
plus1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1+1;
quantity.setText(Integer.toString(inum1));
}
});//plus1 button
minus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1-1;
quantity.setText(Integer.toString(inum1));
}
});//minus1 button
plus2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2+1;
quantity2.setText(Integer.toString(inum2));
}
});//plus2 button
minus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
quantity2.setText(Integer.toString(inum2));
}
});//minus2 button
// Here is where I think I should place the sub-total multiplied by the prices
// and will be out put to the total = espresso_sub_total*espress_price +
// macchiato_sub_total*macchiato_price + and so on for the other drinks
}
}
Now, It may be imperative that I set my Total price to a Text View rather than Edit Text also, thoughts?
So after implementing ideas from both users #useruser3249477 and #Shobhit I have gotten the numbers to stop going below 0 and above 10, but then I tried to add the total together in the Total area of '0'. I have updated code below for both java and the xml file. It crashes as I press the '+' button.
updated Java source code:
package com.example.cofeeshop;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class DrinkMenu extends Activity {
EditText quantity, quantity2;
//TextView total;
Button button, plus1, minus1, plus2, minus2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();
// area for the espresso
plus1 = (Button) findViewById(R.id.button6);
minus1 = (Button) findViewById(R.id.button7);
quantity = (EditText) findViewById(R.id.editText2);
// area for the macchiato
plus2 = (Button) findViewById(R.id.button8);
minus2 = (Button) findViewById(R.id.button9);
quantity2 = (EditText) findViewById(R.id.editText4);
//espresso-sub-total
//macchiato-sub-total
//total = (TextView) findViewById(R.id.textView7);
plus1.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1+1;
if (inum1 > 10) return;
quantity.setText(Integer.toString(inum1));
}
});//plus1 button
minus1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb1 = quantity.getText().toString();
int num1 = Integer.parseInt(numb1);
int inum1 = num1-1;
if (inum1 < 0) return;
quantity.setText(Integer.toString(inum1));
}
});//minus2 button
plus2.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View V){
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2+1;
if (inum2 > 10) return;
quantity2.setText(Integer.toString(inum2));
}
});//plus1 buttons
minus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
if (inum2 < 0) return;
quantity2.setText(Integer.toString(inum2));
}
});
// double subtotal = Double.parseDouble(numb1);
// Here is where I think I should place the sub-total multiplied by the prices
// and will be out put to the total = num1*3;
final TextView total = (TextView) findViewById(R.id.textView7);
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Remove previous price of these items
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal - count*3;
total.setText(newTotal);
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Add the new items price
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal + count*3;
total.setText(newTotal);
}
#Override
public void afterTextChanged(Editable editable) {}
};
quantity.addTextChangedListener(textWatcher);
quantity2.addTextChangedListener(textWatcher);
}
//order button code that is useless to this question.
}
Here is my updated XML file that is updated:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button"
android:layout_marginRight="75dp"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2"
android:background="#FFFFFF"
android:text="Drinks:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="24dp"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/orderbtn"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/orderbtn"
android:background="#FFFFFF"
android:text="Quantity:"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="24dp"
android:textStyle="bold" />
<Button
android:id="#+id/button"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/textView"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Espresso"
android:textSize="24dp" />
<Button
android:id="#+id/button2"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Macchiato"
android:textSize="24dp" />
<Button
android:id="#+id/button3"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button2"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Con Panna"
android:textSize="24dp" />
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button4"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:text="Latte"
android:textSize="24dp" />
<Button
android:id="#+id/button6"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<EditText
android:id="#+id/editText2"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_above="#+id/button2"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/button6"
android:layout_toStartOf="#+id/button6"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<Button
android:id="#+id/button7"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/editText2"
android:layout_toStartOf="#+id/editText2"
android:background="#ffff45df"
android:text="-"
android:textSize="24dp" />
<Button
android:id="#+id/button8"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button2"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<EditText
android:id="#+id/editText3"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8"
android:layout_toStartOf="#+id/button8"
android:background="#FFFFFF"
android:ems="10"
android:inputType="number"
android:text=" 0"
android:textSize="24dp" />
<EditText
android:id="#+id/editText4"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button8"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8"
android:layout_toStartOf="#+id/button8"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24sp" />
<Button
android:id="#+id/button9"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/editText3"
android:layout_toStartOf="#+id/editText3"
android:background="#ffff45df"
android:text="-"
android:textSize="24dp" />
<Button
android:id="#+id/button10"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/button8"
android:layout_alignRight="#+id/button8"
android:layout_alignTop="#+id/button3"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<Button
android:id="#+id/button11"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button4"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<Button
android:id="#+id/button12"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/button5"
android:background="#ffff45df"
android:text="+"
android:textSize="24dp" />
<EditText
android:id="#+id/editText5"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_above="#+id/button4"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/button10"
android:layout_toStartOf="#+id/button10"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<EditText
android:id="#+id/editText6"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button11"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/button11"
android:layout_toStartOf="#+id/button11"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<EditText
android:id="#+id/editText7"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/button12"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/button12"
android:layout_toStartOf="#+id/button12"
android:background="#FFFFFF"
android:digits="0123456789"
android:ems="10"
android:inputType="number"
android:text="0"
android:textSize="24dp" />
<Button
android:id="#+id/button14"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/editText6"
android:layout_toStartOf="#+id/editText6"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />
<Button
android:id="#+id/button15"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/editText7"
android:layout_toStartOf="#+id/editText7"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />
<Button
android:id="#+id/button16"
android:layout_width="30dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/editText5"
android:layout_toStartOf="#+id/editText5"
android:background="#ffff45df"
android:text="-"
android:textSize="24sp" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button7"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button9"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button16"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button14"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/button15"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="$3.00 per drink"
android:textColor="#000000"
android:textSize="20sp" />
<Button
android:id="#+id/button4"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/button3"
android:layout_marginTop="32dp"
android:background="#FFFFFF"
android:text="Americano"
android:textSize="24dp"
android:textColor="#000000" />
<Button
android:id="#+id/orderbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#drawable/buttonround"
android:text="Order"
android:textSize="24sp" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/orderbtn"
android:layout_alignBottom="#+id/orderbtn"
android:layout_alignParentLeft="true"
android:background="#FFFFFF"
android:text="Total: $"
android:textColor="#000000"
android:textSize="24sp" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView8"
android:layout_alignBottom="#+id/textView8"
android:layout_alignLeft="#+id/textView"
android:layout_alignRight="#+id/textView6"
android:layout_alignStart="#+id/textView"
android:background="#FFFFFF"
android:text="0"
android:textSize="24sp" />
</RelativeLayout>
Here are some of the errors coming out.
E/AndroidRuntime(369): FATAL EXCEPTION: main
E/AndroidRuntime(369): android.content.res.Resources$NotFoundException: String resource ID #0x0// seems to be here
E/AndroidRuntime(369): at android.content.res.Resources.getText(Resources.java:201)
A simple check will prevent negative values (using the buttons):
minus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb2 = quantity2.getText().toString();
int num2 = Integer.parseInt(numb2);
int inum2 = num2-1;
if (inum2 < 0) return;
quantity2.setText(Integer.toString(inum2));
}
});
To prevent manually entering negative values, you can set android:digits="0123456789" as #ShobhitPuri suggested.
Then for the total you'll need to set TextWatchers:
final EditText total = (EditText) findViewById(R.id.editText9);
TextWatcher textWatcher = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Remove previous price of these items
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal - count*3;
total.setText(newTotal);
}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i2, int i3) {
// Add the new items price
int count = Integer.parseInt(charSequence.toString());
// Assume total holds text of an integer
int curTotal = Integer.parseInt(total.getText().toString());
int newTotal = curTotal + count*3;
total.setText(newTotal);
}
#Override
public void afterTextChanged(Editable editable) {}
};
// Now set the TextWatcher on every count EditText
// If you have different prices, you'll need multiple TextWatchers
quantity1.addTextChangedListener(textWatcher);
quantity2.addTextChangedListener(textWatcher);
...
"I still can't figure out a way to prevent it from going below 0"
One way is you can add android:digits="0123456789" to your EditText's in xml file. This will prevent user from entering anything but these numbers.
One other way is to do a check in the onClickListener. In this you'll need to check for all the EditText's values using ed.getText() and then check if its integer and is in the range acceptable by you.
One more way is to use addTextChangedListener on EditText. You can listen you what is being inputted there. You can make a check for condition when its less than 0 or an invalid character has been put in. (Its good to limit the keyboard so as to prevent garbage entry at the first place).

How to make Views with an Invisible attribute 'Visible' after clicking a button

I have several Views, text views, and a button that have the android:visibility="invisible" attribute. My goal is to click a button that resides above these 'invisible' widgets, so that these widgets will become visible. I created another java class called 'VisibilityActivity.java" and tried the following method. But for some reason when I run the app, the button doesn't do anything. I don't know what I'm missing.
Here's the code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class VisibilityActivity extends Activity {
private View mVictim;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mVictim = findViewById(R.id.horizontalRule1);
mVictim = findViewById(R.id.TextView03);
mVictim = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mVictim.setVisibility(View.INVISIBLE);
}
};
}
Here is the xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/custom_background"
android:isScrollContainer="true"
android:orientation="vertical"
android:paddingTop="10dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/ratingsInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/yourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp" />
<Button
android:id="#+id/submitRatingButton"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="10dp"
android:background="#drawable/custom_button"
android:text="#string/submitRating"
android:textColor="#color/black1" />
<View
android:id="#+id/horizontalRule1"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_gravity="center"
android:text="#string/summaryInfo"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/black1"
android:visibility="invisible" />
<View
android:id="#+id/horizontalRule2"
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/black1"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:layout_marginBottom="5dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/ourRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="10dp"
android:visibility="invisible" />
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:text="#string/overallRating"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/black1"
android:visibility="invisible" />
<RatingBar
android:id="#+id/ratingBar3"
android:color="#color/black1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:stepSize=".01"
android:layout_marginBottom="40dp"
android:visibility="invisible" />
<Button
android:id="#+id/saveContinueButton3"
android:layout_width="275dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="15dp"
android:background="#drawable/custom_button"
android:text="#string/saveContinue"
android:textColor="#color/black1"
android:onClick="onSaveAndContinue3Clicked"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
Thanks. Help would be greatly appreciated!
I am updating user936414's answer.
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
if( mText.getVisibility() == View.INVISIBLE )
mText.setVisibility(View.VISIBLE);
else
mText.setVisibility(View.INVISIBLE);
if( mRule1.getVisibility() == View.INVISIBLE )
mRule1.setVisibility(View.VISIBLE);
else
mRule1.setVisibility(View.INVISIBLE);
if( mRule2.getVisibility() == View.INVISIBLE )
mRule2.setVisibility(View.VISIBLE);
else
mRule2.setVisibility(View.INVISIBLE);
}
};
Also you might want to experiment with View.GONE.
findViewById(R.id.ratingBar3).setVisibility(View.VISIBLE);
findViewById(R.id.saveContinueButton3).setVisibility(View.VISIBLE);
you made it invisible view invisible again.. try the above code
Try
public class VisibilityActivity extends Activity {
private TextView mText;
private View mRule1, mRule2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_property3);
mText= (TextView)findViewById(R.id.horizontalRule1);
mRule1 = findViewById(R.id.TextView03);
mRule2 = findViewById(R.id.horizontalRule2);
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(mVisibleListener);
}
OnClickListener mVisibleListener = new OnClickListener() {
public void onClick(View v) {
mText.setVisibility(View.VISIBLE);
mRule1.setVisibility(View.VISIBLE);
mRule2.setVisibility(View.VISIBLE);
}
};
}
Button submitRating = (Button) findViewById(R.id.submitRatingButton);
submitRating.setOnClickListener(new View.onClickListener)
{
#Override
public void onClick(View v)
{
//Insert your code here
}
}

button error viewflipper android

i`m trying to do android viewflipper. i have 3 views i would to be changed on button click (next & previous). i wrote these code and the log said there is button widget error. How to solve this?
my xml layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/radial_background">
<RelativeLayout
android:id="#+id/relative_buy1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/buy_previous"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true">
</Button>
<Button
android:id="#+id/buy_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/buy_previous">
</Button>
<Button
android:id="#+id/buy_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/buy_next">
</Button>
</RelativeLayout>
<RelativeLayout
android:layout_alignTop="#id/relative_buy1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<ViewFlipper
android:id="#+id/ViewFlipper01"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/buy_item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
</TextView>
<EditText
android:id="#+id/buy_item_name_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#id/buy_item_name">
</EditText>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/buy_ques_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
</TextView>
<Button
android:id="#+id/buy_food_cat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#id/buy_ques_category">
</Button>
<Button
android:id="#+id/buy_education_cat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#id/buy_food_cat_button">
</Button>
<Button
android:id="#+id/buy_personal_cat_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#id/buy_education_cat_button">
</Button>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/buy_ques_cost"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true">
</TextView>
<EditText
android:id="#+id/buy_ques_cost_et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#id/buy_ques_cost">
</EditText>
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
my activity
package apps.questions;
import android.app.Activity;
import android.content.ContentValues;
import android.graphics.PixelFormat;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.ViewFlipper;
import apps.project.R;
public class BuyActivity extends Activity implements OnClickListener {
ViewFlipper vf;
private static final String FOOD = "Food/Drinks";
private static final String EDUCATION = "Education";
private static final String PERSONAL = "Personal";
Button food_cat;
Button education_cat;
Button personal_cat;
Button next;
Button previous;
Button submit;
TextView item_name;
TextView item_cost;
TextView item_cat;
EditText name;
EditText cost;
ContentValues cv;
String iName;
double iPrice;
String iCategory;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.buy_advice_layout);
inflateView();
/*food_cat.setOnClickListener(this);
education_cat.setOnClickListener(this);
personal_cat.setOnClickListener(this);*/
}
public void onAttachedToWindow() {
super.onAttachedToWindow();
Window window = getWindow();
window.setFormat(PixelFormat.RGBA_8888);
}
private void inflateView() {
// TODO Auto-generated method stub
cv = new ContentValues();
vf = (ViewFlipper) findViewById(R.id.ViewFlipper01);
next = (Button)findViewById(R.id.buy_next);
previous = (Button)findViewById(R.id.buy_previous);
submit = (Button)findViewById(R.id.buy_submit);
/*food_cat = (Button)findViewById(R.id.buy_food_cat_button);
education_cat = (Button)findViewById(R.id.buy_education_cat_button);
personal_cat = (Button)findViewById(R.id.buy_personal_cat_button);*/
item_name = (TextView)findViewById(R.id.buy_item_name);
item_name.setText("Please enter the name of the item you wish to buy");
item_cat = (TextView)findViewById(R.id.buy_ques_category);
item_cat.setText("Please select the category of the item you wish to buy by clicking on the category button");
item_cost = (TextView)findViewById(R.id.buy_ques_cost);
item_cost.setText("Please enter the price of the item you wish to buy");
name = (EditText)findViewById(R.id.buy_item_name_et);
cost = (EditText)findViewById(R.id.buy_ques_cost_et);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == next) {
vf.setAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.push_left_in));
vf.showNext();
}
if (v == previous) {
vf.setAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.push_left_out));
vf.showPrevious();
}
if (v == submit) {
//vf.showPrevious();
}
if (v == food_cat) {
iCategory = FOOD;
}
if (v == education_cat) {
iCategory = EDUCATION;
}
if (v == personal_cat) {
iCategory = PERSONAL;
}
}
}
It looks like you are missing the assignment of your click listeners to your buttons. Use the following code just below your find view by id calls for the buttons.
next = (Button)findViewById(R.id.buy_next);
previous = (Button)findViewById(R.id.buy_previous);
submit = (Button)findViewById(R.id.buy_submit);
//New code
next.setOnClickListener(this);
previous.setOnClickListener(this);
submit.setOnClickListener(this);

Categories

Resources