I am trying to create an app to work on both tablet and legacy phones that adds fragments dynamically based on user interaction. An example of an app that mimics this logic is called pulse
When the user selects a news source, it is dynamically added to the bottom. I am wondering how this is done. Do I have to create new activity every time a new fragment is added?
Anyone, please advise. An example would be duly appreciated.
No, you don't need to create activity. You can add, remove, hide and show fragments in current activity.
Please read Fragment documentation and take a look at FragmentTransaction.
Refer GoogleIO,CheckoutLink this explains how add fragment dynamically
Related
I'm new to Android development and I wanted to ask what is the best solution here.
In my activity_main.xml I have some content and on the bottom there is <android.support.design.widget.BottomNavigationView>
I set some icons and created listener for it. The Navigation contains simple Settings icon, and when it's clicked it should start the SettingsActivity. This navigation shoud be everywhere in the app.
Here comes my question :
What is the best approach when I don't want to implement this BottomNavigationView in every single activity, implementing the same listener over and over again in every activity ? I've heard of using fragments instead of activity but these are now deprecated right? There must be a better way, implementing the Navigation over and over again with exact same code can't be right approach.
Here comes my question : What is the best approach when I don't want
to implement this BottomNavigationView in every single activity,
implementing the same listener over and over again in every activity
You won't need to do that since Fragments are here to help and they're not depreciated but, replaced with AndroidX (new) one: androidx.fragment.app.Fragment. Instead of implementing it over and over again, replace the new Fragment when the other item selected.
Use setOnNavigationItemSelectedListener then do your stuff.
Check the sample here.
While regular com.android.Fragment is deprecated, the support fragment class located in either androidx.fragment.app.Fragment is not deprecated, and is probably what it makes the most sense to use here.
So using the fragment approach, you will have one Activity class with the BottomNavigationView set up. When the user navigates using the BottomNavigationView, you'll change which fragment is being shown in your Activity. This tutorial should be a good starting point for how to enable this functionality.
I often put user registration on one xml file with scroll view.
I have learned when a user sees too many fields they opt out of the app so I am planning to upgrade the layout to look like the one for gmail when you sign up on android app. It appears to be using tabbed view but not tabs available and user navigates to the next view by clicking next (refer to attached images).
First page
second page
MY QUESTION
My question is how do I create this views, do I need many xml layout files or put all stuff on one file and use visibility as in once it is filled and user presses next hide the inputs and reveal the next inputs. Note: Gmail appears to be scrolling to another page the way it would appear when using tabs.
You can use one activity to host multiple fragments. The new Android Studio update has Navigation Editor which you can use to achieve similar results.
it is totally up to you how you want to design this but the best thing is you use Fragment and viewpager instead of using two separate layouts.
See Here how to use fragment with viewpager
There are many ways to do this, If you have less content then play with visibility otherwise ViewFipper ( https://abhiandroid.com/ui/viewflipper ) Or can use Fragments
I have existed code for two activities(ActivityA and ActivityB).
Now I need to have a another activity, which will act as my first and only screen of my application. This activity will be having two tabs on its header part and I need to display each of my Activity when user press on each tab.
I have done some search on this and found TabAcivity will be suitable choice, but it is deprecated now.
I saw many posts which are suggesting Fragments. But I dont want to change my existing code. Can any one suggest me easy method to incorporate this.
you can use Android Tab Layout with Swipeable Views
here i give Link in this link step by step information given. so i think you can get batter idea from that.
Tab Layout with Swipeable Views
you can also download code from this site and also see video in given sit
lets say I have master/detail pattern using fragments, so as usual, the detail fragment would be inside new activity on a phone and on a tablet it would be inside the previous activity that list fragment is in also. Old stuff.
I have some actions to perform on the detail, like share, it would be actionbar item. Old stuff.
But now, I want to implement Contextual ActionBar, or a dialog, it doesnt really matter in my opinion, that would allow sharing right from the list.
My question is, where should I have that sharing code? I want to avoid duplicates. In my head, it cant really be a callback because the detail can be inside the dual activity or inside its single activity. Its just really confusing to me how should I do this.
Hope you get it. Thanks!
where should I have that sharing code? I want to avoid duplicates
Taking a guess that "that sharing code" is your ACTION_SEND invocation, since that is only a half-dozen lines of code or so, tuck that in a static method somewhere that can be accessed by your detail fragment and by the activity that is hosting the list on smaller screens.
Just I have created graph chart using Chart Engine Android
I have used data from my database to use value for chart.
Now I have one activity which is main to display graph chart with left menu options.
So Whenever i am calling chart activity its going to show its own activity sepratly.
I want to embed that chart activity with my main activity with left menu options.
Is it possible? I am using android 4.0
How to solve this task? Please help me friends.
It sounds like your application would benefit from using Fragments The developer site has some very good information you should read.
You can't embed an Activity into another in Android, as Activities are meant to be independent objects.
Since android 3.0, you can use Fragments, which are Activities sub-elements, which you can combine in many different way. For example having your Activity using one left fragment to display a list of items and a right fragment to display the details of the selected item.
You should take a look at the Fragements documentation to implement this in your application.
Try Activity Group.
http://developer.android.com/reference/android/app/ActivityGroup.html