Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android app without a layout

Tags:

android

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.

like image 772
Pablo Elices Avatar asked Feb 15 '23 00:02

Pablo Elices


2 Answers

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>
like image 157
Bunny Rabbit Avatar answered Feb 26 '23 22:02

Bunny Rabbit


Yes you can by adding:

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

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

like image 31
Thomas Kaliakos Avatar answered Feb 26 '23 23:02

Thomas Kaliakos