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>
Related
Tried everything i could find on internet but cant seem to find a way to change the name of an Activity. When i test it it just shows the application name.
AndroidManifest
You should write label tag in activity tag not between activity tag
like this
<activity android:name=".MainActivity"
android:label="label_name(eg>#string/~~">
In your manifest, try android:label attribute:
<activity android:name=".MainActivity"
android:label="your_activity_readable_name">
According to android:label, if the attribute is not set, the label set for the application as a whole is used instead.
Use
setTitle(int titleId)
or
setTitle(CharSequence title)
on your onCreate() in Activity that you want to change the label
I would like to create an app that deals with background pictures of the a user device. But when a user clicks the shortcut icon, the background should change without opening the app and the icon should be somehow animated.
Let's consider this application in Itel:
Before I click on the app shortcut icon
And after click, the app doesn't open but the background changes and the icon animates(see the picture):
How can someone achieve this?
AFAIK we cannot change app icon at runtime. My guess, The icon in the example above may be a widget
About changing background wallpaper (or performing a particular task): I think we can create a launcher activity (a transparent one/without setContentView()) which will finish() itself after triggering a background service that changes the wallpaper (or perform any other task). According to my opinion, this can be a solution for the scenario above though I haven't tried it personally
Best regards, Happy coding :)
tl;dr You can't change it because the icon of your app is registered in the Android Manifest that ships with it.
From the documentation:
Icons and labels
A number of manifest elements have an icon and label attributes for
displaying a small icon and a text label, respectively, to users for
the corresponding app component.
That means that your app will always have the same icon since the manifest cannot be changed in runtime. So my guess is that the app you reference is a system app, with system privileges.
The only icons that you can change are the shortcuts your app creates using this permission:
<uses-permission android:name="com.android.launcher.permission.INSTALL_SHORTCUT"/>
To create a shortcut, please check this answer: https://stackoverflow.com/a/40446734/1574250
To change the background when clicking in your app icon, here is an example (in this example I'm only changing the background color when the app opens):
Class:
public class YourActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set you app icon
setColorWallpaper();
// Finish the activity
finish();
}
/**
* Sets the color wallpaper to the color value in the Clipboard, or to a random color.
*/
private void setColorWallpaper() {
// Try to get the color parameter from the clipboard
Integer colorParam = null;
try {
colorParam = ColorClipboardParameter.getColor(getApplication());
} catch (Exception ignored) {
// An unexpected exception while trying to get the color code from the clipboard
// can crash the app at startup. Ignore any exceptions, we will generate a random
// color anyway.
}
// If there is no valid color value in the clipboard, generate a random color
final int color = (colorParam != null) ? colorParam : GoodRandomColor.nextColor();
try {
// Set the color wallpaper
ColorWallpaper.setColorWallpaper(this, color);
// Success: copy the color code to the clipboard
Utils.copyText(this, Utils.colorToHex(color));
// Go to the home screen
Utils.goHome(this);
} catch (IOException e) {
// Write the stack trace to System.err and copy the reason of the failure to clipboard
e.printStackTrace();
Utils.copyText(this, e.toString());
}
}
}
Manifest:
<application
android:fullBackupContent="true"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:installLocation="auto"
android:label="#string/app_name"
android:theme="#style/AppTheme"
tools:ignore="GoogleAppIndexingWarning">
<activity
android:name=".YourActivity"
android:excludeFromRecents="true"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
Check this project for more info: https://github.com/appgramming/LoneColor-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."
I'm working in the project that to be a template to many apps.
So far everything is configured via JSON.
But I had a doubt: is It possible to change android:label via JSON?
this should to happen at runtime.
<application
android:allowBackup="true"
android:icon="#drawable/icon"
-> android:label="#string/app_name"
That is not at all possible. It is not possible to modify the value of a string resource from code
But, if you want to change the Title Bar of an activity, you can use
setTitle()
try this
this.setTitle("your title");
This will not change the label of the app
I want to make full screen activity with no title bar and no status bar. I tried below mentioned code, it make activity full screen but status bar/notification bar is visible for few seconds and then hides.
Is there any way I can make activity full screen as soon as it appears to user.
Code Tried: I tried it on android 2.3 and 4.1.2
1) Progrmatically:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.main);
}
}
2) Or via AndroidManifest.xml file:
<activity android:name=".MyActivity"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
</activity>
Add the theme to your <application> tag in manifest.xml
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#android:style/Theme.NoTitleBar.Fullscreen">
You are using Theme.NoTitleBar.Fullscreen so please use Theme.Black.NoTitleBar.Fullscreen instead that as i mention below.
android:theme="#android:style/Theme.Black.NoTitleBar.Fullscreen"
It works for me, hope it help to you...:)
You can simply do that by using WindowManager as you did but you just have to make change like;
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_semster_pdf);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
Try to add the code as mentioned above or just check out at this link it will help you
http://www.codespot.info/2017/11/how-to-hide-status-bar-in-android-and.html