Call activity that extends AppCompatActivity inside tab: Android Studio - android

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.

Related

How to Show activity element or view in it's fragment and also access from fragment without move from fragment to activity

How to Show activity element or view in it's fragment and also access from fragment without move from fragment to activity and also tell vice-versa of this scenario.
Here is My Problem:-
I am using Search Edit Box in Activity and I want to use this Search Box into my fragment but i don't want make any search box in fragment, I just want to show this Activity Search Box in Fragment When Ever User type in search box then by using textWatcher track the text and show the result of search box in fragment not in Activity.
Below is the image of UI that i want make it.
You can access activity elements and methods from fragment this way:
Activity template
public class ActivityMain extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content)
}
public void updateView(){
// update your views & vars here
}
}
Fragment template
public class BlankFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}
public void updateActivityView(){
if (getActivity() != null){
((ActivityMain) getActivity()).updateView();
}
}
For vice versa you can access fragment items from activity this way:
ExampleFragment fragment = (ExampleFragment) getFragmentManager().findFragmentById(R.id.example_fragment);
fragment.specific_function_name();
Where R.id.example_fragment is most likely the FrameLayout id inside your xml layout

Different Activity each tab fragment

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

TabHost inside a fragment of an activity

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

Nevigation drawer on Dynamic layout

I have decided my layout on the basis of JSON response and make a layout at run time without any xml . I just take a Activity and call a api that give me json array on the basis of which i make my layout. I have no problem with that but now i want to attach a navigation drawer with this activity . I have used navigation drawer in my previous Activity by making them fragment but in this case i don't know how to convert my activity to fragment because in navigation drawer we need fragment not activity.
i just do not know how to convert this Dynamic ui to fragment
View rootView = inflater.inflate(R.layout.`dashboard`,container, false);
what should i write in place of dashboard because i have no xml for that particular activity ..
I did it this way:
public class BaseFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ScrollView scrollView = new ScrollView(getActivity());
View view = //view from JSON
scrollView.addView(view);
return scrollView;
}
}
in navigation drawer handler
getFragmentManager().beginTransaction().replace(R.id.content_frame, new BaseFragment()).commit();

Android - Calling Activity into Fragments - Navigation Drawer

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.

Categories

Resources