I have a fragment that I would like to display on top of an activity. This is the code that I'm trying right now...
MainActivity.class
FragmentManager fragmentManager = getSupportFragmentManager();
DirectionsFragment directionsFragment = new DirectionsFragment();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.setCustomAnimations(android.R.anim.fade_in, android.R.anim.fade_out);
fragmentTransaction.add(R.id.directions_fragment_framelayout, directionsFragment);
fragmentTransaction.commit();
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<!-- irrelevant parameters -->
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/directions_fragment_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<com.mapbox.mapboxsdk.maps.MapView>
<!-- A BUNCH OF CHILDREN -->
</com.mapbox.mapboxsdk.maps.MapView>
</RelativeLayout>
DirectionsFragment.class
public class DirectionsFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup viewGroup = (ViewGroup) inflater.inflate(R.layout.fragment_directions, container, false);
return viewGroup;
}
}
fragment_directions.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
<!-- irrelevant params -->
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/directions_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
<!-- irrelevant params -->
/>
</android.support.constraint.ConstraintLayout>
The activity itself runs fine; when I trigger the fragment to show, it runs through the FragmentTransaction and fragment view inflation fine (confirmed using logs and breakpoints). Since the fragment is being added to a FrameLayout, why isn't the fragment showing up? Thanks in advance!
I think it will be helpfull
Try to swap your FrameLayout and MapView in activity_main.xml
Your code is working fine but its behind the map view thats why its not showing.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
<!-- irrelevant parameters -->
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.mapbox.mapboxsdk.maps.MapView>
<!-- A BUNCH OF CHILDREN -->
</com.mapbox.mapboxsdk.maps.MapView>
<FrameLayout
android:id="#+id/directions_fragment_framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
Add background color in your fragment layout because fragment loaded but transparent layout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:background="#android:color/white"
android:layout_height="match_parent">
<ListView
android:id="#+id/directions_listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
<!-- irrelevant params -->
/>
</android.support.constraint.ConstraintLayout>
Related
I added navigation drawer on map activity. Now I'm trying to add a fragment on my home activity, but fragment is not showing in activity, but code in fragment class is working.
Is my FrameLayout hiding behind the map fragment?
I'm very new to fragments.
Please help me.
Home class layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:map="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:id="#+id/constrain_layout"
android:orientation="vertical">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
map:layout_constraintBottom_toBottomOf="parent"
map:layout_constraintEnd_toEndOf="parent"
map:layout_constraintStart_toStartOf="parent"
map:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
map:layout_constraintBottom_toBottomOf="parent"
map:layout_constraintEnd_toEndOf="parent"
map:layout_constraintHorizontal_bias="0.0"
map:layout_constraintStart_toStartOf="parent"
map:layout_constraintTop_toTopOf="parent"
map:layout_constraintVertical_bias="0.0"
tools:context=".HomeActivity" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment class
public class RideHistory extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Toast.makeText(getActivity(), "Ride History", Toast.LENGTH_SHORT).show();
View view = inflater.inflate(R.layout.fragment_ride_history,container,false);
return view;
}
}
Fragment xml
<?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=".Fragments.RideHistory">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Ride History" />
</FrameLayout>
Change layout constraints so that both widgets frame layout and map fragment do not overlap eachother.
for ex:
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
map:layout_constraintBottom_toTopOf="#+id/map"
map:layout_constraintEnd_toEndOf="parent"
map:layout_constraintStart_toStartOf="parent"
map:layout_constraintTop_toTopOf="parent" />
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="434dp"
android:layout_height="297dp"
map:layout_constraintBottom_toBottomOf="parent"
map:layout_constraintEnd_toEndOf="parent"
map:layout_constraintHorizontal_bias="0.0"
map:layout_constraintStart_toStartOf="parent"
tools:context=".HomeActivity" />
I have this issue: when I change fragment the TextView below remain and the result is 2 TextView.
I explain better:
I have one TextView with a string "hello word"
when I change fragment I want to change the entire page and set another TextView but the result is 2 TextVIew
thanks a lot!
activityMain.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"/>
</android.support.v4.widget.DrawerLayout>
fragment.xml:
<?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:gravity="center"
android:background="#5ba4e5"
android:layout_height="match_parent"
android:id="#+id/fragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40px"
android:textColor="#ffffff"
android:layout_gravity="center"
android:id="#+id/fragmentText"
/>
</LinearLayout>
this in MainActivity for change the fragment:
private void selectItem(int id) {
// update the main content by replacing fragments
Fragment fragment = new MyFragment();
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
}
and this is MyFragment.java:
public class MyFragment extends Fragment {
public MyFragment() {
// Empty constructor required for fragment subclasses
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment, container, false);
// String planet = getResources().getStringArray(R.array.planets_array)[i];
String planet = "prova fragment";
int imageId = getResources().getIdentifier(planet.toLowerCase(Locale.getDefault()),
"drawable", getActivity().getPackageName());
((TextView) rootView.findViewById(R.id.fragmentText)).setText(planet);
return rootView;
}
}
i use:
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
but content_frame is a FrameLayout and the fragment is a LinearLayout so I change content_frame with the id of a LinearLayout in the content_main.xml and it works.
I am currently working on adding tabs into my activity. For the same, I am using the Google SlidingTabLayout and SlidingTabStrip.
My fragment xml looks like this:
<?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" > >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textViewTab" />
</RelativeLayout>
My contents xml looks like this:
<?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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.musicisfun.allaboutfootball.MainActivity"
tools:showIn="#layout/activity_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout
android:layout_width="match_parent"
android:id="#+id/tabs"
android:layout_height="wrap_content">
</com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
/>
</LinearLayout>
</RelativeLayout>
and my code looks like this:
public static class TabFragment extends Fragment{
public static TabFragment getInstance(int position){
TabFragment tabFragment = new TabFragment();
Bundle bundle = new Bundle();
bundle.putInt("site",position);
tabFragment.setArguments(bundle);
return tabFragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.fragment_tab,container,false);
TextView textViewTab = (TextView) layout.findViewById(R.id.textViewTab);
Bundle bundle = getArguments();
if (bundle!=null){
textViewTab.setText("Current Tab is" + bundle.getInt("site"));
}
return layout;
}
}
My fragment adaptor looks like this:
class TabPagerAdaptor extends FragmentPagerAdapter{
String[] tab_names;
public TabPagerAdaptor(FragmentManager fm) {
super(fm);
tab_names=getResources().getStringArray(R.array.feed_names);
}
#Override
public Fragment getItem(int position) {
TabFragment tabFragment = TabFragment.getInstance(position);
return tabFragment;
}
#Override
public CharSequence getPageTitle(int position) {
return tab_names[position];
}
#Override
public int getCount() {
return 2;
}
}
and this is how i am invoking the tabs:
mViewPager= (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(new TabPagerAdaptor(getSupportFragmentManager()));
mTabs = (SlidingTabLayout) findViewById(R.id.tabs);
mTabs.setViewPager(mViewPager);
But when I run the code, I can't find the textview being shown even though two tabs are working fine.
You cannot view your textView because the height of the ViewPager is 0dp in your xml -
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0dp"
/>
you have to change that to either wrap_content or match_parent-
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
Also, Your LinearLayout's orientation is horizontal and the width of SlidingTabLayout is match_parent. SlidingTabLayout is occupying whole screen width leaving no room to display Viewpager.
Your LinearLayout's orientation should be vertical not horizontal
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout
android:layout_width="match_parent"
android:id="#+id/tabs"
android:layout_height="wrap_content">
</com.example.musicisfun.allaboutfootball.tabs.SlidingTabLayout>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
i am very new to Android Developing, have been developing mainly for ios for several years now but having problems in my first android app.
I am developing an app where at the beginning you have log in, so i created the UI and the login button and so far that works great, the app is able to connect to the database a download the necessary information for it to work via xml.
The downloaded Xml gives the items in the navigation drawer their names,
Basically all of the navigation drawer items should lead you to the same fragment, the content changes when loading the contents of the downloaded xml to it, but the window remains the same.
So in order to do this i created the navigation drawer items programmatically, added the ui for the fragment in a new xml.
When running the app everything works great, once you are logged in you are able to open the NavigationDrawer by swiping right, the right Items and names appear, but when clicking an item... nothing happens.
I used LOGE to know if the code was working and witch menu item was trying to open and it does return always the right menu item etc.
If there is a code in the fragment it executes the code and returns what its expected, but its simply not visible.
Here is my onNavigationItemSelected in MainActivity.
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
String title = item.getTitle().toString();
int order = (int) getMenuItemTag(item);
if (title != "Modos Personales" && title != "Home") {
Log.e("onNavigationItem: ", Integer.toString(order));
Fragment fragment = null;
Class fragmentClass;
fragmentClass = MainFragment.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.flContent, fragment);
fragmentTransaction.commit();
} catch (Exception e) {
e.printStackTrace();
}
//FragmentManager fragmentManager = getFragmentManager();
//android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
//fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
}
Here is the XML of my main activity "activity_main.xml"
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>
Here is app_bar_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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"
tools:context="com.paranoidinteractive.heimer.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
android:foreground="#mipmap/logo_menu"
android:foregroundGravity="center">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:theme="#style/Base.Theme.AppCompat.Light"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
Here is the XML of my Fragment "room_view.xml"
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/room_view"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorScreen"
android:gravity="center">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/textView"
android:layout_gravity="center_vertical" />
</LinearLayout>
and here is my fragmet "MainFragment.java"
public class MainFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
int number = 0;
if (inflater != null){
Log.e("INFLATER NO NULL: ", Integer.toString(number));
} else {
Log.e("INFLATER NULL: ", Integer.toString(number));
}
if (container != null){
Log.e("CONTAINTER NO NULL: ", Integer.toString(number));
} else {
Log.e("CONTAINER NULL: ", Integer.toString(number));
}
return inflater.inflate(R.layout.room_view, container, false);
}
}
And if you view the LOGCAT it allways returns:
01-26 16:27:36.771 9259-9259/com.paranoidinteractive.heimer E/INFLATER NO NULL:: 0
01-26 16:27:36.771 9259-9259/com.paranoidinteractive.heimer E/CONTAINER NO NULL:: 0
So i know its working and getting there but its simply not showing it.
I've followed all the tutorials available and read tons of questions and answers here but none of the answers have helped me so far so i Decided to post this question.
Thank you in advance for your help, hope you can assist me with this.
Sorry for the log post!
I am trying to make a welcome activity that has header fragment xml and bottom view fragment xml that contains buttons to login. Can anyone help me how I will put this two fragment xml to my activity_main.xml?
This is my header.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="220dp">
<ImageView
android:id="#android:id/background"
android:layout_width="match_parent"
android:layout_height="220dp"
android:scaleType="centerCrop"
android:src="#drawable/header_bg" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="bottom|right|end"
android:layout_marginStart="#dimen/android_spaces_large"
android:layout_marginEnd="#dimen/android_spaces_large"
android:layout_marginLeft="#dimen/android_spaces_large"
android:layout_marginRight="#dimen/android_spaces_large"
android:layout_marginBottom="#dimen/android_spaces">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/header_title"
style="#style/Header.TitleText" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/header_subtitle"
style="#style/Header.SubTitleText" />
</LinearLayout>
<View
android:id="#android:id/title"
android:layout_width="wrap_content"
android:layout_height="?android:actionBarSize"
android:background="#layout/header_ab_shadow" />
</FrameLayout>
And my fragment_bottomview.xml that contains only a single button.
<?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">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_gravity="center_horizontal" />
</LinearLayout>
And my activity_main.xml looks like this
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.windows81.welcomingwithheader.MainActivity"
tools:ignore="MergeRootFrame" />
I don't know what I am lacking or I am doing wrong, any tutorial link will really help me.
just make two new classes ,lets call them FragmentA and FragmentB and both should extend fragment
than override onCreateView for both fragmentA and fragmentB and set their view to your xml to the header.xml and fragment_bottomview.xml as shown below
public class FragmentA extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle
savedInstanceState) {
View view=inflater.inflate(R.layout.header,container,false);
return view;
}
}
public class FragmentB extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle
savedInstanceState) {
View view;
view=inflater.inflate(R.layout.fragment_bottomview,container,false);
return view;
}
}
and now to add FragmentA and FragmentB into your activity,put this code in your oncreate activity method
FragmentA fragmentA=new FragmentA();
FragmentManager manager=getFragmentManager();
FragmentTransaction trans=manager.beginTransaction();
trans.add(R.id.container,fragmentA,"fragmenta");/*adding fragment into
the
framelayout with id
container*/
FragmentB fragmentB=new FragmentB();
trans.add(R.id.container,fragmentB,"fragmentb");/*adding fragment into
the
framelayout with id
container*/
trans.commit();
and your probably should use relative layout instead of framelayout