How to make a button reappear in different place when clicked? - java

I started to learn coding in Android studio few weeks ago. Now I'm trying to make an application which has 9 buttons, but you always see just one of them. When you click on visible button it should disappear and show another one and so on.
This is what I've made so far:
MainActivity.java
public class MainActivity extends AppCompatActivity {
Button shownButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
shownButton = (Button) findViewById(R.id.b1);
shownButton.setVisibility(View.VISIBLE);
}
public void buttonDisappear(View view){
shownButton.setVisibility(View.INVISIBLE);
Random generatedNumber = new Random();
int buttonNumber = generatedNumber.nextInt(10 - 0) + 10;
String nextShownButton = "b"+String.valueOf(buttonNumber);
int nextShownButtonId = getResources().getIdentifier(nextShownButton, "id", getPackageName());
Button shownButton = (Button) findViewById(nextShownButtonId);
shownButton.setVisibility(View.VISIBLE);
}
}
In every button property "onClick" I set a function "buttonDisappear". Everything looks fine to me, no errors, but when I run the program and click on the first button (b1), application just stops. Can you tell me why it is happening and how can I fix it? Thank you.
UPDATE
activity_main.xml
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b9" />
<Button
android:id="#+id/b3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b5" />
<Button
android:id="#+id/b4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b5" />
<Button
android:id="#+id/b5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b7" />
<Button
android:id="#+id/b6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b2" />
<Button
android:id="#+id/b7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginTop="48dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/b2" />
<Button
android:id="#+id/b9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:onClick="buttonDisappear"
android:text="#string/mygtukas"
android:visibility="invisible"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />

you said you have only 9 buttons and the name starts from b1-b9
while you are generating a number that is always greater or equal to than 10
public void buttonDisappear(Button view){
shownButton.setVisibility(View.INVISIBLE);
Random generatedNumber = new Random();
int buttonNumber = generatedNumber.nextInt(9)+1;
String nextShownButton = "b"+String.valueOf(buttonNumber);
int nextShownButtonId = getResources().getIdentifier(nextShownButton, "id", getPackageName());
Button shownButton = (Button) findViewById(nextShownButtonId);
shownButton.setVisibility(View.VISIBLE);
}

Related

Nested Spinner Android Design Issues ConstraintLayout

I'm working with spinner for the first time, and I don't understand why the second spinner doesn't look exactly like the first one even though they were created practically the same (the only difference is the data). These are nested.
The design implemented it with "constraint layout"
<Spinner
android:id="#+id/spCategorie"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/categorieText"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/categorieText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:text="Categorie:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Spinner
android:id="#+id/spProduct"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="32dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/productText"
app:layout_constraintTop_toBottomOf="#+id/spCategorie" />
This is how it looks in the emulator
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, R.layout.support_simple_spinner_dropdown_item, productsCat);
arrayAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
categorie.setAdapter(arrayAdapter);
categorie.setOnItemSelectedListener(new SpinnersEvents());
product.setOnItemSelectedListener(new SpinnersEvents());
private class SpinnersEvents implements AdapterView.OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
if (parent.getId() == R.id.spCategorie){
String[] productsName = getProductName(productsCat[position]);
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(getBaseContext(),R.layout.support_simple_spinner_dropdown_item, productsName);
arrayAdapterChild.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
product.setAdapter(arrayAdapterChild);
}else{
price.setText(String.valueOf(tempList.get(position).getPrice()));
imgPrd.setImageResource(tempList.get(position).getImage());
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
}
Please try this in your code may help you, you can also set wrapcontent in spinner or increase the size to view text proper
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
tools:ignore="MissingConstraints">
<TextView
android:id="#+id/categorieText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="Categorie:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="RtlCompat" />
<Spinner
android:id="#+id/spCategorie"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toEndOf="#+id/categorieText"/>
<TextView
android:id="#+id/productText"
android:layout_width="73dp"
android:layout_height="28dp"
android:layout_marginTop="32dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
android:text="Product:"
android:textAlignment="viewStart"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/categorieText"/>
<Spinner
android:id="#+id/spproduct"
android:layout_width="0dp"
android:layout_height="28dp"
android:layout_marginTop="32dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginBottom="10dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_chainStyle="spread"
app:layout_constraintHorizontal_weight="1"
app:layout_constraintTop_toBottomOf="#+id/spCategorie"
app:layout_constraintStart_toEndOf="#+id/categorieText"/>
</androidx.constraintlayout.widget.ConstraintLayout>
The solution was in the code, not in the layout.
The second spinner must be:
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(MainActivity.this,R.layout.support_simple_spinner_dropdown_item, productsName);
instead:
ArrayAdapter<String> arrayAdapterChild = new ArrayAdapter<String>(getBaseContext(),R.layout.support_simple_spinner_dropdown_item, productsName);
In this case, calling the getBaseContext() method generated that particular problem.
Sorry for the confusion.

Button not responding when clicked

I am new to Android Programming and I wanted to make a simple app where the user would enter there name and after they click the button, it would say Hello (name of person). Also the button will change to thanks for clicking me after it is clicked. I have looked through the code but I am having a hard time finding the error. I have the code down below. I will also add the xml under the code.
code
public class MainActivity extends AppCompatActivity {
private EditText yourName;
private TextView outputName;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
yourName = (EditText)findViewById(R.id.inputText);
outputName = (TextView)findViewById(R.id.outputText);
}
public void printHello (View view){
Button button =(Button) view;
((Button)view).setText("Thanks for Clicking Me!");
yourName =(EditText)findViewById(R.id.inputText);
outputName =(TextView)findViewById(R.id.outputText);
outputName.setText("Hello, "+ yourName.getText());
outputName.setVisibility(View.VISIBLE);
}
}
Here is the xml also
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="328dp"
android:text="Talk to Me"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.078" />
<TextView
android:id="#+id/Label1"
android:layout_width="0dp"
android:layout_height="31dp"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="32dp"
android:text=" Please Enter Your Name"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<TextView
android:id="#+id/outputText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="88dp"
android:text="TextView"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/inputText"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/inputText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginStart="8dp"
android:layout_marginTop="72dp"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Label1" />
</android.support.constraint.ConstraintLayout>
You need to have an android:onClick="printHello" inside of the button XML to link the button to the function inside of the Java class. Then any time the button is clicked, it calls printHello and does what you need to.

Unfortunately MyApp has stopped working and there are no visible errors in my code

Here is my MainActivity.java Code:
package abhishekgidde.games.tictactoe;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView r1c1=(ImageView)findViewById(R.id.r1c1);
ImageView r1c2=(ImageView)findViewById(R.id.r1c2);
ImageView r1c3=(ImageView)findViewById(R.id.r1c3);
ImageView r2c1=(ImageView)findViewById(R.id.r2c1);
ImageView r2c2=(ImageView)findViewById(R.id.r2c2);
ImageView r2c3=(ImageView)findViewById(R.id.r2c3);
ImageView r3c1=(ImageView)findViewById(R.id.r3c1);
ImageView r3c2=(ImageView)findViewById(R.id.r3c2);
ImageView r3c3=(ImageView)findViewById(R.id.r3c3);
ImageView mr1c1=(ImageView)findViewById(R.id.mr1c1);
ImageView mr1c2=(ImageView)findViewById(R.id.mr1c2);
ImageView mr1c3=(ImageView)findViewById(R.id.mr1c3);
ImageView mr2c1=(ImageView)findViewById(R.id.mr2c1);
ImageView mr2c2=(ImageView)findViewById(R.id.mr2c2);
ImageView mr2c3=(ImageView)findViewById(R.id.mr2c3);
ImageView mr3c1=(ImageView)findViewById(R.id.mr3c1);
ImageView mr3c2=(ImageView)findViewById(R.id.mr3c2);
ImageView mr3c3=(ImageView)findViewById(R.id.mr3c3);
int turn=1;
public void play(View v)
{
String who_called_me = v.getTag().toString();
if(turn%2==0)
{
//Player1 as even turns belong to p1
//who_called_me the button at r1c1 will call function with int 11 so player1's bottle is visible
switch(who_called_me)
{
case "11": r1c1.setAlpha(1f);
case "12": r1c2.setAlpha(1f);
case "13": r1c3.setAlpha(1f);
case "21": r2c1.setAlpha(1f);
case "22": r2c2.setAlpha(1f);
case "23": r2c3.setAlpha(1f);
case "31": r3c1.setAlpha(1f);
case "32": r3c2.setAlpha(1f);
case "33": r3c3.setAlpha(1f);
}
turn++;
}
else if(turn%2==1)
{
//Player2 as odd turns belong to p2
//who_called_me the button at r1c1 will call function with int 11 so player1's bottle is visible
switch(who_called_me)
{
case "11": mr1c1.setAlpha(1f);
case "12": mr1c2.setAlpha(1f);
case "13": mr1c3.setAlpha(1f);
case "21": mr2c1.setAlpha(1f);
case "22": mr2c2.setAlpha(1f);
case "23": mr2c3.setAlpha(1f);
case "31": mr3c1.setAlpha(1f);
case "32": mr3c2.setAlpha(1f);
case "33": mr3c3.setAlpha(1f);
}
turn++;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//here goes my eraser
r1c1.setAlpha(0f);
r1c2.setAlpha(0f);
r1c3.setAlpha(0f);
r2c1.setAlpha(0f);
r2c2.setAlpha(0f);
r2c3.setAlpha(0f);
r3c1.setAlpha(0f);
r3c2.setAlpha(0f);
r3c3.setAlpha(0f);
mr1c1.setAlpha(0f);
mr1c2.setAlpha(0f);
mr1c3.setAlpha(0f);
mr2c1.setAlpha(0f);
mr2c2.setAlpha(0f);
mr2c3.setAlpha(0f);
mr3c1.setAlpha(0f);
mr3c2.setAlpha(0f);
mr3c3.setAlpha(0f);
}
/* public void clear_all() {
r1c1.setAlpha(0f);
r1c2.setAlpha(0f);
r1c3.setAlpha(0f);
r2c1.setAlpha(0f);
r2c2.setAlpha(0f);
r2c3.setAlpha(0f);
r3c1.setAlpha(0f);
r3c2.setAlpha(0f);
r3c3.setAlpha(0f);
mr1c1.setAlpha(0f);
mr1c2.setAlpha(0f);
mr1c3.setAlpha(0f);
mr2c1.setAlpha(0f);
mr2c2.setAlpha(0f);
mr2c3.setAlpha(0f);
mr3c1.setAlpha(0f);
mr3c2.setAlpha(0f);
mr3c3.setAlpha(0f);
}*/
}
and my xml design is :
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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="abhishekgidde.games.tictactoe.MainActivity">
<ImageView
android:id="#+id/r3c3"
android:layout_width="105dp"
android:layout_height="120dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.861"
app:srcCompat="#drawable/harry" />
<ImageView
android:id="#+id/r3c2"
android:layout_width="105dp"
android:layout_height="120dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.861"
app:srcCompat="#drawable/harry" />
<ImageView
android:id="#+id/r3c1"
android:layout_width="105dp"
android:layout_height="120dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.882"
app:srcCompat="#drawable/harry" />
<ImageView
android:id="#+id/r2c1"
android:layout_width="105dp"
android:layout_height="120dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498"
app:srcCompat="#drawable/harry" />
<ImageView
android:id="#+id/r1c1"
android:layout_width="105dp"
android:layout_height="120dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.115"
app:srcCompat="#drawable/harry" />
<ImageView
android:id="#+id/r2c2"
android:layout_width="105dp"
android:layout_height="120dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498"
app:srcCompat="#drawable/harry" />
<ImageView
android:id="#+id/r2c3"
android:layout_width="105dp"
android:layout_height="120dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498"
app:srcCompat="#drawable/harry" />
<ImageView
android:id="#+id/r1c3"
android:layout_width="105dp"
android:layout_height="120dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.138"
app:srcCompat="#drawable/harry" />
<ImageView
android:id="#+id/r1c2"
android:layout_width="105dp"
android:layout_height="120dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.501"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.138"
app:srcCompat="#drawable/harry" />
<ImageView
android:id="#+id/board"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginTop="2dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.494"
app:srcCompat="#drawable/board" />
<ImageView
android:id="#+id/mr3c3"
android:layout_width="73dp"
android:layout_height="110dp"
android:layout_marginLeft="295dp"
android:layout_marginTop="337dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/milk" />
<ImageView
android:id="#+id/mr3c2"
android:layout_width="73dp"
android:layout_height="110dp"
android:layout_marginLeft="156dp"
android:layout_marginTop="347dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/milk" />
<ImageView
android:id="#+id/mr3c1"
android:layout_width="73dp"
android:layout_height="110dp"
android:layout_marginLeft="25dp"
android:layout_marginTop="337dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/milk" />
<ImageView
android:id="#+id/mr2c1"
android:layout_width="73dp"
android:layout_height="110dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="200dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/milk" />
<ImageView
android:id="#+id/mr2c2"
android:layout_width="73dp"
android:layout_height="110dp"
android:layout_marginLeft="156dp"
android:layout_marginTop="195dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/milk" />
<ImageView
android:id="#+id/mr2c3"
android:layout_width="73dp"
android:layout_height="110dp"
android:layout_marginLeft="295dp"
android:layout_marginTop="200dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/milk" />
<ImageView
android:id="#+id/mr1c3"
android:layout_width="73dp"
android:layout_height="110dp"
android:layout_marginLeft="295dp"
android:layout_marginTop="55dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/milk" />
<ImageView
android:id="#+id/mr1c2"
android:layout_width="73dp"
android:layout_height="110dp"
android:layout_marginLeft="156dp"
android:layout_marginTop="64dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/milk" />
<ImageView
android:id="#+id/mr1c1"
android:layout_width="73dp"
android:layout_height="110dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="64dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/milk" />
<Button
android:id="#+id/button"
android:layout_width="67dp"
android:layout_height="85dp"
android:layout_marginLeft="301dp"
android:text="Claim"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintVertical_bias="0.849"
android:tag="33"/>
<Button
android:id="#+id/button9"
android:layout_width="67dp"
android:layout_height="85dp"
android:layout_marginLeft="159dp"
android:text="Claim"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.849"
android:tag="32"/>
<Button
android:id="#+id/button8"
android:layout_width="67dp"
android:layout_height="85dp"
android:layout_marginLeft="22dp"
android:text="Claim"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.849"
android:tag="31"/>
<Button
android:id="#+id/button7"
android:layout_width="67dp"
android:layout_height="85dp"
android:layout_marginLeft="16dp"
android:text="Claim"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:tag="21"/>
<Button
android:id="#+id/button6"
android:layout_width="67dp"
android:layout_height="85dp"
android:layout_marginLeft="159dp"
android:text="Claim"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:tag="22"/>
<Button
android:id="#+id/button5"
android:layout_width="67dp"
android:layout_height="85dp"
android:layout_marginLeft="295dp"
android:text="Claim"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:tag="23"/>
<Button
android:id="#+id/button4"
android:layout_width="67dp"
android:layout_height="85dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="295dp"
android:layout_marginTop="8dp"
android:onClick="play"
android:tag="13"
android:text="Claim"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.136" />
<Button
android:id="#+id/button3"
android:layout_width="67dp"
android:layout_height="85dp"
android:layout_marginLeft="159dp"
android:layout_marginTop="64dp"
android:onClick="play"
android:tag="12"
android:text="Claim"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="67dp"
android:layout_height="85dp"
android:layout_marginLeft="25dp"
android:onClick="play"
android:text="Claim"
app:layout_constraintLeft_toLeftOf="parent"
android:tag="11"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="80dp" />
</android.support.constraint.ConstraintLayout>
whenever I build the apk the apk is generated successfully and when I run the app it crashes.
here what I am trying to do it make a tic-tac-toe game
the logic I tried to implement is a main play board image it the base and on it there are two images in each box (referred as row 1 column 1 for first box r1c1)
you should call setContentView before calling findViewById. implement onCreate method in your activity and set content view first.
setContentView(R.layout.your_layout_name);
r1c1 = (ImageView) findViewById(R.id.r1c1);
...
Change your onCreate method like this :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView r1c1=(ImageView)findViewById(R.id.r1c1);
ImageView r1c2=(ImageView)findViewById(R.id.r1c2);
ImageView r1c3=(ImageView)findViewById(R.id.r1c3);
ImageView r2c1=(ImageView)findViewById(R.id.r2c1);
ImageView r2c2=(ImageView)findViewById(R.id.r2c2);
ImageView r2c3=(ImageView)findViewById(R.id.r2c3);
ImageView r3c1=(ImageView)findViewById(R.id.r3c1);
ImageView r3c2=(ImageView)findViewById(R.id.r3c2);
ImageView r3c3=(ImageView)findViewById(R.id.r3c3);
ImageView mr1c1=(ImageView)findViewById(R.id.mr1c1);
ImageView mr1c2=(ImageView)findViewById(R.id.mr1c2);
ImageView mr1c3=(ImageView)findViewById(R.id.mr1c3);
ImageView mr2c1=(ImageView)findViewById(R.id.mr2c1);
ImageView mr2c2=(ImageView)findViewById(R.id.mr2c2);
ImageView mr2c3=(ImageView)findViewById(R.id.mr2c3);
ImageView mr3c1=(ImageView)findViewById(R.id.mr3c1);
ImageView mr3c2=(ImageView)findViewById(R.id.mr3c2);
ImageView mr3c3=(ImageView)findViewById(R.id.mr3c3);
int turn=1;
//here goes my eraser
r1c1.setAlpha(0f);
r1c2.setAlpha(0f);
r1c3.setAlpha(0f);
r2c1.setAlpha(0f);
r2c2.setAlpha(0f);
r2c3.setAlpha(0f);
r3c1.setAlpha(0f);
r3c2.setAlpha(0f);
r3c3.setAlpha(0f);
mr1c1.setAlpha(0f);
mr1c2.setAlpha(0f);
mr1c3.setAlpha(0f);
mr2c1.setAlpha(0f);
mr2c2.setAlpha(0f);
mr2c3.setAlpha(0f);
mr3c1.setAlpha(0f);
mr3c2.setAlpha(0f);
mr3c3.setAlpha(0f);
}
set your (ImageView)findViewById(R.id.rici) initializations below setContentView(R.layout.activity_main) in your onCreate methode

How do I parse a double and show it in an EdiText after calculation?

I am a beginner, please just tell me what Im doing wrong with a hint. Dont tell me the solution just a good hint. This is my code:
package com.dietel.preserve;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.Editable;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.EditText;
import android.view.View.OnClickListener;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "myApp";
double fedExPivot1 = 35.00;
double fedExPivot2 = 45.00;
double fed1=850.00;
double fed2= 750.00;
double fedAdd= 2500.00;
String p;
double convertedWeight;
double outShippingCost;
String s;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText weight = (EditText) findViewById(R.id.editText);
p= weight.getText().toString();
convertedWeight= Double.parseDouble(p);
final EditText textvew= (EditText) findViewById(R.id.editText4);
Button calculate = (Button) findViewById(R.id.button);
calculate.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
outShippingCost =shippingCost(convertedWeight);
s=Double.toString(outShippingCost);
textvew.setText(s);
}
});
}
public double shippingCost(double w) {
if (w < fedExPivot1) {
w= w* fed1+fedAdd;
w=w/100;
} else if (w > fedExPivot2){
w= w*fed2/100;
}else if(w>= fedExPivot1){
w= w*fed1/100;
}
return w;
}
}
I'm just trying to get a value from an editText and then convert it into a double and then show it inside another ediText after calculation. Please help me.
This is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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.dietel.preserve.MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="39dp"
android:layout_marginTop="150dp"
android:text="#string/weight"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:ignore="RtlHardcoded" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginTop="34dp"
android:text="#string/sale_price"
android:textSize="18sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView"
tools:ignore="RtlHardcoded" />
<Button
android:id="#+id/button"
android:layout_width="128dp"
android:layout_height="39dp"
android:text="#string/calculate"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintHorizontal_bias="0.566"
android:layout_marginTop="77dp"
app:layout_constraintTop_toBottomOf="#+id/editText"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/editText4"
app:layout_constraintVertical_bias="0.0" />
<EditText
android:id="#+id/editText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:layout_marginRight="53dp"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:layout_marginTop="137dp"
tools:ignore="LabelFor,RtlHardcoded"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1.0" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/editText"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/button"
app:layout_constraintVertical_bias="0.0"
android:layout_marginRight="53dp"
app:layout_constraintRight_toRightOf="parent"
tools:ignore="LabelFor,RtlHardcoded"
android:layout_marginLeft="8dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintHorizontal_bias="1.0" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="114dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="8dp"
android:text="#string/total_cost"
android:textSize="18sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView2"
app:layout_constraintVertical_bias="0.992"
tools:ignore="RtlHardcoded" />
<TextView
android:id="#+id/textView4"
android:layout_width="112dp"
android:layout_height="45dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="256dp"
android:layout_marginTop="92dp"
android:text="#string/enter_info2"
android:textSize="24sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
tools:ignore="RtlHardcoded"
tools:text="Enter Info:" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="104dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="53dp"
android:ems="10"
android:inputType="textPersonName"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
tools:ignore="LabelFor,RtlHardcoded" />
</android.support.constraint.ConstraintLayout>
You asked for a hint: you're fetching the value of the first field in a wrong time, so when you click the button you're processing a wrong value. Examine convertedWeight carefully.

Menu actions and sums in Android app

So I have been trying to develop an app that adds a select quantity together in a menu, all while in Android studio/eclipse.
I am listing off drinks and to the right of each drink is a "-" (the minus) and "+" (the plus) signs, which are buttons respectively, with a "0" in the middle.
At the bottom of the screen, I have a "Total" section.
I have a screen shot of what the menu appears as thus far:
I am looking to have the + and - buttons affect the number that lies in the middle of them. So when the "+" is pressed it adds 1 and if pressed again, it adds 1 and so on, the "-" button will take 1 away from the number back to zero if need be.
It should be able to tally the total price of each drink all together and output the sum to the
"$ 0.00" near the total. The "Order" button just takes me to another screen, which will be tackled at a later date.
I have tried searching for a good many hours and there are so many links I am drowning that I can not find any help that sticks.
I know that the Java code should use the ID of the EditText's but I am confused as to how.
I will post what code I have for the xml and java code. If there is also a manifest I have to add, I can not find it.
drinkmenu.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:id="#+id/mainmenulayout"
android:background="#34D6D9">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Drinks:"
android:id="#+id/textView"
android:background="#FFFFFF"
android:textStyle="bold"
android:textSize="24dp"
android:layout_toStartOf="#+id/textView2"
android:layout_toLeftOf="#+id/textView2"
android:layout_marginRight="75dp"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/button"
android:layout_alignEnd="#+id/button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Quantity:"
android:id="#+id/textView2"
android:textSize="24dp"
android:textStyle="bold"
android:background="#FFFFFF"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/orderbtn"
android:layout_alignEnd="#+id/orderbtn" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Espresso"
android:id="#+id/button"
android:layout_below="#+id/textView"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:textSize="24dp" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Macciato"
android:id="#+id/button2"
android:layout_below="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:textSize="24dp"
android:background="#FFFFFF" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Con Panna"
android:id="#+id/button3"
android:layout_below="#+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:textSize="24dp" />
<Button
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Americano"
android:id="#+id/button4"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:textSize="24dp"
android:background="#FFFFFF" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="240dp"
android:layout_height="wrap_content"
android:text="Latte"
android:id="#+id/button5"
android:layout_below="#+id/button4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="30dp"
android:background="#FFFFFF"
android:textSize="24dp" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button6"
android:layout_alignTop="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:background="#ffff45df"
android:textSize="24dp" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText2"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/button6"
android:layout_toStartOf="#+id/button6"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_above="#+id/button2" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button7"
android:layout_alignTop="#+id/button"
android:layout_toLeftOf="#+id/editText2"
android:layout_toStartOf="#+id/editText2"
android:background="#ffff45df"
android:textSize="24dp" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button8"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText3"
android:layout_toStartOf="#+id/button8"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText4"
android:layout_toStartOf="#+id/button8"
android:text=" 0"
android:textSize="24sp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/button8"
android:layout_alignBottom="#+id/button8" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button9"
android:layout_toStartOf="#+id/editText3"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button2"
android:layout_toLeftOf="#+id/editText3" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button10"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button3"
android:layout_alignRight="#+id/button8"
android:layout_alignEnd="#+id/button8" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button11"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="+"
android:id="#+id/button12"
android:background="#ffff45df"
android:textSize="24dp"
android:layout_alignTop="#+id/button5"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText5"
android:layout_toStartOf="#+id/button10"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/button10"
android:layout_above="#+id/button4" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText6"
android:layout_toStartOf="#+id/button11"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/button11"
android:layout_alignBottom="#+id/button11" />
<EditText
android:layout_width="30dp"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText7"
android:layout_toStartOf="#+id/button12"
android:text=" 0"
android:textSize="24dp"
android:background="#FFFFFF"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/button12"
android:layout_alignBottom="#+id/button12" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button14"
android:layout_toStartOf="#+id/editText6"
android:background="#ffff45df"
android:textSize="24sp"
android:layout_alignTop="#+id/button4"
android:layout_toLeftOf="#+id/editText6" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button15"
android:layout_toStartOf="#+id/editText7"
android:background="#ffff45df"
android:textSize="24sp"
android:layout_alignTop="#+id/button5"
android:layout_toLeftOf="#+id/editText7" />
<Button
android:layout_width="30dp"
android:layout_height="wrap_content"
android:text="-"
android:id="#+id/button16"
android:layout_toStartOf="#+id/editText5"
android:background="#ffff45df"
android:textSize="24sp"
android:layout_alignTop="#+id/button3"
android:layout_toLeftOf="#+id/editText5" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText8"
android:text="Total:"
android:background="#FFFFFF"
android:textSize="24sp"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/textView"
android:layout_toStartOf="#+id/textView" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/editText9"
android:layout_alignBottom="#+id/editText8"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:text=" $ 0.00"
android:background="#FFFFFF"
android:textSize="24sp"
android:layout_alignTop="#+id/editText8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Order"
android:id="#+id/orderbtn"
android:textSize="24sp"
android:background="#drawable/buttonround"
android:layout_alignBottom="#+id/editText9"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<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"/>
</RelativeLayout>
java code:
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.widget.Button;
public class DrinkMenu extends Activity {
Button button;
Button buttton6; Button buttton7; Button buttton8; Button buttton9;
Button buttton10; Button buttton11; Button buttton16; Button buttton14;
Button buttton12; Button buttton15;
// EditText editText2; EditText editText4; EditText editText5;
// EditText editText6; EditText editText7; EditText editText9;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drinkmenu);
addListenerOnButton();
}
private void addListenerOnButton() {
final Context context = this;
button = (Button) findViewById(R.id.orderbtn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Intent intent = new Intent(context, ThirdPartyPaymentMethod.class);
startActivity(intent);
}
});
}
}
I already have my manifest to allow button actions.
I know there are methods out there, but my searches have ended in my frustration. Any help would be appreciated. I am new to this, I enjoy the front end, but the back end is new.
I'll do a part and for remaining you have to do.
Consider,
- is a button with id 'button1' and '+' has button2
Quantity is a EditText which has id qty.
Right, coming to the code :
public class MainActivity extends Activity {
EditText quantity;
Button plus,minus;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
plus = (Button) findViewById(R.id.button1);
minus = (Button) findViewById(R.id.button2);
quantity = (EditText) findViewById(R.id.qty);
plus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb = quantity.getText().toString();
int num1 = Integer.parseInt(numb);
int inum = num1+1;
quantity.setText(Integer.toString(inum));
}
});
minus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String numb = quantity.getText().toString();
int num1 = Integer.parseInt(numb);
int inum = num1-1;
quantity.setText(Integer.toString(inum));
}
});
}
}
For sub total, you just need to multiply quantity and the item price. For the Total Price, you just need to add all the subtotals. Hope it helps!
UPDATE about Catching Zero:
You are getting the quantity here.
String numb = quantity.getText().toString();
int num1 = Integer.parseInt(numb);
So, if the number is 0, perform if else
if (num1==0)
{
//Create a alert showing your quantity is zero.
//here i use toast to display
Toast.makeText(this,"Your quantity is Zero. You can't reduce the quantity", Toast.LENGTH_LONG).show();
}
else
{
// do the stuffs here
}
what you want to do is something like this:
Button button = (Button) findViewById(R.id.my_button);
final TextView textView = (TextView) findViewById(R.id.my_text_view);
button.setOnClickListener(new View.OnClickListener() {
#Overrride
public void onClick(View aView) {
textView.setText("Hi");
}
});
You can write this in the onCreate() method of the Activity. Not tested yet, but this will change the text of the TextView to the desired value. Let me know if I have understood your problem correctly.

Categories

Resources