I created a simple counter and want to add a button that proceeds to another activity, here's what I got:
//counter starts
#Override
public void onClick(View v) {
if (v == btn1){
counter++;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.CYAN);
}
if (v == btn2){
counter--;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.GREEN);
}
if (v == btn3){
counter = 0;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.RED);
}
//There the counter ends
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondAcitivity.this, Aktivity.class);
startActivity(intent);
}});
}}
After the //There the counter ends tag I set an OnClickListener to a button "NEXT" that proceeds to another activity and created an Intent that brings to the activity called "Aktivity".
When I press the button, however, nothing happnendz. halp
Cut and Paste this code in your onCreate() Method
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(SecondAcitivity.this, Aktivity.class);
startActivity(intent);
}});
You can't set click listener to an object inside onClick() Method
Change to
#Override
public void onClick(View v) {
switch(v.getId())
{
case R.id.btn1: // btn1 clicked
counter++;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.CYAN);
break;
case R.id.btn2: // btn2 clicked
counter--;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.GREEN);
break;
case R.id.btn1: // btn3 clicked
counter=0;
scoreText.setText(Integer.toString(counter));
scoreText.setBackgroundColor(Color.RED);
break;
case R.id.next: // assuming button next id is next. btn next clicked
Intent intent = new Intent(SecondAcitivity.this, Aktivity.class);
startActivity(intent);
break;
}
}
Then in onCreate
next.setOnClickListener(this); // similarly fro other buttons
Ans make sure your Activity implements OnClickListener
Instead of switch case you can use if else.
Code:
Button b1, b2;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
b1 = (Button) findViewById(R.id.f);
b2 = (Button) findViewById(R.id.s);
b1.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
b1.setVisibility(View.INVISIBLE);
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
b2.setVisibility(View.INVISIBLE);
}
});
}
#Brontok I tested this code and it works fine. So, onClick on an object can be implemented inside other onClick.
#MiroslavVitula I tested your scenario too. I was able to open the same activity by same method you used. Maybe some other problem.
Related
I want to add a difficulty in my game, for example when I click a category like category "A" it will show to the next layout and the user has to pick a difficulty to play "easy, normal or hard" .
sorry for my bad sentence
here is my categories:
public class CategoryActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.category);
Button A = (Button) findViewById(R.id.btn_a);
Button B = (Button) findViewById(R.id.btn_b);
Button C = (Button) findViewById(R.id.btn_c);
Button D = (Button) findViewById(R.id.btn_d);
Button E = (Button) findViewById(R.id.btn_e);
Button F = (Button) findViewById(R.id.btn_f);
Button G = (Button) findViewById(R.id.btn_g);
Button H = (Button) findViewById(R.id.btn_h);
Button I = (Button) findViewById(R.id.btn_i);
Button J = (Button) findViewById(R.id.btn_j);
A.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
B.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg1) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
C.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg2) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
D.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg3) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
E.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg4) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
F.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg5) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
G.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg6) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
H.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg7) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
I.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg8) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
J.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg9) {
startActivity(new Intent(CategoryActivity.this, DifficultyActivity.class));
}
});
}
You should use radio buttons, so the user can select only one option and not two or more.
This is an example taken from the official android devs website.
public void onRadioButtonClicked(View view) {
// Is the button now checked?
boolean checked = ((RadioButton) view).isChecked();
// Check which radio button was clicked
switch(view.getId()) {
case R.id.radio_pirates:
if (checked)
// Pirates are the best
break;
case R.id.radio_ninjas:
if (checked)
// Ninjas rule
break;
}
}
PS: You're question is not really clear. It makes it difficult to help you. I hope my answer is acceptable. Refer to the manuals and do some tutorials.
I still got confused with StaticClass code which given from my friend to alternative save besides Shared Preferences, already 3 days I tried learned the code and asked but there is still a little problem with a code
this is the latest following code in my selectlevel.class that i have perfected
public class selectlevel extends Activity {
Button f1, f2, f3;
ImageView f2lock, f3lock;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.selectlevel);
f1=(Button)findViewById(R.id.f1);
f1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level1 = new Intent ();
level1.setClassName ("com.example.game", "com.example.game.levelone");
startActivity (level1);
}
});
f2=(Button)findViewById(R.id.f2);
f2lock=(ImageView)findViewById(R.id.f2lock);
f2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v){
// TODO Auto-generated method stub
Intent level2 = new Intent ();
level2.setClassName ("com.example.game", "com.example.game.leveltwo");
startActivity (level2);
}
});
updateLevels();
}
static class PlayerProgress {
private static int progress = 0;
public static void updateProgress(int levelNumber) {
progress = levelNumber;
}
public static int getPlayerProgress() {
return progress;
}
}
public void updateLevels() {
int progress = PlayerProgress.getPlayerProgress();
switch(progress) {
case 1:
f2.setVisibility(View.VISIBLE);
f2lock.setVisibility(View.GONE);
break;
case 2:
break;
// You can expand this to as many levels you'd like.
}
}
and i had use this in my levelone.class to send update progress to 1
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
selectlevel.PlayerProgress.updateProgress(1);
finish();
but when levelone.class finish, f2 button still GONE and f2lock still VISIBLE
nothing is change in selectlevel.class
i wonder it can be visible like this and still visible if the game re-open because the button visibility is saved
can anyone help me to fix a problem in my code? or give explain with another code as solution?
try to call the updateLevels() function also in your onClick funtion like this:
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
selectlevel.PlayerProgress.updateProgress(1);
selectlevel.PlayerProgress.updateLevels();
finish();
}
I am new to coding and am trying to work on a project when my main Activity page has a series of buttons. From these buttons I would like each to open a different activity or command. I been searching and found what I thought SHOULD work, however, it does not. I get a crash when it comes loading the app and then clicking on the button. Below is the code. Any pointers to show my mistake somewhere would be kindly appreciated.
package com.example.finalproject2;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Initialize Buttons
Button b1 = (Button) findViewById(R.id.button1);
Button b2 = (Button) findViewById(R.id.button02);
Button b3 = (Button) findViewById(R.id.button03);
Button b4 = (Button) findViewById(R.id.button01);
Button b5 = (Button) findViewById(R.id.button04);
//Set OnCLickListeners
b1.setOnClickListener(chicagoListener);
b2.setOnClickListener(sanJoseListener);
b3.setOnClickListener(baltimoreListener);
b4.setOnClickListener(westPalmBeachListener);
b5.setOnClickListener(websiteListener); }
private OnClickListener chicagoListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Chicago.class));
}
};
private OnClickListener sanJoseListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, SanJose.class));
}
};
private OnClickListener baltimoreListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, Baltimore.class));
}
};
private OnClickListener westPalmBeachListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this, WestPalmBeach.class));
}
};
private OnClickListener websiteListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com/")));
}
};
{
}
}
Mention your all activity in your app manifesto file (like this):
<activity
android:name="com.example.finalproject2.Chicago"
android:label="#string/app_name" >
</activity>
And try to do somthing like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
startActivity(new Intent(MainActivity.this, Chicago.class));
break;
case R.id.button02:
startActivity(new Intent(MainActivity.this, SanJose.class));
break;
case R.id.button03:
startActivity(new Intent(MainActivity.this, Baltimore.class));
break;
case R.id.button01:
startActivity(new Intent(MainActivity.this, WestPalmBeach.class));
break;
case R.id.button04:
startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse ("http://www.google.com/")));
break;
}
}
Button - Android developer
sample from developer site
Button button = (Button) findViewById(R.id.button_send);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Do something in response to button click
}
});
another good example of how to use onclick
I'm developing a game for android, and currently I'm doing the menu part of the program.
But I have a little problem.
If I add a setOnClickListener to the program, the program crashes.
<!-- language: lang-java -->
public class MakeLoveMenu extends Activity {
/* 0 = New 1 = Load 2 = Statistics 3 = Exit */
Button[] buttons;
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_make_love_menu);
buttonListeners();
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.make_love_menu, menu);
return true;
}
public void buttonListeners() {
buttons[0] = (Button) findViewById(R.id.button_StartNewGame);
buttons[1] = (Button) findViewById(R.id.button_ContinueGame);
buttons[2] = (Button) findViewById(R.id.button_Stats);
buttons[3] = (Button) findViewById(R.id.button_Exit);
buttons[0].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
System.exit(0);
}
});
buttons[1].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
System.exit(0);
}
});
buttons[2].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
System.exit(0);
}
});
buttons[3].setOnClickListener(new OnClickListener() {
public void onClick(View v) {
finish();
System.exit(0);
}
});
}
}
Can you find any errors? Maybe there's a problem with the array handling?
Thanks for your answer!
Based on your code, Button[] buttons is never instantiated, so, you would need something like:
Button[] buttons = new Button[4];
You can't access an array position that doesn't exist, since in your method buttonListeners() you are considering the existence of 4 positions (0 to 3), you need to create them before accessing.
I have a problem
As it can be seen in the picture there are 3 Buttons and a EditText
Need to write in the box which button is pressed and write the corresponding character in the EditText. Just like a keyboard.
Sorry for my bad English.
Like this:
Thanks
You should define a StringBuilder, then every time you press a button add that characted to the StringBuilder and update the content of EditText.
Just a quick snippet:
StringBuilder s = new StringBuilder();
EditText et = (EditText) findViewById(EDITTEXT_ID_PATH);
Button button_q = (Button) findViewById(BUTTON_Q_ID_PATH);
button_q.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s.append("q");
et.setText(s);
}
});
Button button_e = (Button) findViewById(BUTTON_E_ID_PATH);
button_e.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s.append("e");
et.setText(s);
}
});
Button button_w = (Button) findViewById(BUTTON_W_ID_PATH);
button_q.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
s.append("w");
et.setText(s);
}
});
You can also use switch case, here is the completed code
public class MainActivity extends Activity implements OnClickListener{
EditText et1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button)findViewById(R.id.button1);
Button b2 = (Button)findViewById(R.id.button2);
Button b3 = (Button)findViewById(R.id.button3);
et1 = (EditText)findViewById(R.id.editText1);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.button1:
et1.append("Q");
break;
case R.id.button2:
et1.append("E");
break;
case R.id.button3:
et1.append("W");
break;
default:
break;
}
}
}