I think that it's a bit difficult to explain the problem but I'll try. I have a class which extends Fragment and I call it with the follow code
frag = new SearchDoctor();
fragTransaction = getFragmentManager().beginTransaction().replace(R.id.container, frag);
fragTransaction.commit();
Here is a part of class code
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
// TODO Auto-generated method stub
rootView = inflater.inflate(R.layout.search_doctor, container, false);
spinnerCity = (Spinner) rootView.findViewById(R.id.cityList);
The first time that class is called it's alright.
But the next time when I call it thead exit at the line "rootView = inflater.inflate(R.layout.search_doctor, container, false);".
I have understand that the problem is with the fragment that contained in search_doctor.xml (layout) because I had tried it with other layouts many times and it was any problem.
Here is the layout search_doctor.xml
<?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" >
<Spinner
android:id="#+id/cityList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/city_arrays"
android:prompt="#string/city_prompt" />
<Button
android:id="#+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/specialtyList"
android:layout_marginRight="16dp"
android:text="Αναζήτηση" />
<Spinner
android:id="#+id/specialtyList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/cityList"
android:entries="#array/specialty_arrays"
android:prompt="#string/specialty_prompt" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnSearch"
android:layout_marginTop="22dp"
class="com.google.android.gms.maps.SupportMapFragment" />
<ListView
android:id="#+id/resultList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/btnSearch"
android:layout_marginTop="22dp" >
</ListView>
<Button
android:id="#+id/btnMap"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/resultList"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:text="Χάρτης" />
</RelativeLayout>
Please if someone could help me I'll be very pleasure.
Thank's a lot in advance!
This is the solution! I find on the forum so thank you all.
public void onDestroyView()
{
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getSupportFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
Can you try to replace
rootView = inflater.inflate(R.layout.search_doctor, container, false);
with
rootView = inflater.inflate(R.layout.search_doctor, null, false);
Not sure if it helps but if the problem with view hierarchy that might be useful.
Related
I want load some Custom Views within my RelativeLayout but don't know how. The code I've tried doesn't work hence does anyone know how to do this correctly?
XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
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:id="#+id/textView0"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="20dp"
style="#android:style/TextAppearance.Medium" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginBottom="2dp"
style="#android:style/TextAppearance.Medium"
android:layout_below="#id/textView0" />
<View
android:id="#+id/drawing0"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_below="#id/textView1" />
<View
android:id="#+id/drawing1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="2dp"
android:layout_below="#id/drawing0" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Large"
android:layout_below="#id/drawing1" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Large"
android:layout_marginStart="10dp"
android:layout_below="#id/drawing1"
android:layout_toEndOf="#id/textView2" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Medium"
android:layout_below="#id/textView3" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#android:style/TextAppearance.Medium"
android:layout_marginBottom="30dp"
android:layout_below="#id/textView4" />
</RelativeLayout>
</ScrollView>
Shapes.java
public class Shapes extends android.support.v4.app.Fragment {
public Shapes() {
}
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.shapes, container, false);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
View v = getView();
assert v != null;
//blue shape drawing
View cv0 = (View)v.findViewById(R.id.drawing0);
v.addView(new BlueShape(getActivity()));
//green shape drawing
View cv1 = (View)v.findViewById(R.id.drawing1);
v.addView(new GreenShape(getActivity()));
super.onActivityCreated(savedInstanceState);
}
}
BlueShape.java
public class BlueShape extends View {
private final Paint mBlue = new Paint();
...
}
GreenShape.java
public class GreenShape extends View {
private final Paint mGreen = new Paint();
...
}
refer to https://developer.android.com/guide/topics/ui/custom-components.html
specifically under
Modifying an Existing View Type
Use the Custom Component
you can change the xml in your RelativeLayout
<View...
to
<your.package.GreenShape...
if you do not know beforehand what the View is going to be, then change it to
<FrameLayout... instead of <View...
and then add the child (GreenShape or BlueShape) programmatically to that Frame layout
cv0, cv1 are not ViewGroup.
Try to change cv0, cv1 to ViewGroup and move onActivityCreated's code to onViewCreated callback method.
here is reference site.
android/view/ViewGroup
I think that the problem is that you are using v.addView() instead of
cv0.addView()
But I would suggest to use the following instead of your View:
<LinearLayout
android:id="#+id/drawing1"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="2dp"
android:layout_below="#id/drawing0" />
then, inside the Java code:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
LinearLayout cv0 = (LinearLayout)getView().findViewById(R.id.drawing0);
cv0.addView(new BlueShape(getActivity()), 0, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
I'm trying to get an activity to host a fragment, but after inflating the view of the fragment, when I try to access any widgets via findViewById(), it returns null. So in this case mBillTitle is always null, I am not sure what I am missing?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:text="#+id/bill_title"
android:textSize="24dp"
android:gravity="left"/>
....
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#+id/bill_detail_title"
android:textSize="20dp"
android:paddingTop="5dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="5dp">
....
</LinearLayout>
...
</LinearLayout>
And this is the fragment code
#Override
public void onCreate(Bundle savedInstanceBundle) {
super.onCreate(savedInstanceBundle);
mBill = new Bill();
}
#Override
public View onCreateView(
LayoutInflater inflater,
ViewGroup parent,
Bundle savedInstanceBundle) {
View view = inflater.inflate(R.layout.bill_details, parent, false);
mBillTitle = (TextView) view.findViewById(R.id.bill_title);
mBillTitle.setText(mBill.getmShortTitle());
...
return view;
}
android:text="#+id/bill_title"
suppose to be
android:id="#+id/bill_title"
Change android:text to android:id.
I have the following DialogFragment
public class DetailItemFragment extends AppCompatDialogFragment {
#BindView(R.id.task_detail_name)
AppCompatTextView taskDetailNameText;
#BindView(R.id.task_detail_description)
AppCompatTextView taskDetailDescriptionText;
#BindView(R.id.task_detail_priority)
AppCompatTextView taskDetailPriorityText;
#BindView(R.id.toolbar)
Toolbar toolbar;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCanceledOnTouchOutside(true); //doesn't work
dialog.setCancelable(true); //doesn't work
return dialog;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_detail_view, container, false);
ButterKnife.bind(this, view);
//Get the Json back and parse it as a Task
Bundle bundle = getArguments();
final String taskAsJson = bundle.getString("task");
Task task = new Gson().fromJson(taskAsJson, Task.class);
((AppCompatActivity) getActivity()).setSupportActionBar(toolbar);
ActionBar actionBar = ((AppCompatActivity) getActivity()).getSupportActionBar();
if (actionBar != null) {
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
actionBar.setHomeAsUpIndicator(R.drawable.close);
}
setHasOptionsMenu(false);
taskDetailNameText.setText(task.getName());
taskDetailDescriptionText.setText(task.getDescription());
taskDetailPriorityText.setText(task.getPriority().toString());
return view;
}
I perform the Fragment transaction like so
//Put the clicked item into a Bundle for the next fragment to consume
DialogFragment detailItemFragment = new DetailItemFragment();
Bundle args = new Bundle();
args.putString("task", new Gson().toJson(mTaskListAdapter.getItem(position)));
detailItemFragment.setArguments(args);
getActivity().getSupportFragmentManager().beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.add(R.id.task_detail_fragment_container, detailItemFragment)
.addToBackStack(null)
.commit();
I'm trying to accomplish the following:
* On a tablet/large screen, this displays as a Dialog popup
* On a phone, it will display a fullscreen dialog (pretty much trying to replicate how Google Calendar handles opening event details)
This is what my xlarge/activity_main.xml looks like
<?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/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/task_detail_fragment_container"
android:layout_width="800dp"
android:layout_height="400dp"
android:layout_centerInParent="true"
android:elevation="16dp" />
<android.support.design.widget.AppBarLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
style="#style/ToolbarStyle"
android:background="#color/colorPrimary"
app:title="#string/app_name"
app:titleTextColor="#color/white" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/task_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab_add"
android:layout_width="#dimen/fab_normal_size"
android:layout_height="#dimen/fab_normal_size"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="#dimen/fab_padding"
android:layout_marginEnd="#dimen/fab_padding"
android:src="#drawable/add_black"
app:elevation="#dimen/fab_elevation"
app:fabSize="normal"
app:layout_anchorGravity="bottom|right|end"
app:pressedTranslationZ="#dimen/fab_pressed_elevation" />
</RelativeLayout>
You'll notice I have a FrameLayout that is centered on the screen for loading the DetailItemFragment into. Maybe this isn't the correct way to do this--but I want to bring it up in case that turns out to be the issue.
This is what the fragment_detail_view.xml layout looks like
<android.support.design.widget.CoordinatorLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/task_detail_background"
android:clickable="true"
android:elevation="8dp"
tools:context="io.havoc.todo.view.fragments.DetailItemFragment">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:elevation="0dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="#dimen/toolbar_tall_height"
android:background="?attr/colorPrimary">
<android.support.v7.widget.AppCompatTextView
android:id="#+id/task_detail_name"
style="#style/TextAppearance.Widget.AppCompat.Toolbar.Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start|bottom"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:gravity="start|bottom"
android:padding="8dp"
android:text="Task detail"
android:textColor="#color/white"
android:textSize="24sp" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginStart="86dp"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v7.widget.AppCompatTextView
style="#style/TextAppearance.AppCompat.Body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_margin"
android:text="#string/hint_task_description"
android:textColor="#color/colorAccent" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/task_detail_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Description text"
android:textColor="#color/text_primary"
android:textSize="18sp" />
<android.support.v7.widget.AppCompatTextView
style="#style/TextAppearance.AppCompat.Body2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_margin"
android:text="#string/hint_task_priority"
android:textColor="#color/colorAccent" />
<android.support.v7.widget.AppCompatTextView
android:id="#+id/task_detail_priority"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="Priority"
android:textColor="#color/text_primary"
android:textSize="18sp" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
The issue
When the DialogFragment is open on a tablet, the Activity in the background still accepts touches (i.e. I can interact with the Activity as if the dialog wasn't even present); furthermore, I have no idea how to get the DialogFragment to dismiss when touching outside of it. I've tried the following:
dialog.setCanceledOnTouchOutside(true); (in the onCreateDialog() method)
dialog.setCancelable(true); (in the onCreateDialog() method)
dialog.setCanceledOnTouchOutside(false); (in the onCreateDialog() method)
getDialog().setCanceledOnTouchOutside(true); setCancelable(true); (leads to NullPointerExceptions--performed in onCreateView() for the DialogFragment)
I've gone through all the other questions like this I could find and nothing has worked for me--so clearly I'm doing something wrong.
I appreciate any and all help.
Change to .dialog.setCancelable(false);.
If it's a dialog with OK and Cancel buttons, you can use:
.setCancelable(closeActivity == true ? false : true)
The FrameLayout (task_detail_fragment_container) you are adding your DetailItemFragment TO has a different size then the parent, which means it will not cover the entire layout:
...
android:layout_width="800dp"
android:layout_height="400dp"
...
To fix this, make task_detail_fragment_container the same size as the parent layout (match_parent) and cap the size of the dialog in the dialog layout by introducing another Layout after your container layout:
<android.support.design.widget.CoordinatorLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:clickable="true"
android:elevation="8dp"
tools:context="io.havoc.todo.view.fragments.DetailItemFragment">
<FrameLayout
andorid:width="800dp"
android:height="400dp"
android:layout_centerInParent="true">
<android.support.design.widget.AppBarLayout ...
//rest of your xlm here
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
Change this
dialog.setCanceledOnTouchOutside(true);
with
dialog.setCanceledOnTouchOutside(false);
I have successfully implemented Google's SlidingTabLayout and it is working as expected but my view is showing a black screen below it. Like so:
Here is the my activity main layout folder:
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation= "16dp">
<LinearLayout
android:layout_width="220dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="start"
android:id = "#+id/stickylist">
<ViewFlipper
android:id="#+id/flipper1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:flipInterval="2000"
android:inAnimation="#android:anim/fade_in"
android:outAnimation="#android:anim/fade_out"
>
<ImageView
android:src="#drawable/front"
android:layout_width="220dp"
android:layout_height="180dp"
android:contentDescription="#string/str_img1"
/>
<ImageView
android:src="#drawable/field"
android:layout_width="220dp"
android:layout_height="180dp"
android:contentDescription="#string/str_img2"
/>
<ImageView
android:src="#drawable/cafeteria"
android:layout_width="220dp"
android:layout_height="180dp"
android:contentDescription="#string/str_img3"
/>
</ViewFlipper>
<se.emilsjolander.stickylistheaders.StickyListHeadersListView
android:id="#+id/left_drawer"
android:layout_width="220dp"
android:layout_height="fill_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:dividerHeight="1dp"
android:divider="#drawable/customdrawershape"
/>
</LinearLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/fragment_container"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
Then here is where I implement my PagerAdapter in SlidingTabsBasicFragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.tabs, container, false);
....}
tabs.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<xxx.xxx.xxx.SlidingTabLayout
android:id="#+id/pager_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="16dp"/>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_weight ="1"
android:layout_height="0dp"
>
</android.support.v4.view.ViewPager>
</LinearLayout>
Here is where I call on the fragment in my activity class:
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.addToBackStack(null);
SlidingTabsBasicFragment fragment = new SlidingTabsBasicFragment();
transaction.replace(R.id.fragment_container, fragment);
transaction.commit();
I do have other simultaneous fragments ongoing from the main activity that should be displayed. Is that what is going on? Here is the code for that fragment using a different layout for both what is displayed as well as the frame layout for the fragment container.
manager = getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.addToBackStack(null);
EventFragment fragment = new EventFragment();
transaction.add(R.id.fragment_container, fragment);
transaction.commit();
The fragment container for the view I am trying to display that is not showing is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/fragment_container"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
</FrameLayout>
Here is the layout to the first fragment which uses a custom adapter for each list view tile integrated in from a different layout.
<RelativeLayout
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:animateLayoutChanges="true"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:background="#drawable/background">
<TextView
android:id= "#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/string5"
android:layout_centerHorizontal="true"
android:layout_below= "#+id/progressBar"
android:textColor="#FFD600"
android:textSize="20sp"/>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:color="#42A5F5"
android:indeterminateTint="#42A5F5"
android:indeterminateTintMode="multiply"/>
<com.twotoasters.jazzylistview.JazzyListView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/listView"
android:divider="#android:color/transparent"
android:dividerHeight="5dp"
android:paddingRight="10dp"
android:paddingLeft="10dp"
android:layout_above="#+id/layout"/>
</RelativeLayout>
EDIT:
Ok now I can get it show the screen by doing the following:
#Override
public Object instantiateItem(ViewGroup container, int position) {
// Inflate a new layout from our resources
switch (position){
case 0:
view = getActivity().getLayoutInflater().inflate(R.layout.eventlistview,
container, false);
new Event();
break;
case 1:
view = getActivity().getLayoutInflater().inflate(R.layout.twitter,
container, false);
new Twitter();
break;
case 2:
view = getActivity().getLayoutInflater().inflate(R.layout.stafflistview,
container, false);
new Staff();
break;
case 3:
view = getActivity().getLayoutInflater().inflate(R.layout.powerschool,
container, false);
new powerschool();
break;
}
// Add the newly created View to the ViewPager
container.addView(view);
// Return the View
return view;
}
But eventually the progress bar for the fragment layout I have code for is supposed to go away and a list view is to be displayed. However, it will just show this layout forever even though there is more going on in the background. Any ideas?
This is so limited if all you can do is a single layout. Seriously? This is bad.
Wild guesses...
in your tabs.xml: your ViewPager has a weight of android:layout_weight ="1" and a height of android:layout_height="0dp" your slidingtablayout has a height of wrap_content and ther father has not weightsum.. Change their father's weightsum to 1 and see..
let me know if it helps
I have faced such problem. I'm working on an application using ViewPagerIndicator library by Jake Wharton. I would like to change fragment on button click (fragments do change when user swipes through pages though), but my code doesn't work. Any ideas for solving that? In my code below I tried changing FirstFragment to SecondFragment on rightButton click. I would be grateful for any of your help...
FirstFragment.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.layout1, container, false);
rightButton = (Button) view.findViewById(R.id.rightButton);
rightButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
android.support.v4.app.Fragment fragment = new SecondFragment();
android.support.v4.app.FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.fragment1, fragment).commit();
}
});
return view;
}
I change fragments when swiping using this code(I don't know, if it's related to the problem, but I'll post it anyway) TestFragmentAdapter.java file
public Fragment getItem(int position) {
switch (position) {
case 0:
return FirstFragment.newInstance(0);
case 1:
return SecondFragment.newInstance(1);
case 2:
return ThirdFragment.newInstance(2);
default:
return null;
}
}
That's my layout file to FirstFragment.java
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/d_blue"
android:id="#+id/fragment1"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Layout" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/d_blue"
android:orientation="vertical"
android:padding="10dip">
<Button
android:id="#+id/leftButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#android:color/transparent"
android:shadowColor="#color/text_shadow"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2"
android:text="SKIP"
android:textColor="#color/light_font"
android:textSize="14dp"
android:textStyle="bold" />
<Button
android:id="#+id/rightButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:shadowColor="#color/text_shadow"
android:shadowDx="1"
android:shadowDy="1"
android:shadowRadius="2"
android:text="NEXT"
android:textColor="#color/light_font"
android:textSize="14dp"
android:textStyle="bold" />
</RelativeLayout>
</LinearLayout>
How about using setCurrentItem() of the ViewPager instead of replacing the Fragments by yourself.