Android: App Icon as Up Button does not work - android

iam following the basic beginner Tutorial on the Android Developer Site. Now, i there is described how to bring the app icon as the Up Button (see last text): http://developer.android.com/training/basics/actionbar/adding-buttons.html#UpNav
I put "getSupportActionBar().setDisplayHomeAsUpEnabled(true);" in my activitys onCreate() Methods, but on my Device my App has no App Icon as the Button. Here are my activities
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
and
public class DisplayMessageActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
// Get the message from the intent
Intent intent = this.getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(40);
textView.setText(message);
// Set the text view as the activity layout
setContentView(textView);
}
And also my Manifest
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayMessageActivity"
android:label="#string/title_activity_display_message"
android:parentActivityName=".MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.andreas.myapplication.MainActivity" />
</activity>
</application>
And Gradle Infos
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.example.andreas.myapplication"
minSdkVersion 8
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
iam confused because im alredy use the right method for minSdkVersion 8.
Does anyone know, why ma App does not show the Icon as Up-Button?
best Regards

I have encountered the same problem - enabling "display home as up" led to the Up button with an arrow image on it. I have managed to replace arrow with an app icon with the following steps:
Create a new ImageAsset with app icon images in /drawable.
In OnCreate() of child activity, add 2 lines:
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_home);

Substitute meta-data value android:value="com.example.andreas.myapplication.MainActivity" for android:value=".MainActivity"
To support older devices with the support library, also include a element that specifies the parent activity as the value for android.support.PARENT_ACTIVITY as shown below:
<!-- Parent activity meta-data to support 4.0 and lower -->
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
A full explanation is here: http://programmerguru.com/android-tutorial/how-to-add-up-button-on-action-bar/

I believe you need to call getSupportActionBar().setDisplayHomeAsUpEnabled(true); after calling setContentView(textView); in the second activity.
EDIT:
also, main activity should not call getSupportActionBar().setDisplayHomeAsUpEnabled(true); as it leads nowhere. And you should add a button to switch activities or you will never get to DisplayMessageActivity. try something like this:
Intent showmessg = new Intent(getApplicationContext(), DisplayMessageActivity.class);
startActivity(showmessg);
Hope it works for you.
source: http://developer.android.com/training/basics/actionbar/adding-buttons.html#UpNav

I think in android 5 and API 21 we can't use app icon for up action.
if your appcompat support library is v7 rev21 i think you can't use the app icon at least for up action. but u can use it instead of top left caret or you can use app icon right of that caret although if u click it nothing happen.
I have two app completely like that trainings in one of them i use the older appcompat v7 and minSdkVersion="11" and i never added getSupportActionBar().setDisplayHomeAsUpEnabled(true); to it but app have app icon and no problem . but in another app i update my support library a few day ago to rev21 and of course this app's minSdkVersion is "8" but this app hasn't actionBar icon for Up action and if you add top code to it you do nothing.
I search many of sites and just i understand is that in material design google removed app icon for up action because of increasing space on actionbar and etc.
You can see this line in this page.
"When using the Material themes (default in API 21 or newer) the
navigation button (formerly "Home") takes over the space previously
occupied by the application icon. Apps wishing to express a stronger
branding should use their brand colors heavily in the action bar and
other application chrome or use a in place of their standard title
text."

Related

Name of project on ActionBar of every Activity

I am facing an issue since upgrading Android Studio to 2.2... whenever I create a new activity in a project, the name of the project is getting displayed on the ActionBar of the new activity rather than the name of the new activity created. I have just learned intent and the transition from 1 activity to another is just fine but the action bar has the name of the project rather than the name of the current activity. What should i do?
Declare label in manifest:
<activity
android:name=".your_activity"
android:label="name_your_activity_label"/>
<activity
android:name=".your_activity"
android:label="name_your_activity_label" />
Example is just demonstration, edit in future. :P

How to create Android AppWidget that can go into home screen folders and home row (dock)?

I have found several examples of Android apps that have simple widgets that are droppable into home screen folders and the bottom, persistent row on the home screen. For instance, the Gmail label widget, or the Dropbox folder widget. In my attempts to make a similar widget - single cell with icon and text view underneath - the widget can only be by itself on the home screen.
How can I make the most simple widget that can be dropped into a home screen folder or home row just like a normal app icon?
Is it possible what I'm looking for is actually not a widget, but an app icon that would somehow be included in the widget list (but without adding a second app icon in the app list)?
EDIT:
Huge thanks to Kuffs for getting me in the right direction.
The key to this is in what Kuffs points to in the manifest:
...
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
...
As Kuffs mentions, that alone gets your app an entry in the widget list. Good work team.
Widgets can only be placed on the home screen. They cannot be added to the dock on the home screen. Only icons go here.
You need to add a shortcut.
Add the relevant permission to your manifest.
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT" />
Create an activity that will create the shortcut and add it to the manifest like the example below.
e.g
<activity
android:name=".ShortcutActivity"
android:label="#string/app_name" android:exported="true">
<intent-filter>
<action android:name="android.intent.action.CREATE_SHORTCUT" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
If you run this app now, you will be offered the opportunity to add your shortcut like the Gmail/Dropbox apps do (The widget screen will list your new shortcut creating activity). Of course, your activity should actually add the shortcut to be useful.
The activity will be started by the OS with startActivityForResult() and therefore it will be expecting an intent inside the returned intent (to be used by the OS in an onActivityResult call). This info found here: https://stackoverflow.com/a/11449443/1399483
So, create an activity that creates a shortcut such as this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent i= new Intent();
Intent shortcutActivity = new Intent(this, ActivityToLaunch.class);
i.putExtra(Intent.EXTRA_SHORTCUT_INTENT, shortcutActivity);
i.putExtra(Intent.EXTRA_SHORTCUT_NAME, "Shortcut Title");
i.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE,Intent.ShortcutIconResource.fromContext(this, R.drawable.shortcut_icon));
setResult(RESULT_OK, i);
finish();
}
To use a custom icon rather than a drawable resource, replace the extra EXTRA_SHORTCUT_ICON_RESOURCE with the following:
Bitmap bm; // Set to the image you want to use
i.putExtra(Intent.EXTRA_SHORTCUT_ICON, bm);
Hmm, good question! It's certainly true that widgets, even though 1x1, can't be placed inside a folder or the dock.
Maybe it really isn't a widget that's created after selecting the label/folder, but instead a shortcut is made. The 'widget' is a shortcut and the way to put the shortcut on your homescreen is trough the (select label/folder) 'widget'.
With the right permission and code you can create a shortcut from your sourcecode.

How do I change the android actionbar title and icon

I'm trying to do some things on the ActionBar in Android.
I've already added new items in the right side of the action bar.
How can I change the left side of the action bar? I want to change the icon and the text, and I want to add a "Back Button" in the action bar for the other screens
This is very simple to accomplish
If you want to change it in code, call:
setTitle("My new title");
getActionBar().setIcon(R.drawable.my_icon);
And set the values to whatever you please.
Or, in the Android manifest XML file:
<activity android:name=".MyActivity"
android:icon="#drawable/my_icon"
android:label="My new title" />
To enable the back button in your app use:
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
The code should all be placed in your onCreate so that the label/icon changing is transparent to the user, but in reality it can be called anywhere during the activity's lifecycle.
To make a single icon be usable by all your action bars you can do this in your Android Manifest.
<application
android:logo="#drawable/Image">
...
</application>
You just need to add these 3 lines of code. Replace the icon with your own icon. If you want to generate icons use this
getSupportActionBar().setHomeAsUpIndicator(R.drawable.icon_back_arrow);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
In Android 5.0 material design guidelines discourage the use of icon in actionBar
to enable it add the following code
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setLogo(R.mipmap.ic_launcher);
getSupportActionBar().setDisplayUseLogoEnabled(true);
credit goes to author of this article
If you want to change the Action bar title just give the following 1 line code in the onCreate() of your Activity
getActionBar().setTitle("Test");
ActionBar actionBar = getSupportActionBar();
actionBar.setTitle(getString(R.string.titolo));
actionBar.setIcon(R.mipmap.ic_launcher);
actionBar.setDisplayShowHomeEnabled(true);
You can change the icon in your by adding whatever icon you want to your respective drawable folders, then changing this line in your AndroidManifest.xml file:
android:icon="#drawable/ic_launcher"
to match whatever the name of your icon is in there. Or put your icon as ic_launcher, if they're the same icon. As for what it says, add or change whatever strings match up to that in your res/values/strings.xml file. Then, once again in your AndroidManifest.xml file, change this line:
android:label="#string/app_name"
to whatever the string you have in their. You'll have to do this for the application as a whole, and whichever activities you want, but the lines are the same.
Hope this helps.
For that, you can do it in 2 ways: XML or Java. See here: How to change the text on the action bar
So:
XML:
<activity android:name=".Hello_World"
android:label="This is the Hello World Application">
</activity>
Java:
public class TitleBar extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}
final TextView myTitleText = (TextView) findViewById(R.id.myTitle);
if ( myTitleText != null ) {
myTitleText.setText("NEW TITLE");
// user can also set color using "Color" and then "Color value constant"
// myTitleText.setBackgroundColor(Color.GREEN);
}
}
}
For set Title :
getActionBar().setTitle("Title");
For set Icon :
getActionBar().setIcon(R.drawable.YOUR_ICON_NAME);
Add the below code inside an onCreate function in your activity.
setTitle("NewName");
I used the following call inside onNavigationItemSelected:
HomeActivity.this.setTitle(item.getTitle());
Go to manifest in which specific activity you want to change Action bar Title name and write
android:label="Title name"
This work for me:
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeAsUpIndicator(R.mipmap.baseline_dehaze_white_24);
The action bar title will, by default, use the label of the current activity, but you can also set it programmatically via ActionBar.setTitle().
To implement the "Back" (more precisely, "Up") button functionality you're talking about, read the "Using the App Icon for Navigation" section of the Action Bar developer guide.
Finally, to change the icon, the guide covers that as well. In short, the action bar will display the image supplied in android:icon in your manifest's application or activity element, if there is one. The typical practice is to create an application icon (in all of the various densities you'll need) named ic_launcher.png, and place it in your drawable-* directories.
I got non-static method setTitle(CharSequence) cannot be referenced from a static context error because I used setTitle() in static PlaceholderFragment class. I solved it by using getActivity().getActionBar().setTitle("new title");
You can also do as follow :
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main2)
setSupportActionBar(toolbar)
setTitle("Activity 2")
}
Go to AndroidManifest.xml file.
Find the <application> tag
There you can see a attribute
android:label="#string/app_name"
Now go to res > values > strings.xml
Change the
<string name="app_name">MainActivity</string>
to
<string name="app_name">Your Desired Name</string>
Example
AndroidManifest.xml
<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">
<activity
android:name=".MainActivity"
>
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".SubmitForm">
</activity>
</application>
strings.xml
<resources>
<string name="app_name">Your Desired Name</string>
<string name="action_settings">Settings</string>
</resources>

Hide the Android Notification bar on a Sencha Touch 2 app

I've successfully got my application compiled into an apk file, and deployed to my Android devices.
But when I run it there, the notification bar is present and I'd like it to be fullscreen, without the notification bar.
This is the bar at the top of the screen, with the battery usage, wifi/3G connection, new email icon, etc.
How do we hide this in our compiled apps with Sencha Touch 2?
I have set fullscreen: true in the config of the first view which is loaded (the log in screen), and have also set:
Ext.application({
viewport: {
autoMaximize: true
}
});
You could change this in your AndroidManifest.xml:
<application>
<activity
android:theme="#android:style/Theme.NoTitleBar.Fullscreen" >
</activity>
...
</application>
You can actually modify the AndroidManifest.xml template in the SenchaCmd to get this, as explained in the Sencha forums.
That file will be in Sencha/Cmd/<CmdVersion>/stbuild/st-res/android/AndroidManifest.xml. You need to set the theme property on the application tag like this: android:theme="#android:style/Theme.NoTitleBar"
with the whole tag looking like:
<application android:icon="#drawable/icon" android:label="%s" android:theme="#android:style/Theme.NoTitleBar">
However, this solution is still extremely crappy and I'd love to see something that works without patching Sencha's code.
Simply add this to make the screen fullscreen this.setTheme(android.R.style.Theme_NoTitleBar_Fullscreen); before super.onCreate(savedInstanceState);
OR
add this.requestWindowFeature(Window.FEATURE_NO_TITLE); and
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN) before setContentView
For example:
#Override
public void onCreate(Bundle savedInstanceState){
/**sets Theme and Style (must be set BEFORE super.onCreate)*/
this.setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
OR
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
/**Changes Window. Removes title and/or notification bar (must be set BEFORE setContentView)*/
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
(Or you can do it via the Manifest as what homer_simpson said.)

Wikitude personalize view on Android

I am creating an app using Wikitude API, but I haven't been able to customize the view.
I have asked the developers and I know I can't add buttons to the main view in the current release version (for Android), but I am wondering if I can add more buttons to the options menu. Right now when I press it I get just one button that says "Ignore Altitude" can I modify that button and/or add more buttons to that menu?
I have checked other posts but there aren't any answers. The posts are a little bit old so that is why I am asking again.
I haven't found any useful documentation.
Any help is greatly appreciated
if I understood your question correctly, then please try the following: in the method prepareIntent() of your main Activity, you can add up to 3 menu items:
intent.setMenuItem1("Menu 1", YourActivity.CALLBACK_INTENT);
intent.setMenuItem2("Menu 2", YourActivity.ANOTHERCALLBACK_INTENT);
Then you define the callback function as another activity (with dialog, list and stuffs). It works fine this way for me.
I am also playing around a bit with Wikitude, but hard to find something well documented!
Yes , You can Add upto three menu button as if you read doc properly
intent.setMenuItem1("Menu Name", YourActivity.LOCATION_INTENT);
intent.setMenuItem2("Menu Name", YourActivity.LOCATION_THREATS);
intent.setMenuItem3("Menu Name", YourActivity.MAP_INTENT);
With making Intent variable as
public static final String LOCATION_INTENT = "wikitudeapi.mylocationactivity";
Also declare action in manifest as,
<activity android:name=".activities.Your Activity Name"
android:theme="#*android:style/Theme.Translucent.NoTitleBar"
android:screenOrientation="portrait">
<intent-filter>
<action android:name="wikitudeapi.mylocationactivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>

Categories

Resources