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
Related
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?
I have my Activity that displays 3 tabs. I want to call an activity (extends AppCompatActivity) that contains textviews, buttons, editboxes for displaying data from SQLite. When I click the second tab, the activity is displayed but it occupies the whole screen and is not being displayed inside the second tab.
I am using a pager class for my tabs.
This is the fragment tab that should display my activity inside it:
public class settings_tab extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Returning the layout file after inflating
//Change R.layout.tab1 in you classes
Intent intent = new Intent(getActivity(), MainActivity.class);
startActivity(intent);
return inflater.inflate(R.layout.activity_settings_tab, container, false);
}
}
When I call the activity, it is displayed but it occupies the whole screen and is not being displayed inside the second tab
Activities-in-tabs has been deprecated as a technique for six years.
Please use something else for your tabs, such as fragments.
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.
I am trying to make an activity have swipable tabs, with each tab having a different fragment (I dont know yet how many fragments I am going to have, so assume they will be at least 5).
So I am having problems make the parent activity with the tabs (if I said that correctly) and then the fragments themselves have some dynamically added views (text views and one image view) which get their stuff from different async tasks, executed when the fragment is showed.
Don't know if I explained it correctly, but here is my code and I'll ask you please to add in it the needed stuff.
So here is the parrent activity which must host the tabs and the fragments:
public class SecondActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
}
}
and here is one of the fragments (the others are similar):
public class Fragment1 extends Fragment {
LinearLayout layout;
ImageView iv;
String anotherURL;
ArrayList<InfoStuff> ci;
public Fragment1() {
// Empty constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
layout = (LinearLayout) rootView.findViewById(R.id.layout);
iv = (ImageView) rootView.findViewById(R.id.ivPortrait);
Bundle b = this.getArguments();
ci = b.getParcelableArrayList("infoStuff");
regionUrl = b.getString("someURL");
createViews();
return rootView;
}
public void createViews() {
TextView tv;
tv = new TextView(v.getContext());
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("le text");
layout.addView(tv);
tv = new TextView(v.getContext());
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setText("some text");
layout.addView(tv);
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(v.getApplicationContext()).build();
ImageLoader.getInstance().init(config);
String imgUrl = "someURL";
ImageLoader.getInstance().displayImage(imgUrl, iv);
}
}
I am also having troubles making the ImageView work, as it is from an additional library (forgot the name, heres the import though import com.nostra13.universalimageloader.core.ImageLoader;)
EDIT: Sorry, forgot to mention the tabs must be swipeable
The article Creating Navigation tabs using TabHost and Fragments in Android may help you.
In this article, we will develop an Android application containing navigation tabs. There are many ways by which we can add navigation tabs to an application.
Beginning with Android 3.0, the standard way to create navigation tab is using action bar. [...]
For Android 2.x and above, we can use Action bar Sherlock library to create navigation library. [...]
In this application, we are creating navigation tabs using TabHost and Fragments. [...]
This application is developed in Eclipse 3.7.2 with ADT plugin (20.0.2) and Android SDK ( R20.0.1 ) .
I just started doing something very similar to what you want to do. I followed this tutorial on Android Hive, which was very helpful in getting the tabs, swiping, and fragments down. (The fragments in this example are just textviews with different colors, but you can easily figure out where to add in your own views).
Hope this helps - it did for me!
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);
}