I've been a while off from Android... so looking back at my codes wondering if I could do some optimization...
I have tabs using FragmentPagerAdapter as here https://developer.android.com/reference/android/support/v4/app/FragmentPagerAdapter
So within tabs I inflate the layout and do basic stuff like initialize and setup each UI component like Text and so on. Pretty much the way it should be.
Now I have a very generic tab page layout which I isolate it on library for sharing purpose among other apps. This is like a general welcome page. So let's call it WelcomeActivity which reside on lib module and has its layout inflated from itself.
So on App1, App2 and App3 where I have FragmentPagerAdapter can we call WelcomeActivity on lib module for each swipe to "Welcome Tab"?
So I will skip this part
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_pager_list, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("Fragment #" + mNum);
return v;
}
and instead I will call WelcomeActivity on here
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mNum = getArguments() != null ? getArguments().getInt("num") : 1;
Intent myIntent = new Intent(getActivity(), WelcomeActivity.class);
getActivity().startActivity(myIntent);
}
Is this possible? I tried but then WelcomeActivity will appear and never appear again when I swipe back to "Welcome Tab". Did I miss something?
Related
I have 3 tabs, and I'm trying to show a different activity and layout each tab.
I am using the tabs as my app navigation, so when a has changed, the whole layout and activity need to be changed.
Actually only the layout is changing.
Since the MainActivity.java + layout has the PageAdapter in it (the PageAdapter is loading the tab's content),
I have moved my first page activity from the MainActivity.java to a new java activity called showUpdates.java. Now I'm trying to access this activity as my Tab #1 content.
If you still didn't understand what I'm asking for, The following will make it clear:
My 3 tabs are:
Java: TabFragment1.java, XML: activity_show_updates.xml
Java: TabFragment2.java, XML: tab_fragment_2.xml
Java: TabFragment3.java, XML: tab_fragment_3.xml
The TabFragment1.java:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
/**
* Was trying:
* Intent intent = new Intent(...)
* startActivity(intent);
*/
return inflater.inflate(R.layout.activity_show_updates, container, false);
}
As you can see, it's moving to a new layout, activity_show_updates.xml.
What I'm trying to do is - using my showUpdates.java as the Activity of the tab_fragment_1.
I've been trying to use Intent but my app crashed when I created it at TabFragement1.java.
Anyone knows how I can make showUpdates.java as the activity of this layout?
You cannot nest activities. There are fragments for that. Just create a different fragment for each tab and use an Activity to hold all of them. If you need it, you can also create child fragments (although the support is not so good and you might have some problems, but nothing really hard to handle).
If you have viewpager working, you can do the following:
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public int getCount() {
return NUM_ITEMS;
}
#Override
public Fragment getItem(int position) {
if(position == 0){
//return first fragment
}
else if(position == 1){
//return second fragment
}
...
}
and you can do your onCreate logic in onCreateView:
public override View OnCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View yourFragment = inflater.inflate (Resource.Layout.fragment_name, container, false);
... //do stuff
return yourFragment;
}
All you need to do is define which fragments to output in your adapter
I want to create a layout in which upper half is just some area with normal views and lower half is a tab layout .
I've seen some examples but they're all how to create tabs at activity level i.e by extending TabHostActivity which covers all the activity area.
So i decided to create 2 fragments in the activity ,in which lower fragment will have the tablayout.
But the problem is i cant make this fragment class extend Fragment as well as TabHostActivity...
So any help how could i implement this ?
Here's the lower fragment's code -
public class PFrag extends Fragment {
View mRoot;
TabHost tabHost;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
mRoot = inflater.inflate(R.layout.payfrag, container, false);
try {
Resources resources = getResources();
tabHost = (TabHost)mRoot.findViewById(R.id.tabHost);
Intent netbintent = new Intent(getActivity().getApplicationContext(), NB.class);
TabHost.TabSpec tabSpecNB = tabHost.newTabSpec("NB");
tabSpecNB.setIndicator("", resources.getDrawable(R.drawable.netb));
tabSpecNB.setContent(netbintent);
Intent ccardintent = new Intent(getActivity().getApplicationContext(), Cc.class);
TabHost.TabSpec tabSpecCc = tabHost.newTabSpec("CC");
tabSpecCc.setIndicator("", resources.getDrawable(R.drawable.cc));
tabSpecCc.setContent(ccardintent);
tabHost.addTab(tabSpecNB);
tabHost.addTab(tabSpecCc);
} catch(Exception e) {
AlertDialog.Builder ad = new AlertDialog.Builder(getActivity().getApplicationContext());
ad.setMessage(e.toString());
ad.show();
}
return mRoot;
}
}
Sounds like you need to use FragmentTabHost inside your "bottom" fragment.
Another link that could help you out here.
Also, it seems on only work in API 17 and above, so be sure to keep that in mind!
I will not recommend FragmentTabHost its somehow difficult to use and don't have swipe Left-Right. TabHostActivity is also deprecated, use SlidingTabLayout its open source and easily to use and update and have swipe.
SlidingTabLayout
I've looked through the answers on stackoverflow for this newbie android question, but I cannot understand what the optimal solution is.
I am trying to create a simple activity (with a fragment) that has a couple of text fields and a button. When the button is pressed, I would like the button to do something!
So I have my mainActivity class, along with its code fragment. I am trying to correctly set the "onClickListener" to my button object.
When I do this in the "onCreate" method in the activity itself, it throws a nullpointerexception - I'm guessing this is because the button itself is not yet created. However putting this code in the OnStart method would cause it to be called over and over again and seems out of place.
So I moved it to the "Placeholder fragment" - where it is no longer being reported as null, but I cannot use an intent to navigate to another page (because the other pages are not static AND I cannot reference the current activity using "this").
I'm pretty stuck and it seems like something simple.
Scenario #1: trying to initiate it in the OnCreate method (:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_made_my_day);
////////////////////// PROBLEM IS HERE //////////////////////////////////
Button butLogin = (Button) findViewById(R.id.but_login);
butLogin.setOnClickListener(butLogin_OnClickListener);
///////////
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
}
}
Version 2 -- Fragment code, also doesn't work:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_made_my_day, container, false);
Button butLogin = (Button) rootView.findViewById(R.id.but_login);
butLogin.setOnClickListener(butLogin_OnClickListener);
return rootView;
}
First, you can reference the Activity from a Fragment (after it's been attached) by calling getActivity(). That should be enough to launch the intent. For example:
Intent intent = new Intent(getActivity(), ActivityToCall.class);
getActivity().startActivity(intent);
Second, is the but_login Button actually in activity_made_my_day.xml or in fragment_made_my_day.xml? Depending on which, that's the place where you should use findViewById() (activity or fragment).
I've downloaded the sample project from the Android official site http://developer.android.com/training/implementing-navigation/nav-drawer.html. I am trying to understand how the Navigation Drawer works. So, I have one doubt, they call a Fragment for each item from the left menu. In my project, I have a big activity which I am trying to call by this Fragment:
public class HomeFragment extends Fragment {
public HomeFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Intent intent = new Intent();
intent.setClass(getActivity(), ListMatch.class);
startActivity(intent);
View rootView = inflater.inflate(R.layout.list_match, container, false);
return rootView;
}
}
But, if I do that, it calls perfectly my Activity, but the menu disappear. How can I call this activity and keep my menu? Thanks a lot.
Like Raghunandan said, the drawer applies wihin a single activity. It's common to launch a new activity from the "main" drawer, but usually that kind of activity would have the up action set in the action bar to go back to the main activity.
Just created a new project in the newest ADT release in Eclipse and found that it will setup up certain environments for you to get things started. I choose Tabs + Swipe.
It has this code I have question on:
public static class DummyFragment extends Fragment {
public DummyFragment () {
}
public static final String ARG_SECTION_NUMBER = "section_number";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setGravity(Gravity.CENTER);
Bundle args = getArguments();
textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
return textView;
}
}
Both tabs refer to this same fragment. All it does is switch the content on the TextView that has the tab position number on it (1,2, or 3).
The more advanced question first: I want two different Fragments that the tab switches to. In the example code, it points to the same fragment. Where does this change take place? and can I see brief code example?
Easier question: I have two pre-defined XML layouts I'd like to set each tab (or Fragment) with. Do I do this in the actual Fragment? And if so, where? setContentView does't seem to be working in the onCreateView method?
Not really sure what this question is asking exactly, but if I understand correctly the TabHost (or whatever you are using to manage the Fragment tabs) is instantiating multiple instances of the DummyFragment and then attaching each to the screen when a tab is clicked. This is all done behind the scenes... all you need to worry about is implementing the Fragment and telling the TabHost when it should be instantiated/displayed.
Fragments don't have a setContentView method. You should inflate your Fragment's layout from xml in onCreateView. For instance with,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_layout, container, false);
}