I was developing an application that It is a simple game of cards and I added a button, in a dynamic way at the view, which represents a card of the game, but this button doesn't receive any input when It is pressed.
The question is: Why the button does not receive any input?
I've already tried to use the Log to print the message but nothing is printed.
public class MainActivity extends AppCompatActivity {
private Banco banco;
private Giocatore giocatore1;
private Giocatore giocatore2;
private Giocatore giocatoreAttuale;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
giocatore1 = new GiocatoreUmano();
giocatore2 = new GiocatoreUmano();
banco = new Banco(giocatore1,giocatore2);
giocatoreAttuale=giocatore1;
DaiCarteAiGiocatori();
DaiCarteAlPlayerOne();
}
private void DaiCarteAlPlayerOne(){
Button primaCarta = new Button(this); //BUTTON OF THE CARD
primaCarta.setText(giocatore1.getCartaByIndex(0).getSeme().toString()+":"+giocatore1.getCartaByIndex(0).getValore());
ConstraintLayout layout = findViewById(R.id.baseLayout);
layout.addView(primaCarta);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.move_cards); // THE ANIMATION
primaCarta.startAnimation(animation);
//when the button is pressed the log should print the message of the card
primaCarta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
banco.giocaCarte(giocatore1,giocatore1.getCartaByIndex(0));
Log.i("CARTA","Carta Premuta");
}
});
}
private void DaiCarteAiGiocatori(){
banco.pescaCarte();
}
}
Thanks for the help.
see my changes in three places commented by //!!!
public class MainActivity extends AppCompatActivity implements View.OnClickListener { //!!!
private Banco banco;
private Giocatore giocatore1;
private Giocatore giocatore2;
private Giocatore giocatoreAttuale;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
giocatore1 = new GiocatoreUmano();
giocatore2 = new GiocatoreUmano();
banco = new Banco(giocatore1,giocatore2);
giocatoreAttuale=giocatore1;
DaiCarteAiGiocatori();
DaiCarteAlPlayerOne();
}
private void DaiCarteAlPlayerOne(){
Button primaCarta = new Button(this); //BUTTON OF THE CARD
primaCarta.setOnClickListener(MainActivity.this); //!!!
primaCarta.setText(giocatore1.getCartaByIndex(0).getSeme().toString()+":"+giocatore1.getCartaByIndex(0).getValore());
ConstraintLayout layout = findViewById(R.id.baseLayout);
layout.addView(primaCarta);
Animation animation = AnimationUtils.loadAnimation(this, R.anim.move_cards); // THE ANIMATION
primaCarta.startAnimation(animation);
}
//!!!
#Override
public void onClick(View v) {
banco.giocaCarte(giocatore1,giocatore1.getCartaByIndex(0));
Log.i("CARTA","Carta Premuta");
}
private void DaiCarteAiGiocatori(){
banco.pescaCarte();
}
Related
I'm doing a quiz and I don't want to write in all of new Activities this method but I can't make it work. I was trying to make another java class to put there all methods but how do that
Hope u gonna help me guys
//Main class
public class MainActivity extends AppCompatActivity {
private Button button;
BubbleEmitterView bubbleEmitter;
Handler handler = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
emitBubbles();
bubbleEmitter = findViewById(R.id.bubbleEmitter);
button = findViewById(R.id.startButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startQuiz();
}
});
}
public void emitBubbles()
{
handler.postDelayed(new Runnable() {
#Override
public void run() {
//determine the size of the bubbles and the range
int size = new Random().nextInt(81)+20;
bubbleEmitter.emitBubble(size);
emitBubbles();
}
},
//determine the range for the bubbles to appear
new Random().nextInt(301)+100);
}
//question class
public class Question1 extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_question1);
}
}
I am trying to create a custom dialog where the button clicks could be handled by the main activity that instantiates it, something like how the AlertDialog.Builder assigns the listeners via its setPositiveButton() and setNegativeButton() methods.
This is what I've done:
// THE MAIN ACTIVITY
public class main_code extends AppCompatActivity {
private commonModule comMod;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
comMod = new commonModule(this);
}
private void showDialog() {
DialogInterface.OnClickListener dialogHandler =
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// process "PROCEED" button
}
};
commonModule.myDialog customDialog = comMod.new myDialog();
customDialog.inputBox(this, "Submit Results",
"Your results will be submitted.", dialogHandler);
}
}
// THE COMMON MODULE CLASS
public class commonModule {
public commonModule(Context context){
this.context = context;
this.activity = (Activity) context;
}
public class myDialog {
public void showDialog(Activity activity, String title, String message,
DialogInterface.OnClickListener dialogHandler) {
final Dialog panel = new Dialog(activity);
panel.requestWindowFeature(Window.FEATURE_NO_TITLE);
panel.setContentView(R.id.customLayout);
panel.setCancelable(false);
TextView panelTitle = (TextView) panel.findViewById(R.id.title);
panelTitle.setTypeface(fontRes(PlayRegular));
panelTitle.setText(title);
TextView msgboxText = (TextView) panel.findViewById(R.id.content);
msgboxText.setTypeface(fontRes(PlayRegular));
msgboxText.setText(message);
Button button1 = (Button) panel.findViewById(R.id.button1);
button1.setTypeface(fontRes(PlayRegular));
button1.setText("ABORT");
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
panel.dismiss();
}
});
Button button2 = (Button) panel.findViewById(R.id.button2);
button2.setTypeface(fontRes(PlayRegular));
button2.setText("PROCEED");
button2.setOnClickListener(dialogHandler);
// error: View.OnClickListener cannot be applied to Dialog.OnClickListener
panel.show();
}
}
}
I tried using View.OnClickListener() as well, but to no success. I would like the dialog builder to be common and generic, and as such, the click listener should be unique for each dialog.
Any suggestion would be really appreciated.
TIA.
Change your dialogHandler to View.OnClickListener instead of DialogInterface.OnClickListener :
private void showDialog() {
View.OnClickListener dialogHandler =
new View.OnClickListener() {
#Override
public void onClick(View v) {
// process "PROCEED" button
}
};
commonModule.myDialog customDialog = comMod.new myDialog();
customDialog.inputBox(this, "Submit Results",
"Your results will be submitted.", dialogHandler);
}
and in the common module:
public void showDialog(Activity activity, String title, String message,
View.OnClickListener dialogHandler) {
...
button2.setOnClickListener(dialogHandler);
panel.show();
}
I'm facing a problem with the barcode scanner from zxing library for android. Actually, the scanner works fine, but sometimes it's so fast that the user doesn't have time to position the camera so it gets all the barcode, and then the barcode is read partially.I want to know if there is a way to, after starting the camera,have a delay until the first reading. Here is a piece of my code:
public class Produto extends AppCompatActivity {
private IntentIntegrator qrScan;
private Button btnCodBarras;
private Button btnEnviar;
private Button btnExcluir;
private EditText nomeCliente;
private ListView listaProdutos;
private ArrayList<String> produtos = new ArrayList<>();
private MyCustomAdapter adapter;
private SharedPreferences settings;
private String nome;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_produto);
configScan();
btnCodBarras = (Button) findViewById(R.id.btnCodBarras);
btnEnviar = (Button) findViewById(R.id.btnEnviar);
listaProdutos = (ListView) findViewById(R.id.listaProdutos);
nomeCliente = (EditText) findViewById(R.id.cliente);
adapter=new MyCustomAdapter(produtos,getApplicationContext(),listaProdutos);
listaProdutos.setAdapter(adapter);
btnCodBarras.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
qrScan.initiateScan();
}
});
I'm just calling via intent. And I can't seem to be able to modify IntentIntegrator.java and the other zxing files.
Any ideas?
I have search a lot in different sites to solve this problem but I can't till now.
I have a simple app with 50 articles, two buttons, previous and next.
All works fine till the last article 50 where the problem is that the user can't choose the previous button 49, only can load from the beginning.
Here is my code: MainActivity.java
public class MainActivity extends AppCompatActivity {
private TextView meditCenterText;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditCenterText = (TextView) findViewById(R.id.editCenterText);
mStartButton = (Button) findViewById(R.id.startButton);
meditCenterText.setMovementMethod(new ScrollingMovementMethod());
mStartButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String TextView = meditCenterText.getText().toString();
startStory(TextView);
}
});
}
private void startStory(String TextView) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("TextView", TextView);
startActivity(intent);
}
#Override
protected void onResume() {
super.onResume();
meditCenterText.setText("..... ");
}}
StoryActivity.java
public class StoryActivity extends AppCompatActivity {
public static final String TAG = StoryActivity.class.getSimpleName();
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice){
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){
mPreviousButton.setVisibility(View.VISIBLE);
mNextButton.setText("Start From The Beginning");
mNextButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mPreviousButton.setVisibility(View.VISIBLE);
loadPage(0);
}
});
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
});
}}
}
Thanks for help.
It's probably the logic in your Page class. Have a proper look at it, otherwise from the android side everything is alright. One last thing, you should probably use a ViewPager for your app instead of changing the text every time you load a new oage
I'm trying to set onClick listener to a button from a ViewPager's page but when I try to run the app I get NullPointerException
....
public class MainActivity extends Activity {
private ViewPager vPager;
private MyPagerAdapter pagerAdapter;
private TextView tv;
private MyViewPagerListener pagerListener;
private Button deleteAll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView) findViewById(R.id.page_state);
pagerListener = new MyViewPagerListener();
pagerAdapter = new MyPagerAdapter();
vPager = (ViewPager) findViewById(R.id.vPager);
vPager.setAdapter(pagerAdapter);
vPager.setCurrentItem(0);
vPager.setOnPageChangeListener(pagerListener);
//Here is the problem
ClickListener cl = new ClickListener();
deleteAll = (Button) vPager.findViewById(R.id.btnDelete);
deleteAll.setOnClickListener(cl);
}
.....
}
You should register a new OnClickListener object instead of a ClickListener. Also, you have to provide an implementation for the onClick method like this:
new OnClickListener() {
public void onClick(View v) {
// do something when the button is clicked
}
};