RecyclerView Scrollbar is not visible in Dialog Fragment in Android - android

I have a dialog fragment, in which I have a recycler view. I want to add a scrollbar to the recycler view. I added scrollbars="vertical" but it's not showing up. I think the issue has something to do with the themes.
Here is my xml:
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:padding="16dp"
tools:context="com.airxos.app.common.fragments.LayerSwitcherFragment">
<TextView
android:id="#+id/layer_list_title"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:fontFamily="sans-serif"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#color/black_87"
android:text="#string/layers"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/layer_list"
android:layout_marginTop="#dimen/margin_16"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/layer_list_title"
app:layout_constraintEnd_toEndOf="parent"
android:layout_height="0dp"
android:maxHeight="#dimen/info_window_height"
android:layout_width="match_parent"
android:nestedScrollingEnabled="true"
android:scrollbars="vertical"
android:background="#color/white"
android:scrollbarThumbVertical="#drawable/scrollbar"
android:scrollbarSize="5dp"
android:padding="5dp"
android:scrollbarStyle="insideInset"/>
</android.support.constraint.ConstraintLayout>
I am restricting the height and width of the dialog fragment as shown below:
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
Objects.requireNonNull(inflater, "LayoutInflater");
if(getDialog().getWindow() != null) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().setCanceledOnTouchOutside(true);
}
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_layer_switcher, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
Objects.requireNonNull(view, "View");
super.onViewCreated(view, savedInstanceState);
layerList = view.findViewById(R.id.layer_list);
layerList.setAdapter(new LayerListAdapter(context, layers, layerSelectionListener));
layerList.setLayoutManager(new LinearLayoutManager(context));
}
I have already tried everything suggested in this SO question and it did not work.
Other than this I have also tried creating a drawable and adding it as scrollbarThumbVertical
I think it has something to do with the themes because even when I change the background color of constraint layout it is not showing up in the dialog.
EDIT:
I am showing the dialog as shown below:
LayerSwitcherFragment switcherFragment = LayerSwitcherFragment.newInstance(layers);
FragmentTransaction ft = getChildFragmentManager().beginTransaction();
Fragment prev = getChildFragmentManager().findFragmentByTag("layer_dialog");
if (prev != null) {
ft.remove(prev);
}
ft.addToBackStack(null);
switcherFragment.show(ft, "layer_dialog");
EDIT: Issue is solved.
I am sorry for wasting everyone's time. The issue was a stupid mistake we made. My DialogFragment was in a Library. We build the arr file and was using it inside the app. The issue was that we forgot to remove the dialogfragment's XML files from the main app when we refactored it to the arr library. So even after we update the files in the library, the app was picking up the old file in the main app instead of the updated file because the name of the XML was the same.

Try:
android:fadeScrollbars="false"
android:scrollbarAlwaysDrawVerticalTrack="true"

Try This:
In XML:
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:scrollbars="vertical" <!-- type of scrollbar -->
android:scrollbarThumbVertical="#android:color/darker_gray"
android:scrollbarSize="5dp"
android:layout_width="match_parent"
android:layout_height="match_parent" />

In your xml, Just set the android:scrollbars="vertical" it can work.
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
app:layoutManager="android.support.v7.widget.LinearLayoutManager" />

I know this can come kind of late. Try to use a NestedScrollView as the parent of the RecyclerView. Like this:
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/layer_list_title"
app:layout_constraintEnd_toEndOf="parent"
android:scrollbars="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/layer_list"
android:layout_marginTop="#dimen/margin_16"
android:layout_height="wrap_content"
android:maxHeight="#dimen/info_window_height"
android:layout_width="match_parent"
android:nestedScrollingEnabled="true"
android:background="#color/white"
android:padding="5dp"/>
</androidx.core.widget.NestedScrollView>

I guess in DialogFragment you scrollbar is transparent
try this code
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/mTaskListRV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scrollbarSize="5dp"
android:scrollbarThumbVertical="#drawable/shape_progress"
android:scrollbarTrackVertical="#drawable/shape_round_gray"
android:scrollbars="vertical"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:listitem="#layout/cash_out_task_list_item_layout" />

Related

Replacing DrawerLayout's RelativeLayout with a Fragment's layout doesn't work but no error is shown [duplicate]

I have the following XML:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<!--I use this include as container with the FrameLayout below-->
<!--<include layout="#layout/content_main" />-->
<FrameLayout
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/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.improvemybrand.MainActivity"
tools:showIn="#layout/app_bar_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_input_add" />
and the problem is simple:
When I try to replace the container FrameLayout from my Coordinator, it does not work, it shows the new fragment but also keeps the old one, in my simple example, the TextView with Hello world will remains.
To replace, I'm using the following code:
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.content_main, fragment);
transaction.commit();
Any ideas?
FragmentTransactions deal only with Fragments and their Views. The TextView you've defined in your layout is not (part of) a Fragment's View, so it will not be affected by a FragmentTransaction, and the Fragment in your snippet will just be added on top of it.
You have a few options. You could hide or remove the TextView yourself when performing the transaction. This might be preferable, if that simple TextView is all you need initially, and sticking it in a Fragment could be overkill. You could also simply set an opaque background on the Fragment's layout, which will effectively hide the TextView.
The probably best option, however, is to put the TextView in a layout for another Fragment which is loaded at startup. Subsequent transactions will then replace/remove it as you're expecting. Do note that any Fragment you wish to replace/remove at runtime must be loaded dynamically in your code. That is, they cannot be defined in <fragment> elements in your Activity's layout.
You should move TextView into Fragment's layout.
like this.
fragment_hello_world.xml
<?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:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
/>
</LinearLayout>
HelloWorldFragment
public class HelloWorldFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_hello_world, container, false);;
}
}

Why does one of the two fragments in an activity take up the whole screen during run time?

my activity_main.xml has two framelayouts that acts as containers for two fragments .When i run each fragment individually ,they run good .when i try to initialize and display both fragments ,one of the fragments called recents fragment is taking the full screen view .
when i run this fragment individually ,it takes full screen view instead of running in its own container.
and when i try to initialize my second fragment after initializing recents fragment i get an errorNo view found for id 0x7f070044 (com.example.diary:id/allnotesfragment) for fragment BlankFragment{7eb6bf4 (bc0fee9d-4a46-4e2b-ab31-d060c0ec14ae) id=0x7f070044}
activity code where i initialize both the fragments are is given below
package com.example.diary;
public class MainActivity extends AppCompatActivity {
int j;
List<NotesEntry> entries;
RecyclerAdapter adapter;
NotesDatabase db;
int numofitems;
String notesdata = "new_notes";
String defaultnotes = "old_notes";
Context c = this;
GridLayoutManager gridLayoutManager;
RecentsFragment recentsFragment;
ActivityMainBinding binding;
String textfortest;
int id;
FragmentManager fragmentManager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecentsFragment recentsFragment=new RecentsFragment();
BlankFragment blankFragment=new BlankFragment();
fragmentManager =getSupportFragmentManager();
fragmentManager.beginTransaction().add(R.id.recentsfragment,recentsFragment).commit();
fragmentManager.beginTransaction().add(R.id.allnotesfragment,blankFragment).commit();
}
}
its layout xml is
<layout xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/recentsfragment"
android:layout_width="395dp"
android:layout_height="325dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.618"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view"></FrameLayout>
<view
android:id="#+id/view"
class="android.widget.Button"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginBottom="8dp"
android:background="#android:color/darker_gray"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:layout_editor_absoluteX="-51dp" />
<FrameLayout
android:id="#+id/allnotesfragment"
android:layout_width="395dp"
android:layout_height="327dp"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toTopOf="#+id/view"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"></FrameLayout>
i tried to implement it on other layouts too ,but the result is same.
thanks for your time
Your layout is most likely incorrect. Try something like this.
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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/main_constraint_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/fragment_container1"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="#id/some_view"/>
<view
android:id="#+id/some_view"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#id/fragment_container1"
app:layout_constraintBottom_toTopOf="#id/fragment_container2"/>
<FrameLayout
android:id="#+id/fragment_container2"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#id/some_view"
app:layout_constraintBottom_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
After spending a lot of time on it, I found that the problem is not with the layout but with the fragment itself.
In the oncreateView method of a fragment, I used databinding to refer to view instead of findViewById() and I have implemented a wrong way of databinding.
so by implementing databinding in the following way, I solved my problem.
fragmentBlankBinding = FragmentBlankBinding.inflate(inflater,container,false);
fragmentBlankBinding.recyclerView.setAdapter(adapter);
fragmentBlankBinding.recyclerView.setLayoutManager(layoutManager);
view=fragmentBlankBinding.getRoot();
return view;
Note: you need not refer to any layouts using this method as the layout is automatically fetched from databinding layout file of your fragment.
Hope it helps someone

Background Image when RecyclerView is empty

Do you know how to set a background image when the recycler view is empty? Like Gmail app...
Question 2: is there a repository where I can find official images by Google for this purpose?
EXAMPLE
Yes, there is an official, Google maintained Github repository. Contains the latest version of their material icons, which are used on all Google apps.
Google's Material Design Icons Repository
And, regarding your first question: yes.
You can check if the adapter you are using to populate your RecyclerView is empty (or/and if the adapter has no items). If true, show the image, else, deal with the RecyclerView. Like this:
if (mAdapter.getItemCount() == 0) {
// show my background image
// do stuff
} else {
// RecyclerView stuff
}
To elaborate a bit on #herrmartell answer and to manage streched drawables as background: This solution switches the howl recView for another linearlayout having the same effect as you request but looks in my case prettier after trying lots of different possibilities. If you change your list you have to call the handler function abviously again.
Your Fragment that handles the RecyclerView:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
//...
//setup recView like -> recView = view.findViewById(R.id.yourListRecyclerView);
//adapter setup
//set to recView
handleRecyclerViewState(view);
}
private void handleRecyclerViewState(View view) {
View emptyView = view.findViewById(R.id.empty_view);
if (adapter.getItemCount() == 0) {
recView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
} else {
recView.setVisibility(View.VISIBLE);
emptyView.setVisibility(View.GONE);
}
}
Inside your RecView layout xml:
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/yourListRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:paddingBottom="80dp"
android:scaleType="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0" />
<include
android:id="#+id/empty_view"
android:layout_gravity="center_horizontal|bottom"
layout="#layout/empty_view"
android:layout_height="match_parent"
android:layout_width="match_parent">
</include>
Exact layouting and design might differ for sure.
Here the Layout of the empty_view.xml that shows a drawable svg image and a message underneath:
<?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:gravity="center"
android:orientation="vertical"
android:visibility="gone"
tools:visibility="visible">
<ImageView
android:id="#+id/empty_image"
android:layout_width="220dp"
android:layout_height="250dp"
android:src="#drawable/ic_no_files" />
<TextView
android:id="#+id/emptyMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Nothing here!"
android:alpha="0.3"
android:textColor="?android:attr/textColor" />

How to disable PreferenceFragment transparency?

I guess it's ok to set background color in some average fragment with average layout, but here I've got PreferenceFragment, which layout (PreferenceScreen) doesn't support android:background field. What's a neat way to handle it?
Add following to PreferenceFragment class declaration
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
if (view != null) {
view.setBackgroundColor(getResources().getColor(android.R.color.background_dark));
}
return view;
}
Adding my answer since I had a similar issue with PreferenceFragmentCompat that wasn't quite resolved by just adding a color to the background, and this was a top result while googling the problem.
I had the same issue where using PreferenceFragmentCompat would work, but the settings background was transparent and you could still see views in the underlying activty - changing .add() to .replace() in the getSupportFragmentManger yielded the same overlay.
The solution of changing the background color worked. However, for my build the getColor() method was deprecated. I instead use the following code.
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
getListView().setBackgroundColor(Color.WHITE);
}
This solved the transparency issue except for one button, which was still clickable. For the sake of brevity, the issue was I was trying to replace the layout that contained the activity views.
What ended up working was creating an empty layout at the highest order and using .replace() on that layout. The buttons are now covered and no longer clickable.
XML where button was still visible above preferences fragment.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--I was replacing this layout that had all the activity views-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="parent">
<TextView
android:text="Z"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/earth_acc_z"
app:layout_constraintTop_toBottomOf="#+id/earth_acc_y"
app:layout_constraintStart_toStartOf="#+id/earth_acc_y"
app:layout_constraintEnd_toEndOf="#+id/earth_acc_y"
app:layout_constraintHorizontal_bias="1.0"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/toggleRecording"
app:layout_constraintStart_toStartOf="#+id/toggleRecording"
android:layout_marginStart="8dp"/>
</FrameLayout>
</android.support.constraint.ConstraintLayout>
XML of working example with new empty constraint.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<!--FrameLayout with two nested ConstraintLayouts-->
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintEnd_toStartOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintTop_toBottomOf="parent">
<!--ConstraintLayout with acitivty views-->
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/frameLayout">
<TextView
android:text="Z"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/earth_acc_z"
app:layout_constraintTop_toBottomOf="#+id/earth_acc_y"
app:layout_constraintStart_toStartOf="#+id/earth_acc_y"
app:layout_constraintEnd_toEndOf="#+id/earth_acc_y"
app:layout_constraintHorizontal_bias="1.0"/>
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button" android:layout_marginTop="8dp"
app:layout_constraintTop_toBottomOf="#+id/toggleRecording"
app:layout_constraintStart_toStartOf="#+id/toggleRecording"
android:layout_marginStart="8dp"/>
</android.support.constraint.ConstraintLayout>
<!--Preference fragment should replace this empty layout-->
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/preferenceFragment"
app:layout_constraintTop_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.constraint.ConstraintLayout>
</FrameLayout>
</android.support.constraint.ConstraintLayout>

Android replace fragment still displays some of the replaced fragment

I have a tab that contains a linear layout with a searchview and a listview. When the user clicks one of the search results, I want to replace that layout with another layout containing the details of the selection.
What happens when I replace the search fragment only the listview portion of the layout gets replaced; the searchview is still visible and active on the screen.
Here is my search layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<SearchView
android:id="#+id/public_calendar_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
</SearchView>
<ListView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1.0"
android:choiceMode="singleChoice" />
<TextView
android:id="#android:id/empty"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center"/>
</LinearLayout>
Here is the detail layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/detail_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="vertical" >
<Space
android:layout_width="0dip"
android:layout_height="20dip"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal" >
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".1" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".8"
android:text="#string/label_Name" />
<Space
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".1" />
</LinearLayout>
.........
</LinearLayout>
And my code to replace the search fragment:
FragmentTransaction transaction = getFragmentManager().beginTransaction();
// Replace whatever is in the fragment_container view with this fragment,
// and add the transaction to the back stack.
DetailFragment fragment = DetailFragment.newInstance(cal);
transaction.replace(R.id.search_layout, fragment);
transaction.addToBackStack(null);
transaction.commit();
Detail fragment creation:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.detail_layout, container, false);
return v;
}
static DetailFragment newInstance(String cal) {
DetailFragment d = new DetailFragment();
return d;
}
What am I doing wrong? How can I get the entire search layout to be replaced.
Thanks!
I figured out my problem. Marco wasn't exactly right but he pointed me in the right direction.
The problem was the the container ID I was passing to the replace method was the ID of the fragment I was replacing, not the ID of the fragment container. This seems to explain why some of the original fragment controls were remaining after the replace - that entire fragment wasn't being replaced.
I changed it to get the fragment container view ID, and that worked! Here's the code:
transaction.replace(((ViewGroup)(getView().getParent())).getId(), fragment);
I found the answer for getting the container view ID of a fragment here, Get fragment's container view id.
I was using the correct ID, then also it wasnt going away. Turns out that you need to set the Background color to your needed color in new Fragment layout, like this.
<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:orientation="vertical"
android:background="#color/white_color">
To solve the problem, set the android:background property to “?android:windowBackground” in the fragment layout files.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:windowBackground"
tools:context=".MainFragment">
...
</LinearLayout>
In Fragment_2
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:windowBackground"
tools:context=".SomeFragment">
...
</LinearLayout>
I was having the same problem, and the solution didn't work for me. Maybe this will help someone...
I had added a fragment inside the activity layout xml file. This caused a lot of issues.
Instead, add a frame layout in your xml file, and then programatically add and replace fragments. This causes the whole fragment to be replaced.
I had the same problem once, and this is my solution.
Try to do this, use FrameLayout instead of fragment in your layout file. Just like this:
<fragment android:id="#+id/your_fragment" />
Then, add your default fragment in activity method onCreate().
getFragmentManager().beginTransaction()
.add(R.id.your_fragment, YourFragment)
.commit();
Now, the old fragment can be replaced correctly, it looks great.
This worked for me, and I hope it helps you too.

Categories

Resources