What is the difference between view and activity in android development? - android

When do I need to create a new activity and when do I need to change the view?
My app needs to do:
Screen #1
two big buttons(sort of menu)
Screen #2
list of items - depend on the selection on prev screen
Screen #3
another list - depend on the selection on prev screen
Screen #4
show item
All screens need to have the same menu menu (the last has another button)
Do I need to create an activity for each screen or just change the view in the same activity?
Maybe I need to create a parent class myBase that will extend activity and all my activities will extend him?

A View in Android is a widget that displays something. Buttons, listviews, imageviews, etc. are all subclasses of View. When you say "change view" I assume you mean change the layout by using setContentView(). This usually only needs to be done once per activity. An Activity is basically what you are referring to as a screen. To answer your question, it sounds like you need four separate activities (one for each screen).

You should create separate activities for you screens. Android handles the back button of the device by popping the current activity out from the stack and displaying the last one. So if for example the user wants to return to screen 2 for another selection, the back button does this.

The "right" way to do it is to use Activity for each screen and use the <include> tag for the menu you would like to be in all screens.
This way you will have the "back" button act as it should be and it would be easier for you to handle when switching screens.
To use the , you should put the things you want to reuse into extra files.
Then you can use it as follows:
<!-- my_header.xml -->
<?xml version="1.0" encoding="UTF-8"?>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text01"/>
In another file include it with:
<include layout="#layout/my_header" />
<!-- your other stuff -->

activity is like canvas where you put your drawing as view.Yes you can set all above four view in single activity but it will depend how you handle it and does your app needs it to be done like this.

View is Display System of Android where you define layout to put subclasses of View in it eg. Buttons, Images etc. But Activity is Screen System of Android where you put display as well as user-interaction,(or whatever which can be contained in full-screen Window.)
Now for your question,you are creating full window screen#2 ,screen#3... ,so it is activity.
In these screen you can define layout/or Views.
I hope it helps.

You should create 4 xml files... and play Around changing content using setContentView(R.Layout.yourxml);..
you can do this using single Activity.. it depends on how big the class becomes.. if its too heavy with lot of different stuff, for the sake of cohision and to avoid coupling use multiple Activities

Related

Create an activity that doesn't take up the entire screen

Is it possible to create an activity in Android that doesn't take up the entire screen? I would like to create one that is only 1 pixel x 1 pixel wide and launch it.
No it is not. An Activity is designed to be the current active screen.
You can 'fool' people with the view that goes with the Activity, such as making it (partly) transparent but your Activity will still be the one receiving touches.
Create activity with dialog Theme by using this
android:theme="#android:style/Theme.Dialog"
Activities are often presented to the user as full-screen windows,
They can also be used in other ways: as floating windows (via a theme with windowIsFloating set)
or
embedded inside of another activity (using ActivityGroup).
If you want to create with given pixel you can not do it.. Instead you can go for Fragments, DialogInterface in Android which can be created with size specified in layout xmls

Is there a way to load an activity into a certain part of the screen?

On my app, I have a menu that I want to display for every activity and different page I have. The problem is that I can't find a way to apply this menu to ALL activities, nor can I find a way to load an activity into a certain part of the screen.
There is a generous sized rectangle in the center of the screen and I would love to have it so that whenever I open a new screen that screens layout loads inside the rectangle.
Or am I just going to have to copy and paste the xml code for every layout?
You should consider using Fragments. You can define a Fragment that can be reused in every Activity in your application without having to duplicate its logic between different Activities. Hope this helps.
You cant load Activity to a certain part of screen. For that (as suggested by #Egor) use Fragment.
However, regarding the common menu thinggy, I suggest you to create a base class which extends Activity and contains all the stuff you want to be commonly available in your activities. Later, use your base class to extend your activities.
There are someways to do this.
One is to write a Custom Menu that you can inflate by onCreateOptionsMenu, alternatively with the ActionBar.
Also you can do this by writing a separate xml with your menu and add it to all your layout-xmls with the <include> Tag.
The third way is using the Fragments-Class: link
you should never be cutting and pasting xml code from one layout to the next.. that is why we have includes:
<include layout="#layout/header"/>
don't over think your problem, you should look into fragments so that you can create reusable widgets like a menu, or extend your activity to abstract away your menu logic.

Dynamically load layout in Android

My application requires 2 screens and for this I have created two different XML layout files using RelativeLayout. One layout file loads whenever I run my Activity. Now I want to load the second layout on to the same Activity , when user click on a button in OptionsMenu and also when user press Back button the first screen loads instead of exiting the application. So that i don't need to create another Intent in my application.
Ideally there should be two different activities present in your application.
You can add or remove a view component in a view but if you are looking for two completely different screens then i would suggest you to go for a new activity.
I dint get what you meant by "and also when user press Bakc button the first screen loads instead of exiting the application"
If you dont want to show the first screen just finish() the activity.
Did you try re-calling the setContentView?
Or you may prefer using the ViewFlipper,
Good example here.
I would suggest a rather simpler means.
Put both your layouts in the single XML and show/gone them appropriately as need be. I dont think u need anything more complicated :-)

Android Activities vs Views

Sorry, I know that this topic has been covered a bit. I've read the related posts and am still a bit confused. I am working on an app that while the prototype will have 3 main screens, it will eventually have dozens. Each screen will present either dynmically changing status or take user input. To visualize, it is required to be laid out similar to how MS Word or a typical PC is. It has a status bar at the top and a navigation bar at the bottom that is common to all screens (slight tweaks for some screens, like different icons) in the middle is what I would call a view pane that needs to be updated with a applicable layout.
The status, nav bar, and each screen are defined in their own layout xml file. For my first swag at it I just used a ViewFlipper and loaded the 3 screen layouts into it. However that means that currently I have one main Activity which will not be maintainable as I continue to add screens.
It feels right to me that each screen layout should have an associated Activity class that understands how to control that screen. I need to figure out how to load that into the center pane dynamically. However I thought I read in another post that using multiple Activities can be a CPU and RAM drain.
Currently I tried making one of the screens it's own Activity and kick that off from the main Activity by creating an Intent and than calling startActivity. However that causes the new screen Activity to reside on top of the main Activity. The interesting thing is that then pressing the back button dismissed that activity and returns me to the main.
So far I haven't figured out how to setup having a different Activity control what happens in the center pane.
If I continue down the multiple Activity path, should my main Activity be inheriting from ActivityGroup?
Are using View classes more applicable in this case?
I know this has been a long post. I'd appreciate any advice.
Thanks!
CB
As you noticed, Android will implicitly track a stack of started activities in a task, and the 'back' button ends the top one, reactivating the next one down. I would advise you to think about which kinds of things the user might expect the back button to do, and make it so that activities are separated along those lines.
I haven't played with ActivityGroup so I can't advise you there. If you go with completely separate activities, you can have them all use the same "shell" content view with the common nav/status bar. Have a superclass or utility class handle populating and managing that from there. Then use a a LayoutInflater (you can call getLayoutInflater()) to fill in the middle with your Activity-specific view.
If you want one of the activities to have multiple screens, you might still end up with a ViewFlipper in the center slot. Again, you want to have an Activity transition wherever you want the user to be able to go "back"; that also means you may NOT want to have a change of activities in cases where screens are closely related or part of the same logical thing-being-done. (You can override the back button's behavior, but unless you have a good reason to, it's best to just arrange the app so that Android's basic setup helps your app's UI rather than working at cross purposes.)
If you want to use activities in the fashion you talked about, you might look into using a tab activity. It actually works in the way you want, you just need to hide the tab widget and put your navigation bar there instead. Or, you could go a little deeper and make you own similar tab-like ActivityGroup like Walter mentioned if you have more time.
You could use a view pager with fragments to accomplish the flip between the different views but still allow your activity to have full control over it. The activity can control the menus while the fragment controls your viewing area. This way your back button will properly dismiss the activity containing all pages related to the activity instead of walking down the stack.

Android Development: Switching between Views without losing onClickListeners

On my application I'm developing, the main.xml layout (the default layout of my app) has a few buttons that have been assigned onClickListeners (not the implementation way).
One of those buttons I want to have the ability to take you to another view. On the other view (preview.xml), there's another button that takes you back to the main.xml view.
I've used setContentView in the onClickListeners of those buttons and this works fine so far, but after you click the button that takes you back to main.xml, the buttons on main.xml have lost their onClick functionalities.
How can I get this to work right? I presume using setContentView isn't the right way to do this.
Your best bet, at Konstantin says above is to use Activities, as you will come across these a lot whilst developing for android. you can read about them here Activities. I assume you want to pass something onto the preview.xml page? If so, I'd recommend either putting it as an extra in the Intent used to start the activity (see the link) or creating a static reference in the activity (which you set before you launch it).
I'd say use two different activities and switch between them. Another option can be ViewSwitcher.

Categories

Resources