hi there I have 2 XML layout and 2 java class. I want merg them.
in main.activity.java I call them by this method:
switch (position % 4) {
case 0:
return WebViewGoogle.newInstance();
case 1:
return ScrollFragment.newInstance();
}
ScrollFragment.java:
public class ScrollFragment extends Fragment {
private ObservableScrollView mScrollView;
public static ScrollFragment newInstance() {
return new ScrollFragment();
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_scroll, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mScrollView = (ObservableScrollView) view.findViewById(R.id.scrollView);
MaterialViewPagerHelper.registerScrollView(getActivity(), mScrollView, null);
}
}
Also this is WebViewGoogle.java
public class WebViewGoogle extends Fragment {
private ObservableWebView mWebView;
public static WebViewGoogle newInstance() {
return new WebViewGoogle();
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.webview_layout, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
mWebView = (ObservableWebView) view.findViewById(R.id.webViewPars);
MaterialViewPagerHelper.preLoadInjectHeader(mWebView);
mWebView.setWebViewClient(new WebViewClient() {
#Override
public void onPageFinished(WebView view, String url) {
MaterialViewPagerHelper.injectHeader(mWebView, true);
}
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
});
mWebView.loadUrl("http://www.google.com");
MaterialViewPagerHelper.registerWebView(getActivity(), mWebView, null);
}
}
Now I have 2 xml file with this content:
fragment_scroll.xml:
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/material_view_pager_placeholder"/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/cardMarginVertical"
android:layout_marginLeft="#dimen/cardMarginHorizontal"
android:layout_marginRight="#dimen/cardMarginHorizontal"
android:layout_marginTop="#dimen/cardMarginVertical"
android:background="#android:color/white"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1000dp"
android:orientation="vertical">
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
and another xml is webview_layout.xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
>
<com.github.ksoichiro.android.observablescrollview.ObservableWebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webViewPars"
android:background="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
These 2 layout and their java class work correctly now.
But I want to merge them. why? because I want put webview in a CardView
So I merhe 2 xml layouts in these way: (newlayout.xml)
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/material_view_pager_placeholder"/>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="#dimen/cardMarginVertical"
android:layout_marginLeft="#dimen/cardMarginHorizontal"
android:layout_marginRight="#dimen/cardMarginHorizontal"
android:layout_marginTop="#dimen/cardMarginVertical"
android:background="#android:color/white"
app:cardElevation="4dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="1000dp"
android:orientation="vertical">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:paddingRight="5dp"
>
<com.github.ksoichiro.android.observablescrollview.ObservableWebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webViewPars"
android:background="#android:color/transparent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
I think it is true but how can I do it for their java class?
step 1:
I create a new java claaa:
newclass.java
step 2
in main_activity I call it:
case 3: return newclass.newInstance();
Now I have 2 ID in newlayout: scrollView and webViewPars...now how can I merge 2 java class ...?
Related
Like a lot of other people on the stackoverflow, for example:
https://ru.stackoverflow.com/questions/571704/%D0%A0%D0%B0%D0%B7%D0%BD%D1%8B%D0%B9-%D0%BA%D0%BE%D0%BD%D1%82%D0%B5%D0%BD%D1%82-%D0%BD%D0%B0-%D1%81%D1%82%D1%80%D0%B0%D0%BD%D0%B8%D1%86%D0%B0%D1%85-viewpager
I'm trying to create ViewPager with different layouts, but with:
https://github.com/ogaclejapan/SmartTabLayout
Expectation: beautiful ViewPager with 3 tabs, which can switch different layouts/fragments by clicking on the buttons or by swipe.
Reality: beautiful ViewPager with 3 tabs, which are doing nothing.
Looking for your help, thanks.
What I have done in MainActivity class:
public class MainActivity extends AppCompatActivity
{
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setElevation(0); //toolbar shadow
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
ViewGroup tab = findViewById(R.id.linear_test);
tab.addView(LayoutInflater.from(this).inflate(R.layout.activity_main, tab, false));
ViewPager viewPager = findViewById(R.id.viewpager);
SmartTabLayout viewPagerTab = findViewById(R.id.viewpagertab);
setup(viewPagerTab);
FragmentPagerItems pages = new FragmentPagerItems(this);
pages.add(FragmentPagerItem.of("Вкладка 1", DemoFragment.class));
pages.add(FragmentPagerItem.of("Вкладка 2", DemoFragment.class));
pages.add(FragmentPagerItem.of("Вкладка 3", DemoFragment.class));
FragmentPagerItemAdapter adapter = new FragmentPagerItemAdapter(
getSupportFragmentManager(), pages);
viewPager.setAdapter(adapter);
viewPagerTab.setViewPager(viewPager);
}
public void setup(final SmartTabLayout layout) {
//Do nothing.
}
}
What I have done in my DemoFragment:
public class DemoFragment extends Fragment {
static final String ARGUMENT_PAGE_NUMBER = "arg_page_number";
private int page;
static DemoFragment newInstance(int page) {
DemoFragment pageFragment = new DemoFragment();
Bundle arguments = new Bundle();
arguments.putInt(ARGUMENT_PAGE_NUMBER, page);
pageFragment.setArguments(arguments);
return pageFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
// View view = inflater.inflate(R.layout.activity_main, container, false);
page = getArguments().getInt("someInt", 0);
return inflater.inflate(R.layout.activity_main, container, false);
// return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
int position = FragmentPagerItem.getPosition(getArguments());
getItem(position);
}
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show FirstFragment
return DemoFragment.newInstance(1);
case 1: // Fragment # 0 - This will show FirstFragment different title
return DemoFragment.newInstance(2);
case 2: // Fragment # 1 - This will show SecondFragment
return DemoFragment.newInstance(3);
default:
return null;
}
}
}
Here is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<LinearLayout
android:id="#+id/linear_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints"
android:background="#color/colorPrimary"
android:orientation="horizontal" >
<com.ogaclejapan.smarttablayout.SmartTabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/viewpagertab"
android:layout_width="match_parent"
android:layout_height="#dimen/tab_height_large"
app:stl_customTabTextLayoutId="#layout/test_layout"
app:stl_customTabTextViewId="#id/custom_tab_text"
app:stl_distributeEvenly="true"
app:stl_defaultTabTextAllCaps="false"
app:stl_indicatorInterpolation="linear"
app:stl_defaultTabTextColor="#color/white"
app:stl_indicatorColor="#color/colorBlue"
app:stl_underlineColor="#color/colorBlue"
app:stl_defaultTabBackground="#color/colorPrimary"
app:stl_indicatorThickness="3dp"
app:stl_underlineThickness="1dp"
/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/viewpagertab"
/>
</LinearLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/playlist_recycler"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linear_test" />
<TextView
android:id="#+id/dl_params"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:visibility="gone"
android:layout_weight="1"
tools:ignore="MissingConstraints"/>
</android.support.constraint.ConstraintLayout>
And finally test_layout which one is filling those SmartTabLayout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/tab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?attr/selectableItemBackground"
>
<android.support.v4.widget.Space
android:id="#+id/center_anchor"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerInParent="true"
/>
<aectann.pr1.TintableImageView
android:id="#+id/custom_tab_icon"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_above="#id/center_anchor"
android:layout_centerHorizontal="true"
android:scaleType="center"
android:src="#drawable/custom_icon"
/>
<TextView
android:id="#+id/custom_tab_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/center_anchor"
android:layout_centerHorizontal="true"
android:layout_marginTop="2dp"
android:maxLines="1"
android:textSize="14sp"
android:textColor="#color/white"
/>
</RelativeLayout>
Any ideas, guys?
I have a parent layout that is used to inflate various fragments. On one such fragment layout I need to access the view elements inside the fragment class.
I'm using below code to get a reference for a text view inside the fragments layout.
CreateTaskFragment.java
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
View view = getView();
endDateView = (TextView)view.findViewById(R.id.estimated_end_input);
endDateView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Date","I'm clicked");
}
});
}
The endDateView is returning null.
fragment_create_task.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.tetrissoft.dailyplanner.CreateTaskFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/estimated_end_date"
android:textSize="15sp"
android:layout_marginTop="10dp"
android:layout_marginLeft="15dp"
android:id="#+id/estimated_end_label"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/task_end_date_input"
android:textSize="18sp"
android:inputType="date"
android:layout_marginTop="20dp"
android:layout_marginLeft="15dp"
android:layout_below="#+id/estimated_end_input"/>
</LinearLayout>
MainActivity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/drawer_layout">
<!-- The Main Content Layout -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="25dp"
app:elevation="4dp"
android:clickable="true"
android:src="#drawable/ic_create_black_24dp"
android:id="#+id/fab_button"/>
</FrameLayout>
<!-- The Navigation Drawer-->
<ListView
...
</ListView>
</android.support.v4.widget.DrawerLayout>
How can I get the reference to the view elements of CreateTaskFragment?
Copy below method in your fragment and remove your onActivityCreated method from fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_create_task, container, false);
TextView endDateView = (TextView)rootView .findViewById(R.id.estimated_end_input);
endDateView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i("Date","I'm clicked");
}
});
return rootView;
}
This is the activity
public class Homepage extends FragmentActivity implements ChatFragment.OnFragmentInteractionListener{
SearchFragment searchFragment;
ChatFragment chatFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_homepage);
searchFragment = new SearchFragment();
chatFragment = new ChatFragment();
if(savedInstanceState == null){
getFragmentManager().beginTransaction().replace(R.id.homepageFragment,chatFragment).commit();
}
}
#Override
protected void onResume(){
Bundle bundle = getIntent().getExtras();
if(bundle != null){
//text.setText("user_id: " + bundle.get("user_id") + " ,username: " + bundle.get("username") + " ,password: " + bundle.get("password"));
}
super.onResume();
}
#Override
public void onChatFragmentInteraction(Uri uri){
}
public void openSearchFragment(View view){
if(!searchFragment.isAdded())
getFragmentManager().beginTransaction().replace(R.id.homepageFragment,searchFragment).commit();
}
public void openChatFragment(View view){
if(!chatFragment.isAdded())
getFragmentManager().beginTransaction().replace(R.id.homepageFragment,chatFragment).commit();
}
public void openProfile(View view){
}
}
This is the layout of the activity
I like making my activities screen responsive.
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="the_activity_location"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="100"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:layout_weight="90"
android:orientation="vertical">
<fragment
android:id="#+id/homepageFragment"
android:name="com.example.summer.toothbrush.SearchFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:layout="#layout/fragment_search"/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:backgroundTint="#color/whiteClr"
android:background="#color/whiteClr"
android:layout_weight="10"
android:orientation="horizontal"
android:weightSum="100">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/search_img"
android:background="#null"
android:layout_weight="25"
android:layout_marginLeft="0dp"
android:layout_gravity="center"
android:onClick="openSearchFragment"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/chat_img"
android:background="#null"
android:layout_weight="50"
android:layout_gravity="center"
android:onClick="openChatFragment"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/profile_img"
android:background="#null"
android:layout_weight="25"
android:layout_gravity="center"
android:onClick="openProfile"/>
</LinearLayout>
</LinearLayout>
This is the fragment
public class SearchFragment extends ListFragment implements AdapterView.OnItemClickListener{
ArrayList<String> list;
ListViewAdapter myAdapter;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstance){
View view = inflater.inflate(R.layout.fragment_search,container,false);
list = new ArrayList<String>();
list.add("I");
list.add("am");
list.add("Iron");
myAdapter = new ListViewAdapter(list,getActivity().getBaseContext());
setListAdapter(myAdapter);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
getListView().setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
}
}
The Layout of the fragment is inside a linear layout consisting of a
ListView with "#android:id/list" as the ID and a TextView.
Don't worry about the adapter. it's working fine.. I tested it out on an activity. Works perfectly fine.
This is what it looks like now
It is correct. You have two SearchFragment. One declared static in your layout, the other added through a FragmentTransaction. Fragments declared static in the layout can't be replaced using a programmatically transaction. Just replace
<fragment
android:id="#+id/homepageFragment"
android:name="com.example.summer.toothbrush.SearchFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:layout="#layout/fragment_search"/>
with
<FrameLayout
android:id="#+id/homepageFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
and it is going to work as you expect
I have an application with a side menu created from fragments.
The problem comes when I want to create tabs in one of the fragments.
I have tried many examples and not work for me.
fragment_home.xml
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal"
></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
></FrameLayout>
</LinearLayout>
FragmentHome.java
public class FragmentInicio extends Fragment {
private FragmentTabHost mTabHost;
public FragmentInicio() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.fragment_home, container, false);
try{
//HERE TABHOST????
}catch(Exception e){
Log.e("UDI", e.getMessage());
}
return view;
}
#Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Log.d("EVENT", "OnCreate");
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onViewCreated(view, savedInstanceState);
}
#Override
public void onAttach(Activity activity) {
// TODO Auto-generated method stub
super.onAttach(activity);
((HomeActivity) activity).onSectionAttached(1);
}
}
What can I do?
Example:
http://webdemo.com.es/sample2.jpg
Thank you!
I know I'm late but this might help someone else.
To use FragmentTabHost inside fragment you can do this.
Create layout resource file
fragment_tab_host.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
FrameLayout with id realtabcontent is which will show current tab content which is in this case FirstFragment, SecondFragment and ThirdFragment
TabHostWidgetFragment.java code will be
public class TabHostWidgetFragment extends Fragment {
private static final String TAG = TabHostWidgetFragment.class.getSimpleName();
public TabHostWidgetFragment() { }
private FragmentTabHost fragmentTabHost;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
Log.i(TAG, "onCreateView: ");
return inflater.inflate(R.layout.fragment_frag_tab_host_widget, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
fragmentTabHost = view.findViewById(android.R.id.tabhost);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
Log.i(TAG, "onActivityCreated: ");
fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("First Tab").setIndicator("First Tab"), FirstFragment.class, null);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Second Tab").setIndicator("Second Tab"), SecondFragment.class, null);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Third Tab").setIndicator("Third Tab"), ThirdFragment.class, null);
}
public class FirstFragment extends Fragment {
private static final String TAG = FirstFragment.class.getSimpleName();
public FirstFragment() { }
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
Log.i(TAG, "onCreateView: ");
return inflater.inflate(R.layout.fragment_first, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
}
}
public class SecondFragment extends Fragment {
private static final String TAG = SecondFragment.class.getSimpleName();
public SecondFragment() { }
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
Log.i(TAG, "onCreateView: ");
return inflater.inflate(R.layout.fragment_second, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
}
}
public class ThirdFragment extends Fragment {
private static final String TAG = ThirdFragment.class.getSimpleName();
public ThirdFragment() { }
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
Log.i(TAG, "onCreateView: ");
return inflater.inflate(R.layout.fragment_third, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Log.i(TAG, "onViewCreated: ");
}
}
}
Layout resources for FristFragment, SecondFragment and ThirdFragment
fragment_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/firstTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/nature1"/>
</LinearLayout>
fragment_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/secondTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/nature2"/>
</LinearLayout>
fragment_third.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/thirdTab"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="centerInside"
android:src="#drawable/nature3"/>
</LinearLayout>
If you want to make it swipeable then add ViewPager after FrameLayout with id realtabcontent as written below and implement viewPager logic.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
And TabHostWidgetFragment.java class will be
Create separate or inner MyTabFactory.java class
public class MyTabFactory implements TabHost.TabContentFactory {
private final Context context;
public MyTabFactory(Context context) {
this.context = context;
}
#Override
public View createTabContent(String s) {
View v = new View(context);
v.setMinimumWidth(0);
v.setMinimumHeight(0);
return v;
}
}
And then add tabs to tabhost like this. This is essential to setContent with MyTabFactory. If you add tabs like I mentioned above in code snippet, there will be issue which is, It will add fragments two times, one because of TabHost and one because of ViewPager.
fragmentTabHost.setup(getActivity(), getChildFragmentManager(), R.id.realtabcontent);
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("First Tab").setIndicator("First Tab").setContent(new MyTabFactory(getActivity())));
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Second Tab").setIndicator("Second Tab").setContent(new MyTabFactory(getActivity())));
fragmentTabHost.addTab(fragmentTabHost.newTabSpec("Third Tab").setIndicator("Third Tab").setContent(new MyTabFactory(getActivity())));
You can either use android.support.v4.app.FragmentTabHost or TabHost It will work same for both.
I have created a ViewPager with three "pages". The code is this
MainActivity.java
public class MainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
PagerTabStrip pagerTabStrip = (PagerTabStrip) findViewById(R.id.pager_tab_strip);
FragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(
getSupportFragmentManager());
viewPager.setAdapter(fragmentPagerAdapter);
pagerTabStrip.setDrawFullUnderline(true);
pagerTabStrip.setTabIndicatorColor(Color.DKGRAY);
}
}
MyFragmentPageAdapter.java
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
private String[] pageTitle = {
"Page1", "Page2", "Page3"
};
public MyFragmentPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
Fragment fragment = new PageFragment();
Bundle arguments = new Bundle();
arguments.putString("pageIndex", Integer.toString(position + 1));
fragment.setArguments(arguments);
return fragment;
}
#Override
public int getCount() {
return pageTitle.length;
}
#Override
public CharSequence getPageTitle(int position) {
return pageTitle[position];
}
}
PageFragment.java
public class PageFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page, null);
return view;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<android.support.v4.view.PagerTabStrip
android:id="#+id/pager_tab_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33B5E5"
android:textColor="#FFFFFF"
android:paddingTop="10dp"
android:paddingBottom="10dp" />
</android.support.v4.view.ViewPager>
</RelativeLayout>
fragment_page.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:textSize="16sp"
android:textStyle="italic"
android:gravity="center_horizontal"
android:textColor="#color/red"
android:text="#string/inf" />
<TextView
android:id="#+id/textView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="60dp"
android:textSize="28sp"
android:gravity="center_horizontal"
android:textStyle="bold"
android:text="#string/ben" />
<TextView
android:id="#+id/textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="130dp"
android:gravity="center_horizontal"
android:textSize="18sp"
android:text="Prova"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_centerInParent="true"
android:text="#string/verifica" />
</RelativeLayout>
But now i visualize in all three pages the same thing. If I for example on page 2 I want a TextView with the text "This is the 2 page" and on the third page a TextView with the text "This is the page 3" and in the first page two TextView with the button ... how can I? I'm going crazy, please let pass me the code to do this thing. Please.
Once you inflate PageFragment's layout you need to get a reference of the TextView so you can display the position on it via the Bundle you are passing using setArguments(). Use your view variable inside onCreateView() to get a reference of the TextView. (i.e. view.findViewById()). Then use getArguments() in your PageFragment to retrieve the Bundle with that has position, and set the TextView to that value.
this is a good example for what you want.
Just create a function in your page fragment class to configure elements and modify onCreateView to attach childs.
public class PageFragment extends Fragment {
TextView tv1 ;
// ...
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = getActivity().getLayoutInflater().inflate(R.layout.fragment_page, null);
/// Attach your childs
tv1 = view.findViewById(R.id.textView1);
return view;
}
public void configure(int position) {
tv1 .setText("This is "+ position);
}
}
Then just call the function configure in getItem function
#Override
public Fragment getItem(int position) {
PageFragment fr = new PageFragment();
fr.configure(position + 1);
return fr;
}