I am currently developing a java application
since Im a noob
I dont have any idea how to move between two initcomponent view
for example when the user click search than the whole view change to a new view with different classes
than there are some cases that the user press the back button then go to the previous view
and how to pass the parameters between the two view
thx I'll appreciate your reply
probably you have look at CardLayout, then there no needed create a new Window or another separated View
Related
So im wondering how I can change what is displayed on the phone screen without the need for creating a new activity each time I wish to do so.
For example in a simple game im trying to make: there will be a small row of buttons (inventory, stats, save, options, etc). When I press one of these buttons, how can I change the view within the same activity to show the appropriate data without having to create an entirely new task, if possible.
Two possibilities here depending on what you're really trying to accomplish.
1: If this is just another xml layout you want to display I would suggest using fragments.
http://developer.android.com/guide/components/fragments.html
2: If this is graphical ( often true with a game ) you will need to extend SurfaceView and
implement a drawing thread. http://developer.android.com/reference/android/view/SurfaceView.html
( There are multiple examples of how to do this if you google for SurfaceView example ).
It's not a good practice but if it's simple enough you can just use view.SetVisibility(View.Gone) to views you wish to hide and view.SetVisibility(View.Visible) to the views you wish to show.
I have defined my Relative Layout using the drag and drop tool in Eclipse, so all of my buttons are laid out how I wish. My issue is when I set the onClick listener, that calls a method in another class. So to be able to redraw items on screen, I need to access the layout manager so I can add and remove buttons from the screen as well as update textViews. I have done all of this in a demo I made in Java, and I used a JPanel with GridBagConstraints. Now that I am moving to Android, a system I haven't done much development in, I am at the point where I have to learn some new stuff. For example in my demo I made I could do this:
grid.remove(trueButton);
grid.add(falseButton);
grid.remove(textField);
grid.add(backButton);
Essentially I want to be able to do the same sort of thing in my Android app. If you guys need more info I can provide, I wasn't really sure how much would be needed since I am looking at really just where to start. Everything has been declared in the XML since the drag and drop part of Eclipse does that all for me. It is just the Java part that is giving me some issue.
Why not just setVisibility of the buttons you wish to hide/show? Same with the TextViews.
You can set visibility to 'GONE' and it will be as if the view has been removed (taking up no space in the layout and not responding to touch events.).
I hope I can explain this properly.
I'm making an android app that, when you open it, it connects to a JSON server, pulls down the data (GPS coords) and dynamically creates a "menu" based on what it received (View1). This will consist of a few buttons, which when clicked, will load a MapView (View2) with the coords gotten from the JSON represented as markers on the map.
I start off with setContentView(R.layout.menu) then get the data, onbuttonClick I load setContentView(R.layout.map) and draw the markers. The problem is, I have an onLocationChangedListener that goes through the code to set up the menu initially. When it tries to build the menu when the mapView is open, I get a force close. Unfortunately, this code also updates the user location and the locations of the overlays and re-draws the map.
My question is: Can I do a check on a layout to say something like if (isActive) so that I can perform actions only if the current view is in focus?
OR should I scrap the whole thing and start again with a better layout? (Suggestions welcome)
Summary::: I have 2 views (menu,map). Need access to same data across both. Currently works with setContentView() but gives me a Force Close when actions are performed on inactive view.
If I understand correctly, you are using setContentView(someLayoutId) to change each time what the Activity is displaying. This is not the way android apps usually work. When you retrieve a resource, you need a root element to reference it, that's why you get the exceptions when the View is not "active".
You have several other options to evaluate:
Create a new MapActivity to show the map
Create a TabActivity and add the map as a new tab
Use a ViewSwitcher to decide whether to show the map or not.
I am developing a Java application, well, it's actually a small game. I want to build up the application as follows: when it starts, a window should appear which has a menu with four choices: 'Start game', 'Options', 'Highscores' and 'Quit'. If you then click game, the game starts, preferrably in the same window, if you click options, well you know the drill.
How should I program this? At the moment, I'm considering using a CardLayout, but I'm not sure this is the right way to do this.
Do you guys maybe have another proposition?
Basically you have four different views: the menu view, the game view, the options view and the highscores view. And you want only one to be displayed at any time. CardLayout fulfills that requirement - only one panel is visible at a time and you can switch between panels (like from menu to highscores, back to menu, then to game). Looks fine to me.
Functionally you can do this with any LayoutManager you choose. The only difference is going to be aesthetics. This is a very open ended question and GUI programming is a complex area. The best way to get better is with practice. Try it out with CardLayout and when you run into specific problems make sure to ask them.
My application has a custom view which contains other custom views of a different type. The subviews have their own click listeners (which I can't change, as these are in 3rd party libraries). How can I intercept a user's click at the level of my view to do some processing, and then pass the click on to the proper subview?
Justin, you can play with dispatchTouchEvent() or onInterceptTouchEvent().
I'm not entirely sure about this, but in Java what I would do is define various subview objects in my main view and simply send those to the draw/paint function. That way I'd only have ONE click listener which would be in the view anyways.
Can you do something like that in Android?