How to make old custom ActionBar appear on new SDK? - android

I migrated my old Android (4.4 Kitkat) project from Eclipse to Android Studio.
When I run the app I have no ActionBar anymore!
Here is my main Activity class file:
public class Main extends Activity {
...
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.options, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent;
switch (item.getItemId()) {
case R.id.Open:
intent = new Intent(this, Open.class);
startActivity(intent);
return true;
case R.id.Email;
intent = new Intent(this, Email.class);
startActivity(intent);
return true;
case R.id.Compartir:
intent = new Intent(this, Compartir.class);
startActivity(intent);
return true;
case R.id.About:
intent = new Intent(this, About.class);
startActivity(intent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Here is my styles.xml file:
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light"></style>
<style name="AppTheme" parent="AppBaseTheme">
<item name="android:actionOverflowButtonStyle">#style/MyActionButtonOverflow</item>
</style>
<style name="MyActionButtonOverflow" parent="android:style/Widget.Holo.Light.ActionButton.Overflow">
<item name="android:src">#drawable/ic_action_expand</item>
<item name="android:background">?android:attr/actionBarItemBackground</item>
<item name="android:contentDescription">"Lala"</item>
</style>
</resources>
that I call from manifest file:
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
In the project I have options.xml, as you can see it creates the content of the Action Bar: 1 buttons + the option button that is a dropdown menu with 3 buttons.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/Open"
android:title="#string/option_open"
android:icon="#drawable/ic_action_open"
android:showAsAction="ifRoom" />
<item android:id="#+id/Email"
android:title="#string/option_email"
android:icon="#drawable/ic_action_person"
android:showAsAction="never" />
<item android:id="#+id/Compartir"
android:title="#string/option_compartir"
android:showAsAction="never" />
<item android:id="#+id/About"
android:title="#string/option_about"
android:showAsAction="never" />
</menu>
Anyway, if I build the app I get no ActionBar on my phone. I had it when I build the app with Eclipse ADT. I think the difference is the SDK old version that I used before. Then, how to make the ActionBar appears again with that custom theme?

I think you have to go back to Holo theme:
<style name="AppBaseTheme" parent="android:Theme.Holo.Light">

try this:
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.DarkActionBar">

Related

How to add menu options when theme is NoActionBar in style.xml

I have use Fragments so, I had to use theme NoActionBar, but when I inflate option menu it does not showing anything.How to resolve this?
Here is my Code:
MainActivity.java
> #Override
> public boolean onCreateOptionsMenu(Menu menu){
> MenuInflater inflater =getMenuInflater();
> inflater.inflate(R.menu.options,menu);
> return super.onCreateOptionsMenu(menu);
> }
> #Override
> public boolean onOptionsItemSelected(MenuItem item){
> switch(item.getItemId()){
> case R.id.item1 :
> Intent box = new Intent(MainActivity.this,Developer.class);
> startActivity(box);
> break;
> }
> return true;
> }
Styles.xml
<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>
options.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:title="Developer" android:id="#+id/item1"/>
</menu>
You can add menu right to your toolbar using android.support.v7.widget.Toolbar#inflateMenu method. This is example it kotlin
with(toolbar) {
inflateMenu(R.menu.menu_account_detail)
setOnMenuItemClickListener {
if (it.itemId == R.id.search) {
doSearch()
}
true
}
}

How to change android top toolbar menu item icon size

How can I change the size of a menu item in the android toolbar? Currently the menus have a very small size and I want to increase the size. If anyone knows please help me to find a solution.
app_bar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryColor"
android:minHeight="?attr/actionBarSize"
android:paddingTop="#dimen/app_bar_top_padding"
android:theme="#style/ToolBarStyle"/>
Styles.xml
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="AppTheme.Base"> </style>
<!-- Application theme. -->
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#color/primaryColor</item>
<item name="colorPrimaryDark">#color/primaryColorDark</item>
<item name="colorAccent">#color/accentColor</item>
<item name="colorControlHighlight">#color/primary_high_light</item>
<!-- <item name="textColorPrimary">#color/ColorPrimaryText</item> -->
</style>
<style name="ToolBarStyle" parent="ThemeOverlay.AppCompat.Dark">
<item name="android:textColorPrimary">#ffffff</item>
</style>
<style name="option" parent="Theme.AppCompat.Light">
</style>
<style name="Dialog" parent="Base.Theme.AppCompat.Light.Dialog">
</style>
menu_option.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">
<item android:id="#+id/homes"
android:title="homes"
android:icon="#drawable/ic_action_home1"
app:showAsAction="always"/>
<item android:id="#+id/profilepreview"
android:title="ProfilePreview"
android:icon="#drawable/profile"
app:showAsAction="always"/>
<item android:id="#+id/findPeople"
android:title="findPeople"
android:icon="#drawable/ic_action_search1"
app:showAsAction="always"/>
<item android:id="#+id/log"
android:title="log"
android:icon="#drawable/ic_action_logout1"
app:showAsAction="always"/>
</menu>
MainActivity.java
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_option, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.profilepreview:
startActivity(new Intent(this, ProfilePreview.class));
return true;
case R.id.homes:
mFragment = new HomeFragment();
break;
case R.id.findPeople:
mFragment = new FindFriendsFragment();
break;
case R.id.log:
M.logOut(this);
mIntent = new Intent(this, LoginActivity.class);
finish();
}
if (mFragment != null && mIntent == null) {
mFragmentManager = getFragmentManager();
mFragmentManager.beginTransaction()
.replace(R.id.frameContainer, mFragment).commit();
} else {
startActivity(mIntent);
mIntent = null;
}return super.onOptionsItemSelected(item);
}
You may design a custom layout and add it as your actionlayout. ALso make sure you do not add any icon in menu.xml file.
<item android:id="#+id/homes"
android:title="homes"
app:actionLayout="#layout/your_custom_layout"
app:showAsAction="always"/>
Then give min_height in your custom layout.
I ran into this problem too and the only workaround I was able to have was to use smaller images. A size of 24dp from the icons here (https://design.google.com/icons) fits nicely into my toolbar.
I suggest you to read this blog for custom toolbar.
https://stablekernel.com/using-custom-views-as-menu-items/
instead of adding drawable_icon to your menu-xml file, you can use "app:actionLayout" in that menu-xml file to specify the other xml-file(where you are going to create custom toolbar icon).
Just increase the size of the vector image(in the right) from the default 24x24 to a different size but after a certain size it won't scale up. The size might seem odd if it is put beside a view inflated(with orange background in the left) in the custom toolbar as shown in the image, only way to fix this is to inflate another view in the toolbar with the vector as the background.

options menu action bar

Can anyone see why my help icon isn't showing in the action bar? I have pasted the relevant parts of my code below
Thank you
menu topline.xml:
`
<item
android:id="#+id/gohome_id"
android:title="Home"
trial10:showAsAction="ifRoom"
/>
<item
android:id="#+id/helpme_id"
android:title="help"
android:icon="#drawable/ic_questionmark"
android:orderInCategory="200"
trial10:showAsAction="always"
/>
`
styles.xml:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
</style>
<style name="CustomActionBarTheme"
parent="#android:style/Theme.Holo.Light.DarkActionBar">
<item name="android:actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar"
parent="#android:style/Widget.Holo.Light.ActionBar.Solid.Inverse">
<item name="android:background">#drawable/logo3</item>
<item name="android:icon">#drawable/leaflogo</item>
</style>
<style name="orangestyle" parent="#android:style/Theme.NoTitleBar">
<item name="android:windowBackground">#color/orange</item>
</style>'
This is in my activity java:
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.topline, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch(item.getItemId()){
case R.id.gohome_id:
gohome();
break;
}
return true;
}
finally, my Manifest:
<activity
android:name=".test1"
android:label="Test"
android:theme="#style/CustomActionBarTheme" >
</activity>
try this
<item
android:id="#+id/gohome_id"
android:title="Home"
android:showAsAction="ifRoom"
/>
<item
android:id="#+id/helpme_id"
android:title="help"
android:icon="#drawable/ic_questionmark"
android:showAsAction="always"
/>
Did you put your item in a menu tag?
<menu ....>
<item.../>
<item.../>
</menu>
If yes, change trial10:showAsAction with app:showAsAction. Just alt enter if there's an error on app.
Also delete that order line from home item

Android option menu: change size of icon

I am using option menu, i want to increase the size of my icon...
means icon are too small... how can increase their width or height ....
or how remove space around icon.. i.e padding, margin etc...
i want a menu with icon at bottom...
what i should do???
<activity
android:name="com.example.parentengagementtracking.BaseActivity"
android:label="#string/title_activity_base"
android:theme="#style/MenuTheme" >
<!-- android:theme="#style/Theme.FixedSize"> -->
</activity>
<style name="MenuTheme" parent="android:Theme">
<item name="android:panelFullBackground">#drawable/back11</item>
<item name="android:padding">1dip</item>
<item name="android:drawablePadding">1dip</item>
</style>
public class BaseActivity extends Activity {
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_parent_home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
Bundle objBundle;
Intent objIntent;
switch (item.getItemId())
{
case R.id.menu_about_us:
objIntent=null;
objIntent = new Intent(BaseActivity.this,InfoActivity.class);
objBundle=new Bundle();
objBundle.putString("source","BaseActivity");
objIntent.putExtras(objBundle);
finish();
startActivity(objIntent);
return true;
case R.id.menu_profile:
objIntent=null;
objIntent = new Intent(BaseActivity.this,UserProfile.class);
finish();
startActivity(objIntent);
return true;
case R.id.menu_activity:
objIntent=null;
objIntent = new Intent(BaseActivity.this,ViewActivity.class);
finish();
startActivity(objIntent);
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
<!-- android:icon="#drawable/homen" -->
<item
android:id="#+id/menu_profile"
android:orderInCategory="2"
android:showAsAction="always"
android:icon="#drawable/profile1"
android:title="Profile"/>
<item
android:id="#+id/menu_activity"
android:orderInCategory="3"
android:showAsAction="always"
android:icon="#drawable/reports"
android:title="Reports"/>
<item
android:id="#+id/menu_about_us"
android:icon="#drawable/aboutusn"
android:orderInCategory="4"
android:showAsAction="always"
android:title="Aboutus"/>
how can i set padding, margin, width and height etc of icon..??
Your image should be 24*24 or 36*36.
You can solve this problem by using "app:actionLayout".
<item
android:id="#+id/menu1"
app:actionLayout="#layout/custom_action_bar" //this line will change icon
android:visible="true"
android:title="Menu 1"
app:showAsAction="always" />
Refer this docs Custom menu icons to add listeners to custom menu.

Duplicate Menu Items Showing Up and One Not Showing at All; Crashing as well

Okay, so I've been doing pretty well with developing my Android App but then I run into this issue. I tried to make another Menu Item to an Option Menu which I've never had problems with before but now, as described in the question, one of the items is showing up twice and another isn't showing up at all and then when I enter the Credits activity it crashes. Below is the codes for the res/menu/menu.xml And MainActivity.java (first one)
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu); return true; }
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle item selection
switch (item.getItemId()) {
case R.id.quit:
return true;
case R.id.new_game:
Intent intent = new Intent(this, New_Game.class);
startActivity(intent);
return true;
case R.id.visit_site:
Intent inten = new Intent(this, Site.class);
startActivity(inten);
return true;
case R.id.stay:
Intent inte = new Intent(this, MainActivity.class);
startActivity(inte);
return true;
case R.id.credits:
Intent i = new Intent(this, Credits.class);
startActivity(i);
return true;
case R.id.exit:
Intent in = new Intent(Intent.ACTION_MAIN);
in.addCategory(Intent.CATEGORY_HOME);
in.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(in);
finish();
System.exit(0);
return true;
}return false;
}
Here is the XML Menu
<menu
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/visit_site"
android:title="Visit the Medieval Site!" />
<item
android:id="#+id/quit"
android:title="Exit">
<item
android:id="#+id/credits"
android:title="Credits"
/>
<menu
xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/stay"
android:title="Cancel"
/>
<item
android:id="#+id/exit"
android:title="Exit" />
</menu>
</item>
<item
android:id="#+id/new_game"
android:title="Start the Game"
/>
</menu>
I set up the credits as just a normal "Hello World" Activity in the Java file for it Here is the Android Manifest declaration for the activity. I'm including this just in the case that the error was in there by chance.
<activity android:name=".Credits"
android:label="Application Credits"
android:configChanges="orientation|keyboardHidden"
android:screenOrientation="landscape" >
</activity>
If anybody could please tell me where I messed up and what I need to do tof ix it.
Here is the Credits.java asked by Sam below
package com.apw.games.rpg.medieval;
import android.app.*;
import android.os.*;
import android.view.*;
public class Credits extends Activity
{
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.credits);
}
}
Your menu layout is incorrect. The problem is "Credits", you cannot have nested items: <item><item></item></item>. Either move "Credits" into the submenu or move it into the regular menu:
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/visit_site"
android:title="Visit the Medieval Site!"/>
<item
android:id="#+id/quit"
android:title="Exit">
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/stay"
android:title="Cancel"/>
<!-- I moved credits here -->
<item
android:id="#+id/credits"
android:title="Credits"/>
<item
android:id="#+id/exit"
android:title="Exit"/>
</menu>
</item>
<item
android:id="#+id/new_game"
android:title="Start the Game"/>
<!-- Or you can move credits here -->
</menu>
Notice the layers are <menu> <item> <menu> <item> </item> </menu> </item> </menu>.

Categories

Resources