Android navigation through multiple layouts - java

i have this app that i want to have a specific way of navigation. And so i made a research but i got confused. And i am begginer in android development.
i want to ask what kind of layout or anything i can use to achieve that. I dont search for super specific answer, just what is the thing that is doing the job i want. So there it is:
the blue block("choose a brand" view) has to be on that position at all times and only changing the text if needed. I want when one image button is clicked the whole green block with the image grid change into another xml layout. I want to call multiple layouts in the green block when i interact with the buttons of those layouts. Atm the green block is an <include layout ="layout.xml"/>
I really apreciate any answer. Sorry if is basic but i really tried to find the thing i need but so far i see solutions that prevent me from using simple inflaters. Thanks in advance

You have to use fragments for this scenario.
You will have a LinearLayout where the first element will be your blue block.
The second element will be a FrameLayout that you will change into the Fragment you need (usually it will have the ID container).
Create a Fragment and set the layout your layout.xml file.
Create a second Fragment with the desired layout you would like to change the green block.
As you click on a imageButton you will have to change the current Fragment with the desired one. Here you will see how to send objects to fragments.
You can find here a way to switch between fragments. In the ft.replace method the first id is the FrameLayout you will use as a container(see above).
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.container, new NewFragmentToReplace(), "NewFragmentTag");
ft.commit();
Be careful when you import fragments. If you use the support package you will have to use getSupportFragmentManager();
Read more on the android developer about fragments

I think what you are looking for is Fragment class, which can be used as reusable code that can be linked to a UI/layout.
From android documentation:
To create a dynamic and multi-pane user interface on Android, you need to encapsulate UI components and activity behaviors into modules that you can swap into and out of your activities. You can create these modules with the Fragment class, which behaves somewhat like a nested activity that can define its own layout and manage its own lifecycle.
Read more here

Related

How to re-use same fragment across viewPager

I have a fragment with a textView. I want the textView to change when I scroll across the viewPager.I want to reuse the same fragment rather than creating a new fragment for displaying each text.How do I do this? I hope pro developers can understand what I mean.
You cannot attach a same fragment into activity twice simultaneously. If you want the behavior you mentioned don't use ViewPager. Just use swipe gesture detection in a layout and maybe some animations.

For supporting different screen size why should i use fragment in android instead of activity

I am beginner to android..I am started new android project..for supporting
different screen size..in fragment documentation they given to use fragment..but
why cant i use activity in android..if i use activity or fragment..which i should i use in this both..please dont give link of activity or fragment..please anyone answer me..i dont know which to use?...i want about all documentation they given about activity and fragment but i dint understand which to use..below is the link i read about fragment..if i use activity i should do more codings?
https://developer.android.com/guide/components/fragments.html
In fact you can't use a Fragment alone, Fragments are inside the Activity.
One point of using the Fragments for supporting different screen sizes is the ability to implement some views like a "Master/Detail" view.
A Fragment, as its name says, is a part of a bigger controller "the Activity", its reference can be removed and it's cleaner than having a big massive Activity to handle all the states of a view.
So the use case is completely depends on your project and its User Interface. I'd be glad to help you if you give me more information about your project and its design.
I think you will need at least one activity. And then for better handling different device rotations and screen sizes you can use one or more fragments inside this activity.
I try to explain this with an example:
You want to create a nice music player app which should look nice in portrait and landscape mode.
You split your app up into three fragments:
Here you can see how the app looks in portrait mode. The activity shows two fragments: The first fragment only consists of a listview. There the song titles are listed. On the bottom you can see the second fragment, which displays the song title of the current playing song and got a button for pausing the music.
When your user uses the music player app on a tablet in landscape mode you have more space for displaying stuff. Then the activity shows the list fragment (which also gets displayed in portrait mode) and it shows a third fragment which shows detailed information about the current playing song (e.g. the album image) and a progress bar.
By using fragments you only need to write the code for the list once.
Sweet and Simple thing, What i recommend is always use Fragments,
But for Fragment you will require Activity.
Take it in this way , Activity is a Canvas on which you can put any number of Fragments.
Whatever your UI is always use Fragment present on a activity if you want to show one screen even then also, So that you will always have Flexibilty to use all those cool things which fragments provides,maybe in future or in current.
If you use activity it has limits,FOR EXAMPLE, LIKE in INSTAGRAM AT BOTTOM, It has FIVE OPTIONS, Suppose THOSE OPTIONS ARE ON A ACTIVITY AND BY CLICKING ON THEM YOU CAN SWITCH TO DIFFERENT FRAGMENTS.
For more info:
Here is the most accepted answer for this topic.

Architecture for a drawer layout app

I am building an app with a drawer layout similar to the Android Facebook app. I am wondering what the best method for architecture is. Should I have a main activity which is responsible for the action bar, and then have it use fragments to display the content of each menu item, or should I be using one activity to manage the action bar, and then have each menu item kick off entirely separate activities?
I could also imagine building multiple activities, which each have to manage the action bar. This option seems the worst.
You have two architecture options here
MainActivity with Fragments
ParentActivity that handles drawer and lots of Activities that extends this Activity.
I have tried both in different projects and found some things worth sharing.
For me The MainActivity that handles drawer and then using Fragments to fill the display is the best.
You will need to handle callbacks from specific Fragments in your MainActivity and redirect them to the specific Fragment they came from. This is mainly if you use Interfaces in objects lower in the Arcitecture chain since you sometimes need to pass down Activity to certain objects. This generates more code that are not as generic as one might want in top level architecture node.
If you are using a ParentActivity and extending it for each ChildActivity, you can write all specific code in the child, meaning that the toplevel ParentActivity will almost only have generic code.
If you are using the ParentActivity with ChildActivities and you are switching between Activities, you fill get the graphic when an Activity closes and the next opens every time a user switches between navigation objects. If you use Fragments this wont happen as the Fragment will be switched in the background. The user will also experience that the navigation drawer will be closed and recreated each time he clicks on an item there.
Its also unnessecary to recreate the navigation drawer with each click on an item. This is a minus for the ParentActivity approach.
With the ParentActivity approach you will also have to keep track of how the backbutton should function, this will be autoaticly handled for you with Fragments. Also when starting new Activities you have to choose if a new Activity should be created or if the old should be killed etc.
Just my 5c, hopes it helps :)
The best way is to use one Activity with one Fragment per section/view.
Take a look at the design documentation.
Also see the Tutorial and Sample Application. It's fairly straight-forward.
You will have one activity which manages ActionBar, Drawer (ListView!) and Fragment.
Every time it clicks an item in the ListView it updates the fragment with the new view.
If you use different Activities then you should use intents with a very bad effect, use a different activity only if needed (if it's totally unrelated to the current activity maybe?)
Official documentation: http://developer.android.com/training/implementing-navigation/nav-drawer.html
If you got any problem in creating this, online you can found more tutorials but the official is very great.
You should have the activity holding the actionbar & drawer
When using a drawer you should not start new activities from within the drawer but fragment instead
Good post & video about it: https://plus.google.com/u/0/+RomanNurik/posts/3nMVVQzUTjG
another good read: http://www.androiduipatterns.com/2013/05/the-new-navigation-drawer-pattern.html
And finally this is a must see also (check the slides or the video): https://plus.google.com/u/0/+NickButcher/posts/1jeyV2n1ZpM

Should this be 2 fragments or 2 activities?

I have a tabbed interface for my program - there are two tabs: take photo and view photos.
As the name suggests, the user can take a photo in "take photo" and the user can view photos taken in "view photos". Right now the way its set up I use one single MainActivity and I have TakePhotoFragment and ViewPhotoFragment -- question is: does this contradict the principles in which Fragments are supposed to be used in? I don't really anticipate having both fragments displayed in a single screen (e.g. on a tablet), but I don't see how I can use one activity for each because of the limitations of the tabbed interface (when I created the activity in eclipse, I was prompted to select what kind of layout, I chose tabbed layout, and automatically code for fragments within an activity corresponding to several tabs was generated)
Can anyone help? Should "take photo" and "view photos" be fragments or activities?
It should definitely be fragments.
This does in no way contradict anything, plus I do not understand your concern about showing both fragments in a single screen. If you do not want that to happen, you just program accordingly. That is certainly not something that just happens because of the choices that you have mentioned so far.
Fragments is the best method you can use for the purpose you mentioned above. You can check the below links to know about the usage of fragments.
Creating a fragment
Fragments
android fragments
android fragments tutorial

Group of Views (controls) on multiple screens

I am working on an Android project where a group of buttons needs to show on the bottom of every screen (activity) in the application. The group of buttons are basically a navigation bar. I want to know the best way to do this without creating new buttons for every activity. I have been around programming (C++/C#) for many years but am pretty new to Android and Java so if someone can point me in a general direction, it would be appreciated.
Thanks.
I bet you need to use "include" tag for xml layouts. It's the best when you need to reuse some UI components. See http://www.curious-creature.org/2009/02/25/android-layout-trick-2-include-to-reuse/ for the examples and description.
To elaborate on Konstantin's answer, after you've used include, you'll need to bind actions to these buttons.
If the buttons should have the same action regardless of the activity they are in, use the include tag to create their layout and then create a parent NavigationActivity (or whatever else you want to call it) class from which all your other activites will inherits.
In the parent NavigationActivity class' onCreate method, you can set up the onClickListener (and other needed stuff) for the buttons.

Categories

Resources