Java Event listening compared to AS3 - java

ok first of all sorry if its a stupid question, but I have ADHD and my head is going to explode after reading a lot of useless details, I just want a small, clear example and I'll predict the rest!
Basically I am playing with the Android SDK, and thought its similar to Adobe Flex, and everything went smooth until I tried listening to a Button click.
in AS3:
private function onAddedToStage(event:Event):void
{
myButton.addEventListener(MouseEvent.CLICK,handleClick);
}
private function handleClick(event:MouseEvent):void
{
trace("You clicked: " + event.currentTarget.name);
}
what I've done so far with trial and error:
private Button _okButton;
private EditText _name;
private View _view;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Log.d("trace", "it works!");
_view = getCurrentFocus();
_name = (EditText) _view.findViewById(R.id.editText1);
_okButton = (Button) _view.findViewById(R.id.button1);
_name.setText("Yupee I found it!");
OnClickListener l = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Log.d("trace", "Button clicked");
}
};
_okButton.setOnClickListener(l);
}
The code above crashes the application
Thanks in advance.

replace _view by this.

Related

Can't get a game loop to work in Android Studio

So I understand the basics of java programming but when I'm trying to use my little knowledge in android studio it make everything harder having classes and different files needing to be referenced. Coming from python, when making a simple game I would define different functions, then run them in a game loop like
while running:
or something similar. I know to define something in java you go like
public void Example() {}
but when I use this in java, when I try to run the program my game either instantly crashes or doesnt load anything.
The code at the moment is
public class MainActivity extends AppCompatActivity {
//Variables
Boolean running = true;
public int years = 0;
//Setup Year Counter
TextView textView = (TextView) findViewById(R.id.year_counter);
//Advance Button
public void advance() {
ImageButton button = (ImageButton) findViewById(R.id.advance);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
years += 1;
textView.setText("" + years + "");
}
});
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Game Loop
while (running) {
advance();
}
}
}
And this results in the app not opening.
Any help at all would mean a lot to me.
Thanks in advance :)
Although I don't actually see a crash, since you didn't really upload one, I can see why you might think your app wont work.
What you are doing constantly in the while loop is that you are only setting the button's click listener over and over again.
public class MainActivity extends AppCompatActivity {
//Variables
Boolean running = true;
public int years = 0;
//Setup Year Counter
TextView textView;
ImageButton button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// you set up your views once, after the layout is inflated
setUpViews();
// you initialize your buttons functionality
initClickEvents();
//Game Loop
while (running) {
// do other stuff
}
}
private void setUpViews() {
textView = (TextView) findViewById(R.id.year_counter);
button = (ImageButton) findViewById(R.id.advance);
}
private void initClickEvents() {
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
years += 1;
textView.setText("" + years + "");
}
});
}
}
I'd recommend working with a game engine.
If you want to stick with java, libGDX is an option.
But if language (and IDE) doesn't matter, than a better option is Godot. The reason why I recommend Godot over some of the more popular game engines is because it's open source, 100% free and plus GDScript (godot's scripting language) is heavily influenced by python.
If you want to make you own game engine in java check out this: https://java-design-patterns.com/patterns/game-loop/#
Keep in mind that the while loop is calling the advance method. So every loop you are setting the view for button and then setting an OnClickListener for it. Not everything needs to be in the while loop.
You must implement (override) the render method (it is called in the game loop).
I suggest trying a complete example in the documentation.
``
#Override
public void render() {
ScreenUtils.clear(0, 0, 0.2f, 1);
advance();
...
``

Multiple Step Actions on one Button Click

I am currently trying to solve a java based problem on Android Studio.
I have puzzled my head over this problem looking in many forums and webpages to not find any solution in the last two days. So I am seeking for help here now.
I have programmed a Button that when clicked causes a textview to swipe out of the screen with an animation. After that I would like the old text ("First Text") of the Textview to be replaced with another text ("New Text") appearing on the same place where the old text was. All of this should happen with only one click on the button step-after-step.
My problem with my code is that the old text is replaced by the new text first and then causes the animation.
Does anybody now a solution for this problem?
I would be really very grateful for any help!
This is my code below.
public class FirstActivity extends AppCompatActivity {
Animation slideleft;
Button btn1;
TextView txt1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.first_layout);
btn1 = (Button) findViewById(R.id.btn1);
txt1 = (TextView) findViewById(R.id.txt1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
slideleft = AnimationUtils.loadAnimation(FirstActivity.this, R.anim.slide_left);
txt1.startAnimation(slideleft);
txt1.setText("New Text");
}
});
}
}
you can use onAnimationEnd method of animation listener and change the text inside it.
animation.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationEnd(Animation anim) {
txt1 = (TextView) findViewById(R.id.txt1);
txt1.setText("New Text");
}
});
you can place which event execute after the animation place your code
anim.setAnimationListener(new Animation.AnimationListener(){
#Override
public void onAnimationStart(Animation arg0) {
}
#Override
public void onAnimationRepeat(Animation arg0) {
}
#Override
public void onAnimationEnd(Animation arg0) {
//your_code
}
});

how to use java onclick shuffle array

I am learning how to use strings and onlclick in java. I have written a programme below which shuffle three names and then outputs them into three buttons.
When I click on Paul, I want the message to be displayed in message box. Since Paul will be in a button each time. I am puzzled on how to attach my message to Paul.
Paul moves around due to the use of array. I understand this is a tough question, but I also know, there are some very clever ppl out there who love a challenge.
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void generate(View view) {
String [] names = new String[3];
names[0] = "Bob";
names[1] = "Paul";
names[2] = "Mike";
Button btn_a = (Button) findViewById(R.id.a);
Button btn_b = (Button) findViewById(R.id.b);
Button btn_c = (Button) findViewById(R.id.c);
TextView message = (TextView)findViewById(R.id.message);
Arrays.asList(names);
Collections.shuffle(Arrays.asList(names));
btn_a.setText(names[0]);
btn_b.setText(names[1]);
btn_c.setText(names[2]);
}
public void a1(View view) {
}
public void b1(View view) {
}
public void c1(View view) {
}
}
This is a trick practical implementation in Java where a single listener is used for multiple buttons, rather than one listener for each button, so that each button's content determines what happens, not each button's listener. Helps for dynamic button grids (i.e. an 8x8 chessboard) to not define 64 listeners and code them all.
I don't have an Android IDE on hand, so this is pseudo-code, but you should be able to get the gist from this.
//Create a Universal Listener for all our buttons
OnClickListener listener = new View.OnClickListener() {
public void onClick(View v) {
Button b = (Button)v;
String text = b.getText().toString(); //get the button's name
if(text.equals("Paul")) {
//do anything for Paul ONLY in here
}
}
});
btn_a.setOnClickListener(listener); //give all the buttons the same listener, but only Paul's listener will do anything when you click on it
btn_b.setOnClickListener(listener);
btn_c.setOnClickListener(listener);
Using info from: http://developer.android.com/reference/android/widget/Button.html and https://stackoverflow.com/a/5620816/2958086

Convert Text from EditText to Integer Value Error

So I'm developing currently my own private App which I want to use only for me and maybe some friends.
Well I'm german so my english is maybe not the best I hope you can forgive me.
Now my Problem is that I want to set in my Optionsmenu a Budget for the current month to keep track of. I'm doing that by using an EditText with a Button.
Now I want to save this String which is getting entered in my EditText to a String value and a Integer value because I want to show the Value in a TextView on my MainPage and use the Integer value to calculate my current budget I got and so on.
Now I'm showing you guys my code and I hope u can tell me whats wrong with it.
I'm trying to get my Value in the Options class which is related to my OptionsMenu and later Trying to get the Value out of my Options class into my Main class.
public class Options extends Activity {
Button GoBack, SetBudget;
private int Budget;
String BudgetString = "";
EditText BudgetEdit;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.options);
GoBack = (Button) findViewById(R.id.button2);
GoBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent GoBackToMain = new Intent("com.lunarrepublic.STARTINGPOINT");
startActivity(GoBackToMain);
}
});
SetBudget = (Button) findViewById(R.id.button8);
SetBudget.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
BudgetEdit = (EditText) findViewById(R.id.editText1);
BudgetString = EditText.getText();
//In the Line above this is the error "Type mismatch. Cannot convert Editable to String
Budget = Integer.parseInt(BudgetString);
}
});
}
But if I try to set my "BudgetString" to Editable It won't work either.
The GoBack Button is unnecessary for my problem here so you don't have to read over it.
So I hope you guys understood what my problem is and can maybe help me getting it fixed
Thanks in advance
This is wrong
BudgetString = EditText.getText();
Use below one
BudgetString = BudgetEdit.getText().toString();
change this
SetBudget.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
BudgetEdit = (EditText) findViewById(R.id.editText1);
BudgetString = EditText.getText();
//In the Line above this is the error "Type mismatch. Cannot convert Editable to String
Budget = Integer.parseInt(BudgetString);
}
});
to
SetBudget.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
BudgetEdit = (EditText) Options.this.findViewById(R.id.editText1);
BudgetString = BudgetEdit.getText();
//In the Line above this is the error "Type mismatch. Cannot convert Editable to String
Budget = Integer.parseInt(BudgetString);
}
});

Android Layout fails to open

I have this code in one of the java file of my application.
public class Board_Play1 extends Activity {
int d,a=0,b=0,turn=2;
Random random = new Random();
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.board_play1);
while(a!=100 && b!=100)
{
if(turn%2==0)
{
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// TODO Auto-generated method stub
d=random.nextInt(6)+1;
EditText diceno = (EditText) findViewById(R.id.editText1);
diceno.setText(String.valueOf(d));
}
});
}turn++;
}
}
}
I come to this java file from another java file. All the problem I get is when this file doesn't have any while loop as in code it runs fine. But with including the while loop on navigating to this layout turns screen black and no further process can be done. If we press back button we have a pop out message saying Your application isn't responding. Do You want to close? Yes No.
Why is that happening. All things I included in while loop are perfect. What is causing for this problem?
I think you have an infinite loop. the condition in your while loop is always true because a and b values are never incremented.
And the reason why you're seeing black screen is that this infinite loop is blocking the Main UI Thread.
Seems that this is an infinite loop to me.
int a=0, b=0;
These values never change in your code and you are using them in your while loop's conditional.
Try something like:
while(a!=100 && b!=100)
{
if(turn%2==0)
{
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
d=random.nextInt(6)+1;
EditText diceno = (EditText) findViewById(R.id.editText1);
diceno.setText(String.valueOf(d));
}
});
}
turn++;
// a = somevalue based on what you're trying to do.
// b = somevalue based on what you're trying to do.
}

Categories

Resources