Pablo Elices
Pablo Elices

Reputation: 43

Android app without a layout

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.

Upvotes: 4

Views: 4402

Answers (3)

Bunny Rabbit
Bunny Rabbit

Reputation: 8411

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>

Upvotes: 5

ImonBayazid
ImonBayazid

Reputation: 1096

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"

Upvotes: 0

Thomas Kaliakos
Thomas Kaliakos

Reputation: 3304

Yes you can by adding:

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

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

Upvotes: 3

Related Questions