Dynamically load layout in Android - 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 :-)

Related

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

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

Android: two activities, one tab

What I want to do is the following:
I have 4 tabs in total, but in 1 tab i want to have multiple screens which the user can change using the menu
I can capture the event of when the user presses the right button, so no help needed there:)
what I can't do is starting the Activity without removing the TabBar!
I found some information about changing the view of the framelayout, but i really want to start another activity in the layout, not just change the view (I want to keep my code clean you see;)
thanks in advance
so in Tab A I want to be able to use Activity A and B
The only solution I found is still replacing the view, but doing it through ActivityGroup, so the code would still look normally and would be separated in activities. Works great, but there still is needs for hacks like back button press and stuff.
Scrapped this idea, though, since later I wanted more flexible and more stylish tab bar, so I wrote my own. Works like a charm and took some 2 working days to code.

How to Use Menu in Complete Application in Android

I am working on an application in android. Where I need to use menu through out application. Actually it will be used for navigating within application from one activity to other and so on... I am wondering that it will need to write onOptionMenuCreat and OnOptionMenuSelect methods in each activity. Also if I make one parent activity where these two methods could be written even then on each activity Option Menu will be created again. So handling same menu while it is created again is becoming work of afaik. Any good design suggestions are most welcomed. Another very important thing how to map that user was on witch activity last time while selecting same menu again. My app has total five menu options so that means there are five different ways to move within application simultaneously.
Please Do tell me solution for both issues. Thank You.
You create the Menu in an Activity and inherit all the other Activities from this Activity.
To answer the second part.
create a different method for each menu handling in the parent activity. If you need a different menu/menu_item behaviour for your activity, just override that method.

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.

Modifying application workflow to use TabActivity

This question actually has two parts.
The first part:
I've been developing my first app for a couple of weeks now. I have 5 screens and everything seems well. However, I'm considering changing the app's navigation to a TabView.
I haven't delved much into it, but I'm hoping someone can save me a little bit of time. It seems that people don't generally place Activities inside each tab. They simply point the tab content to a View. This is where my major setbacks are. 1) I already have Activity classes full of code and 2) I can't quickly guess how the structure of an app using TabView looks. For example, where do I put the handler code for clicking a button on a View? Does it all just get dumped into the TabView Activity somehow?
What I would like is if you could please give me a quick synopsis of what I'm looking at doing, answers to any questions you think I may have, and point me toward some resources for creating TabView applications. A quick Google search really just shows me how to create a TabView Activity and add a couple tabs to it. The code doesn't go any deeper. For example, say I have a layout xml to show in one of my tab's content pane, where does the code go for clicking a button I have in that layout?
The second part:
I've added a TabActivity to wrap the Activities I currently have in. At the moment I have Activities populating the content of my tabs (though ultimately I'd like to do this in the most efficient fashion, which doesn't seem to be having Activities be tab content). I've noticed something rather annoying. My MAIN Activity is an Activity I wrote for my user to log in to their account. After logging in, they are taken to my Tab Activity. Here is what happens:
When I am on my Tab Activity and I "minimize" the app by clicking the Home button and then launch it again, I don't get taken back to the Tab Activity. I get taken to my log in Activity. Why? I don't have the launchMode of my Tab Activity set to singleInstance... or is it singleInstance by default? How can I make the app re-launch showing the Tab Activity (ideally by setting some parameter, assuming I'm doing something wrong, and not having to save this data off somewhere and reading it and programmatically telling it what to go to)?
Thank you for all your time and help
I don't have a comment on the advisability avoiding the use of sub-activities in TabActivity. As for handlers -- if you aren't going to embed views instead of activities, then all the android:onclick type handler settings in your layout XML will call methods on the TabActivity. This is because they go to methods on the views' Context, which is the generally the nearest containing Activity. If you want to split your code up further without using Activities, I believe you'll have to use findViewById calls on the tab content views after you've set them up, and bind the handlers manually from there in your code.

Categories

Resources