Is it possible to run Activity and Fragment simultaneously? - android

I am looking for a way to run activity and fragment at the same time where activity will show two buttons to transition from one activity to the other whereas fragment will show live camera view in the background.
is it possible?

Fragments are the part of activity. So it's obvious that activity and fragment run simultaneously run. Fragment does not have it's own context. After read your question I think you want that you can change the fragments but buttom buttom are place there for all fragment. You can do this like:
<?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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
android:layout_above="#+id/button"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/button"/>
</RelativeLayout>
add and replace your fragment on framelayout so each fragment have the button at bottom which is the part of activity.

Related

SwipeRefreshLayout disappears after replacing Fragments

I have a simple app that contains an Activity(acts as host activity) with two Fragments(FragmentA and FragmentB).
There is a SwipeRefreshLayout in the FragmentA. When the SwipeRefreshLayout is refreshing, we replace FragmentA with FragmentB. If we go back to FragmentA(by pressing back button), the SwipeRefreshLayout disappears. For understanding what I mean, please see this image:
How can I prevent SwipeRefreshLayout from disappearing after replacing the Fragments? My classes are very simple, I just replaced FragmentA with FragmentB
fragment_a.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me!"
android:layout_gravity="center"/>
</FrameLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
Instead of this may be you should use your SwipeFreshLayout in activity's xml file which is parent to both fragments.

How to have a view over several activities

How can I have my BottomNavigationView in several activities but without declaring it there?
i declared my Bottom navigation view in my main_activity.xml and handle the click logic within MainActivity class, but since I want to switch between 3 activities (Map, Friends, Chats) with the Bottomnavigationview i'd have to have 3 bottom navigation views over three activities, but since that's performance inefficient, I want to handle all the click events and initialisation of the view inside one class and one Layout
(MainActivity class and main_activity.xml ).
What can I do? or is there a better way of doing this?
You should use fragments instead of activities. Have only 1 activity ie MainActivity, add BottomNavigationView and fragments above the BottomNavigationView. And on clicking the BottomNavigationView, replace the fragments.
Your XML would be something 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/bottom_nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="#id/bottom_nav_view"></FrameLayout>
From your activity, add fragments in FrameLayout. Your BottomNavigationView will remain intact. Just replace fragments in this FrameLayout on clicking NavigationView tabs.

Swiping a fragment not full page.

I currently have 1 activity that jas 2 fragments top half and botton half. I want to swipe the bottom fragment only. (java implented)
Before i go learning the wrong method could someone please tell me which method I should use.
Do you sill use pageView and a fragment adapter or something.
Any usefull tutorials that do similar would be good so i can figure it out.
Define fragment and viewpager in the same activity. in the fragment show thetop fragment and in viewpager, write one pager adapter and invoke all the child fragments into that pager. This solves your problem.
Your activity layout:
<?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:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<space
android:id="#+id/layout"
android:layout_width="match_parent"
center_horizontal="true"
center_vertical="true"
android:layout_height="0.5dp">
<fragment
android:id="#+id/layout"
android:layout_width="match_parent"
layout_above="#id/space_layout"
android:layout_height="match_parent"/>
<android.support.v7.widget.viewpager
android:id="#+id/layout"
android:layout_width="match_parent"
layout_below="#id/space_layout"
android:layout_height="match_parent">
Then define PagerAdapter class and show all your child fragments in that.
all the best

How to add a Floating Action Button to all the pages of a ViewPager

I'm developing an app where the main activity consists of a SwipeView layout.
The activity consists of 2000+ pages, which are individually populated by texts, which are in form of short facts.
I want to add a Floating Action Button to those pages which when clicked, will mark the FACT as a favourite, which the user can view later.
How can I implement that without having to add a button to all the pages individually ( which would be impractical and absurd).
You can put your button on the Activity or fragment that holds your ViewPager, instead of on each ViewPager's adapter
<?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.support.v4.view.ViewPager
android:id="#+id/vpMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ImageView
android:layout_margin="15dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="45dp"
android:layout_height="45dp"
android:src="#drawable/btn_favorite"/>
</RelativeLayout>

How to swap fragments with different layouts in android?

I'm developing an android application making use of fragments which is more of a Master/Detail form. I want the main activity to consist of a list fragment on the left side, based on the item chosen on the left i want to display a fragment with different layout on the right side.
(Note: each fragment on the right require different layouts/views)
All the examples that I've come across make use of only one common fragment on the right by changing some values in it or swapping/replacing new fragments having same layout.
If someone could shed some light on this problem then it will help me immensely. Thanks!
If you are using framelayouts to hold your fragments, it's the same as those other you mention. You just instantiate your fragment (whatever the layout) and swap it into the framelayout in place of the other one.
If you have hardcoded your fragments into the XML, you won't be able to do that (as far as I've been able to determine).
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frames"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/hline1"
android:layout_below="#id/horizontalline"
android:orientation="horizontal" >
<FrameLayout
android:id="#+id/leftpane"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight=".4" />
<TextView
android:id="#+id/verticalline"
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#color/bar_background"
android:gravity="center_horizontal"
android:paddingLeft="5dip"
android:paddingRight="5dip" />
<FrameLayout
android:id="#+id/rightpane"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" >
</FrameLayout>
</LinearLayout>
Then you use the id for the framelayout and the name of your instantiated fragment to put your fragment into the framelayout.
EventListFragment eventlist = new EventListFragment();
getFragmentManager().beginTransaction().replace(R.id.leftpane, eventlist).commit();
EventDetailFragment eventadd = new EventDetailFragment();
getFragmentManager().beginTransaction().replace(R.id.rightpane, eventadd).commit();
When you want to change the contents, you do the same thing again (the following would replace the fragment in the right pane with a new/different fragment, which can have it's own, different, layout associated with it):
EventSuperDetailFragment eventsuper = new EventSuperDetailFragment();
getFragmentManager().beginTransaction().replace(R.id.rightpane, eventsuper).commit();

Categories

Resources