Add and replace one fragment with another on button click - android

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..

Related

Issue in replacing content through fragment in naviagtion drawer

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.

OnClickListener and fragments causing application crash

I'm trying to use fragments to programatically change the text on a screen. To do this I'm setting up an on click listener on a text view and then if it's clicked starting a fragment manager, replacing the current fragment with the new fragment. However, this causes my app to crash when it's started.
From reading the crash report it seems like the error is happening at tv1.setOnClickLIstener...
Finally, Android Studio keeps giving me a type mismatch when I use fragment or support fragment. That is why you see android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Java Code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//begin transaction
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
//replace the contents of the container with the new fragment
ft.replace(R.id.placeHolder, new SplashScreenFragment());
ft.commit();
TextView tv1 = (TextView) findViewById(R.id.whatIsHumanTrafficing);
tv1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
android.support.v4.app.FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.placeHolder, new whatIsHumanTrafficing());
ft.commit();
}
});
}
}
XML from activity_main:
<?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"
>
<FrameLayout
android:id="#+id/placeHolder"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
XML displayed before click:
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.piatt.worksafe.MainActivity"
android:id="#+id/splashScreenId"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Work Safe!"
android:textSize="36sp"
android:layout_centerHorizontal="true"
android:paddingBottom="32dp"
android:id="#+id/title"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="What is Human Trafficing?"
android:layout_below="#+id/title"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/whatIsHumanTrafficing"
android:clickable="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How do I get safe labor?"
android:layout_below="#+id/whatIsHumanTrafficing"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/howDoIGetSafeLabor"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How do I check that my job / job offer is legal?"
android:layout_below="#+id/howDoIGetSafeLabor"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:gravity="center"
android:id="#+id/checkLegality"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How can I get help?"
android:layout_below="#+id/checkLegality"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/getHelp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="About us"
android:layout_below="#+id/getHelp"
android:layout_centerHorizontal="true"
android:textSize="16sp"
android:layout_alignParentBottom="true"
android:gravity="bottom"
android:id="#+id/aboutUs"
/>
</RelativeLayout>
XML to be displayed after click on text view:
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Human Trafficing Is:"
android:textSize="36sp"
android:layout_centerHorizontal="true"
android:paddingBottom="32dp"
android:id="#+id/title"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Confiscation of travel documents"
android:layout_below="#+id/HTdescription1"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/HTdescription1"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Unregistered Labor"
android:layout_below="#+id/HTdescription1"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/HDdescription2"
android:layout_gravity="center"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Isolation from friends and family"
android:layout_below="#+id/HTdescription2"
android:layout_centerHorizontal="true"
android:textSize="24sp"
android:padding="16dp"
android:id="#+id/HDdescription3"
android:layout_gravity="center"
/>
</LinearLayout>
Fragment named human trafficking that I'm trying to inflate:
public class whatIsHumanTrafficing extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState){
//inflate the layout for this fragment
return inflater.inflate(R.layout.what_is_human_trafficing, container, false);
}
}
You have used activity_main in your MainActivity as follows
setContentView(R.layout.activity_main);
and you are accessing whatIsHumanTrafficingnamed TextView in the same MainActivity which is not possible as it is not present in activity_main and which is present in another xml. So you are getting NullPointerException there.

Fragment doesn't match_parent

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

Moved Layout- Now findViewByID Can't Use It

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.

How can I display the layout containing the buttons down on the screen

With the layout below the buttons are being shown on top of the page. How can I make them appear below, after the container, down on the page?
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/container"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_vertical|center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/Main"
android:onClick="ClickHomePage"
android:text= "#string/Home"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/map"
android:onClick="ClickClients"
android:text= "#string/Clients"
>
</Button>
</LinearLayout>
</LinearLayout>
The MainActivity looks as follows and the HomePage.xml is also below
public class MainActivity extends ActionBarActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
GetButtonClicked(5);
setContentView(R.layout.activity_main);
}
public void GetButtonClicked(int position)
{
// update the main content by replacing fragments
Fragment fragment = null;
switch (position){
case 0:
fragment= new FirstPageFragment();
break;
default:
fragment = new FirstPageFragment();
break;
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void ClickHomePage(View view){
GetButtonClicked(0);
}
public void ClickClients(View view){
GetButtonClicked();
}
}
<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="com.test.FirstPageFragment" android:background="#drawable/theme">
</FrameLayout>
This is the FirstPage
public class FirstPageFragment extends Fragment {
public FirstPageFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_first_page, container, false);
}
}
Use ralative layout insted of linear.
Make a different layout for bottom buttons, and then at the last of your container layout use this:
<include android:id="#+id/bottomLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
layout="#layout/bottom_buttons"/>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:orientation="horizontal" >
<Button
android:id="#+id/Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickHomePage"
android:text="#string/Home" />
<Button
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickClients"
android:text="#string/Clients" >
</Button>
</LinearLayout>
Try this
Use RelativeLayout with android:layout_alignParentBottom="true" step by step guide is available here.
Your skeleton for layout should look like this
<RelativeLayout
<LinearLayout android:layout_alignParentBottom="true">
<Button
<Button
</LinearLayout>
</RelativeLayout>
Final Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/Main"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickHomePage"
android:text="Home" />
<Button
android:layout_marginLeft="30dp"
android:id="#+id/map"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:onClick="ClickClients"
android:text="Clients" >
</Button>
</LinearLayout>
The issue was that I did not include the container in another 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="com.test.MainPageActivityActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/container">
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_alignParentTop="#+id/container">
and insert buttons layout here....
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:id="#+id/Main"
android:onClick="ClickHomePage"
android:text= "#string/Home"
/>

Categories

Resources