Im trying to make a basic program where you press a button and it adds to an integer. Im having trouble updating the text view.
package com.example.curtis.simpleapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int num;
TextView numview;
private void updateNumber(){
TextView numview = (TextView)findViewById(R.id.Number);
numview.setText(num);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupButton();
}
private void setupButton() {
Button button = (Button) findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
num = num + 1;
numview.setText(num);
}
});
}
Help would be great im trying to get into android and books havent been very helpful
There are a few mistakes here. First, you are never calling updateNumber() so numviewis never set. Moreover, you have two TextView numView once global in your MainActivity and again in updateNumber(). There is a better way to do this. But first go to activity_main.xml file and within the button tag add the the line android:onClick="onClick" something like this :
<Button
android:id="#+id/button1"
...
android:onClick="onClick"/>
Next in your MainActivity.java, add the following lines of code :
package com.example.curtis.simpleapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int num = 0;
TextView numview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numview = (TextView)findViewById(R.id.Number);
}
public void onClick(View view){
num += 1;
numview.setText(String.valueOf(num));
}
}
onClick() will be automatically called when your button is clicked because of the android:onClick property.
you have two different objects with same name and it would cause error.
one of them is defined in your class (TextView numview;) and the other one is defined in your function update number.
in your onClick method you want to change numView defined in your class and it's not initialized. you have to add change your code :
package com.example.curtis.simpleapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int num;
TextView numview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
numview = (TextView)findViewById(R.id.Number);
setupButton();
}
private void setupButton() {
Button button = (Button) findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
num = num + 1;
numview.setText(num);
}
});
}
Put this code in your onClick function
num = num + 1;
updateNumber();
You are never calling updateNumber()
package com.example.curtis.simpleapplication;
import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
int num=0;
TextView numview;
private void updateNumber(int number){
numview = (TextView)findViewById(R.id.Number);
numview.setText(number);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setupButton();
}
private void setupButton() {
Button button = (Button) findViewById(R.id.Button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
num = num + 1;
updateNumber(num);
}
});
}
Related
I am learning Android and I have build a simple Android app that shows Toasts for starters.
In the main activity I have a button that when I press it it shows a Toast and another button that takes you to another activity. This is the code:
package ro.serbab.notes;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.addnotexml);
}
});
Button button2 = (Button) findViewById(R.id.button5);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Serban Android",
Toast.LENGTH_SHORT).show();
}
});
}
}
This is the second activity:
package ro.serbab.notes;
import androidx.appcompat.app.AppCompatActivity;
import android.app.AlertDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import ro.serbab.notes.R;
public class addnote extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.addnotexml);
Button button3 = (Button) findViewById(R.id.button2);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"Serban
Android",Toast.LENGTH_SHORT).show();
}
});
}
}
The button from the second activity doesn't show any toast. I keep pressing it and it doesn't work.
remove setContentView(R.layout.addnotexml); this line from onClick and add
when you want to go 2nd activity onClick the button this code will help you out
Intent intent = new Intent(MainActivity.this, 2ndActivity.class);
startActivity(intent);
i get some problem when i use the intent, he said "cannot resolve constructor Intent('....')" , i dont know what is wrong in my code but you can see what i m importing here
package com.example.noen.myintentapp;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.btn_move);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.btn_move:
Intent intn = new Intent(MainActivity.this,target1.class);
break;
}
}
}
I am not sure why I am getting this error: imgur.com/a/Hxz5O
Everything seems to be in the right methods and everything so it is a mystery to me why I am getting the error.
Here is my code:
package org.flinthill.finalprojectv2;
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;
public class mainactivity extends AppCompatActivity {
Button SuSe;
#Override
protected void onCreate(Bundle savedInstanceState) {
final Button SuSe = (Button) findViewById(R.id.SuSe);
SuSe.setOnClickListener(new View.OnClickListener() {
{
new View.OnClickListener() {
#Override
public void onClick(View view){
}
};
}
});
}
}
You are declaring the View.OnClickListener inside another View.OnClickListener for no reason. Remove the second one e.g.
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
}
});
You need setContentView() and your clickListener not correct.
package org.flinthill.finalprojectv2;
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;
public class mainactivity extends AppCompatActivity {
Button SuSe;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myView);
SuSe = (Button)findViewById(R.id.MyButtonId);
SuSe.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
}
});
}
}
Keep getting a class or interface expected error at the end of the first set of closing brackets, I have a feeling it has to do with the onCreate method, but I'm not sure how to go about fixing it.
package shake.shake;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import android.content.Intent;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainPage extends AppCompatActivity {
private static Button ShakeButton1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
OnClickButtonListener();
}
}
public void OnClickButtonListener()
{
ShakeButton1 = (Button)findViewById(R.id.ShakeButton);
ShakeButton1.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intentSecondActivity = new
Intent(MainPage.this, ShakePage.class);
startActivity(intentSecondActivity);
}
}
);
}}
you have extra braces here
public class MainPage extends AppCompatActivity {
private static Button ShakeButton1;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_page);
OnClickButtonListener();
}
} <-------- extra brace.
It's better having a look at codes before posting.
Your method public void OnClickButtonListener() is outside the class MainPage. Move that method inside your class.
I am writing code for a poker game on eclipse and I am stuck on how to make the card clicked in the cardHand area show up in the playedCards area and have it removed from the deck I am drawing it from. Here's the code:
package com.viktorengineering.poker254;
import java.util.Random;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import java.util.Random;
public class Poker extends Activity {
private ImageView mViewDeck;
private ImageView mViewHand;
private ImageView mViewPlayedCards;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.getWindow().setBackgroundDrawableResource(R.drawable.green_back);
mViewDeck = (ImageView) findViewById(R.id.deckImage);
mViewDeck.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
int[] imagesArray = {R.drawable.clubs_ace, R.drawable.hearts_seven, R.drawable.diamonds_five,
R.drawable.clubs_three, R.drawable.hearts_eight, R.drawable.spades_six};
Random random = new Random();
mViewHand.setImageResource(imagesArray[new Random().nextInt(6)]);
}
});
mViewHand = (ImageView) findViewById(R.id.cardHand);
mViewHand.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mViewPlayedCards.setImageResource(0);
mViewHand.setImageResource(0);
}
});
mViewPlayedCards = (ImageView) findViewById(R.id.playedCards);
}
}
Your Image in Drawable Folder means use bellow code
mViewPlayedCards.setImageResource(R.drawable.icon);
mViewHand.setImageResource(R.drawable.icon_home);