This question already has answers here:
Android: how to load fragment into FrameLayout
(3 answers)
Closed 4 years ago.
I have created a bottom navigation, when icon is clicked fragment does not load on frame layout instead shows on bottom navigation
Here's the MainActivity.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:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_drawer"
tools:context="com.safarpar.safarpar.MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.BottomNavigationView
android:id="#+id/main_nav"
android:layout_width="match_parent"
android:layout_height="#dimen/_56sdp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
app:menu="#menu/bottom_nav_menu">
</android.support.design.widget.BottomNavigationView>
<FrameLayout
android:id="#+id/main_frame"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/main_nav">
</FrameLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/nav_view"
android:layout_gravity="start">
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
And the MainActivity.java
mFrame=(FrameLayout)findViewById(R.id.main_frame);
mbottomNavigationView=(BottomNavigationView)findViewById(R.id.main_nav);
mDrawerLayout=(DrawerLayout)findViewById(R.id.main_drawer);
mToggle = new
ActionBarDrawerToggle(this,mDrawerLayout,R.string.open,R.string.close);
//navigationdrawer
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//OnclickHandeler();
homeFragment= new HomeFragment();
bookingFragment= new BookingFragment();
mbottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.nav_home:
setFragment(homeFragment);
return true;
case R.id.nav_booking:
setFragment(bookingFragment);
default:
return true;
}
}
private void setFragment(android.support.v4.app.Fragment Fragment) {
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_nav,Fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
});
And the Fragment.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="com.safarpar.safarpar.HomeFragment">
<LinearLayout
android:id="#+id/Linear1"
android:layout_width="match_parent"
android:layout_height="#dimen/_200sdp"
android:background="#drawable/front_slider"
android:gravity="top"
android:orientation="horizontal">
</LinearLayout>
<LinearLayout
android:id="#+id/linear_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/Linear1"
android:layout_marginTop="#dimen/_10sdp"
android:orientation="horizontal"
android:padding="#dimen/_10sdp">
<Button
android:id="#+id/main_flight"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:layout_marginLeft="#dimen/_19sdp"
android:layout_marginRight="#dimen/_7sdp"
android:background="#drawable/flight_button_state" />
<Button
android:id="#+id/main_hotel"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:layout_marginLeft="#dimen/_10sdp"
android:layout_marginRight="#dimen/_7sdp"
android:background="#drawable/hotel_button_state" />
<Button
android:id="#+id/main_bus"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:layout_marginLeft="#dimen/_7sdp"
android:layout_marginRight="#dimen/_10sdp"
android:background="#drawable/bus_button_state" />
<Button
android:id="#+id/main_cab"
android:layout_width="#dimen/_50sdp"
android:layout_height="#dimen/_50sdp"
android:layout_marginLeft="#dimen/_7sdp"
android:layout_marginRight="#dimen/_10sdp"
android:background="#drawable/cab_button_state" />
</LinearLayout>
</RelativeLayout>
Change the code for setting the Fragment like the following.
private void setFragment(android.support.v4.app.Fragment Fragment) {
android.support.v4.app.FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.main_frame,Fragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();
}
You need to use main_frame instead of main_nav here.
Related
I have two fragments, WishlistFragment and GoShoppingFragment and a button "Continue Shopping". On button click i want WishlistFragment replaced with GoShoppingFragment.
This is implementation of onClick.
public void onClickShopNow() {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = new GoShoppingFragment();
fragmentManager
.beginTransaction()
.replace(R.id.container, fragment)
.addToBackStack(null)
.commit();
if(fragmentManager.getBackStackEntryCount() > 0) {
fragmentManager.popBackStackImmediate();
}
}
Issue here is, when I click on button "Continue shopping" then WishlistFragment gets relaced GoShoppingFragment but I get output like this. WishlistFragment remains in the background. How do I solve this issue?
GoShopping layout:
<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=".ui.store.goshopping.GoShoppingFragment">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/swipe_refresh_go_shopping"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_go_shopping"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible" />
<LinearLayout
android:id="#+id/moreLoading"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#color/transparent"
android:gravity="center"
android:orientation="horizontal"
android:visibility="gone">
<ProgressBar
android:id="#+id/moreLoadingIndicator"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:text="#string/label_loading_more" />
</LinearLayout></RelativeLayout>
Wishlist layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.launchbyte.appio.ui.store.mywishlist.MyWishlistFragment">
<LinearLayout
android:id="#+id/layout_continue_shopping"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="#+id/txt_wishlist_empty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:text="#string/wishlist_empty" />
<TextView
android:id="#+id/txt_wishlist_add_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="8dp"
android:text="#string/wishlist_add_items" />
<Button
android:id="#+id/button_continue_shopping"
android:layout_width="150dp"
android:layout_height="48dp"
android:layout_gravity="center"
android:layout_marginTop="24dp"
android:background="#drawable/primary_color_button_selector"
android:text="#string/continue_shopping" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_my_wishlist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fadeScrollbars="true"
android:visibility="gone" />
<ProgressBar
android:id="#+id/progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="visible" />
</RelativeLayout></FrameLayout>
set background of your fragment and setClickable(true) inside each of your fragment xml.
Setting background color to White in parent layout fixed the issue.
Remove this :
if(fragmentManager.getBackStackEntryCount() > 0) {
fragmentManager.popBackStackImmediate();
}
Also in your GoShopping layout: in root tag add:
<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"
android:clickable="true"
android:background="?android:attr/windowBackground"
tools:context=".ui.store.goshopping.GoShoppingFragment">
public void onClickShopNow() {
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = new GoShoppingFragment();
Fragment WishlistFragment =
fragmentManager.getFragmentByTag("TagName");
fragmentManager
.beginTransaction()
.remove(WishlistFragment)
.commit();
fragmentManager
.beginTransaction()
.add(R.id.container, fragment,"TagHere")
.commit();
}
I hope this help you..
I have a framelayout in which there are several child, but when I replace it with fragment it doesn't replace it instead it overlaps with the framelayout xml file. I have read some documentation but I'm unable find the right one.
a small help would be great. Thank you!
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_notification) {
Fragment fragment = NotificationFragment.newInstance();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.replace(R.id.content_frame, fragment).commit();}
return super.onOptionsItemSelected(item);
}
xml file:
<?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:background="#bb4297f2"
android:id="#+id/content_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.bulletin.theinvincible.nautical.MainActivity"
tools:showIn="#layout/app_bar_main">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:id="#+id/hello"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Hi Ashu, What does your yacht need today?"
android:textSize="25sp" />
<Button
android:background="#drawable/button_round"
android:id="#+id/button_bookslip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Book a Slip" />
<Button
android:id="#+id/button_hireCaptain"
android:background="#drawable/button_round"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hire a Captain" />
<Button
android:id="#+id/button_hireCrewMember"
android:background="#drawable/button_round"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Hire a Crew Member" />
<Button
android:id="#+id/button_more"
android:background="#drawable/button_round"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="More..." />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginLeft="50dp"
android:text="Provide Services to Boat Owners? Get Work Here" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
Try the below code:
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_notification) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction().replace(R.id.content_frame, new NotificationFragment()).commit();
return super.onOptionsItemSelected(item);
}
Hope this helps.
I just started to use fragments but I have problems to use them correctly.
This is my case. I'm using this BottomBar with some items, when user clicks on items fragments change. That's ok, but I can't set a match_parent on the second card's height. I'd like to use all the remaining space.
This is the result:
This is code in my CDetails.java where I use the BottomBar and the FragmentManager:
// BottomBar
mBottomBar = BottomBar.attach(CustomersDetails.this, savedInstanceState);
//mBottomBar.hideShadow();
mBottomBar.noNavBarGoodness();
mBottomBar.noResizeGoodness();
//mBottomBar.noScalingGoodness();
mBottomBar.noTabletGoodness();
//mBottomBar.useFixedMode();
mBottomBar.setItems(R.menu.bottom_navigation_customers);
mBottomBar.setOnMenuTabClickListener(new OnMenuTabClickListener() {
#Override
public void onMenuTabSelected(#IdRes int menuItemId) {
if (menuItemId == R.id.bottomBarCustomerNotes) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
CustomersFragmentsNotes notes = new CustomersFragmentsNotes();
fragmentTransaction.replace(R.id.fragment_container, notes);
fragmentTransaction.commit();
}
if (menuItemId == R.id.bottomBarCustomerTickets) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
CustomersFragmentsTickets tcks = new CustomersFragmentsTickets();
fragmentTransaction.replace(R.id.fragment_container, tcks);
fragmentTransaction.commit();
}
if (menuItemId == R.id.bottomBarCustomerContracts) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
CustomersFragmentContracts cntrs = new CustomersFragmentContracts();
fragmentTransaction.replace(R.id.fragment_container, cntrs);
fragmentTransaction.commit();
}
if (menuItemId == R.id.bottomBarCustomerContacts) {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
CustomersFragmentContacts cnts = new CustomersFragmentContacts();;
fragmentTransaction.replace(R.id.fragment_container, cnts);
fragmentTransaction.commit();
}
}
#Override
public void onMenuTabReSelected(#IdRes int menuItemId) {
}
});
//Setting colors
mBottomBar.mapColorForTab(0, "#F44336");
mBottomBar.mapColorForTab(1, "#7E57C2");
mBottomBar.mapColorForTab(2, "#5C6BC0");
mBottomBar.mapColorForTab(3, "#42A5F5");
This is my activity_cdetails.xml (the container):
<?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/left_drawer_activity_cdetails"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/right_drawer_activity_cdetails"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/sfondo_sfumato"
>
<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"
>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay"
android:id="#+id/appbar_activity_cdetails">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_activity_cdetails"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay" />
</android.support.design.widget.AppBarLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginTop="55dp"
>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
card_view:cardCornerRadius="5dp"
card_view:cardBackgroundColor="#FFFFFF"
card_view:cardElevation="7dp"
>
<!--<ScrollView-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent">-->
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:weightSum="1"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/title_cdetails"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="Title"
android:textSize="22sp"
android:typeface="serif"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:textSize="18sp"
android:layout_below="#id/title_cdetails"
android:id="#+id/cdetails_nome"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_below="#id/cdetails_nome"
android:text="New Text"
android:textSize="18sp"
android:id="#+id/cdetails_città "
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_below="#id/cdetails_città "
android:textSize="18sp"
android:id="#+id/cdetails_indirizzo"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:layout_below="#id/cdetails_indirizzo"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:textSize="18sp"
android:id="#+id/cdetails_provincia"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:layout_below="#id/cdetails_provincia"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:textSize="18sp"
android:id="#+id/cdetails_phone"
android:autoLink="phone"
/>
</RelativeLayout>
<!--</ScrollView>-->
</android.support.v7.widget.CardView>
<!-- Fragment Container -->
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
</LinearLayout>
</LinearLayout>
</ScrollView>
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view_right_activity_cdetails"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_right_c"
app:menu="#menu/menu_right_c" />
</android.support.v4.widget.DrawerLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view_left_activity_cdetails"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/menu_left_drawer" />
</android.support.v4.widget.DrawerLayout>
And this is activity_c_fragment_notes.xml (first fragment):
<?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.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="10dp"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
card_view:cardCornerRadius="5dp"
card_view:cardBackgroundColor="#B2EBF2"
card_view:cardElevation="7dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:text="This is Notes"
/>
</android.support.v7.widget.CardView>
</LinearLayout>
Could anyone please tell me where I'm wrong?
Thanks in advance.
Edit
RRR solution works only on the default selected item, the first fragment. If I change item in the BottomBar second card's layout is the same as the pic I posted. Any other ideas?
use android:fillViewport=true for your ScrollView inside CoordinatorLayout
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
fillViewPort defines whether the scrollview should stretch its content
to fill the viewport.
source
I decided to use fragments as an alternative to activities, so I cut up my activity_main.xml into 2 different XML files: activity_main.xml and fragment_main.xml. The problem is that the relativeLayout previously in activity_main that I was referencing in java is no longer working after I moved it to the other XML file. It looks like you need to set the content view of whatever XML file you're using in order to findViewByID, but filling in my graph with barchart data might require both; the fragment_main.xml needing to be set as my content view (it's the blank that I'm filling in with other fragments) and the activity_main needing to be set to display it as a part of my main screen. The fSetGraph(); is using this graph: https://github.com/PhilJay/MPAndroidChart
Out of curiousity: do layout inflaters come into play anywhere in here?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawer;
private Toolbar toolbar;
private RecyclerView recyclerView;
private NavigationView nvDrawer;
FloatingActionButton fab;
FragmentManager manager = getSupportFragmentManager();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
fSetGraph();
fsetFAB();
// setContentView(R.layout.activity_main);
// fSetToolBar();
// fSetDrawer();
// fSetDrawerContent();
// fsetInitialFragment();
}
private void fSetGraph() {
.
. //Lots of code here about setting the graph. Not important.
.
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayoutForChart);
rl.addView(MainActivity.barChartGlobal,
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
}
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"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/linearLayoutMainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<!-- The ActionBar -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/relativeLayoutActivity"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#FFFFFF">
<!--Fragments placed here-->
</RelativeLayout>
</LinearLayout>
<!-- The navigation drawer -->
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/drawer_header">
<!--<include-->
<!--layout="#layout/drawer_header"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="160dp"/>-->
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
fragment_main.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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Here on down is the fragment I'm using in activity_main.xml-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:id="#+id/relativeLayoutForChart"
android:layout_above="#+id/linearLayoutForCenterReference"
android:layout_below="#+id/linearLayoutHeader">
<com.github.mikephil.charting.charts.BarChart
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/chart"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="false" />
</RelativeLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/linearLayoutForCenterReference">
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/linearLayoutLabels"
android:gravity="center_vertical|center_horizontal"
android:layout_alignTop="#+id/linearLayoutForCenterReference"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false"
android:layout_centerHorizontal="true"
android:layout_toRightOf="#+id/relativeLayoutSchedule"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="tv1"
android:id="#+id/textView1"
android:textSize="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="tv2"
android:id="#+id/textView2"
android:textSize="15dp"
android:layout_marginLeft="52dp"
android:layout_marginRight="52dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="tv3"
android:id="#+id/textView3"
android:textSize="15dp" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:id="#+id/linearLayoutHeader"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true"
android:gravity="center_horizontal">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="One"
android:id="#+id/textViewOne"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/textViewState"
android:layout_toStartOf="#+id/textViewState" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Two"
android:id="#+id/textViewTwo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="50dp"
android:layout_marginRight="40dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Three"
android:id="#+id/textViewThree"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textViewState"
android:layout_toEndOf="#+id/textViewState" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayoutLabels"
android:layout_centerHorizontal="true"
android:id="#+id/linearLayoutActivityFeed"
android:gravity="center_vertical|center_horizontal"
android:paddingTop="0dp"
android:layout_marginTop="10dp"
android:layout_marginBottom="3dp">
<ImageView
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/imageView2"
android:src="#drawable/ic_assignment_black_24dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Activity Feed"
android:id="#+id/textViewActivityFeed"
android:layout_marginBottom="1dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/linearLayoutActivityFeed"
android:layout_centerHorizontal="true"
android:id="#+id/relativeLayoutForTabs">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:gravity="right">
<android.support.design.widget.TabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_scrollFlags="scroll|enterAlways"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1"
android:background="#android:color/white">
</android.support.v4.view.ViewPager>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_add_black_24dp"
android:layout_marginBottom="20dp"
android:layout_marginRight="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/drawer_recyclerView"
android:layout_width="match_parent"
android:layout_gravity="start"
android:layout_height="match_parent"
android:background="#FFFFFF">
</android.support.v7.widget.RecyclerView>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayoutForCenterReference"
android:layout_above="#+id/linearLayoutActivityFeed"
android:layout_alignParentStart="false"
android:layout_alignParentEnd="false"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="false"
android:id="#+id/relativeLayoutSchedule"
android:layout_alignParentTop="false"
android:gravity="center_vertical"
android:layout_alignTop="#+id/linearLayoutLabels"
android:layout_marginLeft="15dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/imageViewSchedule"
android:src="#drawable/ic_event_black_24dp"
android:contentDescription="Event Icon" />
</RelativeLayout>
</RelativeLayout>
Trying to run that ending part of fSetGraph() with the contentView set to activity_main instead of fragment_main (which the graph is in) gives me the error:
java.lang.RuntimeException: Unable to start activity ComponentInfo{...MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.RelativeLayout.addView(android.view.View, android.view.ViewGroup$LayoutParams)' on a null object reference
Nav Drawer code
switch (menuItem.getItemId()) {
case R.id.navigation_item_log_in:
DialogPopupSignInFragment alertDialogSignInCustom = new DialogPopupSignInFragment();
alertDialogSignInCustom.show(manager, "DialogSignIn");
break;
case R.id.navigation_item_home:
FragmentMain fragmentMain = new FragmentMain();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.relativeLayoutActivity, fragmentMain, "Home");
transaction.commit();
setTitle("App Home");
break;
case R.id.my_stats:
FragmentMyStats fragmentMyStats = new FragmentMyStats();
FragmentTransaction transactionMyStats = manager.beginTransaction();
transactionMyStats.replace(R.id.relativeLayoutActivity, fragmentMyStats , "MyStats");
transactionMyStats.commit();
setTitle("My Stats");
break;
case R.id.navigation_item_winners:
FragmentWinners fragmentWinners = new FragmentWinners();
FragmentTransaction transactionWinners = manager.beginTransaction();
transactionWinners.replace(R.id.relativeLayoutActivity, fragmentWinners, "Winners");
transactionWinners.commit();
setTitle("Winners");
break;
case R.id.navigation_item_settings:
FragmentSettings fragmentSettings = new FragmentSettings();
FragmentTransaction transactionSettings = manager.beginTransaction();
transactionSettings.replace(R.id.relativeLayoutActivity, fragmentSettings, "Settings");
transactionSettings.commit();
setTitle("Settings");
break;
case R.id.navigation_item_about:
FragmentAbout fragmentAbout = new FragmentAbout();
FragmentTransaction transactionAbout = manager.beginTransaction();
transactionAbout.replace(R.id.relativeLayoutActivity, fragmentAbout, "About");
transactionAbout.commit();
setTitle("About");
break;
}
Setting up my Drawer in MainActivity
private void fSetDrawer() {
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.app_name, R.string.app_name);
mDrawer.setDrawerListener(drawerToggle);
drawerToggle.syncState();
}
Navigation Drawer XML from activity_main
<android.support.design.widget.NavigationView
android:id="#+id/nvView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:menu="#menu/drawer_view"
app:headerLayout="#layout/drawer_header">
<!--<include-->
<!--layout="#layout/drawer_header"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="160dp"/>-->
</android.support.design.widget.NavigationView>
Navigation Drawer Menu
<group
android:checkableBehavior="single">
<item
android:id="#+id/navigation_item_log_in"
android:icon="#drawable/ic_person_black_24dp"
android:title="Log In">
</item>
<item
android:id="#+id/navigation_item_home"
android:icon="#drawable/ic_home_black_24dp"
android:title="Home">
</item>
<item
android:id="#+id/my_stats"
android:icon="#drawable/ic_developer_board_black_24dp"
android:title="My Stats">
</item>
<item
android:id="#+id/navigation_item_winners"
android:icon="#drawable/ic_wb_iridescent_black_24dp"
android:title="Winners">
</item>
<item
android:id="#+id/navigation_item_settings"
android:icon="#drawable/ic_settings_black_24dp"
android:title="Settings">
</item>
<item
android:id="#+id/navigation_item_about"
android:icon="#drawable/ic_local_library_black_24dp"
android:title="About">
</item>
<!--Recycler View-->
</group>
Yes its to do with layout inflaters. What is happening is when you use findViewById, the activity ONLY checks the layout in its setContectView which in your case is activity_main. Because of this the activity is unable to find r1. The solution to this is the inflate r1 in a fragment and then reference it by your main activity.
import android.support.v4.app.Fragment; //Edit : make sure its this
public class MapFragment extends Fragment{
View v //EDIT
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
v=inflater.inflate(R.layout.fragment_main, container, false);
RelativeLayout rl = (RelativeLayout) v.findViewById(R.id.relativeLayoutForChart); //EDIT
rl.addView(barchart,
new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
return v;
}
Make a new class with this in it. Then in your main activity type in this code
FragmentTransaction ft = ((FragmentActivity)mContext).getSupportFragmentManager().beginTransaction();
MapFragment mapFragment=new MapFragment();
ft.replace(R.id.fragment_map, placeOrderFragment).addToBackStack(null).commit();
In your main_activity xml, add a with id of map_fragment.
Basically the flow will be your map will be put into the fragment xml, this will then be inflated by the fragment class, this will then be used by the main activity.
EDIT : Tweaked code. Do have a look. When a layout is being inflated, to find view groups within that layout, it has to passed as well along with findViewById so a small change is needed. Also when you define the fragment, make sure you import the v4. one so its backward compatible.
I have been working on adding the new appcompat-v7:21.0.0 to my project. For some reason, my overflow menu on my actionbar decides to overlap my actionbar when opening. It should be opening below the actionbar.
Here is my code for base layout:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"/>
<RelativeLayout
android:id="#+id/top_status"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:layout_gravity="top|center"
android:background="#color/message_bottom_background"
android:gravity="center_horizontal"
tools:ignore="UselessParent"
android:visibility="gone">
<ImageView
android:id="#+id/top_warning_thumb"
android:src="#drawable/ic_action_warning"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:contentDescription="#string/top_warning_thumb_desc"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:visibility="gone"/>
<TextView
android:id="#+id/top_warning_text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:text="#string/test_text_shortest"
android:layout_toEndOf="#id/top_warning_thumb"
android:layout_toRightOf="#id/top_warning_thumb"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:textSize="20sp"
android:textColor="#color/theme_primary_background"
android:layout_marginTop="1sp"
/>
</RelativeLayout>
</FrameLayout>
<!-- The navigation drawer -->
<LinearLayout
android:id="#+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical"
android:background="#color/card_background">
<TextView
android:id="#+id/left_status"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:textSize="25sp"
android:lineSpacingExtra="5sp"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/test_text_shortest"
android:paddingLeft="0sp"
android:paddingRight="0sp"
android:textColor="#color/card_background"
/>
<ListView android:id="#+id/left_drawer_child"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="#color/transparent"
android:dividerHeight="1dp"
android:paddingLeft="0sp"
android:paddingRight="0sp" />
</LinearLayout>
<!--android:divider="#666"-->
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
Here is my code from my main activity that pertains to menus:
public class MainActivity extends ActionBarActivity implements WifiDiagnosticFragment
.OnFragmentInteractionListener,DispatchFragment.OnFragmentInteractionListener,
TextMessageViewerFragment.OnFragmentInteractionListener,
SupervisorFragment.OnFragmentInteractionListener,
WorklistFragment.OnFragmentInteractionListener, Observer {
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.base_menu, menu);
if (BuildConfig.BUILD_TYPE.equals("debug")){
menu.findItem(R.id.device_admin).setVisible(true);
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Pass the event to ActionBarDrawerToggle, if it returns true,
// then it has handled the app icon touch event
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
// Handle your other action bar items...
switch (item.getItemId()) {
case R.id.help:
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
HelpDialog helpDialog = new HelpDialog();
helpDialog.show(fm,"Help");
return true;
case R.id.device_admin:
fm = getSupportFragmentManager();
AdminDialog adminDialog = new AdminDialog();
adminDialog.show(fm,"Device Admin");
return true;
default:
return super.onOptionsItemSelected(item);
}
//return super.onOptionsItemSelected(item);
}
}
As rciovati said, this is the desired behavior: google.com/design/spec/components/menus.html