Add fragment below another overlapping - java

I'm was trying to display fragment below another fragment, but i'm facing Issue with view, when i invoke the 2nd fragment that should come below first fragment it's overlap.
My First fragment container RelativeLayout with Id mainLayout and 2nd fragment with Id carLayout.
I assume the issue with XML code.
My XML
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/topLayout">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:layout_weight="1" />
</LinearLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainLayout">
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/carLayout">
</RelativeLayout>
</LinearLayout>
i need to set fragment 2 below fragment 1
but it'as overlapping.

as I understand you need to set two fragment in same view
// add your first fragment
FragmentTransaction transaction = getFragmentManager().beginTransaction().add(frame_container1 , YOUR_FIRST_FRAGMENT).commit;
// add your second fragment
FragmentTransaction transaction = getFragmentManager().beginTransaction().add(frame_container2 , YOUR_SECOND_FRAGMENT).commit;
<linearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2">
<FrameLayout
android:id="#+id/frame_container1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<FrameLayout
android:id="#+id/frame_container2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</linearLayout>
or simply
<fragment
xmlns:android="http://schemas.android.com/apk/res/android"
android:name="YOUR FRAGMENT CLASS"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
</fragment>

carLayout is a RelativeLayout so new fragment will be over the last one, change it to a LinearLayout with orientation vertical.

You need another view below your carLayout.
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/anotherLayout"
android:layout_below="#id/carLayout">
</RelativeLayout>
The 'add' method adds a fragment to a view, so you can modify your method to use a different container every time:
public void replaceFragment(Fragment someFragment, int containerId) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.add(containerId , someFragment);
transaction.addToBackStack(null);
transaction.commit();
}
case R.id.home_menu:
fragment = new mainFrog();
replaceFragment(fragment,R.id.carLayout);
public void Haraj_cars(View view) {
fragment = new carFrog();
replaceFragment(fragment,R.id.anotherLayout);
}

Related

Android scroll to top next replacing fragment in scrollview

this is my layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_scrollview"
android:layout_width="match_parent"
android:scrollbars="vertical"
android:fillViewport="true"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/space.small"
android:orientation="vertical"
android:padding="#dimen/space.medium">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="#dimen/space.medium">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/bla_bla_bla"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/my_relative_layout">
</RelativeLayout>
</LinearLayout>
</ScrollView>
it is a fragment inside my tab menu, when this tab will be create the relative layout will be replaced by a variable number of elements:
android.support.v4.app.FragmentTransaction ft = getChildFragmentManager().beginTransaction();
ft.replace(R.id.my_relative_layout, myListFragment).commit();
mHolder.scrollView.scrollTo(0,0); //not work
But my fragment doesn't scroll to top, it works only with a delay like:
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
mHolder.scrollView.scrollTo(0,0);
}
}, 100);
How can I do that in a right way? I won't use a delay
Add
android:descendantFocusability="blocksDescendants"
to
"RelativeLayout"

Fragment replaces entire screen

I have the following structures and fragment replace/add does not only replace its host view but whole screen. What can cause this?
Main Activity layout without data
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".activity.MainActivity">
<FrameLayout
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="12"
app:defaultNavHost="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_gravity="bottom"
android:layout_weight="1"
app:labelVisibilityMode="unlabeled"
android:background="#drawable/transparent_button"
app:onNavigationItemSelected="#{viewModel::onNavigationItemSelected}"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior"
app:menu="#menu/bottom_navigation_menu" />
</LinearLayout>
Fragment layout
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?android:attr/actionBarSize"
tools:context=".fragment.HomeFragment">
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="20dp"
android:background="#drawable/transparent_button"
android:onClick="#{viewModel::onFloatingButtonClicked}"
android:src="#drawable/ic_baseline_add_24" />
</FrameLayout>
Main activity onCreate
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mainViewModel = ViewModelProviders.of(this, new ViewModelFactory(getApplication())).get(MainViewModel.class);
activityMainBinding.setViewModel(mainViewModel);
activityMainBinding.executePendingBindings();
fragmentList.put(FragmentsEnum.HOME.getValue(), new HomeFragment());
fragmentList.put(FragmentsEnum.PROFILE.getValue(), new ProfileFragment());
Fragments.addFragment(getSupportFragmentManager(), R.id.nav_host_fragment, fragmentList.get(FragmentsEnum.HOME.getValue()), FragmentsEnum.HOME.getValue()); // DEFAULT FRAGMENT
}
Fragments util
public static void addFragment(FragmentManager manager, int containerId, Fragment fragment, String tag) {
if (!manager.isStateSaved()) {
manager.beginTransaction().add(containerId, fragment, tag).commit();
}
}
On onview of main activity I am setting Home Fragment active when the activity starts. But it hides my bottom nav bar and replaces entire screen.
I think that this is a problem with the usage of LinearLayout as the root element for the fragment and bottomnav. Try replacing it with either ConstraintLayout or CoordinatorLayout. If none of those work try removing the top padding from the Fragment.
Your FrameLayout will just overlap the BottomNavigationView. Furthermore you should use the FragmentContainerView for your nav host. You'd want to have your FragmentContainerView explicitly above your BottomNavigationView. I changed your LinearLayout to a ConstraintLayout with proper constraints:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintBottom_toTopOf="#+id/bottom_nav"
app:defaultNavHost="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:labelVisibilityMode="unlabeled"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
android:background="#drawable/transparent_button"
app:onNavigationItemSelected="#{viewModel::onNavigationItemSelected}"
app:layout_behavior="#string/hide_bottom_view_on_scroll_behavior"
app:menu="#menu/bottom_navigation_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>

open fragment under parent activity

I want to open a fragment in activity but under objects of Activity.
in my code RelativeLayout is parent and LinearLayout is child
but when open fragment on relativelayout I lose linearlayout :(
this is my code:
Main Activity Java:
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private Context context;
private Button fragment1 , fragment2;
private RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = getApplicationContext();
fragment1 = this.findViewById(R.id.fragment1);
fragment2 = this.findViewById(R.id.fragment2);
relativeLayout = this.findViewById(R.id.relativeLayout);
fragment1.setOnClickListener(this);
fragment2.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.fragment1:
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.replace(R.id.relativeLayout , new fragment1());
ft.commit();
break;
case R.id.fragment2:
FragmentManager fm2 = getSupportFragmentManager();
FragmentTransaction ft2 = fm2.beginTransaction();
ft2.replace(R.id.relativeLayout , new fragment2());
ft2.commit();
break;
}
}
}
Main Activity XML:
<?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"
tools:context=".MainActivity"
android:id="#+id/relativeLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/layout"
android:background="#F00"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment 1"
android:id="#+id/fragment1"
android:gravity="center"
android:layout_gravity="center"/>
<Button
android:id="#+id/fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="fragment 2" />
</LinearLayout>
</RelativeLayout>
fragment1 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#0F0">
</android.support.constraint.ConstraintLayout>
fragment2 XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00F">
</android.support.constraint.ConstraintLayout>
This is Main Activity: enter image description here
and when click on fragment1: enter image description here
Plz Help Me :)
TNX
Add a framelayout in relativelayout before the linearlayout and inflate the fragment in the framelayout.
<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"
tools:context=".MainActivity"
android:id="#+id/relativeLayout">
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/linearLayout"
android:background="#F00"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment 1"
android:id="#+id/fragment1"
android:gravity="center"
android:layout_gravity="center"/>
<Button
android:id="#+id/fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="fragment 2" />
</LinearLayout>
</RelativeLayout>
I think you should change your Fragment1 XML and Fragment2 XML from Constraint Layout to LinearLayout. It should work else initiate your LineaLayout in your MainActivity.
You are inflating fragments on Parent Layout. Make two child of Relativelayout and inflate fragment one of them like this:
<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"
tools:context=".MainActivity"
android:id="#+id/relativeLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:id="#+id/linearLayout"
android:background="#F00"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment 1"
android:id="#+id/fragment1"
android:gravity="center"
android:layout_gravity="center"/>
<Button
android:id="#+id/fragment2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="fragment 2" />
</LinearLayout>
<!-- inflate fragments on FrameLayout -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayout">
</FrameLayout>
</RelativeLayout>
Inflate your fragments on this FrameLayout
....Hope this helps :)

Height MapFragment Layout not work

I'm build an app that contain two tabhost (top and botton) and in one tab, I want to put a Google Map through the Google API. All works well but the map does not display correctly. You can see better in this picture:
My code is:
fragment_mapa.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/map_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment"/>
</RelativeLayout>
MapaFragment.java
public class MapaFragment extends Fragment {
public MapaFragment(){}
public GoogleMap mapa;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_mapa, container, false);
if (container != null) {
container.removeAllViews();
}
GoogleMap map = ((SupportMapFragment) getActivity().getSupportFragmentManager()
.findFragmentById(R.id.map)).getMap();
((MainActivity) getActivity()).setBar();
//Asigno el nombre de la vista e integro tabhost-sliding
((MainActivity) getActivity()).setTitle("Mapa");
((MainActivity) getActivity()).integrationMenu();
return rootView;
}
public void onDestroyView() {
super.onDestroyView();
Log.d("DESTROY", "onDestroyView");
Fragment f = getActivity()
.getSupportFragmentManager().findFragmentById(R.id.map);
if (f != null) {
getActivity().getSupportFragmentManager()
.beginTransaction().remove(f).commit();
}
}
}
And my activity_main.xml that contain the two tabhost and a framelayout that call the fragments:
<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">
<!-- Estructura de los dos tabhost -->
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- TABHOST SUPERIOR -->
<RelativeLayout
android:id="#+id/l1"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:gravity="right"
android:layout_above="#+id/l2"
android:layout_alignParentTop="true">
<android.support.v4.app.FragmentTabHost
android:id="#+id/tabhost_sup"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:layout_weight="0"/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0"/>
</ScrollView>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</RelativeLayout>
<!-- TABHOST INFERIOR -->
<RelativeLayout
android:id="#+id/l2"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dip"
android:layout_weight="1"
android:gravity="right"
android:layout_alignParentBottom="true"
android:background="#android:color/background_light" >
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/RelativeLayout1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:layout_marginBottom="0dp"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</RelativeLayout>
</RelativeLayout>
<!-- Listview to display slider menu -->
<ListView
android:id="#+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
<ListView
android:id="#+id/right_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:choiceMode="singleChoice"
android:divider="#color/list_divider"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector"
android:background="#color/list_background"/>
</android.support.v4.widget.DrawerLayout>
Removing scrollview is not convinient solution.Instead of it try android:FillViewPort="True" for Scrollview in your layout solve your problem for all screen where you may require scroll for your activity content.Hope this helps you!!
SOLUTION (by Hardik):
Remove the scrollview in my activity_main.xml and it works well.
Use hierarchiviewer of android tools to check layout values.
For the future:
If you have a ScrollView then elements must specify their height (width for horizontal scroll view) otherwise what height should they have, infinite?
I also had an issue where the exact thing happened when placing the map fragment inside a Relative Layout that had height = Wrap_Content, with minimum height.
Setting a constant height resolved the issue, same idea.
came across this same issue but my map fragment is inside a scrollview and it needs to have a scrollview since there are also other views aside from the map. if it only contains the map, then yes scrollview is not needed.

Add 2 of the Same Fragments to the Same Container

I'm trying to, as you can see in the title, add 2 of the same fragments to the same container. I'm using a fragmentransaction to add my fragment. It works the first time, but when I try to add a second fragment, the fragment is replaced.
Here's my addition code:
public void addNewCategoryFragment(Category c) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
CategoryFragment categoryFragment = new CategoryFragment(c.name);
fragmentTransaction.add(R.id.fragment_root, categoryFragment, String.valueOf(c.getID()));
fragmentTransaction.commit();
}
Here's the xml that I'm trying to add the fragment to:
<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:background="#drawable/school"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/fragment_root"
android:layout_width="match_parent"
android:layout_height="413dp"
android:orientation="vertical" >
</LinearLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/submit_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:text="Submit" />
<Button
android:id="#+id/new_category_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|left"
android:onClick="createNewCategory"
android:text="New Category" />
</FrameLayout>
at a glance the java looks fine, it could be the layout. without knowing the layout of your fragment it's hard to say but my guess is it is just being covered up. Try putting a ScrollView around them or setting isScrollContainer to true like this:
<LinearLayout
android:id="#+id/fragment_root"
android:layout_width="match_parent"
android:layout_height="413dp"
android:orientation="vertical"
android:isScrollContainer="true">
</LinearLayout>
i'd also replace the "fill_parent" values with "match_parent" in the rest of the layout because fill_parent is deprecated

Categories

Resources