I'm making an application on Android which is to contain 2 games. I've done this but I've come across a problem that the two buttons that are supposed to extend to different GameViews are extending to the same GameViews. The classes both have different names and I've tried changing some of the content to specifically refer to the BallGameView class however it causes the program not to compile. I've had a look around to see if there's anything on extending to 2 separate GameViews in a single application but not came across anything so far.
EDIT: For clarity, the problem is that the buttons that should open different games are opening the same game. The game is compiling but not as I want it to. I'll post the code that's referring to the different games below.
public class BallGameActivity extends Activity {
GameView GV;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ballgame);
GV = new GameView(this);
setContentView(GV);
To refer to Ball Game.
public class BallSplash extends Activity implements View.OnClickListener {
Button playBallButton;
Button guideButton;
Intent ballIntent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ball_splash);
playBallButton = (Button)findViewById(R.id.startBallGame);
guideButton = (Button)findViewById(R.id.guideButton);
playBallButton.setOnClickListener(this);
guideButton.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.startBallGame:
ballIntent = new Intent(this,BallGameActivity.class);
startActivity(ballIntent);
break;
case R.id.guideButton:
ballIntent = new Intent(this,Guide.class);
startActivity(ballIntent);
break;
}
}
To refer to Sprite Game
public class Splash extends Activity implements View.OnClickListener {
Button playButton;
Button instructionButton;
Intent intent;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
playButton = (Button)findViewById(R.id.toPlay);
instructionButton = (Button)findViewById(R.id.toInstructions);
playButton.setOnClickListener(this);
instructionButton.setOnClickListener(this);
}
public void onClick(View view) {
switch (view.getId()) {
case R.id.toPlay:
intent = new Intent(this,GameActivity.class);
startActivity(intent);
break;
case R.id.toInstructions:
intent = new Intent(this,InstructionActivity.class);
startActivity(intent);
break;
}
}
Maybe the problem exists because you call setContentView() twice. You should remove the wrong one.
The correct way to do it is to call that class that you want to use as the view
BallGameView GV = new BallGameView(this);
setContentView(GV);
in your current code you are instantiating GameView but that isn't what you want for this Activity. You need to instantiate BallGameView GV instead. Since you are using
GV = new GameView(this);
setContentView();
in both Activities, you are seeing the same thing no matter which Activity you start. So the problem was never with starting an activity or extending anything.
Also, you can remove
setContentView(R.layout.activity_ballgame);
That line will set the Activity to use that layout. But since you are calling setContentView() again and using the class which extends SurfaceView then it will set the Activity to use that as the layout instead, making the above line useless.
Related
I call a view class from my activity. Then the view class calls the same activity. Here is the problem, once the activity comes back up, it won't register any more button pushes.(I'm trying to call another view class. Here is some code:
View Class
public class AnimationView extends View {
Activity myActivity;
//...
public AnimationView(Context context, Activity activity) {
super(context);
//...
}
#Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//...
myActivity.setContentView(R.layout.activity_home);
}
}
Home Activity
public class HomeActivity extends AppCompatActivity {
private AnimationView mDrawViewA;
///...
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
mDrawViewA = new AnimationView(this,this);
start = (Button) findViewById(R.id.startButton);
//...
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//...
setContentView(mDrawViewA);
//calls more views
//......
});
}
I realize now maybe I should have been calling the view classes in different activities, but I would very much like a get all the view classes working within the same activity.
The problem is you're calling setContentView every time you press the "start" button. This method will overwrite the current layout (if any) with the new value you're setting.
What you can do to get the result you're expecting, which, from what I understand, is to add a new AnimationView to your current layout on every button click, you can try something like this:
start.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AnimationView animationView = new AnimationView(getApplicationContext());
// I'm using ConstraintLayout as an example, since I don't know exactly what layout you're using
ConstraintLayout.LayoutParams params = new ConstraintLayout.LayoutParams(WRAP_CONTENT, WRAP_CONTENT);
// Set the layout params the way you want
addContentView(animationView, params); // This is where the magic happens
}
});
In short, addContentView is the method you should use when you want to add new views into your activity's root layout.
PS.: It's a terribly bad practice to let the views "know" the activity controlling it. It's always the opposite way around: the activity/fragment knows the view(s) it's controlling.
I have a problem and I can't seem to set 2 onClickListener for 2 separate buttons located on 2 different layout, when running the program, it cause an exception to occur.
btnClickToSecondPage button is located in activity_main.xml layout and btnObjClickToGoToFirstPage button is located at second_activity.xml layout.
The java code for my program is located below here
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(getWindow().FEATURE_NO_TITLE);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button btnObjClickToGoToSecondPage = (Button) findViewById(R.id.btnClickToSecondPage);
Button btnObjClickToGoToFirstPage = (Button) findViewById(R.id.btnChangetoFirstPage);
btnObjClickToGoToFirstPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.second_activity);
}
}
);
btnObjClickToGoToSecondPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.activity_main);
}
}
);
} }
Please help me rectified the problem thanks.
Please implement the View.Onclick listener not Button.onclick listener
btnObjClickToGoToFirstPage.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick (View v)
{
setContentView(R.layout.second_activity);
}
}
);
It is not a proper way switching pages in Android. Use two activities for switching pages with intents.
Intent newPage = new Intent (this, YourActivityNameForNewPage.class);
startActivity(newPage);
Put above code in your button's onClick().
If you want to show a new page you either start a new activity or start a new fragment.
Changing the contentView is not the correct way to approach this and should not be done.
You refer to the documentation on activities here.
Assuming you have another Activity called SecondActivity here is how you would start it:
btnObjClickToGoToSecondPage.setOnClickListener(
new Button.OnClickListener(){
#Override
public void onClick (View v)
{
Intent intent = new Intent(MainActivity.this, SecondActivity.class);
startActivity(intent);
}
}
);
And then you define the layout in the XML of the new activity, that is second_activity.xml
If they all have similar layouts using a fragment is also a good option.
Basically you start a new activity or fragment to show anything new or change the data dynamically say on your button's onClick().
This question may further clear your doubts:
What is setContentView(R.layout.main)?
I'm new to Java and I encountered the following code:
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = new Button(this);
button.setText("Touch That!");
button.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View v) {
MainActivity.this.onButtonClick(v);
}
});
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.rootlayout);
relativeLayout.addView(button);
}
public void onButtonClick(View view){
//do something when button is clicked.
}
}
I didn't understand the syntax, View.OnClickListener() c'tor is called and it is followed by {} and overidding method.
what does this syntax stand for?
to which object this refers?
My guess is the button. but if I'm right why to use MainActivity.this instead of this? (the object that invoked the method)
This is an anonymous class declaration. It means that you will override some methods inside the class dynamically.
Take a look at this arcticle:
http://docs.oracle.com/javase/tutorial/java/javaOO/anonymousclasses.html
About your second question, MainActivity.this refers to the instance of the Activity you are currently in. If you call only this, it would refer to the actual object. When you call MainActivity.this, you will get the instance of MainActivity you are in, even if there is more activities created. Take a look at Android's activity lifecycle.
What's the difference between this and Activity.this
Hope it helps.
By calling
new View.OnClickListener(){}
you are creating an object implementing interface OnClickListerner that requires you to implement the click method.
Someone can correct if I am wrong.
I currently making a application however, I'm faced unable to start the activity componentsInfo. I found a lots of solutions on the net, but none of them is works for me. I try to clean my project file and restart Eclipse, errors still occur. I try to debug with DDBS, and find a 'setOnClickListener' had something wrong. I try to edit it. But error still occur.
Note: Implements view.OnClickListener didn't works for me also.
public class MainActivity extends Activity{
private Button mBreakfast;
private Button mLunch;
private Button mDinner;
private Button mSnack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
mBreakfast = (Button) findViewById(R.id.btn_breakfast);
mLunch = (Button) findViewById(R.id.btn_lunch);
mDinner = (Button) findViewById(R.id.btn_dinner);
mSnack = (Button) findViewById(R.id.btn_snack);
mBreakfast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,RestInfoFragment.class);
startActivity(intent);
}
});
}
}
And well it is probably a mistake to import!
if so:
import android.content.DialogInterface.OnClickListener;
change to:
import android.view.View.OnClickListener;
RestInfoFragment is a fragment, and you want to start it on click of the button?
Fragments aren't invoked like Activities using Intents.To start a fragment, use this code:
getFragmentManager().beginTransaction()
.replace(R.id.container, new RestInfoFragment).commit();
From your naming , I guess the RestInfoFragment is a Fragment, and you are using a button click in another activity to navigate to the RestInfoFragment, like an Activity.
You can't use Fragment as an activity. Either change your RestInforFragment to extend a FragmentActivity or inflate the fragment in a view.
If you extends FragmentActivity , you don't need any changes to the current MainActivity.
If you are using Fragment
getFragmentManager().beginTransaction()
.replace(R.id.container, new RestInfoFragment).commit();
If your RestInfoFragment is using the whole screen , I recommend the use of FragmentActivity.
I'm trying to create an activity that adds a dynamic fragment at runtime. From that fragment I want to be able to open six other fragments on button click. [Going to use a case to implement this most likely]
Think of it as a windows 8 UI; with 6 buttons, each one opens a new fragment.
Unfortunately I have no idea how to go about this. I can't seem to get the button to pass data back to the main activity. I've also lost quite a bit of my code due to a git mishap. Here's what I recreated.
If you have any tips on coding style, syntax, java, OO- those are all welcome too. I'm coming from a C background. My end goal would be to create a replaceFragment(Frag) method for some easy syntactic sugar later on. Though I couldn't implement that with any success so far.
Another small question with fragments - I'm trying to add them dynamically at run-time - do I need to create all of them at run time? So each one needs a .add [Drink fragment, Menu fragment] or do I just need to do the .replace
SingleFragmentActivity.java
public abstract class SingleFragmentActivity extends FragmentActivity{
protected abstract Fragment createFragment();
FragmentManager fm = getSupportFragmentManager();
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //Lock screen orientation for app
Fragment frag = fm.findFragmentById(R.id.fragment_container);
fm.beginTransaction()
.add(R.id.fragment_container,frag)
.commit();
}
}
Customer_Activity.java
public class Customer_Activity extends SingleFragmentActivity {
public static Context appContext;
#Override
protected Fragment createFragment() {
return new CustomerSelectionFragment();
}
}
CustomerSelectionFragment
public class CustomerSelectionFragment extends Fragment implements OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.customer_selection_fragment, container, false);
//Buttons should be placed here?
Button btnDrink = (Button) v.findViewById(R.id.Drink);
btnDrink.setOnClickListener(this);
Button btnMenu = (Button) v.findViewById(R.id.Menu);
btnDrink.setOnClickListener(this);
return v;
}
//implement the onClick method here
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {
case R.id.Drink:
//Not sure how to pass "Create Drink Fragment to activity?
break;
case R.id.Menu:
//Pass Create Menu fragment to activity?
break;
}
}
}
Totally ok with people editing my post for good-faith reasons [clarity, etc].
Any communication between fragments should be done via activity . Here is the link to developers site http://developer.android.com/training/basics/fragments/communicating.html , the tutorial is about communicating between fragments and pretty much explains everything.