Hi guys I can't get my dropdown menu to show up in my action bar.
In the menu.item.xml, the menu bar appears in the menu split view, however upon loading it on the emulator the menu disappears.
I can't find any solutions in the existing forums on StackOverflow.
Menu.item.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item
android:id="#+id/miProfile"
android:icon="#drawable/ic_action_name"
app:showAsAction="ifRoom"
android:title="Profile"
android:iconTint="#color/white"
>
<menu>
<item
android:id="#+id/menuSortNewest"
android:title="Sort by newest" />
<item
android:id="#+id/menuSortRating"
android:title="Sort by rating" />
</menu>
</item>
</menu>
Main activity implementation of the onOptionsItemSelected
override fun onOptionsItemSelected(item: MenuItem): Boolean{
val navHostFragment =
supportFragmentManager.findFragmentById(R.id.fragmentContainerView) as NavHostFragment
val navController = navHostFragment.navController
return when (item.itemId) {
R.id.menuSortNewest -> {
navController.navigate(R.id.action_home2_to_hamster)
true
}
R.id.menuSortRating -> {
navController.navigate(R.id.action_home2_to_dog)
true
}
else -> super.onOptionsItemSelected(item)
}
}
Toolbar in the activitymain.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:minHeight="?attr/actionBarSize"
android:layout_width="match_parent"
android:layout_height="90dp"
app:titleTextColor="#android:color/white"
android:background="#color/black"
android:gravity="center"
>
</androidx.appcompat.widget.Toolbar>
</LinearLayout>
Related
i followed one of the tutorials in creating navigation bar in android studio. But in the end i the result i got was that then i click item in the navigation bar it closes instantly without taking any action and changing fragment.
you can slide it to the side and open but whenever i am trying to push item to trigger some kind of action even toast it doesn't work. it just closes to the side like it doesn't react to any action.
public class TrackerActivity extends AppCompatActivity implements LocationListener, NavigationView.OnNavigationItemSelectedListener {
private DrawerLayout drawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tracker);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Menu kad matytume kuris pazymetas
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
//first time opening activity if statement is for rotating screen so it wouldn;t reload
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new TruckTrailerRegistrationFragment()).commit();
navigationView.setCheckedItem(R.id.nav_truck_trailer_registration);
}
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_truck_trailer_registration:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new TruckTrailerRegistrationFragment()).commit();
break;
case R.id.nav_list_orders:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new ListOfOrdersFragment()).commit();
break;
case R.id.nav_information:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new InformationFragment()).commit();
break;
case R.id.nav_logout:
Toast.makeText(this, "logout", Toast.LENGTH_SHORT).show();
break;
}
drawer.closeDrawer(GravityCompat.START);
return true;
}
// We don't want to leave activity when menu is open we want to close it
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
Drawerlayout main activity it has some other things like textview which currently doesn't work so i haven't posted that
``` <?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"
android:screenOrientation="portrait"
tools:context=".TrackerActivity"
tools:openDrawer="start">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/drawer_menu" />```
Drawer_menu.xml
``` <?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_truck_trailer_registration"
android:icon="#drawable/ic_truck_trailer"
android:title="Register transport" />
<item
android:id="#+id/nav_list_orders"
android:icon="#drawable/ic_list_of_orders"
android:title="List of orders" />
<item
android:id="#+id/nav_logout"
android:icon="#drawable/ic_logout"
android:title="Logout" />
<item
android:id="#+id/nav_information"
android:icon="#drawable/ic_information"
android:title="Information" />
</group>
</menu>```
androidManifest.xml file haven't changed anything
```
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.bachbachbach.trucktracker">
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity android:name=".TrackerActivity"></activity>
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>```
styles.xml file
```
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
</resources>```
Any help would help.
I'm trying to build a simple app with GoogleMap API and I have custom tool bar.
The problem: I want to remove the default toolbar from the fragment.
activity .xml:
<LinearLayout
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=".activities.BillboardsMap"
android:orientation="vertical">
<include
android:id="#+id/tool_bar_back_billbo"
layout="#layout/tool_bar_back_billbo"
/>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
onCreate function:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityBillboardsMapBinding.inflate(layoutInflater)
setContentView(binding.root)
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
val mapFragment = supportFragmentManager
.findFragmentById(R.id.map) as SupportMapFragment
mapFragment.getMapAsync(this)
}
Is it possible to remove the toolbar with the title "BillboardsMap"?
Thanks in advance.
Try this -
STEP 1 -In style.xml or theme.xml add this custom new style.
<style name="NoToolbar" parent="Theme.MaterialComponents.Light.NoActionBar">
<item name="android:windowNoTitle">true</item>
</style>
STEP 2- In manifest.xml add this code for your activity.
<activity
....
android:theme="#style/NoToolbar" //ADD this line for your activity class
> </activity>
I seem to be stuck at the MainActivity.kt file and my app won't debug as it shows the mentioned errors.
This is for a music player app with a Navigation Drawer.
Edit : I have added the layout file after the comment as requested.
class MainActivity : AppCompatActivity(), NavigationView.OnNavigationItemSelectedListener {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setSupportActionBar(toolbar)
fab.setOnClickListener { view->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
val toggle = ActionBarDrawerToggle(
this, drawer_layout, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
drawer_layout.addDrawerListener(toggle)
toggle.syncState()
nav_view.setNavigationItemSelectedListener(this)
}
//as someone asked for the layout file as well here it is
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
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
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_gradient" />
<ImageView
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background="#drawable/echo_logo" />
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/navigation_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/header"/>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
There is no component called fab in your xml layout file. You still have the line of code that access a component called fab from your class. Remove
fab.setOnClickListener { view->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
}
It should get it to work.
so I'm fairly new to Java and Android development and I've hit a snag.
So I have created a custom Actionbar with much more height. It has at least 1 visible textview (depending on the fragment). My issue is I want my textviews aligned to the left of the app bar (directly below the nav drawer icon) but when my nav drawer button is generated in the top left it pushes my views to the right.
Does anyone know how to stop the toolbar's child views being essentially pushed over when the icon is added. (code below)
App Action Bar XML:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.arcturusapps.grassrootsorganiser.MainActivity">
<!--TODO: Fix the Hard Coded Height -->
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="125dp"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="125dp"
android:background="#drawable/header_grass"
app:contentInsetStart="0dp"
app:popupTheme="#style/AppTheme.PopupOverlay">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tool_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="85dp"
android:fontFamily="sans-serif-light"
android:text="Team Organiser"
android:textColor="#color/white"
android:textSize="12pt" />
<ImageView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginRight="6dp"
android:src="#drawable/logo_arc_header" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:src="#android:drawable/ic_dialog_email" />
Main Activity Java:
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
Main Activity
<?xml version="1.0" encoding="utf-8"?>
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
How I want it to look
How it Currently looks
P.S I am aware I shouldn't manually set 'dp' values for margins etc. at this point I am just trying to get it to look right. I'll go back and make it more dynamically scalable later.
i am using a list view. I use an adapter to add the items for this list. I am trying to add contextmenu for each item of the list but i have problems for this.
Here is my code
the item list :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/bgActivity"
android:orientation="horizontal" >
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:textColor="#color/textColorContact"
android:textSize="20sp"
android:textStyle="bold" />
<Button
android:id="#+id/pick_options"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:background="#drawable/app_icon"
android:text="#string/label_options" >
</Button>
</RelativeLayout>
the list :
<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" >
<ListView
android:id="#+id/lst_contacts"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/bgContactItem" />
</RelativeLayout>
The menu :
<menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/CtxLblOpc1" android:title="OpcEtiqueta1"></item>
<item android:id="#+id/CtxLblOpc2" android:title="OpcEtiqueta2"></item>
</menu>
java code :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_contacts);
// Getting reference to listview
ListView lstContacts = (ListView) findViewById(R.id.lst_contacts);
registerForContextMenu(lstContacts);
}
#Override
public void onCreateContextMenu(ContextMenu menu,
View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_options, menu);
}
/** Actions for the ContextMenu items */
#Override
public boolean onContextItemSelected(MenuItem item) {
//AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch (item.getItemId()) {
case R.id.CtxLblOpc1:
Toast.makeText(getApplicationContext(),
"Option 1",
Toast.LENGTH_LONG).show();
return true;
case R.id.CtxLblOpc2:
Toast.makeText(getApplicationContext(),
"Option 2",
Toast.LENGTH_LONG).show();
return true;
default:
return super.onContextItemSelected(item);
}
}
but i not see the contextmenu, please help, thanks
I guess you should try to add (in layout)
android:descendantFocusability="blocksDescendants"
in your layout.
You can give a look here
I suggest you to use the context menu with ActionMode.Callback. If you want an example look here
Note: Links are from my blog