I am developing an app where i am using tabwidget with 2 tab views. When the app is visible on screen, in first tab some data needs to be displayed in listview which i gets it from database. If the data is empty in listview by default second tab needs to be visible.
when the activity starts if the list is empty i want to start the other tab. if list is not empty the present tab needs to be visible..
The below image gives some more detailed information about my question.
Here as per the above images when app is visible to the user for first time, the data will be empty. so, i want to display AddGroup Tab to add groups. How is it possible to focus on Add Group Tab if list is empty in Groups Tab...
Setting the currently viewed tab can be done with
myTabHost.setCurrentTab(tabPos);
How you get to this TabHost object depends on where you are initialising the list: if it is done in the parent activity, (which extends TabActivity), you can use
getTabHost().setCurrentTab(tabPos);
If you need access to the TabHost from inside another activity (ie. when your tabs are activities) or View implementation (inside the ListView), add a method to your parent Activity that can be called by the classes in it, or objects that have a reference to it. Put this
public void switchTab(int tabPos){
getTabHost().setCurrentTab(tabPos);
}
in the parent activity, and then use something of the form
((MyParentTabActivity) MyChildTabActivity.this.getParent()).switchTab(1);
to access it, depending on where you want to switch it from.
Related
I have a ListView that represents a list of folders and when I click one item, I want to load another list that shows the content of this folder. How can I link these views together and to be able to go back to the first one with the back button ?
Well, since you didn't provide a code in your question, I will try giving an answer in a descriptive manner.
You can use fragments to do this. Your base Activity's layout must have a fragment container which you will use to display your fragment containing the first ListView data. Once after you click on a cell, you call the constructor of the second ListView, and replace the current content of the fragment container with the newly created fragment.
You may implement a back feature by implementing an ArrayList in your Activity and appending the fragments into that array list as the user navigates through the list. onBack pressed you can call the top most fragment from that Array list and assign it to the fragment container.
This should work well, given there are not too many types of ListViews that you may want to implement.
How can creat a SwipeView that starts with the layout of an intermediary fragment from the collection instead of the first?
e.g.
I have a list of 10 emails of the same person and I click the 3th most recent to show the content in another Activity.
After that, I want to swipe to left and go to the 2nd most recent and swipe to right and go to 4th most recent.
I am following the tutorial
but it seems that the first layout displayed to the user always will correspond to first position from the collection of Fragments.
In my example the user will just be able to swipe to righ and see the emails from 3 to 10.
You can use ViewPager's setCurrentItem() method to specify the initially shown fragment by index, although you should only do this only on the first initialization (i.e. when savedInstanceState is null) so that the ViewPager can persist it's selection through Activity restarts.
I have an activity with a menu of items running down the left side that are textViews. when the user selects one of the textViews it will put a listView in the rest of the area of the activity taking up the other 2\3 of the screen. when the user touches a different textView on that left side menu of the page, it will open a different corresponding listView of items.
i was considering putting a large listview on the screen for this purpose. however one other way is instead of using a regular listView in the activity, i could put a list fragment in there and switch between fragments.
The third choice is to put a fragment in there and put a listView inside of that fragment. I have never used ListFragment before.
which would be the best plan considering that there will be no orientation change? the activity will be locked in the vertical / portrait orientation. this will be running on a tablet, not used on smaller devices like phones.
i don't know if i will need to use loaders, because the list will not be long and the contents of the list will be text only.
is there any advantage to using listFragment over the other choices?
The three choices for this activity:
activity with ListView on it
activity with ListFragment on it
activity with fragment on it that has ListView inside of theFragment`
A ListFragment is basically a slightly specialized Fragment which makes handling a ListView present in the layout of the Fragment easier by offering some convenience methods(like getListView() so you don't need to search for the widget yourself, a method to get the adapter of the ListView etc). If you need a Fragment with a ListView, use a ListFragment. So in the end it's about deciding between a ListView and a ListFragment.
Between the two options, taking in consideration your scenario, I would simply use a ListViewbecause it's plain simple. The need of a Loader isn't a problem as you could use the LoaderManager of the Activity for the ListView.
However, you didn't mention how the BACK button should be handled. If you want to offer the user the possibility of navigating back through his choices use a Listfragment to get that for free from the system.
I'm building this application where I have 2 activities. Both of them consist of 3 fragment - one for title, one for content and one for tab control. It is shown at image below.
First activity serves for showing list of some data's headers (item name etc.), search, app info etc. When user presses item in list, app takes him to another activity to show him detail of chosen item. This "details" activity has 6 different content fragments and user switch between them via buttons in tab control fragment (I did switching between content fragments by showing chosen one and hiding all others - I don't know if it's right way, it's my firs app so it came to my mind at first :) ).
And what I would like to do is: When I'm in detail and I swipe left/right then I want app to take me to previous/next item's detail, to same fragment where I currently was in (so not to next content fragment, but to detail of next item in 1st activity's list).
Is this somehow possible please? Because I have totally no clue how to do it :)
And what I would like to do is: When I'm in detail and I swipe
left/rigt then I want app to take me to previous/next item's detail,
to same fragment where I currently was in (so not to next content
fragment, but to detail of next item in 1st activity's list).
If you want to swipe left-right then you would need a ViewPager widget. I'm not sure how should your details activity behave so I'm providing you with two options. Do you want to be able to switch to the next/previous item's details only when a certain fragment is the one currently viewed by the user(from the 6 content fragments, which I assume are related and show various data for a single item)? If yes then in that desired fragment you would replace the current content of the fragment(which will only act as a container) with a ViewPager and use nested fragments for the actual content. If the user switches to the details of a previous/next item's details and then suddenly wants to see the data for that item from one of the remaining 5 content fragments then you would need to have some updates method on them to refresh the data to show the current item(a OnPageChangeListener will be useful here).
Second option, is if you want to allow the user to swipe left/right from any of the 6 content fragments. If this is the case you would use the same method as above but you'll modify each of those 6 fragments.
Showing the next/previous item is easy, just get some sort of identifier of the data(a position, id), retrieve the whole used data(as in the first activity) and then cycle between it.
I'm new to Java & Android development. I'm trying to develop an app that has 2 tabs, 1 tab has a listview inside of it. When you click on an item in the list, it takes you to another list.. and then you select another item, onto another list, ect.. until they reach the final page which I have setup as a non-selectable list. My question is.. should I create a new activity every time the user clicks on an item in the list? Or is that something normally done with changing views? If done with views, doesn't that pretty much disable their use of going back with the back button?
In the other tab I have an area with a list I guess in which you can remove items off of the list.. Now would I create a new activity for this and put this tab activity on every activity of the lists? I guess this part is what got me confused.. if I hadn't had the other tab my current setup of creating a new activity as the user drills down the lists worked just fine.
This all might sounds a little confusing but let me know if you guys need further explanation..
I would use changeable views only when the views are conceptually showing the same data from a different viewpoint. Since you say "it takes you to another list", I'd say use a separate activity.
As for the tab, my understanding is that you can model each tab as a separate activity, I'm not clear on why you would "put this tab activity on every activity of the lists"? Are you saying that one tab (the "remove" tab) is dependent on what's showing on the other tab ("the list tab")? Without knowing more about the context, but first instinct would be to use a separate "remove tab" activity and model the tab host as having separate activities per tab.