I have a fragment (fragment_1) which gets displayed when the main activity starts and displays a full screen layout (layout_1). Now i have added a button, clicking on which will start a new fragment (fragment_2)and replace the entire layout (layout_1) with my new layout (layout_2).
My first fragment (fragment_1) has a ScrollView as my root view.
<ScrollView
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:fitsSystemWindows="true"
android:background="#fff"
android:scrollbarSize="0dp"
android:id="#+id/scroll_main"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true">
....
</RelativeLayout>
</ScrollView>
Now i want to start a new fragment (fragment_2) on a button click and display another layout (layout_2) but i don't know how to do that.
My main activity had a frame layout like this
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/root_container">
And i used to set this as my default layout using
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
After this i start my first fragment and display the layout_1.
Now from fragment_1 i want to start fragment_2 and display the layout_2. I have read online articles and documentation where i can't find and solution regarding this. Pleas Note :" I don't want to simply replace a part of my layout_1 and replace it with my layout_2 rather i want my layout_1 totally gets replaced with layout_2 on a button click"
Replacing fragments seems like a job for the fragment manager.
This answer might help you Replacing a fragment with another fragment inside activity group
Related
Im trying to find a method to freeze the activity state, but without success.
Im creating a game, and I want to add a pause button. When the user clicks, the activity should freeze with the components, and when click again, keep from where it was before. Is that possible to do that?
Please check the solution it can be helpful for u. As u want to freez the screen for sometime than u have to play with View Visibility.
For it u have to create the view like i have use the rlView in my below layout. Visible it when u want to freez the screen.And again when u want to remove it than set the visibility of view is gone..
We can manage the View visiblity like below:-
RelativeLayout rlView= (RelativeLayout) findViewById(R.id.rlView)
YOUR_BUTTON_CLICK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(rlView.getVisibility()==View.VISIBLE)
{
rlView.setVisibility(View.GONE);
}
else {
rlView.setVisibility(View.VISIBLE);
}
}
});
}
Please check the layout:-
<?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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
HERE IS YOUR WHOLE LAYOUT VIEW
</RelativeLayout>
<RelativeLayout
android:id="#+id/rlView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:background="#android:color/transparent"
>
</RelativeLayout>
</RelativeLayout>
My Problem
I've been trying to get my Recycler View's items to animate as I scroll down. These items are loaded from a local XML and passed through my custom adapter.
I'm using this library for my adapter animation
I noticed that no matter what I tried, the items would just not animate at all, or they would animate all at once (every item would slide in on screen at the same time).
What I've Tried So Far
Utilizing the library I mentioned, I tried animating items in my RecyclerView.Adapter.
> IndexFragment.java snippet
//Create an adapter
SongAdapter adapter = new SongAdapter(songs, getContext());
//Set adapter
AlphaInAnimationAdapter alphaAdapter = new AlphaInAnimationAdapter(adapter);
alphaAdapter.setFirstOnly(false);
recyclerView.setAdapter(adapter);
recyclerView.setHasFixedSize(true);
This one failed somehow. I also tried the setItemAnimator method which also failed.
recyclerView.setItemAnimator(new SlideInLeftAnimator());
Layout Structure
So I assume it has something to do with my RecyclerView being inside of two NestedScrollView's.
Another person seemed to be having a similar issue
Below are some portions of my layout files
activity_main.xml
....
<!-- Fragment Container -->
<android.support.v4.widget.NestedScrollView
android:id="#+id/contentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
....
This is where I put my fragments
index_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/nestedScrollView"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</android.support.v4.widget.NestedScrollView>
This is pretty much my Recyclerview and its parent Nested Scrollview.
item_song.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp">
<!--I have two text views in here. Removed them just for this snippet -->
</LinearLayout>
<!-- This displays a background on each item when I press them AKA touch effects -->
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:clickable="true"
android:background="?android:attr/selectableItemBackground" />
</FrameLayout>
This is the structure of each item in the Recyclerview. I inflate this layout for each item. Pretty straightforward.
Any ideas as to what I could do to solve this issue? Thanks.
So after much testing, I finally came up with a solution that worked.
Change the fragment container in activity_main.xml from NestedScrollView to RelativeLayout or perhaps LinearLayout.
Remove the parent NestedScrollView and keep only the RecyclerView in index_fragment.xml.
Animations now work at last. Yay!
Apparently, it is a bad idea to put your RecyclerView inside of a NestedScrollView.
I read somewhere that because of having to load the proper height of its child layout, the NestedScrollView has to wait for it to completely load, in order to know the total height of the RecyclerView. However, I'm not totally sure if that's the reason, so feel free to correct me.
Hope this helps someone!
The Android developer documentation for fragments says this:
"Note: When you add a fragment to an activity layout by defining the
fragment in the layout XML file, you cannot remove the fragment at
runtime. If you plan to swap your fragments in and out during user
interaction, you must add the fragment to the activity when the
activity first starts, as shown in the next lesson."
And partly because of this I got into the habit of always using the fragment manager to add/remove fragments to/from my user interface (even if I did not want to "hot swap" them at runtime). I am 100% certain that when I tried to remove/replace "hard wired" XML fragments at runtime my app crashed with an exception.
I haven't really worked with XML fragments for months, but today, on a lark, I decided to play around and I find that I am able to swap XML fragments for other fragments and...it works? I can't find anything online that discusses a recent change in this behavior. It just works.
My layout code is here:
<?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:id="#+id/fl_frag"
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"
android:orientation="vertical"
tools:context="mobappdev.demo.myapplication.MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:id="#+id/frag"
android:name="mobappdev.demo.myapplication.BlankFragment"/>
<Button
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="#string/click_me"
android:onClick="clickMe"/>
</LinearLayout>
And here is the code that replaces the fragment:
public void clickMe(View view) {
FragmentManager manager = getSupportFragmentManager();
BlankFragment newFrag = BlankFragment.newInstance();
Fragment oldFrag = manager.findFragmentById(R.id.frag);
Log.i("TESTING", "old frag is null: " + (oldFrag == null));
manager.beginTransaction()
.replace(R.id.fl_frag, newFrag)
.commit();
}
It works without any problems. I've tried variations (putting the XML fragment in a FrameLayout for example) and it all seems to work just fine. I even tried variations such as remove/add and just remove and it all works without a problem.
So what am I missing?
Instead of replacing the fragment itself, you're adding a new fragment on top of that one. It's just a small issue with IDs, but you're replacing using the FragmentManager on the container (LinearLayout) instead of doing it on the fragment itself (R.id.fl_frag instead of R.id.frag)
I have an activity that contains a fragment and the fragment contains a customview. The customview contains an edittext and directly below it a recyclerview with height of match_parent. When the user gets focus on the edittext a softinput keyboard appears.. unfortunately this keyboard hides half the recyclerview underneath it. I want the recyclerview to resize to the remaining visible height of the screen (under the edittext) so that items in the list aren't hidden by the keyboard.
I've tried setting adjustresize for the activity in the manifest but it has no effect:
android:windowSoftInputMode="adjustResize"
I've tried setting my app theme to a base theme (in case something in my custom theme was causing a problem) - no effect:
android:theme="#style/Theme.AppCompat.NoActionBar"
I've checked to make sure there are no framelayouts anywhere down the hierarchy to the view that has to be resized (I read an SO thread that it could be a problem) - there are none.
I've checked the activity, the fragment, the customview and every adapter connected to make sure that I'm not calling getWindow anywhere (I'm not).
The customview is inflated at runtime from a viewstub. Could this have a connection?
Edit:
contents of the parent:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
...
<ViewStub
android:id="#+id/viewstub_outername"
android:layout="#layout/viewstub_thatIneed"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
contents of the viewstub:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewstub_root"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#android:color/white"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:visibility="invisible"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:visibility="invisible"
/>
</RelativeLayout>
Showing the keyboard thusly:
public static void showSoftKeyboard(Activity activity) {
View view = activity.getCurrentFocus();
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
Don't do anything in manifest file. Just use
((LinearLayoutManager)recyclerView.getLayoutManager()).setStackFromEnd(true);
This will take care of recyclerview resizing.
window.decorView.setOnApplyWindowInsetsListener { view, insets ->
val insetsCompat = toWindowInsetsCompat(insets, view)
mainNavigation.isGone = insetsCompat.isVisible(ime())
view.onApplyWindowInsets(insets)
}
Please have a look at the following 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"
tools:context=".MainActivity"
android:background="#drawable/background_main" >
</RelativeLayout>
When I run this, all the tings are perfect and the background image of the main layout also get displayed in Emulator. But, when I run this code in Phone, the background image is missing! What is wrong?
I found the issue by my self. My app is multi screen supporting so I had to put those images (with different sizes) into all the related drawable folders.
<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"
tools:context=".MainActivity"
android:background="#drawable/background_main" >
xmlns:tools are most probably used for the activity the tools UI editor uses to render your layout (it will find the right theme based on the activity).
So if you can just remove tools
OR in .MainActivity java class. define your background source.
//outside your onCreate
RelativeLayout relativeLayout;
Context ctx;
//inside your onCreate
relativeLayout = new RelativeLayout(this);
relativeLayout.setBackgroundResource(R.drawable.background_main);
setContentView(relativeLayout);