Textview in Fragment not working - java

I am currently working on adding tabs into my activity. For the same, I am using the Google SlidingTabLayout and SlidingTabStrip.
My fragment xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" > >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textViewTab" />
</RelativeLayout>
My contents xml looks like this:
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.musicisfun.allaboutfootball.MainActivity"
tools:showIn="#layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout
android:layout_width="match_parent"
android:id="#+id/tabs"
android:layout_height="wrap_content">
</com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
/>
</LinearLayout>
</RelativeLayout>
and my code looks like this:
public static class TabFragment extends Fragment{
public static TabFragment getInstance(int position){
TabFragment tabFragment = new TabFragment();
Bundle bundle = new Bundle();
bundle.putInt("site",position);
tabFragment.setArguments(bundle);
return tabFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_tab,container,false);
TextView textViewTab = (TextView) layout.findViewById(R.id.textViewTab);
Bundle bundle = getArguments();
if (bundle!=null){
textViewTab.setText("Current Tab is" + bundle.getInt("site"));
}
return layout;
}
}
My fragment adaptor looks like this:
class TabPagerAdaptor extends FragmentPagerAdapter{
String[] tab_names;
public TabPagerAdaptor(FragmentManager fm) {
super(fm);
tab_names=getResources().getStringArray(R.array.feed_names);
}
#Override
public Fragment getItem(int position) {
TabFragment tabFragment = TabFragment.getInstance(position);
return tabFragment;
}
#Override
public CharSequence getPageTitle(int position) {
return tab_names[position];
}
#Override
public int getCount() {
return 2;
}
}
and this is how i am invoking the tabs:
mViewPager= (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new TabPagerAdaptor(getSupportFragmentManager()));
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setViewPager(mViewPager);
But when I run the code, I can't find the textview being shown even though two tabs are working fine.

You cannot view your textView because the height of the ViewPager is 0dp in your xml -
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
/>
you have to change that to either wrap_content or match_parent-
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Also, Your LinearLayout's orientation is horizontal and the width of SlidingTabLayout is match_parent. SlidingTabLayout is occupying whole screen width leaving no room to display Viewpager.
Your LinearLayout's orientation should be vertical not horizontal
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout
android:layout_width="match_parent"
android:id="#+id/tabs"
android:layout_height="wrap_content">
</com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>

Related

How to set a layout on top of a ViewPager widget in Android Studio

I want to have a layout constantly on top of everything and visible all the time, however, once I swipe to the next element in the ViewPager, the FrameLayout is getting behind the ViewPager.
<RelativeLayout 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:gravity="center_vertical"
tools:context=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/videosViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
</androidx.viewpager2.widget.ViewPager2>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<FrameLayout
android:id="#+id/preview_display_layout"
android:layout_width="200dp"
android:layout_height="200dp"
android:visibility="visible"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="216dp"
tools:layout_editor_absoluteY="0dp">
</FrameLayout>
</RelativeLayout>
</RelativeLayout>
I tried to place a button instead of and on top of the FrameLayout and, guess what, the button remains on top of everything without going behind the ViewPager after each sroll.
<?xml version="1.0" encoding="utf-8"?>
<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=".MainActivity">
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/videosViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp">
</androidx.viewpager2.widget.ViewPager2>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_width="197dp"
android:layout_height="286dp">
</Button>
<FrameLayout
android:id="#+id/preview_display_layout"
android:layout_width="197dp"
android:layout_height="286dp"
android:visibility="visible"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteX="216dp"
tools:layout_editor_absoluteY="0dp">
</FrameLayout>
</RelativeLayout>
</FrameLayout>
Here is my code in MainActivity
private ViewPager2 videosViewPager;
private SurfaceView previewDisplayView;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(getContentViewLayoutResId());
previewDisplayView = new SurfaceView(this);
setupPreviewDisplayView();
videosViewPager = findViewById(R.id.videosViewPager);
List<VideoItem> videoItems = new ArrayList<>();
VideoItem life = new VideoItem();
String path = "android.resource://"+getPackageName()+"/"+ R.raw.video;
life.videoURL = Uri.parse(path);
videoItems.add(life);
VideoItem rest = new VideoItem();
String path2 = "android.resource://"+getPackageName()+"/"+ R.raw.video1;
rest.videoURL = Uri.parse(path);
videoItems.add(rest);
videosViewPager.setAdapter(new VideoAdapter(videoItems));
}
private void setupPreviewDisplayView() {
previewDisplayView.setVisibility(View.GONE);
ViewGroup viewGroup = findViewById(R.id.preview_display_layout);
viewGroup.addView(previewDisplayView);
previewDisplayView
.getHolder()
.addCallback(
new SurfaceHolder.Callback() {
#Override
public void surfaceCreated(SurfaceHolder holder) {
processor.getVideoSurfaceOutput().setSurface(holder.getSurface());
Log.d("Surface","Surface Created");
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
onPreviewDisplaySurfaceChanged(holder, format, width, height);
Log.d("Surface","Surface Changed");
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
processor.getVideoSurfaceOutput().setSurface(null);
Log.d("Surface","Surface destroy");
}
});
}
I guess my issue is in adding SurfaceView(previewDisplayView)in ViewGroup.
What can I do to make the previewDisplayView get always on top of everything? By the way, I tried bringToFront(), but after a swipe the previewDisplayLayout is automatically going behind the ViewPager.
Thank you in advance
Have you tried android:elevation property ? I think it is what you're looking for,
Hope will help

Static content with Viewpager in Android

I've tried multiple ways of adding a footer and a header in activity_main.xml which has a viewpager inside it. So far when I add a header above the viewpager, it's shown in final result but the footer isn't.
Here is the xml code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg50"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF0000"
android:orientation="vertical">
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/colorAccent"
app:tabTextColor="#FFF" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF0000"
android:orientation="vertical">
</LinearLayout>
And here is the activity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new LoginFragment(), "Login");
adapter.addFragment(new RegisterFragment(), "Register");
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFragment(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
}
sreenshot of the result
So the question is ,how to display content below the view pager.
Change your viewpager layout height to wrap_content.
Or just change your root layout to <RelativeLayout> and set add this tag to your view pager.
Like so:
<RelativeLayout>
...
<ViewPager
...
android:layout_height="match_parent"
android:layout_above="#id/your_footer_id"/>
...
</RelativeLayout>
If you want a static footer below ViewPager you can use layout below.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:orientation="vertical">
<LinearLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#FF0000"
android:orientation="vertical">
</LinearLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:background=""
android:minHeight="?attr/actionBarSize"
app:tabGravity="fill"
app:tabMode="fixed"
app:tabSelectedTextColor="#color/colorAccent"
app:tabTextColor="#FFF" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/footer"
android:layout_below="#+id/tabs" />
<LinearLayout
android:id="#+id/footer"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#FF0000"
android:orientation="vertical"></LinearLayout>
</RelativeLayout>
Set to your viewpager:
android:layout_height="0dp"
android:layout_weight="1"
This will make it fill all available space in LinearLayout after other children are laid out.

Change fragment but text reamin android

I have this issue: when I change fragment the TextView below remain and the result is 2 TextView.
I explain better:
I have one TextView with a string "hello word"
when I change fragment I want to change the entire page and set another TextView but the result is 2 TextVIew
thanks a lot!
activityMain.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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:gravity="center"
android:background="#5ba4e5"
android:layout_height="match_parent"
android:id="#+id/fragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40px"
android:textColor="#ffffff"
android:layout_gravity="center"
android:id="#+id/fragmentText"
/>
</LinearLayout>
this in MainActivity for change the fragment:
private void selectItem(int id) {
// update the main content by replacing fragments
Fragment fragment = new MyFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
and this is MyFragment.java:
public class MyFragment extends Fragment {
public MyFragment() {
// Empty constructor required for fragment subclasses
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container, false);
// String planet = getResources().getStringArray(R.array.planets_array)[i];
String planet = "prova fragment";
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((TextView) rootView.findViewById(R.id.fragmentText)).setText(planet);
return rootView;
}
}
i use:
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
but content_frame is a FrameLayout and the fragment is a LinearLayout so I change content_frame with the id of a LinearLayout in the content_main.xml and it works.

ViewPagerIndicator not showing up

I am using JakeWharton's ViewPagerIndicator on my landscape app but I think I am messing up with the layout. The ViewPagerIndicator does not show up. I just started learning Android Development so any help is appreciated!
My guess is that it has to do with my Linear Layout...should it be relative? Am I covering up the pager indicator? I know that if I move the indicator code above the view pager, I see the indicator but everything else is white. My fragment's layout is a relative layout with height and width matching the parent.
My app is an activity with fragments.
Edit: I set the indicator height to wrap content but now it floats on the top of the screen. How do I make it go down?
My_Activity.java
public class MyActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
MyPagerAdapter mAdapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(mAdapter);
CirclePageIndicator circleInd = (CirclePageIndicator) findViewById(R.id.circles);
circleInd.setViewPager(pager);
circleInd.setStrokeColor(0xAA000000);
//circleInd.setCurrentItem(mAdapter.getCount() - 1);
}
private class MyPagerAdapter extends FragmentPagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0: return FirstFragment.newInstance("First");
case 1: return SecondFragment.newInstance("Second");
default: return FirstFragment.newInstance("First");
}
}
#Override
public int getCount() {
return 2;
}
}
}
and the XML
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context="com.example.frontrowqa.myfirstapplication.MyActivity">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circles"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_gravity="bottom" />
</LinearLayout>
And my fragment
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<EditText
android:id="#+id/PasscodeText"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_width="fill_parent"
android:hint="Passcode"
android:backgroundTint="#android:color/black" />
<EditText
android:id="#+id/ipAddressText"
android:layout_below="#id/PasscodeText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="IP Address"
android:backgroundTint="#android:color/black" />
<EditText
android:id="#+id/intercomText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/ipAddressText"
android:hint="Intercom Event Number"
android:backgroundTint="#android:color/black"/>
<TextView
android:id="#+id/tvFragFirst"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="26dp"
android:text="TextView" />
</RelativeLayout>
Set your activity xml like this:
<?xml version="1.0" encoding="utf-8"?>
<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">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circles"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:padding="10dp" />
</LinearLayout>

Fragment layout not being inflated

I have an application where the device switches fragments depending on your orientation. Each one has a text view and an inflater in the respective java class. The application runs fine, but doesn't inflate the fragments. I am a beginner when it comes to fragments. Here is my code:
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Configuration configInfo = getResources().getConfiguration();
if(configInfo.orientation == Configuration.ORIENTATION_LANDSCAPE){
LandscapeFragment fragmentLandscape = new LandscapeFragment();
fragmentTransaction.replace(android.R.id.content, fragmentLandscape);
}else{
PortraitFragment fragmentPortrait = new PortraitFragment();
fragmentTransaction.replace(android.R.id.content, fragmentPortrait);
}
fragmentTransaction.commit();
}
}
activity_main.xml
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/landscape_fragment" />
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/portrait_fragment" />
</RelativeLayout>
LandscapeFragment.java
public class LandscapeFragment extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("Landscape Fragment Created");
View v = inflater.inflate(R.layout.landscape_fragment, container, false);
return v;
}
}
landscape_fragment.xml
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/landscape_text_view"
android:textSize="30sp" />
</RelativeLayout>
PortraitFragment.java
public class PortraitFragment extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
System.out.println("Portrait Fragment Created");
View v = inflater.inflate(R.layout.portrait_fragment, container, false);
return v;
}
}
portrait_fragment.xml
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<TextView android:text="#string/portrait_text_view" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textSize="30sp"/>
</RelativeLayout>
When you need change the layout at run time , you should not declare the fragment inside the activity's layout file. Replace your activity_main.xml with
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_content" />
</RelativeLayout>
and change your fragment creation code to
if(configInfo.orientation == Configuration.ORIENTATION_LANDSCAPE){
LandscapeFragment fragmentLandscape = new LandscapeFragment();
fragmentTransaction.replace(R.id.main_content, fragmentLandscape);
}else{
PortraitFragment fragmentPortrait = new PortraitFragment();
fragmentTransaction.replace(R.id.main_content, fragmentPortrait);
}
the doc here will help you understand more about fragments
Do not code anything in MainActivity,
As the link given by #jobcrazy do it directly in xml:
<fragment
android:name="yourpackage.PortraitFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/landscape_fragment" />
<fragment
android:name="yourpackage.LandscapeFragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/portrait_fragment" />
That should solve it!

Categories

Resources