4

I'm a totally noob on Android, is there a way to execute an app without a layout? The process would be like: Click app icon -> run some code (Without prompting any window) -> display toast.

djot
  • 2,952
  • 4
  • 19
  • 28
Pablo Elices
  • 43
  • 1
  • 4

3 Answers3

5

The trick is to open a transparent activity, show the toast and finish the activity, which makes it look like only the toast is displayed because the activity which opened was transparent.

To do this you can do.

public class MainActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
       Toast.makeText(this, messageToBeDisplayed, Toast.LENGTH_SHORT).show();
       // finish the activity as soon as it opened.
       this.finish();
   }
}

Also you need to give a transparent theme to your activity by specifying it in AndroidManifest.xml, For which you can use NoDisplayeTheme provided by Android like this.

<activity android:name="TransparentActivity"
          android:theme="@android:style/Theme.NoDisplay">
</activity>
Bunny Rabbit
  • 8,213
  • 16
  • 66
  • 106
3

Yes you can by adding:

android:theme="@android:style/Theme.NoDisplay"

in your activity in Android manifest.
Check this answer for more details.

Community
  • 1
  • 1
Thomas Kaliakos
  • 3,274
  • 4
  • 25
  • 39
0

Use this:

public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

   Toast.makeText(this, "", Toast.LENGTH_SHORT).show();

   this.finish();
     }
  }

and in manifest file add: android:theme="@android:style/Theme.NoDisplay"

ImonBayazid
  • 1,066
  • 3
  • 16
  • 41