AppCompatActivity with Toolbar: menu items not showing on Toolbar - android-studio

scratching my head. if someone could help that would be great. I am switching from extends ActionBarActivity to extends AppCompatActivity with Toolbar and need help displaying items on toolbar.
I am using extends AppCompatActivity included android.support.v7.app.AppCompatActivity; then inside onCreate() have:
Toolbar jobListToolbar = (Toolbar) findViewById(R.id.job_list_toolbar);
setSupportActionBar(jobListToolbar);
then in xml file I have
<android.support.v7.widget.Toolbar
android:id="#+id/job_list_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"/>
then in menu file I have items and everything seems to work, however not the way I want.
Toolbar/ActionBar is displayed on top of the screen, but no menu items showing on it. If I press phone original menu button then onCreateOptionsMenu(Menu menu) is initiated for the first time, and menu is showing. But it shows just above this phone button. But I want those buttons to show on Toolbar/ActionBar on top of the screen. I read somewhere that if phone has hardware menu button it will not show menu items in Toolbar/ActionBar, but I had it somehow done with extends ActionBarActivity, but there should be a way to do this with extends AppCompatActivity. Need to move to newer recommended way by google, but not sure how to show items on that Toolbar/ActionBar on the top of the screen.

After a day of research on how to switch to AppCompat Toolbar finally found the solution. Two must do steps (No 2 being the one that nobody talks about):
I.) couple good resources on how to set up Toolbar can be found in this video Toolbar Tutorial and also in this StackOverflow: toolbar button
II.) MAKE SURE YOU PLACE THIS CODE AT THE BOTTOM OF onCreate():
toolbar= (Toolbar) findViewById(R.id.job_list_toolbar);
setSupportActionBar(toolbar);
if(getSupportActionBar()!=null) {
getSupportActionBar().setDisplayShowHomeEnabled(true);
}
My problem was that I had some code in onCreate() that modified the toolbar buttons. That code needed to be run before toolbar was opened. If only first two lines of the code above included toolbar will show, but no menu will be inflated and no buttons. When I called getSupportActionBar().setDisplayShowHomeEnabled(true); thats when onCreateOptionsMenu(Menu menu) is called (if no true enabled it is not called), also make sure you have getMenuInflater().inflate(R.menu.job_list, menu); inside onCreateOptionsMenu(Menu menu).
If the code that calls for menu to open getSupportActionBar().setDisplayShowHomeEnabled(true); is not at the end of onCreate() you might be scratching your head why the menu is not showing. So make sure you execute your relevant onCreate() code before you call the menu to open.

MyActivity:
extends AppCompatActivity
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarhome);
setSupportActionBar(toolbar);
activity_my.xml: If you want you can delete the last line 0 ep. They are the starting point of 0 to launching toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarhome"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:titleTextAppearance="#style/Toolbar.TitleText"
app:layout_collapseMode="pin"
android:background="#color/colorPrimary"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp" >
//designed the way you like
</android.support.v7.widget.Toolbar>
manifest:
<activity
android:name=".MyActivity"
android:theme="#style/AppTheme.NoActionBar"/>
style:
<style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light"> </style>
<style name="Toolbar.TitleText" parent="TextAppearance.Widget.AppCompat.Toolbar.Title">
<item name="android:textSize">20sp</item>
<item name="android:textStyle">italic</item>
</style>
<style name="AppTheme.NoActionBar">
<item name="windowActionBar">false</item>
<item name="windowNoTitle">true</item>
</style>

Related

How to transform the floating action buttons to full screen and open new activity in android studio and vice versa?

I would like to transition between two activities(Activity A and Activity B).Activity A contains a floating action button (FAB) on the bottom right.
When the FAB is clicked, the FAB should transform into a new surface that spans the entire screen and that entire screen is Activity B.
When the back button is pressed in Activity B then the whole screen surface should shrink back to FAB which is now Activity A.
How can I do that?
Yes. You can actually do that with the Shared Element transitions in android.
It's easy to use. Just follow the steps:
Enable Window Content Transitions in your style XML which you are using in the activity.
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
<!-- Define the attribute here. -->
<item name="android:windowContentTransitions">true</item>
</style>
Set the common transition name in both your views. For example, You want to animate FAB to the full screen view of Activity B. So you have to apply the transition name to your FAB & the parent view of Activity B.
<LinearLayout ...>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end|bottom"
android:layout_margin="16dp"
android:transitionName="transition_fab"/>
...
</LinearLayout>
Now in your activity xml of Activity B.
<RelativeLayout
android:id="#+id/root_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:transitionName="transition_fab"/>
<TextView
... />
</RelativeLayout>
Now, Open Activity with the Element Transition, Create ActivityOptions in Activity A and put the common object name in it like shown below.
FloatingActionButton fab = findViewById(R.id.fab);
Intent intent = new Intent(ActivityA.this, ActivityB.class);
ActivityOptions options = ActivityOptions
.makeSceneTransitionAnimation(this, fab, "transition_fab");
startActivity(intent, options.toBundle());
And that's it! You can now see your FAB animating from Activity A to the Full screen in Activity B.
You can find more details about the Shared Element Transition here - Android Shared element transitions

Android: create an ImageButton for Speaker On/Off

I am writing an app, where in a game a score is counted and displayed.
Now I would like to give the opportunity to switch ON/Off the speakers. I think, to use an ImageButton should be the solution.
But how do I "dim" the button, when speaker is Off, but the button has to remain active. invisibility or disable is not the solution. Is ist best, to change the Image in the button?
Does anybody has an example for a switch On/Off button please.
I am working on AndroidStudio4.1 and Kotlin
Is better to have a checkbox, because that view handles true/false states by default. You have to create a drawable for the button and then set it as a button.
Let's say your drawable is cb_selector_sound
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_sound_on" android:state_checked="true" />
<item android:drawable="#drawable/ic_sound_off" android:state_checked="false" />
</selector>
and then use it in the button like this:
<CheckBox
android:id="#+id/soundCheckbox"
android:button="#drawable/cb_selector_sound"
.../>
So then you can listen the checkbox changing
//find the view
soundCheckbox.setOnCheckeChangedListener {_, isChecked ->
if (isChecked) {
//turn sound on
} else {
//turn sound off
}
}
So the view worries about the appearance and you worried about the logic

Android Studio Material design menu inflation

I am trying to inflate a menu when selecting items from the list for multiple deletion, but instead I get a new menu bar on top of the current one.
http://imgur.com/XRAEnBM.jpg
I used the default navigation drawer activity and then made my fragments.
I am using listView.setMultiChoiceModeListener this is my code
Java Code:
#Override
public boolean onCreateActionMode(ActionMode mode, Menu menu) {
mode.getMenuInflater().inflate(R.menu.delete, menu);
return true;
}
XML Code in menu:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/delete_id"
android:title="Delete"
android:icon="#drawable/ic_action_delete"/>
</menu>
The Java code happens in on onResume() in one of my fragments.
I am a beginner so please be kind thanks :)
Here is the solution, just add these two lines in your stlyes
<item name="windowActionModeOverlay">true</item>
<item name="actionModeBackground">#color/colorPrimary</item>

How to Set Focus on an Item in the Action Bar on Launch

I had developed an Android app that works fine on smartphones. When trying on Amazon Fire TV, I see that no view has focus on launch and the key presses on the remote controller don't have any effect. How can I make an item on the action bar have focus (the action item should have focus, namely blue lines around it). Since no view seems to have focus, the remote controller does not work.
Do I have to add something to the layout code, which is as follows:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/menu_item_favorites"
android:icon="#drawable/ic_action_favorites"
android:title="#string/favorites"
android:showAsAction="always" />
...
...
...
</menu>
or do I have to set focus programmatically in onCreate or onStart?

Is there any sliding menu with visible column before click?

I've searched but no result.
I would like have menu where you see still column and when you click on it, it will slide right or left showing the rest of layout. What is more it should slide over my activity.
Any help?
I'm looking for something like that:
I found sliding menu from jfeinstein only but when you swipe, your screen is moving also.
Thanks in advance.
This is probably what you need.
Example:
activity_main.xml:
<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" >
<shared.ui.actionscontentview.ActionsContentView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:actions_layout="#layout/actions"
app:actions_spacing="50dp"
app:content_layout="#layout/content"
app:shadow_drawable="#drawable/shadow"
app:shadow_width="8dip"
app:spacing="64dip"
app:spacing_type="right_offset" />
</RelativeLayout>
app:actions_layout="#layout/actions" - this is the layout for your left side menu. You can put there everything you want
app:actions_spacing - offset from left side in dp - that means how much the left menu will be visible
app:content_layout="#layout/content" - this is the layout for main content. You can also put there everything. For example there you can have a FrameLayout and you can attach a fragment from code based on what menu item a user clicked
app:spacing_type="right_offset" and app:spacing="64dip" - that means that when left menu is opened, then how much the main content will be visible
MainActivity.java:
#Override
protected void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.activity_main);
mSlidingMenu = (ActionsContentView) findViewById(R.id.content);
//this will open menu
mSlidingMenu.showActions();
//this will close menu
mSlidingMenu.showContent();
}

Resources