How to center menu items ina androidstudio - java

I made a menu with a navigationView on Android Studio and I put a menu inside with two groups and items. I want to center all the items but it didn't work. Can you help me please?
Here the code
Activity mains
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:layout_centerHorizontal="true"
tools:openDrawer="start">
<include
layout="#layout/landing_page"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/grey_middle"
app:itemTextColor="#color/menu"
app:menu="#menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
and the activity_main_drawer
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_normal"
android:icon="#drawable/ic_menu_camera"
android:title="#string/menu_normal"
android:visible="false" />
<item
android:id="#+id/nav_avance"
android:icon="#drawable/ic_menu_gallery"
android:title="#string/menu_avance" />
<item
android:id="#+id/nav_hot"
android:icon="#drawable/ic_menu_slideshow"
android:title="#string/menu_hot"/>
<item
android:id="#+id/nav_premium"
android:icon="#drawable/ic_menu_slideshow"
android:title="#string/menu_premium" />
</group>
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_stat"
android:title="#string/menu_stat" />
<item
android:id="#+id/nav_trophees"
android:title="#string/menu_trophees" />
<item
android:id="#+id/nav_langues"
android:title="#string/menu_langues" />
<item
android:id="#+id/nav_noter"
android:title="#string/menu_noter" />
<item
android:id="#+id/nav_contact"
android:title="#string/menu_contact"/>
</group>
</menu>
Thanks, everyone, I hope you will succeed to help me.

Related

android app crash when included code after toolbar

when I clear my content of homepage xml file, apps running successfully. But when I add content again, My apps crash.
activity_home_page.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
android:id="#+id/app_bar_home_page"
layout="#layout/app_bar_home_page"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_home_page"
app:menu="#menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
app_bar_homepage.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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=".HomePage">
<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/Theme.BjkuApps.AppBarOverlay">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/Theme.BjkuApps.PopupOverlay" />
</com.google.android.material.appbar.AppBarLayout>
<include layout="#layout/content_home_page"/>
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/loginMoreFabIdForHomePage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginEnd="#dimen/fab_margin"
android:layout_marginBottom="16dp"
android:text="#string/login_for_more"
android:backgroundTint="#color/loginForMoreButton"
android:textColor="#color/white"
tools:targetApi="lollipop" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
content_of_homepage.xml
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="10"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/app_bar_home_page">
<ViewFlipper
android:id="#+id/viewFlipperID"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3.4"
android:layout_marginBottom="8dp"/>
<ScrollView
android:id="#+id/scrollViewID"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="6.6"
android:scrollbars="none">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/home_item_recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>
When I clear "content of homepage" code, this apps successfully running like now...
Image link - https://drive.google.com/file/d/1qkE9YDuOs1V2Mq7ma4Cj-7khEO7sGsCV/view?usp=sharing
theme.xml
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Theme.BjkuApps" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Primary brand color. -->
<item name="colorPrimary">#color/default_Color_main</item>
<item name="colorPrimaryVariant">#color/purple_700</item>
<item name="colorOnPrimary">#color/white</item>
<!-- Secondary brand color. -->
<item name="colorSecondary">#color/teal_200</item>
<item name="colorSecondaryVariant">#color/teal_700</item>
<item name="colorOnSecondary">#color/black</item>
<!-- Status bar color. -->
<item name="android:statusBarColor" tools:targetApi="l">?attr/colorPrimaryVariant</item>
<!-- Customize your theme here. -->
</style>
<style name="Theme.BjkuApps.NoActionBar" parent="Theme.BjkuApps">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
</style>
<style name="Theme.BjkuApps.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />
<style name="Theme.BjkuApps.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />
android manifast.file
<activity
android:name=".HomePage"
android:exported="false"
android:label="#string/title_activity_home_page"
android:theme="#style/Theme.BjkuApps.NoActionBar" />
change this code in themes.xml
<style name="Theme.BjkuApps" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
to this:
<style name="Theme.BjkuApps" parent="Theme.MaterialComponents.DayNight.NoActionBar">
this may help you
Your layout file name is content_of_homepage.xml but you have included layout content_home_page here
<include layout="#layout/content_home_page"/>
Change content_home_page to content_of_homepage

Not able to select options from side menu and navigate to different screens

I have made one dashboard screen in that screen I added side bar navigation so for this I made sidemenu.xml and headerfile.xml and I added these both sidemenu.xml and headerfile.xml to dashboard.xml now I want when I click on any options from side bar navigation it should navigate to their respective screens.
sidemenu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
//menu one
<group android:checkableBehavior="single">
<item
android:id="#+id/profile_menu"
android:icon="#drawable/profile"
android:title="Profile" />
<item
android:id="#+id/home_menu"
android:icon="#drawable/ic_outline_home_24"
android:title="Home" />
<item
android:id="#+id/dashboard_menu"
android:icon="#drawable/ic_outline_dashboard_24"
android:title="Dashboard" />
<item
android:id="#+id/services_icon"
android:icon="#drawable/ic_outline_miscellaneous_services_24"
android:title="Services" />
</group>
// menu two
<group
android:checkableBehavior="single">
<item android:title="Authentication">
<menu>
<item
android:id="#+id/login_menu"
android:icon="#drawable/ic_baseline_login_24"
android:title="Login" />
<item
android:id="#+id/logout_menu"
android:icon="#drawable/ic_outline_logout_24"
android:title="Logout" />
</menu>
</item>
</group>
//menu three
<item android:title="Sharing">
<menu>
<item
android:id="#+id/share_menu"
android:icon="#drawable/ic_baseline_share_24"
android:title="Share" />
<item
android:id="#+id/refer_menu"
android:icon="#drawable/ic_baseline_send_24"
android:title="Refer" />
</menu>
</item>
</menu>
activity_dashboard.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".dashboard"
tools:openDrawer="start">
<com.google.android.material.navigation.NavigationView
android:id="#+id/navigationview"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/headerfile"
app:menu="#menu/sidemenu" />
First, Make sure that you want to navigate different fragment or layout. If you want to navigate different fragment then add all IDs of sidemenu.xml in mobile_navigation.xml (make sidemenu.xml and mobile_navigation.xml id same) and if you know how to? then please add code of your navigation>mobile_navigation.xml and also code of MainActivity. Because half code cannot solve any problem.

Android: FragmentTabHost Widget not displaying Title

Im trying to design a custom TabWidget with FragentTabHost following these two guides for the tab layout:
https://maxalley.wordpress.com/2014/09/08/android-styling-a-tab-layout-with-fragmenttabhost-and-fragments/
http://joshclemm.com/blog/custom-android-tabs/
Im building FragmentTabHost with three fragments, the first fragment have a expandable list view.
Everything works fine, i can set backgroundcolor, the icon (if i want to) and the indicator works as well, as the divider does(if i want to) but the tabtitle does not appear for some reason and i cant find the reason why it dont show up.
If i remove tab_indicator.xml from android:background="#drawable/tab_indicator" in tab_layout.xml and set the backgroundcolor to whatever color the tab title shows up, but then i obviuosly dont get the indicators.
Do anyone have a clue whats wrong and what im missing. I have tried to solve this problem and searching för some days now and it starts to get very frustrating because its seems like something simple.
tab_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/tab_indicator">
<TextView
android:id="#+id/tabText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:gravity="center_vertical|center_horizontal"
android:textColor="#drawable/tab_text"
android:textSize="15dp"
/>
</LinearLayout>
tab_indicator.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Non focused states-->
<item android:state_focused="false" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected" />
<item android:state_focused="false" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected" />
<!-- Focused states-->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="false" android:drawable="#drawable/tab_unselected" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#drawable/tab_selected" />
<!-- Pressed-->
<item android:state_focused="true" android:state_selected="false" android:state_pressed="true" android:drawable="#drawable/tab_pressed" />
<item android:state_focused="true" android:state_selected="true" android:state_pressed="true" android:drawable="#drawable/tab_pressed" />
</selector>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.app.FragmentTabHost
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tabHost"
android:layout_gravity="center_horizontal"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="35dp"
android:showDividers="middle"
android:layout_weight="0"
android:orientation="horizontal"
/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</FrameLayout>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
tab_selected.xml
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:top="-7dp" android:bottom="0dp" android:left="-7dp" android:right="-7dp">
<shape
android:shape="rectangle">
<padding android:left="10dp"
android:top="15dp"
android:right="10dp"
android:bottom="15dp" />
<stroke android:width="4dp" android:color="#FF00ccff" />
<solid android:color="#2fff00"
/>
</shape>
</item>
</layer-list>
Solved it, if i change android:layout_height in FragmentTabHost and TabWidget it displays the text, simple as that. But in my tast the height is to big ^^ I want it to be smaller like 35dp and it can be done if i change the padding in tab_selected.xml, _unselected and _pressed, and then i can change the height in TabWidget activity_main.xml else the text wont fit the tab.

Android DrawerMenu selected item not showing properly in UI

I want to make menu item in different color when it is clicked and also persist the color as it was selected from the list. currently it is not showing the green color when an item is selected
shape_drawer_menu_item.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/drawer_menu" android:state_pressed="false" />
<item android:drawable="#color/drawer_menu_selected_item" android:state_checked="true"/>
<item android:drawable="#color/drawer_menu_selected_item" android:state_activated="true"/>
<item android:drawable="#color/drawer_menu_selected_item" android:state_pressed="true"/>
<item android:drawable="#color/drawer_menu_selected_item" android:state_active="true" />
</selector>
menu_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_featured"
android:icon="#drawable/ic_nav_featured"
android:title="#string/title_nav_featured"
android:checkable="true"
android:checked="true"/>
<item
android:id="#+id/nav_tour"
android:icon="#drawable/ic_nav_tour"
android:title="#string/title_nav_tour" />
</group>
</menu>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#1C3553">
<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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_scrollFlags="scroll|enterAlways">
<include layout="#layout/toolbar" />
</FrameLayout>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frame_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/appbar"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/spacing_large"
android:clickable="true"
android:src="#drawable/ic_search"
android:tint="#android:color/white"
app:layout_anchor="#id/frame_content"
app:layout_anchorGravity="end|bottom" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="#dimen/drawer_menu_width"
android:layout_height="match_parent"
android:layout_gravity="start"
app:itemTextColor="#android:color/white"
app:itemBackground="#drawable/shape_drawer_menu_item"
android:fitsSystemWindows="false"
android:scrollbars="none"
android:background="#2F3847"
app:headerLayout="#layout/nav_header"
app:itemIconTint="#android:color/white"
app:menu="#menu/menu_drawer"
/>
</android.support.v4.widget.DrawerLayout>
ActivityMain.java
OnCreate() =>
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem item) {
actionBar.setTitle(item.getTitle().toString());
//item.setChecked(true);
navigationView.setCheckedItem(item.getItemId());
boolean s=item.isEnabled();
return onItemSelected(item.getItemId());
}
});
You need to remove group checkable behaviour attribute and put only put checkable = true in your item, it will works. Best of luck!!
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<group>
<item
android:id="#+id/nav_featured"
android:icon="#drawable/ic_nav_featured"
android:title="#string/title_nav_featured"
android:checkable="true"/>
<item
android:id="#+id/nav_tour"
android:icon="#drawable/ic_nav_tour"
android:title="#string/title_nav_tour"
android:checkable="true" />
</group>
</menu>

ShareActivityProvider does not show up as a button

My ShareActionProvider does not show an icon. My app crashes if I showAsAction. It does provide a share action, I just want it to show a button with an icon instead of the word, and I want to set showAsAction to "always." My inflate code is here:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent" android:layout_height="match_parent"
android:fitsSystemWindows="true" tools:context=".MainActivity"
style="#style/AppTheme">
<android.support.design.widget.AppBarLayout android:id="#+id/appbar"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar android:id="#+id/toolbar"
android:layout_width="match_parent" android:layout_height="?
attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:layout_scrollFlags="scroll|enterAlways">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabGravity="fill" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager android:id="#+id/container"
android:layout_width="match_parent" android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
For the menu, my XML is here. I'm using the v7 compatibility library.
<?xml version="1.0" encoding="utf-8"?>
<menu 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" tools:context=".MainActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never"
/>
<item
android:id="#+id/menu_item_share"
android:title="Share"
app:actionProviderClass="support.v7.android.widget.ShareActionProvider"
app:showAsAction="never"/>
</menu>
Try replacing your menu with
<?xml version="1.0" encoding="utf-8"?>
<menu 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" tools:context=".MainActivity">
<item android:id="#+id/action_settings"
android:title="#string/action_settings"
android:orderInCategory="100" app:showAsAction="never"
/>
<item
android:id="#+id/menu_item_share"
android:title="Share"
app:actionProviderClass="support.v7.android.widget.ShareActionProvider"
android:icon="#android:drawable/ic_menu_share"
app:showAsAction="ifRoom"/>
</menu>
Changes - Add icon to share and change showAsAction to ifRoom
Cheers,
Sha

Categories

Resources