I have a simple Activity with an ImageView:
<RelativeLayout
android:id="#+id/rl_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/iv_picture"
android:layout_width="100dp"
android:layout_height="100dp"
android:transitionName="#string/pic_viewer_transition" />
</RelativeLayout>
I want to share this ImageView to the Fragment in this Activity. Here's Fragment XML:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/black">
<ImageView
android:id="#+id/iv_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:transitionName="#string/pic_viewer_trasition"/>
Here's my code to provide Sharing:
iv_picture.setOnClickListener {
val f = PictureViewer()
val args = Bundle().apply {
putParcelable("TESTING", picInfo)
}
f.arguments = args
f.sharedElementEnterTransition = TransitionCustom()
f.sharedElementReturnTransition = TransitionCustom()
supportFragmentManager.beginTransaction()
.addSharedElement(iv_picture, activity.getString(R.string.pic_viewer_transition))
.replace(R.id.rl_container, f)
.addToBackStack(null)
.commit()
}
But nothing happens. This code creates and shows my Fragment without any transition. What am I doing wrong?
Related
I am using recyclerview. Then problem is when i click one of the row, new fragment should be open. It opens but, old fragment still appears. this is my screenshot:
As you see, when i click "opel" new fragment opens. But it is overlapping.
This is my fragment 1:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvarackategori"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container">
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
This is my new fragment:
<?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=".ui.aracYedekParca.chevrolet.ChevroletYedekParcaFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Chevrolet Yedek Parca" />
</FrameLayout>
this is my code:
override fun userItemClick(position: Int) {
//super.userItemClick(position) No need of it.
when (position) {
0 -> {
var fragmentOpel=OpelYedekParcaFragment()
var transaction = manager.beginTransaction()
transaction.replace(R.id.container,fragmentOpel)
transaction.commit()
Log.e("aki", "opel: " + position)
} //start a fragment
1 -> {
var fragmentChevrolet=ChevroletYedekParcaFragment()
var transaction = manager.beginTransaction()
transaction.replace(R.id.container,fragmentChevrolet)
transaction.commit()
Log.e("aki", "chevrolet: " + position)
} //start a fragment
Add a background to your new Fragment:
<?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"
android:background:"#android:color/white"
tools:context=".ui.aracYedekParca.chevrolet.ChevroletYedekParcaFragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Chevrolet Yedek Parca" />
</FrameLayout>
Basically that's how fragments work.
The methods name replace might be misleading, it doesn't replace the fragments it only lay a fragment in a layout.
so if you want the other fragment to be dismissed you have to remove it. otherwise just setting a background for your 2nd Fragment should workaround your issue
I have one activity (Navigation Controller) that controls multiple fragments. The initial screen the user sees is a single fragment placed into one container. When I click a button to a different fragment I need to add the first fragment to the backstack and add the two new fragments to different containers than the first one. The two fragments are a map fragment on top and a profile details fragment on the bottom Is this even possible?
The code is below.
Navigation Controller Home Fragment (this is the main screen when the app starts):
public void home(Bundle savedInstanceState){
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState != null) {
return;
}
// Create a new Fragment to be placed in the activity layout
CurrentFriendsFragment firstFragment = new CurrentFriendsFragment();
// Add the fragment to the 'fragment_container' FrameLayout
fragmentTransaction
.setCustomAnimations(R.animator.fade_in, R.animator.fade_out)
.replace(R.id.fragment_container, firstFragment, "firstFragment")
.commit();
}
}
Navigation Controller Profile Fragment:
#Override
public void onProfileButtonClicked() {
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
if (findViewById(R.id.fragment_container) != null) {
if (savedInstanceState1 != null) {
return;
}
MapFragment mapFragment = new MapFragment();
// Create a new Fragment to be placed in the activity layout
ProfileFragment profileFragment = new ProfileFragment();
// Add the fragment to the 'fragment_container' FrameLayout
fragmentTransaction
.setCustomAnimations(R.animator.slide_in_up, R.animator.slide_out_up, R.animator.slide_in_down, R.animator.slide_out_down)
.add(R.id.fragment_container_bottom_large, profileFragment)
.add(R.id.fragment_container_top_small, mapFragment)
.commit();
}
}
And here is the main activities xml file. Note the three different containers. "fragment_container" for full screen fragments, and "fragment_container_top_small", "fragment_container_bottom_large" for multiple fragments on one page.
<?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/activity_navigation"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.parse.starter.ViewControllers.NavigationController"
android:background="#color/palette_darkwhite">
<include layout="#layout/tool_bar"
android:id="#+id/toolbar_layout" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="#+id/toolbar_layout"
android:layout_above="#+id/bottom_navigation_navbar">
<RelativeLayout
android:id="#+id/fragment_container_top_small"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
</RelativeLayout>
<FrameLayout
android:id="#+id/fragment_container_bottom_large"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3">
</FrameLayout>
</LinearLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar_layout"
android:layout_above="#+id/bottom_navigation_navbar">
</FrameLayout>
<FrameLayout
android:id="#+id/fragment_container_popup"
android:layout_width="275dp"
android:layout_height="275dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/bottom_navigation_navbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/palette_lightwhite">
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_horizontal"
android:background="#null"
app:srcCompat="#drawable/collpaseup" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_vertical">
<LinearLayout android:id="#+id/thumbnail2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="3dp"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dp">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_picture_navbar"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="center_vertical"
app:civ_border_color="#FF000000"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thumbnail2"
android:layout_toRightOf="#+id/thumbnail2"
android:textColor="#color/palette_primarycolor"
android:id="#+id/nameLabel"
android:text="Name"
android:textSize="15sp"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/nameLabel"
android:layout_toRightOf="#+id/thumbnail2"
android:textColor="#color/palette_primarycolor"
android:id="#+id/locationLabel"
android:text="Location"
android:textSize="12sp"
android:textStyle="bold"/>
</RelativeLayout>
</android.support.design.widget.BottomNavigationView>
</RelativeLayout>
Have 3 containers and use View.GONE to make the 1st disappear and then View.VISIBLE to make the other 2 appear.
I am trying to create a swappable fragements on my android app
Only half the part of fragment seems to be replacing
screenshot:
The xml file for the main is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_main"
tools:context=".MainActivity"
android:id="#+id/contentlayout">
<fragment
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/header_menufragment"
class="com.example.www.newapp.header_menu" />
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/dynamicfragment"
class="com.example.www.newapp.mainfragment"/>
</LinearLayout>
the whole dynamicfragment is supposed to be replaced by projectfragment on click of Project button.
project.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getFragmentManager()
.beginTransaction()
.replace(R.id.dynamicfragment, new projectfragment())
.addToBackStack(null)
.commit();
}
});
the projectfragment class has
public class projectfragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_project, container,false);
}
}
the layout file is
<?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:layout_height="match_parent"
android:paddingLeft="15dp"
android:paddingRight="15dp"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:background="#color/colorDarkRed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight=".5">
<ImageButton
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
<ImageButton
android:id="#+id/img2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
<ImageButton
android:id="#+id/img3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:src="#drawable/imgfile"/>
</LinearLayout>
Kindly help me solve this issue.
You cannot replace a fragment defined statically in the layout file. You can only replace fragments that you added dynamically via a FragmentTransaction.
Use FrameLayout as container and replace Fragment inside that.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent" android:layout_height="match_parent">
<FrameLayout android:id="#+id/fragment_container" android:layout_weight="1"
android:layout_width="match_parent" android:layout_height="match_parent" />
</LinearLayout>
On Activity add Fragment same as you do:
getFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new projectfragment())
.addToBackStack(null)
.commit();
For more info refer below link:
link1
link2
that worked for me try this way
WishaFriend ff = new WishaFriend();
android.app.FragmentManager fm = getActivity().getFragmentManager();
android.app.FragmentTransaction ft = fm.beginTransaction();
ft.replace(((ViewGroup) getView().getParent()).getId(), ff);
ft.commit();
dialog.dismiss();
try add this
ft.replace(((ViewGroup) getView().getParent()).getId(), ff);
I'm having overlay in my application, I did a search and I found some solutions that apparently were simple, but did not work for me. Probably I have a little confusion because I don't understand why it's not working.
So, in my activity_main.xml file I have a FrameLayout element 'container_body'. In my MainActivity I'm set the content of this xml. In this activity I have a drawer-list with some fragments.
When I click in any fragment I'm seeing overlay.
In MainActivity I'm calling the displayView that is responsible to make the fragments transitions, I'm replacing the container_body element for the content of fragment, but this doesn't working.
Any suggestions, please?
[Images] The first screen
The fragment screen
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new PostView();
title = getString(R.string.title_section1);
break;
case 1:
fragment = new RegionView();
title = getString(R.string.title_section2);
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:contentInsetStart="72dp" />
<FrameLayout
android:id="#+id/container_body"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.25"
android:layout_gravity="top|left|bottom|right">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/tile_bg"
android:orientation="vertical" >
<ListView
android:id="#+id/list_view_messages"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#null"
android:divider="#null"
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true">
</ListView>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/llMsgCompose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal"
android:weightSum="3" >
<EditText
android:id="#+id/inputMsg"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#color/bg_msg_input"
android:textColor="#color/text_msg_input"
android:paddingLeft="6dp"
android:paddingRight="6dp"/>
<Button
android:id="#+id/btnSend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/bg_btn_join"
android:textColor="#color/white"
android:text="#string/btn_send" />
</LinearLayout>
</FrameLayout>
<com.heinrichreimersoftware.materialdrawer.DrawerView
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
Fragment
public class PostView extends Fragment implements CommonPresenter {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_post, null);
return (view);
}
I created an activity with a navigation drawer and a FrameLayout. This frame layout should contain the fragment which corresponds the selected navigation item. So I followed the instructions in android tutorials, but it doesn't work.
Problem:
It seems to me the fragments are only added and not replaced. What am I doing wrong?
My activity xml:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainViewDrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<FrameLayout
android:id="#+id/mainViewContentFrame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/drawer_content_padding" >
</FrameLayout>
<ListView android:id="#+id/mainViewDrawerList"
android:layout_width="#dimen/drawer_size"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="none"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:background="#color/drawer_background"
/>
</android.support.v4.widget.DrawerLayout>
my first fragment xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingLeft="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_horizontal_margin"
android:paddingBottom="#dimen/activity_horizontal_margin"
android:id="#id/fragmentProfile">
<TextView
style="#android:style/TextAppearance.Small"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/mainViewProfileGivenNameLabel"
/>
<TextView
android:id="#+id/profileGivenNameTextView"
style="#android:style/TextAppearance.Large"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
my second fragment xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#id/fragmentProtocol">
<ListView android:id="#+id/protocolTableListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:transcriptMode="alwaysScroll"
android:cacheColorHint="#00000000"
android:listSelector="#android:color/transparent"/>
</LinearLayout>
my transition method in the activity:
private void switchToFragment(int navigationId, boolean addToBackStack){
FragmentManager fragmentManager = getFragmentManager();
String fragmentTag = null;
boolean fragmentCreated = false;
switch(navigationId) {
case NavigationItemProvider.NAVIGATIONITEM_PROFILE:
fragmentTag = NAVIGATIONTAG_PROFILE;
break;
case NavigationItemProvider.NAVIGATIONITEM_PROTOCOL:
fragmentTag = NAVIGATIONTAG_PROTOCOL;
break;
}
// determine if the fragment is already instanciated otherwise create
FragmentBase fragment = (FragmentBase)fragmentManager.findFragmentByTag(fragmentTag);
if (fragment == null){
if (fragmentTag == NAVIGATIONTAG_PROFILE) {
fragment = new ProfileFragment();
fragmentCreated = true;
}
else if(fragmentTag == NAVIGATIONTAG_PROTOCOL) {
fragment = new ProtocolFragment();
fragmentCreated = true;
}
}
// switch to fragment
if (fragment.isVisible()){
return;
}
FragmentTransaction transaction = fragmentManager.beginTransaction();
if (fragmentCreated) {
transaction.replace(R.id.mainViewContentFrame, fragment, fragmentTag);
}
else{
transaction.show(fragment);
}
if(addToBackStack)
{
transaction.addToBackStack(null);
}
transaction.commit();
}
solved the problem:
In onCreate I returned the result of super.onCreate and not the result of Layoutinfalter.inflate and in addition I didn't call inflate with last parameter set to false.