I'm making a simple "novelty" app that counts how many sneezes are done, it's more of a fun app that builds up experience until I do my huge project during the summer. I have two buttons, one adds a sneeze and the other clears how many sneezes there currently are. It holds the highest number of sneezes that there were previously. The problem is, the TextViews never update, they only initialize to zero. I used a Toast.makeText() to make sure that the buttons are working (they are). Any help is appreciated. Thanks
Java code:
public class MainActivity extends ActionBarActivity {
private int record_number = 0;
private int current_number = 0;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
Button add_one = (Button) findViewById(R.id.addone);
Button clear_1 = (Button) findViewById(R.id.clear);
add_one.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
current_number += 1;
Toast.makeText(MainActivity.this, "Button Clicked " + current_number, Toast.LENGTH_SHORT).show();
}
});
clear_1.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
current_number = 0;
Toast.makeText(MainActivity.this, "Button Clicked", Toast.LENGTH_SHORT).show();
}
});
TextView rec_text = (TextView) findViewById(R.id.record_num);
TextView cur_text = (TextView) findViewById(R.id.current_num);
if (current_number >= record_number)
{
record_number = current_number;
}
rec_text.setText(String.valueOf(record_number));
cur_text.setText(String.valueOf(current_number));
}
}
XML:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.michail.sneezecounter.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Sneeze Counter"
android:id="#+id/title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Record Number of Sneezes:"
android:id="#+id/textView"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:layout_marginTop="72dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add one Sneeze"
android:id="#+id/addone"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="76dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Clear Current Number of Sneezes"
android:id="#+id/clear"
android:layout_marginBottom="13dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/record_num"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:singleLine="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Current Number of Sneezes:"
android:id="#+id/currentLabel"
android:layout_centerVertical="true"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_below="#+id/currentLabel"
android:layout_alignLeft="#+id/record_num"
android:layout_alignStart="#+id/record_num"
android:layout_marginTop="21dp"
android:id="#+id/current_num" />
</RelativeLayout>
You need to update the text of the TextViews inside the onClickListeners. In fact, all your logic for counting, clearing, and recording the record needs to be done in your onClickListeners (or methods called by them). Right now you only do it once in onCreate, then never again. You can do this in onCreate:
final TextView cur_text = (TextView) findViewById(R.id.current_num);
add_one.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
current_number += 1;
cur_text.setText(Integer.toString(current_number);
}
});
And similar for the other TextView & onClickListener.
You only set the contents of your TextViews once. You should update them every time you get a click event. Specifically:
add_one.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
current_number += 1;
if (current_number >= record_number)
{
record_number = current_number;
rec_text.setText(String.valueOf(record_number));
}
cur_text.setText(String.valueOf(current_number));
}
});
clear_1.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
current_number = 0;
cur_text.setText(String.valueOf(current_number));
}
});
NOTE: if your variables current_number, record_number, cur_text, and rec_text aren't already declared as class member variables, you'll want to do that do that so that they're accessible once you leave the scope of the method you're doing all this in (I assume it's onCreate(...).
What you are going to need to do here is update the labels during the on click events of the button. You currently only update them on activity create. This doesn't execute every time there is a click. Can I answer any questions about the fixed up version below?
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
final TextView rec_text = (TextView) findViewById(R.id.record_num);
final TextView cur_text = (TextView) findViewById(R.id.current_num);
Button add_one = (Button) findViewById(R.id.addone);
Button clear_1 = (Button) findViewById(R.id.clear);
add_one.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
current_number += 1;
if (current_number >= record_number)
record_number = current_number;
cur_text.setText(String.valueOf(current_number));
rec_text.setText(String.valueOf(record_number));
}
});
clear_1.setOnClickListener(new OnClickListener() {
public void onClick(View v)
{
current_number = 0;
cur_text.setText(String.valueOf(current_number));
rec_text.setText(String.valueOf(record_number));
}
});
cur_text.setText(String.valueOf(current_number));
rec_text.setText(String.valueOf(record_number));
}
Related
I want to move a cursor in EditText with custom left and right buttons. When I click the left button then it should go one character left and when I click a right button then it should go one character right.
I created a simple project for it. To move cursor You just can use this function: editText.setSelection(position). But before calling this function You have to check if You are at the start/end because You can get exception java.lang.IndexOutOfBoundsException.
EditText editText;
Button buttonBackward, buttonForward;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = findViewById(R.id.editText);
buttonBackward = findViewById(R.id.buttonBackward);
buttonForward = findViewById(R.id.buttonForward);
buttonForward.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (editText.getSelectionEnd() < editText.getText().toString().length())
{
editText.setSelection(editText.getSelectionEnd() + 1);
}
else
{
//end of string, cannot move cursor forward
}
}
});
buttonBackward.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if (editText.getSelectionStart() > 0)
{
editText.setSelection(editText.getSelectionEnd() - 1);
}
else
{
//start of string, cannot move cursor backward
}
}
});
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/buttonForward"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Forward" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<Button
android:id="#+id/buttonBackward"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Backward" />
</LinearLayout>
I'm doing an android application where I have to dynamically put a button inside the table row. The problem is that the button that I create is stretched as in the image below:
I also put some of the application code here below so you can understand better.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_punteggio);
showAlertDialog_modality(R.layout.dialog_change_modality);
final TableLayout tableLayout = findViewById(R.id.tableLayout);
final RelativeLayout relativeLayout = findViewById(R.id.relativeLayout);
final Button btn_settings = findViewById(R.id.btn_settings);
Bundle datipassati = getIntent().getExtras();
String player = datipassati.getString("players");
giocatore = player.split("%");
Log.d("TAG", "array: " + giocatore[0]);
for (int i = 0; i < giocatore.length; i++) {
punti[i] = 0;
TableRow tbrow = new TableRow(this);
final TextView t3v = new TextView(this);
txPunti[i] = t3v;
//------------------------- Textview Player
final TextView t1v = new TextView(this);
t1v.setText(giocatore[i].toUpperCase());
t1v.setTextColor(Color.BLACK);
t1v.setGravity(Gravity.CENTER);
t1v.setTextSize(20);
t1v.setWidth(400);
tbrow.addView(t1v);
//-------------------- BTN MENO
Button btnMeno = new Button(this);
btnMeno.setText("-");
btnMeno.setTextColor(Color.BLACK);
btnMeno.setGravity(Gravity.CENTER);
tbrow.addView(btnMeno);
btnMeno.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
removepoint(t3v);
}
});
// --------------------- TEXT VIEW PUNTI
t3v.setText(punti[i]+"");
t3v.setTextSize(25);
t3v.setMaxWidth(150);
t3v.setPadding(20,0,20,0);
t3v.setTypeface(t3v.getTypeface(), Typeface.BOLD);
t3v.setTextColor(Color.RED);
t3v.setGravity(Gravity.CENTER);
tbrow.addView(t3v);
//----------------------------- BTN PIU
Button btnPiu = new Button(this);
btnPiu.setBackground(ContextCompat.getDrawable(Activity_punteggio.this, R.drawable.ic_add_circle_black_24dp));
btnPiu.setTextColor(Color.BLACK);
btnPiu.setGravity(Gravity.CENTER);
tbrow.addView(btnPiu);
btnPiu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addPoint(t3v, t1v);
}
});
tableLayout.addView(tbrow);
}
btn_settings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showAlertDialog_modality(R.layout.dialog_change_modality);
}
});
}
Here's also the xml file:
<?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:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Activity_punteggio">
<Button
android:id="#+id/btn_settings"
android:layout_width="55dp"
android:layout_height="32dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:layout_marginRight="1dp"
android:background="#android:color/transparent"
android:drawableBottom="#drawable/ic_settings_black_24dp" />
<TextView
android:id="#+id/title_player"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:text="Chip-Chop"
android:textSize="25dp"
android:textStyle="bold|italic" />
<TableLayout
android:id="#+id/tableLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="80dp">
</TableLayout>
</RelativeLayout>
I hope you could help me.
Please check this solution. setImageResource is the method assign
image resource. Set background method is recommended for the
background of the button.
ImageButton btnPiu = new ImageButton(this);
btnPiu.setImageDrawable(ContextCompat.getDrawable(Activity_punteggio.this, R.drawable.ic_add_circle_black_24dp));
btnPiu.setTextColor(Color.BLACK);
btnPiu.setGravity(Gravity.CENTER);
tbrow.addView(btnPiu);
btnPiu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
addPoint(t3v, t1v);
}
});
tableLayout.addView(tbrow);
I used ImageButton other than Button.
setImageDrawable
try to edit android:layout_height="32dp" to android:layout_height="55dp"
android:id="#+id/btn_settings"
android:layout_width="55dp"
android:layout_height="55dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:layout_marginRight="1dp"
android:background="#android:color/transparent"
android:drawableBottom="#drawable/ic_settings_black_24dp" />
I hope it will work with you .
THIS is make very complex coding, Make it simpler.Don't create whole row dynamically in FOR loop. Instead of that
First Create Separate XML for that row.
Write Below code and your row append into tablelayout.
for (int i = 0; i < giocatore.length; i++) {
View trView = LayoutInflater.from(getActivity()).inflate(R.layout.xmlID,null);
TextView lblGuestName;
lblGuestName = trView.findViewById(R.id.lbl);
lblGuestName.setText("Guest 1");
tableLayout.addView(trView);
}
This Way you easily configure any complex row in tablelayout. No need to make it dynamically.
Feel free to reply.
I'm building a GPA calculator with android studio
What I have:
I have an edit text for credit unit and another for grade with different ID's
I have a method for the button
the app runs fine but when i input a value for the creditunit and a value for the grade, nothing happens when i click the button.
here is my xml
edit text for credit unit
<EditText
android:id="#+id/txt_credits"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_weight="1"
android:background="#drawable/border"
android:ems="4"
android:gravity="center"
android:hint="e.g. 0, 2, 3 "
android:inputType="textPersonName|number|numberDecimal" />
edit text for grade
EditText
android:id="#+id/txt_grade"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="6dp"
android:layout_weight="1"
android:background="#drawable/border"
android:ems="4"
android:gravity="center"
android:hint="e.g. A, B, C "
android:inputType="textCapCharacters"
android:paddingLeft="16dp" />
button
<Button
android:id="#+id/button2"
android:layout_marginBottom="6dp"
android:textAllCaps="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginTop="32dp"
android:onClick="calcGpa"
android:text="get gpa"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Large"/>
here is my java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLayout = (LinearLayout) findViewById(R.id.linearLayout);
mEditCreditText = (EditText) findViewById(R.id.txt_credits);
mEditGradeText = (EditText) findViewById(R.id.txt_grade);
mButton = (Button) findViewById(R.id.button2);
mButton.setOnClickListener(onClick());
}
private View.OnClickListener onClick() {
return new View.OnClickListener() {
#Override
public void onClick(View v) {
allCredits.add(mEditCreditText);
allGrades.add(mEditGradeText);
}
};
}
public int pointPerSubj(int number,String grade) {
int point;
if (grade.equalsIgnoreCase("A"))
point = 5;
else if (grade.equalsIgnoreCase("B"))
point = 4;
else if (grade.equalsIgnoreCase("C"))
point = 3;
else if (grade.equalsIgnoreCase("D"))
point = 2;
else
point = 0;
int perSubjectPoint = number * point;
return perSubjectPoint;
}
public void calcGpa (View v) {
gpa = perSubjectPoint/totalCredits;
display(gpa);
}
// display method
private void display(int number) {
TextView gpaTextView = (TextView) findViewById(R.id.gpa_text_view);
gpaTextView.setText("" + (number));
}
}
I don't have enough reputation to comment, else I would have asked for more code in the comments.
Your onClickListener adds your EditTexts to what I'm assuming are ArrayLists of EditTexts: allCredits and allGrades
In the first place, if you meant to insert the text, then those should be ArrayLists of strings, and you should be doing:
allCredits.add(mEditCreditText.getText().toString());
allGrades.add(mEditGradeText.getText().toString());
Also, your code does not indicate where any of your methods calcGpa and pointPerSubj are called.
I believe you are simply not calling the required methods to display
anything.
After much research, I have been unable to figure out how I can initialize my TextView in my java file in Android Studio. The TextView in question is located in a different layout file so I don't know the correct syntax to use. I think my question is similar to: Null pointer Exception on .setOnClickListener
But the solution for him is not working for me.
Here is my troublesome code:
Microsoft = (Button) findViewById(R.id.Microsoft);
Microsoft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
//TextView score = (TextView) findViewById(R.id.score);
TextView score = (TextView)
score.findViewById(R.id.question2);
(score).setText(0);
}
});
TheFindViewById part is the part I need.
FULL CODE VVVV
package org.flinthill.finalprojectv3;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
import android.widget.Toast;
import android.text.method.DigitsKeyListener;
import android.text.InputFilter;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button SuSe;
Button DOS;
Button B;
Button BIOS;
Button Microsoft;
Button LenBosackandSandyLerner;
Button HaskelDiklah;
Button SteveWozniak;
SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText(0);
}
});
DOS = (Button) findViewById(R.id.DOS);
DOS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("0");
}
});
B = (Button) findViewById(R.id.B);
B.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("0");
}
});
BIOS = (Button) findViewById(R.id.BIOS);
BIOS.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Right!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("1");
setContentView(R.layout.question2);
}
});
//QUESTION 2
Microsoft = (Button) findViewById(R.id.Microsoft);
Microsoft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
//TextView score = (TextView) findViewById(R.id.score);
TextView score = (TextView)
score.findViewById(R.id.question2);
(score).setText(0);
}
});
/*LenBosackandSandyLerner = (Button)
findViewById(R.id.LenBosackandSandyLerner);
LenBosackandSandyLerner.setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Right!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("2");
setContentView(R.layout.question3);
}
});
HaskelDiklah = (Button) findViewById(R.id.HaskelDiklah);
HaskelDiklah.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("0");
}
});
SteveWozniak = (Button) findViewById(R.id.SteveWozniak);
SteveWozniak.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "Wrong!",
Toast.LENGTH_SHORT).show();
TextView score = (TextView) findViewById(R.id.score);
(score).setText("0");
}
});*/
}
}
XML CODE:
LAYOUT 1:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/android"
tools:context="org.flinthill.finalprojectv3.MainActivity">
<TextView
android:layout_width="wrap_content"
android:id="#+id/LUL"
android:textColor="#color/LightGreen"
android:layout_height="wrap_content"
android:typeface="serif"
android:text="Which is NOT an OS?"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:textSize="24sp"/>
<Button
android:id="#+id/SuSe"
android:onClick="SuSeClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SuSe"
android:layout_marginTop="100dp"
android:layout_below="#+id/LUL"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/BIOS"
android:onClick="BIOSClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BIOS"
android:layout_below="#+id/SuSe"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/DOS"
android:onClick="DOSClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DOS"
android:layout_below="#+id/BIOS"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/B"
android:onClick="BClick"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="B"
android:layout_below="#+id/DOS"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/score"
android:textColor="#color/colorAccent"
android:text="0"
android:textSize="32dp"
android:layout_below="#+id/LUL"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
</RelativeLayout>
LAYOUT 2:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/question2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#drawable/android"
tools:context="org.flinthill.finalprojectv3.MainActivity">
<TextView
android:layout_width="wrap_content"
android:id="#+id/question2text"
android:textColor="#color/LightGreen"
android:layout_height="wrap_content"
android:typeface="serif"
android:text="Who created Cisco"
android:layout_centerHorizontal="true"
android:layout_marginTop="32dp"
android:textSize="24sp"/>
<Button
android:id="#+id/Microsoft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Microsoft"
android:layout_marginTop="100dp"
android:layout_below="#+id/question2text"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/LenBosackandSandyLerner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Len Bosack and Sandy Lerner"
android:layout_below="#+id/Microsoft"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/HaskelDiklah"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Haskel Diklah"
android:layout_below="#+id/LenBosackandSandyLerner"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/SteveWozniak"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Steve Wozniak"
android:layout_below="#+id/HaskelDiklah"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/score"
android:textColor="#color/colorAccent"
android:text="1"
android:textSize="32dp"
android:layout_below="#+id/question2text"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
</RelativeLayout>
//Change the name id in the xml file
Button microsoftButton = (Button) findViewById(R.id.microsoftButton);
TextView scoreTextView = (TextView) findViewById(R.id.scoreTextView);
microsoftButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Wrong!", Toast.LENGTH_SHORT).show();
// This was declared above so now you can use it. You can only set it to a String not to an Integer.
scoreTextView.setText("0");
}
});
You cannot do this unless your TextView is within the layout your Activity, Fragment or Dialog controls.
The findViewById method looks for Views within the layout previously configured by the setContentView(layout) or the layout inflated in your Fragment or Dialog. If it does not find anything, your TextView will have a null reference.
I am currently a high school student who decided to try and take up Android dev for fun, but I am stumped. I have an image button for blue team and for red team. The score goes up automatically for blue team. What i dont know how to do is when you hit the red button, the image buttons make the red teams score go up and vice versa.
Here is my java code
package com.example.puremmacompetitionjiujitsuscorer;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
import android.widget.Toast;
class MainActivity extends Activity {
private int blueScore = 0;
private int redScore = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ImageButton sweep = (ImageButton) findViewById(R.id.imageButton5);
final ImageButton pass = (ImageButton) findViewById(R.id.imageButton8);
final ImageButton mount = (ImageButton) findViewById(R.id.imageButton4);
final ImageButton backMount = (ImageButton) findViewById(R.id.imageButton3);
final ImageButton kneeOnBelly = (ImageButton) findViewById(R.id.imageButton7);
final ImageButton takeDown = (ImageButton) findViewById(R.id.imageButton6);
final TextView blueScoreCount = (TextView) findViewById(R.id.blueScore1);
takeDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
blueScore += 2;
blueScoreCount.setText("" + blueScore);
}
});
sweep.setOnClickListener(new View.OnClickListener(){
public void onClick(View w) {
blueScore += 2;
blueScoreCount.setText("" + blueScore);
}
});
pass.setOnClickListener(new View.OnClickListener(){
public void onClick(View q){
blueScore += 3;
blueScoreCount.setText("" + blueScore);
}
});
mount.setOnClickListener(new View.OnClickListener(){
public void onClick(View t){
blueScore += 4;
blueScoreCount.setText("" + blueScore);
}
});
backMount.setOnClickListener(new View.OnClickListener(){
public void onClick(View s){
blueScore += 4;
blueScoreCount.setText("" + blueScore);
}
});
kneeOnBelly.setOnClickListener(new View.OnClickListener(){
public void onClick(View g){
blueScore += 2;
blueScoreCount.setText("" + blueScore);
}
});
};
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
Here is my XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/purebig" >
<TextView
android:id="#+id/redScore1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/hint"
android:maxLength="2"
android:textIsSelectable="false" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/stop" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/play"/>
<ImageButton
android:id="#+id/imageButton7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageButton2"
android:layout_alignParentLeft="true"
android:src="#drawable/kneeonbelly"
android:onClick="addTwo" />
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/imageButton7"
android:src="#drawable/backmount"
android:onClick="addFour" />
<ImageButton
android:id="#+id/imageButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageButton3"
android:layout_alignParentRight="true"
android:src="#drawable/mount"
android:onClick="addFour" />
<ImageButton
android:id="#+id/imageButton8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/imageButton7"
android:layout_centerHorizontal="true"
android:src="#drawable/pass"
android:onClick="addThree" />
<ImageButton
android:id="#+id/imageButton6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/imageButton8"
android:src="#drawable/takedown"
android:onClick="addTwo" />
<ImageButton
android:id="#+id/imageButton5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/imageButton8"
android:layout_below="#+id/imageButton8"
android:src="#drawable/sweep"
android:onClick="addTwo" />
<ImageButton
android:id="#+id/imageButton10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/imageButton9"
android:src="#drawable/red" />
<ImageButton
android:id="#+id/imageButton9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/imageButton1"
android:src="#drawable/blue" />
<TextView
android:id="#+id/blueScore1"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="#string/hint"
android:maxLength="2"
android:textIsSelectable="false"/>
<requestFocus />
</RelativeLayout>
I think I possibly understand you. If I do, you can use a flag to see which Button was pressed. So the flow would be like
Red button pressed -> flag = red -> Score Button pressed -> red score += score
In code it would be like
String flag = "";
public void onRedBtnClick(View v)
{
flag = "red";
}
public void scoreBtnClick(View v)
{
if (!flag.equals(""));
{
if ("red".equals(flag))
{
redScore += score;
}
if ("blue".equals(flag))
{
blueScore += score;
}
}
This is obviously done really quick and you will have to adjust your variables to your needs. But if I understand what you want then this would give you the basic logic