Issue in replacing content through fragment in naviagtion drawer - android

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.

Related

Add and replace one fragment with another on button click

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

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

Unable to click the right to left DrawerLayout items

I have an activity that displays a drawer layout when a menu item is clicked thus its direction is from right to left. I manage to display the navigation menu using the following code.
public class Forecast_details extends ActionBarActivity implements OnChartValueSelectedListener {
....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
//return false;
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
if(id==R.id.Options){
if (mDrawerlayout.isDrawerOpen(Gravity.END)){
mDrawerlayout.closeDrawer(Gravity.END);
}else
mDrawerlayout.openDrawer(Gravity.END);
}
return super.onOptionsItemSelected(item);
}
My Navigation drawer contains a static list so I did not bother to make a listview out of it as seen below,
However I cannot click my items inside the Drawer Layout, or anything in the view even v4.widget.DrawerLayout in my xml is unclickable. Below is the two xml file that I used.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
></include>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout_right"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<include layout="#layout/nav_drawer_right" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.36" >
....
</LinearLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
nav_drawer_right.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView_right"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#2c3e50"
android:paddingTop="20dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/container_fragment2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginTop="1dp" >
</FrameLayout>
<LinearLayout
android:id="#+id/linearLayout_menuconatiner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:clickable="true"
android:orientation="vertical"
android:paddingLeft="2dp"
android:paddingRight="2dp" >
<ImageView
android:id="#+id/imageView_forward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:clickable="true"
android:onClick="SendClick"
android:padding="5dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_forward" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_request"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_request" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_reloadvnet"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_updatevnet" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_reloadvsms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_updatevsms" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
<ImageView
android:id="#+id/imageView_feedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/darkblue"
android:padding="5dp"
android:src="#drawable/ic_feedback" />
<View
android:layout_width="fill_parent"
android:layout_height="3dip"
android:background="#color/separatorcolor"
android:paddingLeft="2dp"
android:paddingRight="2dp" />
</LinearLayout>
</RelativeLayout>
</ScrollView>
Please help.. I tried a lot of things to make it work like using a listview instead but I still have no luck.. please do help, Im new at using drawer layout so there may be some more things I need to know.. Thank you in advance..
To prevent return OnClickListener to parent layout require set android:clickable="true" in drawerLayout container.
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout_right"
android:layout_gravity="end"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="2.36" >
....
</ScrollView>
<include layout="#layout/nav_drawer_right" />
</android.support.v4.widget.DrawerLayout>
nav_drawer_right.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView_right"
android:layout_width="100dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#2c3e50"
android:paddingTop="20dp" >
....
</ScrollView>
in your Activity
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
DrawerLayout mDrawerLayout= (DrawerLayout) findViewById(R.id.drawer_layout_right);
ScrollView sV = (ScrollView) findViewById(R.id.scrollView1);
mDrawerLayout.openDrawer(Gravity.RIGHT);
sV.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("drawer click >>>>>>>>>>>>>>>>>>");
}
});
}
}

Nothing displayed on the screen under the toolbar

I have Activiti. There is a toolbar and Fragment. But under the toolbar, this fragment is not shown. Logs all right, a fragment of work, the only problem is only with the display.
This is my code of Activity:
Toolbar toolbar;
public void initToolbar() {
toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
setTitle(R.string.app_name);
toolbar.setBackgroundResource(R.drawable.header);
toolbar.setTitleTextColor(getResources().getColor(android.R.color.white));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
initToolbar();
// Create the AccountHeader
DrawerClass.drawer(this, toolbar);
ProfileFragment profileFragment = new ProfileFragment();
FragmentTransaction transaction = getSupportFragmentManager()
.beginTransaction();
transaction.add(R.id.profile_fragment_layout, profileFragment);
transaction.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_general, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
And this layout of Activity:
<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" tools:context=".GeneralActivity"
android:background="#color/event_background">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/event_background"
android:titleTextColor="#color/text_color"
android:elevation="4dp"
android:minHeight="?attr/actionBarSize"
android:paddingTop="#dimen/tool_bar_top_padding"
android:layout_marginTop="-25dp"
android:transitionName="actionBar">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/profile_fragment_layout"
android:layout_below="#+id/toolbar1">
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
app:elevation="4dp"
android:src="#drawable/pencil"
app:layout_anchor="#id/toolbar1"
app:layout_anchorGravity="bottom|right|end"
app:borderWidth="0dp"/>
</android.support.design.widget.CoordinatorLayout>
If You need it's code of my Fragment:
View view;
#Bind(R.id.dad_img)
CircleImageView dad_image;
#Bind(R.id.mum_img)
CircleImageView mum_image;
public void buildDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Диалог");
builder.setMessage("Выберите действие");
builder.setCancelable(true);
builder.setPositiveButton("Сообщение", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); // Отпускает диалоговое окно
}
});
builder.setNegativeButton("Перевод", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss(); // Отпускает диалоговое окно
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.profile_fragment, container, false);
ButterKnife.bind(this, view);
Log.d("FRAGMENT", "working");
dad_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buildDialog();
}
});
mum_image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
buildDialog();
}
});
return view;
}
And layout of fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/text_color">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
android:id="#+id/profile_school"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/study"
android:gravity="center"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="5">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="16sp"
android:paddingLeft="10dp"
android:text="#string/drawer_second_line"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="16sp"
android:paddingLeft="10dp"
android:layout_marginBottom="10dp"
android:text="#string/nickname" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="#+id/divider1"
android:layout_below="#+id/profile_school"
android:background="#android:color/darker_gray"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="10dp"
android:layout_below="#+id/divider1"
android:id="#+id/profile_phone"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/phone"
android:layout_marginTop="2dp"
android:gravity="center"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="5">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="16sp"
android:layout_margin="10dp"
android:layout_centerVertical="true"
android:text="#string/phone"/>
</RelativeLayout>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/profile_phone"
android:id="#+id/divider2"
android:background="#android:color/darker_gray"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
android:layout_below="#+id/divider2"
android:id="#+id/profile_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/parents"
android:gravity="center"
android:layout_weight="1"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="5">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dad_img"
android:layout_marginLeft="10dp"
android:src="#mipmap/ic_launcher"/>
<de.hdodenhof.circleimageview.CircleImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mum_img"
android:layout_marginLeft="10dp"
android:layout_below="#+id/dad_img"
android:layout_marginTop="5dp"
android:src="#mipmap/ic_launcher"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="16sp"
android:text="#string/mum_name"
android:id="#+id/textView2"
android:layout_marginBottom="15dp"
android:layout_marginLeft="10dp"
android:layout_alignBottom="#+id/dad_img"
android:layout_toRightOf="#+id/dad_img"
android:layout_toEndOf="#+id/dad_img" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000"
android:textSize="16sp"
android:text="#string/dad_name"
android:id="#+id/textView3"
android:layout_marginTop="15dp"
android:layout_marginLeft="10dp"
android:layout_alignTop="#+id/mum_img"
android:layout_toRightOf="#+id/mum_img"
android:layout_toEndOf="#+id/mum_img" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
Please help me to find my mistake:)
Repeat: When I open my Activity and Fragment load, I don't see anything at view under toolbar
I fixed this problem, I added Relative layout into CoordinatorLayout:
<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" tools:context=".GeneralActivity"
android:background="#color/event_background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/event_background"
android:titleTextColor="#color/text_color"
android:minHeight="?attr/actionBarSize"
android:paddingTop="#dimen/tool_bar_top_padding"
android:layout_marginTop="-25dp"
android:transitionName="actionBar1">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/profile_fragment_layout"
android:layout_below="#+id/toolbar1">
</FrameLayout>
</RelativeLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
app:elevation="4dp"
android:src="#drawable/pencil"
app:layout_anchor="#id/toolbar1"
app:layout_anchorGravity="bottom|right|end"
app:borderWidth="0dp"/>
</android.support.design.widget.CoordinatorLayout>

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