Remove icon from launcher - java

It is most popular problem and no one know how to resolve it...
For example:
<application
android:allowBackup="true"
android:icon="#drawable/add"
android:theme="#style/CustomTheme"
android:showAsAction="ifRoom|withText">
And now i need to hide android:icon or just i wanna to remove it
<application
android:allowBackup="true"
android:theme="#style/CustomTheme"
android:showAsAction="ifRoom|withText">
But in this case i have default android icon -_-
It is possible to permanent remove this icon?

This can remove your launcher icon after next reboot:
PackageManager pm = getPackageManager();
pm.setComponentEnabledSetting(getComponentName(), PackageManager.COMPONENT_ENABLED_STATE_DISABLED, PackageManager.DONT_KILL_APP);
Also this link can help you too:
Hide application launcher icon in title bar when activity starts in android

You may create you own icon picture.
So you may try just create picture which has necessary size and consist from transparent background only.

I have a solution! I had began learn java and android a couple of day ago, so my solution need verify by someone more experienced then me. Here u have the solution:
Step1
In AndroidManifest.xml file application section we need to declare NoTitleBar theme.
<application
...
android:theme="#android:style/Theme.NoTitleBar">
...
</application>
Step2
Now we have no title bar when our application loader (We set Theme.NoTitleBar), so we need to create it or change android:theme. I take a second option, so:
public class MainActivity extends SherlockActivity {
...
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
....
}
....
}
R.style.AppTheme is a style created by us but we can use anyone available style from android API.
Step3
Now our title bar is back but still we have our icon and title. So we need turn off it and now we have two options for do this. First is when we use android api 11 or later and we not need use ActionBarSherlock libs and second is for application lower then api 11.
API 11 or lower
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
actionBar = getActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
...
}
...
API 11 or later
...
#Override
public boolean onCreateOptionsMenu(Menu menu) {
actionBar = getSupportActionBar();
actionBar.setDisplayShowHomeEnabled(false);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
...
}
...
So, in first case we use default getActionBar() to manage actionBar but in second case we use getSupportActionBar from ActionBarSherlock
Done :)

Related

setDisplayHomeAsUpEnabled not working in one of my Activities but everywhere else it does work

For some reason this doesn't work in one of my activities but everywhere else it works fine when i press the back arrow in the toolbar it goes back to my MainActivity.
import androidx.appcompat.widget.Toolbar;
//Initializing toolbar
mToolbar = findViewById(R.id.mToolbar);
setSupportActionBar(mToolbar);
if((getSupportActionBar() != null)){
getSupportActionBar().setTitle(R.string.equalizerTitle);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_home_up);
}
Manifest
<activity
android:name=".activities.EqualizerActivity"
android:parentActivityName=".activities.MainActivity"
android:screenOrientation="portrait"/>
EDIT
This activity uses a constraint layout as parent but i don't see why this would be the cause, all other activities use RelativeLayout and there they work fine.
Looks like you are using onOptionsItemSelected() method to handle menu buttons. The issue is that the "back" button is also kind of a menu button.
In order for Android to know that you don't have a specific handler for the button and it should be handled by the framework, you need to let Android know it via returning false when you don't "consume" the event. According to the docs:
onOptionsItemSelected()
boolean Return false to allow normal menu processing to proceed, true to consume it here.
So, you should have smth like this:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case YOUR_BUTTON:
// do something
return true;
}
// By default, allow Android to work on it
return false;

Lock screen orientation when targeting Android API 27 with a non-opaque activity

I have an activity that has android:windowIsTranslucent set to true and android:windowBackground set to a translucent background. I just changed my target and compile sdk version to 27, and I get an exception when launching this activity now:
java.lang.IllegalStateException: Only fullscreen opaque activities can request orientation
Since this is a new sdk, there isn't anything online about it yet (and it seems to result from this line of code: https://android.googlesource.com/platform/frameworks/base.git/+/master/core/java/android/app/Activity.java#987 )
Is there any way to get around this? The app doesn't crash if I take out android:screenOrientation="portrait" from my manifest for this activity, but I would like to be able to keep it like that.
I also faced the same problem.
As others said, If I deleted android:screenOrientation="portrait" or overrided it with android:screenOrientation="unspecified", then the exception was gone.
And it seems that the front activity's orientation follows the behind activity's orientation.
I thought about it.
If the front activity is transparent and the behind activity's orientation is different,
the display becomes strange.
So, I can understand why this check logic was added
However, I wonder that why this problem was not occurred in Developer Preview 8.0.0.
The workaround is to set targetSdk back to 26.
The reason why is your application crashes is here in this commit.
As you can see here, you are not the only one - this behavior has been reported to Google as issue. It has been fixed, but we don't know how and when it will be released.
I can also confirm what "sofakingforever" says in comments, if there is non-translucent activity with fixed orientation behind your translucent, the translucent will not rotate. So you can just remove android:screenOrientation="portrait" from manifest as well.
The solution worked for me is deleting
android:screenOrientation="portrait"
from all the full screen transparent activities which means their theme contains
<item name="android:windowIsTranslucent">true</item>
Also to make sure that orientation works correct for below Oreo I added this to the onCreate() of the activities.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// This activity is a fullscreen transparent activity, so after Oreo Android doesn't allow fullscreen
// transparent activities to specify android:screenOrientation="portrait" in the manifest. It will pick up
// from the background activity. But for below Oreo we should make sure that requested orientation is portrait.
if (VERSION.SDK_INT < VERSION_CODES.O) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
}
I solved this issues by changing this line in NoActionBar styles
In target version 27 only i got this issue and i solved by using below line
<item name="android:windowIsTranslucent">false</item>
So what I did was remove any screenOrientation property from manifest and add it to my BaseActivity (from which all my activities extend), this code
if(!(this instanceof TranslucentActivity)){
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
The TranslucentActivity will have the orientation from the Activity behind.
it seems like it's a new feature/bug on API 27.
However, you can delete
android:screenOrientation
Or
android:screenOrientation="unspecified"
I recently faced the issue and here's the solution.
No need to change the screen orientation parameter which you set at the android manifest file.
Just add two folders in
res>values
as res>values-v26
and res>values-v27
Then copy your styles.xml and themes.xml file there.
and change the following parameters from TRUE to FALSE.
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowIsTranslucent">false</item>
It will work.
A common bug of Android 8.0
Thanks #JerabekJakub. My test result - keep sdk 27 and remove the following lines can also solve the crash.
android:configChanges="orientation"
android:screenOrientation="portrait"
1) Remove this
android:screenOrientation="portrait"
from minifiest.xml
2) on Activity add these two lines
protected void onCreate(Bundle savedInstanceState) {
setOrientation(this)
super.onCreate(savedInstanceState);
// other other all code here
}
3) Just copy-paste the code in your Activity
public static void setOrientation(Activity context) {
if (android.os.Build.VERSION.SDK_INT == Build.VERSION_CODES.O)
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED);
else
context.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
I had this problem on Android 8 using BottomSheetDialogs. It was crashing every time I open some translucent bottom sheet dialog.
I fixed the problem by adding new theme.xml in values-v26, with same theme as default one, but completely removing windowIsTranslucent item from there.
After that, I still had translucent bottom sheet dialogs in my app and it is working perfectly.
Found it interesting. It may help someone using these dialogs.

What might be preventing the HomeAsUp icon being displayed

I've been working on an app with multiple ActionBar activities, and from the outset the HomeAsUp button has been visible and working on all of the activities selected from the MainActivity. Today I noticed that it is no longer there - but I can't figure out what I have changed that would affect it.
The most significant change in recent days is that I migrated the project from Eclipse to Android Studio because I was having problems with Eclipse frequently corrupting my project files and requiring a complete reinstall.
This is a sample of the code in the main activity which creates the new activities:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
Intent intent=null;
int id = item.getItemId();
switch (id) {
case R.id.action_list:
intent = new Intent(this, StudyListActivity.class);
break;
// etc
}
if (intent != null) {
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
return true;
}
return super.onOptionsItemSelected(item);
}
Here is the declaration of the intent in the manifest
<activity
android:name="com.example.app.StudyListActivity"
android:label="#string/activity_list"
android:parentActivityName="com.example.app.MainActivity" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value="com.example.app.MainActivity" />
</activity>
since it stopped working I have also tried putting
getSupportActionBar().setHomeButtonEnabled(true);
in the onCreate method of the called activity (as per Display back button on action bar) but that makes no difference.
So I'm wondering if there is anything that got stripped out in my migration from eclipse which is preventing it showing (or something else I could have changed which might inadvertently have disabled it)
you should enable it via setDisplayOptions
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_HOME_AS_UP | ActionBar.DISPLAY_SHOW_HOME);
You could also try
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
In the activity you want the HomeAsUp icon in

android application crashes when rotating the screen

My app crashec when rotating the device. I know it crashes because whenI rotate the screen it tries to create new View to draw and display but how can it create the view and display it properly on the launch but crashes when trying to create the second new view?
I dont know what part of my code I should post here? Can you give a clue to solve this problem?
I found something usefull. I put try/catch statement and now when I rotate the device, it doesnt crash but displays a blank page, when I re-rotate to the old position it display the page. here is my code
#Override
protected void onCreate(Bundle savedInstanceState) {
try{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
} catch (Exception e) {
Log.i("my_error", e.getMessage());
}
}
but eclipse cannot catch the exception.
How can I solve this problem?
There are two possible ways to do this:
1) You don't care about the orientation change of the screen and you just want to be "resized". Just add the following properties in your Manifest file:
<activity
android:name=".SplashScreen"
android:configChanges="orientation|keyboardHidden|screenSize" ... />
and add this lines of code in your activity:
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// do nothing, just override
}
2) You do care about the orientation change. Then there are a couple of things that you should know about the activity lifecycle in an orientation change.
Firstly the onConfigurationChanged() is called and you should store in a Bundle everything that you need. For example the user has editted an EditText and you should save the value.
Why to do that? Because afterwards the onCreate() method will be called again in order for you to implement a different layout or alter something in the Views. You should then take the previously saved Bundle and restore the variables that you saved in your newly created layout.
Just add the android:configChanges="orientation" to your activity
<activity android:name=".MyActivity"
android:configChanges="orientation"/>
if you given like this, then oncreate will not called if your screen orientation is changed.
If you need more information about this you can check this link
There are a couple of things you can do:
1) Ignore screen orientation changes. If your app is only designed for portrait mode, most likely it won't look good in landscape mode and vice versa. Best is to specify the target orientation in your activities.
2) If you want to allow orientation changes, then you should have written some code re-create the view with different/modified layout. Mind sharing the code with us?
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
//Override this and recreate your view or set adapter here
}
Dont forget to add android:configChanges="orientation" in your manifest.
On a side note you can fix orientation through manifest like this -
android:screenOrientation="portrait"
Add this to your Activity tag in the manifest:
android:configChanges="orientation|screenSize"
This will cause that the activity is not recreated on rotation change.
The flag sreenSize is for newer devices(API level 13 or higher), with including only the orientation flag the activity will stil recreate when rotating on devices running API level 13 or higher.

ActionBar Compatibility issue with API 15

I have an issue with actionBar compatibility on API level 15 for Android. The up button doesn't work well with this API level
I use the sample project called "actionbarcompat" provided in the android-sdk folder, so I have imported all class's and I extends all my activities with ActionBarActivity. I also add this piece of code in the Manifest for all my activities :
<activity
android:name="fr.appsolute.rescue.MyActivity"
android:label="#string/title_activity_info"
android:parentActivityName=".MainActivity"
android:screenOrientation="portrait" >
<meta-data
android:name="android.support.PARENT_ACTIVITY"
android:value=".MainActivity" />
</activity>
and in my ActionBarActivity :
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
This permit the user to touch the up button (with the app icon) to return to the MainActivity
This code works well with Android 2.3.3 and 4.2, but doesn't work with 4.0.1 and I don't understand why. When the user touch the up button, nothing happens.
Thanks for your help.
PS : I can't use an external library, I have to use native code
Not sure why it isn't working. I agree it is strange that it only fails on API 15.
An alternative to calling NavUtils.navigateUpFromSameTask(this) could be calling finish(). This would close your current activity and go to the previous activity in the stack. It may give you the same desired result.
Solved it using ActionBarSherlock

Categories