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();
}
Related
EDIT!
I have this eror if i remove 1 from Oncreate1
http://i.stack.imgur.com/pYTLv.png
I don't know why my code it doesn't work, I have no errors but nothing happens.
Some help?
here is the code:
#Override
public void onBackPressed() {
super.onBackPressed();
}
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
addButtonClickListner();
}
public void addButtonClickListner() {
Button btnNavigator = (Button)findViewById(R.id.play);
btnNavigator.setOnClickListener(new OnClickListener() {
public void onClick(View arg) {
Intent intent =new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);
}
}
Your code is in a method called onCreate1(). It's not an activity lifecycle method. Nothing happens because your code is not run. Rename the method to onCreate().
It's a good habit to always add the #Override annotation to methods where you intend to override a framework method, such as here. If the annotated method doesn't really override a method, you'll get a compile-time error.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
Button btnNavigator = (Button)findViewById(R.id.play);
btnNavigator.setOnClickListener(new OnClickListener(){
public void onClick(View arg)
{
Intent intent =new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);
}
});
}
Try this
This should work
#override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
Button btnNavigator = (Button)findViewById(R.id.play);
btnNavigator.setOnClickListener(new OnClickListener(){
public void onClick(View arg)
{
Intent intent =new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);
}
});
}
Make sure #Override annotation always come with override method (e.g onCreate, onResume,...)
Your method name is a new one "onCreate1" please rename it to "onCreate".
Maybe look like:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.wallpaper);
addButtonClickListner();
}
public void addButtonClickListner(){
Button btnNavigator = (Button)findViewById(R.id.play);
btnNavigator.setOnClickListener(new OnClickListener(){
public void onClick(View arg) {
Intent intent =new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(intent);
}
});
}
Beside that, you should print out some thing every where you want to know how to the app work.
Log.d("some text","some text");
In the android sdk, I have programmed 2 buttons to bring me to 2 different layouts. However, only the first one I have programmed will work, while the second one will do completely nothing. I will provide the code if you can catch any things I missed, please tell me what you think. This is one of my first applications to run on android, so try to explain your suggestion as simple as you could.
Code:
public class MainActivity extends Activity {
private final String T = "Settings";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
private final String T2 = "ManualAdd";
protected void onCreate1(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
feederButton();
}
private void settingsButton() {
Button messageButton = (Button) findViewById(R.id.Settings);
View.OnClickListener myListener = new View.OnClickListener() {#
Override
public void onClick(View v) {
Log.i(T, "You clicked the settings button!");
startActivity(new Intent(MainActivity.this, SettingsActivity.class));
}
};
messageButton.setOnClickListener(myListener);
}
private void feederButton() {
Button feedButton = (Button) findViewById(R.id.AddFeeder);
View.OnClickListener my2ndListener = new View.OnClickListener() {
public void onClick(View v) {
Log.i(T2, "You clicked the add button!");
startActivity(new Intent(MainActivity.this, ManualAdd.class));
}
};
feedButton.setOnClickListener(my2ndListener);
}
}
You cannot make a different onCreate() method for every button you have. The reason why only one of your Buttons work is because only the onCreate() method is only called. The system has no idea about onCreate1(). To get both of your buttons working change
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
}
to
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
settingsButton();
feederButton();
}
You can completely delete onCreate1() from your source code, it is now obsolete.
It would also be a good idea to follow the official Android "First App" tutorial instead.
Try this code
public class MainActivity extends Activity {
Button Your_Button_Name;
Button Your_Button_Name;
Activity activity;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.Your_Layout_Name);
//In (R.id.Your_Btn_Name) that's the name that you gave your button in the XML
activity = this;
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name = (Button) findViewById(R.id.Your_Btn_Name);
Your_Button_Name.setOnClickListener(listener);
Your_Button_Name.setOnClickListener(listener);
}
private View.OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
case (R.id.Your_Btn_Name):
startActivity(new Intent(MainActivity.this, Your_Layout_Name.class));
break;
}
}
};
}
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.
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'm trying to switch the views, but when I'm in the second view, the back event click doesnt work.. I don't know what's wrong.
Pls, see my code and help me!
Part1
Part2
public class t extends Activity implements OnClickListener {
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
volta = (Button) findViewById(R.id.button2);
volta.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v == volta) {
startActivity(new Intent(t.this, MainActivity.class));
}
}
}
You have to override onBackPressed. Change your MainActivity as below
public class MainActivity extends Activity {
private boolean goBack = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
goBack = true;
setContentView(R.layout.janela2);
}
});
}
#Override
public void onBackPressed() {
//If you have switched to R.layout.janela2 then go back
if (goBack){
setContentView(R.layout.activity_main);
goBack = false;
return;
}
//else do default action
super.onBackPressed();
}
}
Just do the following code, I hope it might help you
MainActivity.java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button sobre = (Button) findViewById(R.id.button1);
sobre.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this, t.class);
startActivity(intent);
}
});
}
}
In t.java
public class t extends Activity{
Button volta;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.janela2);
}
#Override
public void onStop() {
super.onStop();
finish();
}
}
If you want two layouts then use viewflipper. If you want two activities (java classes) AND two layouts separately then use:
Intent i = new Intent (this, myClass.class);
startActivity(i);
To start the Activity and NOT setcontentview
So here:
public void onClick(View v) {
startActivity(new Intent (MainActivity.this, t.class));
OR IN THE CASE OF T.CLASS:
startActivity(new Intent (t.this, MainActivity.class));
}
You have to override onBackPressed() method if you want to back button functionality in your application. i.e.
public void onBackPressed() {
Intent start = new Intent(CurrentClass.this,Next_Activity.class);
startActivity(start);
finishActivity(0);
}