How to Hide an Icon of a two-icons App from App Drawer

cb56

Member
Licensed User
Longtime User
Hi,
i've developed an App with two activities - Main and Activity2 - and two differen icons and labels by insert this code in the Manifest Editor:

B4X:
'This code will be applied to the manifest file during compilation.
'You do not need to modify it in most cases.
'See this link for for more information: http://www.b4x.com/forum/showthread.php?p=78136
AddManifestText(
<uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
<supports-screens android:largeScreens="true" 
    android:normalScreens="true" 
    android:smallScreens="true" 
    android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")
SetActivityAttribute(Main, android:icon, "@drawable/star") 
SetActivityAttribute(Activity2, android:icon, "@drawable/stars") 
SetActivityAttribute(Main, android:label, "B4a 1") 
SetActivityAttribute(Activity2, android:label, "B4a 2") 
AddActivityText(Activity2,
<intent-filter>
 <action android:name="android.intent.action.MAIN" />
  <category android:name="android.intent.category.LAUNCHER" />
</intent-filter>)
'End of default text.

This is the result Manifest

B4X:
<?xml version="1.0" encoding="utf-8"?>
<manifest
   xmlns:android="http://schemas.android.com/apk/res/android"
   package="cb56.testtwoicons"
   android:versionCode="1"
   android:versionName="1"
   android:installLocation="internalOnly">
   
   <uses-sdk android:minSdkVersion="4" android:targetSdkVersion="14"/>
   <supports-screens android:largeScreens="true" 
       android:normalScreens="true" 
       android:smallScreens="true" 
       android:anyDensity="true"/>
   <uses-permission android:name="android.permission.INTERNET"/>
   <uses-permission android:name="android.permission.BLUETOOTH"/>
   <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
   <application
      android:icon="@drawable/icon"
      android:label="Test 2 Icons">
      <activity
         android:windowSoftInputMode="stateHidden"
         android:launchMode="singleTop"
         android:name=".main"
         android:label="B4a 1"
         android:screenOrientation="unspecified"
         android:icon="@drawable/star">
         <intent-filter>
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
         
      </activity>
      <activity
         android:windowSoftInputMode="stateHidden"
         android:launchMode="singleTop"
         android:name=".activity2"
         android:label="B4a 2"
         android:screenOrientation="unspecified"
         android:icon="@drawable/stars">
         
         <intent-filter>
          <action android:name="android.intent.action.MAIN" />
           <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
      </activity>
   </application>
</manifest>

The question is: can a program code hide and unhide only one of the two icons?

Mybe the way is to use Reflector and call "PackageManager" with method "setComponentEnabledSetting"?

Someone can explain how to do?

thanks

cb56
 
Last edited:

cb56

Member
Licensed User
Longtime User
Do you want to programmatically change the visible icon? I'm pretty sure that it is not possible.

I did and it works using Reflector and calling "setApplicationEnabledSetting" but this hides both icons.

I tried to hide only one icon with "setComponentEnabledSetting" which should allow you to disable an Activity but I can not make the call.


B4X:
setComponentEnabledSetting(ComponentName componentName, int newState, int flags)

//Set the enabled setting for a package component (activity, receiver, service, provider).
PackageManager | Android Developers

I do not understand how to pass as a parameter the class "ComponentName" as Reflector expects arrays or strings.

B4X:
Obj1.Target = Obj1.RunMethod4("setComponentEnabledSetting", args2, Types2)

cb56
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
This code will help you:
B4X:
Dim r As Reflector
Dim cn As Object = r.CreateObject2("android.content.ComponentName", _
   Array As Object("<package>", "<class>"), Array As String("java.lang.String", "java.lang.String"))
r.Target = <package manager Object>
'COMPONENT_ENABLED_STATE_ENABLED = 1
'COMPONENT_ENABLED_STATE_DISABLED = 2
r.RunMethod4("setComponentEnabledSetting", Array As Object(cn, 2, 0), _
   Array As String("android.content.ComponentName", "java.lang.int", "java.lang.int"))
 
Upvote 0

cb56

Member
Licensed User
Longtime User
Thanks for your help, this code works perfectly! :sign0098:

B4X:
Sub PMsetComponentEnabledSetting (enable As Boolean)

   Dim r As Reflector
   
   Dim cn As Object = r.CreateObject2("android.content.ComponentName",  _
      Array As Object("cb56.testtwoicons", "cb56.testtwoicons.activity2"), Array As String("java.lang.String", "java.lang.String"))
   
   r.Target = r.GetContext
   r.Target = r.RunMethod("getPackageManager")
   
   'COMPONENT_ENABLED_STATE_ENABLED = 1
   'COMPONENT_ENABLED_STATE_DISABLED = 2
   
   Dim e As Int

   If enable = True Then
      e = 1
   Else
      e = 2
   End If
   
   r.Target = r.RunMethod4("setComponentEnabledSetting", Array As Object(cn, e, 0), _
      Array As String("android.content.ComponentName", "java.lang.int", "java.lang.int"))

End Sub

cb56
 
Upvote 0

corsaro

Member
Licensed User
Longtime User
Thanks for your help, this code works perfectly! :sign0098:

B4X:
Sub PMsetComponentEnabledSetting (enable As Boolean)

   Dim r As Reflector
   
   Dim cn As Object = r.CreateObject2("android.content.ComponentName",  _
      Array As Object("cb56.testtwoicons", "cb56.testtwoicons.activity2"), Array As String("java.lang.String", "java.lang.String"))
   
   r.Target = r.GetContext
   r.Target = r.RunMethod("getPackageManager")
   
   'COMPONENT_ENABLED_STATE_ENABLED = 1
   'COMPONENT_ENABLED_STATE_DISABLED = 2
   
   Dim e As Int

   If enable = True Then
      e = 1
   Else
      e = 2
   End If
   
   r.Target = r.RunMethod4("setComponentEnabledSetting", Array As Object(cn, e, 0), _
      Array As String("android.content.ComponentName", "java.lang.int", "java.lang.int"))

End Sub

cb56




I am new is using reflection library.

cb56's code works fine to me. I just changed "cb56.testtwoicons", "cb56.testtwoicons.activity2" respectively with my package name and activity name.

Where can we find practical examples of reflector library usage, both with original java code and b4a code, so to learn better howto use this powerful library ? :sign0104:
 
Last edited:
Upvote 0

johnnyv

Member
Licensed User
Longtime User
I cannot get the code above to hide the icon in my app. I have a single activity. How can I get setComponentEnabledSettings to hide the icon in the launcher?
 
Upvote 0

johnnyv

Member
Licensed User
Longtime User
I tried the recommendation in that thread. While adding that code to the manifest file DOES hide the icon, it makes it so that I cannot launch the app after installation, AND my service doesn't start on boot as designed. I replied in another thread, so I will watch that thread.
 
Upvote 0
Top