1

Code

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="@null"
android:id="@+id/relativelayout"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto">

<Button
    android:id="@+id/button5"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_centerVertical="true"
    android:layout_marginEnd="9dp"
    android:layout_toStartOf="@+id/button12"
    android:background="@drawable/service_button_background"
    android:text="Akash" />

<Button
    android:id="@+id/button6"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_above="@+id/button5"
    android:layout_alignStart="@+id/button7"
    android:background="@drawable/service_button_background"
    android:text="Sanjana" />

<Button
    android:id="@+id/button7"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_alignStart="@+id/button5"
    android:layout_below="@+id/button5"
    android:layout_marginStart="19dp"
    android:background="@drawable/service_button_background"
    android:text="Sahana" />

<Button
    android:id="@+id/button12"
    android:layout_width="70dp"
    android:layout_height="70dp"
    android:layout_above="@+id/button5"
    android:layout_alignParentEnd="true"
    android:layout_marginBottom="30dp"
    android:background="@drawable/service_button_background"
    android:text="Akash" />

<Button
    android:layout_width="70dp"
    android:background="@drawable/service_button_background"
    android:text="Akash"
    android:layout_height="70dp"
    android:layout_alignParentEnd="true"
    android:layout_alignTop="@+id/button7"
    android:layout_marginTop="30dp"
    android:id="@+id/button11" />

Code

public class FloatingWindow extends Service{
WindowManager wm;
RelativeLayout ll;
LayoutInflater li;

@Override
public IBinder onBind(Intent intent) {
    // TODO Auto-generated method stub
    return null;
}

@Override
public void onCreate() {
    // TODO Auto-generated method stub
    super.onCreate();
    final View myview;
    li = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
    wm = (WindowManager) getSystemService(WINDOW_SERVICE);


    WindowManager.LayoutParams params = new WindowManager.LayoutParams(
            WindowManager.LayoutParams.TYPE_INPUT_METHOD |
            WindowManager.LayoutParams.TYPE_SYSTEM_OVERLAY,// | WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
            WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL,
            PixelFormat.TRANSLUCENT);

    /////////////////////////Another params


    params = new WindowManager.LayoutParams(
       750,1250,
            WindowManager.LayoutParams.TYPE_PHONE,
            WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
            PixelFormat.TRANSLUCENT);

    params.gravity = Gravity.RIGHT | Gravity.CENTER;
    myview = li.inflate(R.layout.service_pie, null); // your layout here

    wm.addView(myview, params);
    params.x = 0;
    params.y = 0

}

@Override
public void onDestroy() {
    super.onDestroy();
    stopSelf();
}

I have made the main layouts background as null but its still there... like how do i remove it... Meaning i have this service and when i start that service this activity will be inflated... But i want only the buttons to be inflated and not the whole activity... How do i achieve this.........................................................................

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115

3 Answers3

1

Simply add this in your manifest.

<activity
    android:name=".YOUR_ACTIVITY"
    android:theme="@android:style/Theme.Translucent.NoTitleBar" />

In addition to this set

android:background="@android:color/transparent" 

to your root element

Vir Rajpurohit
  • 1,869
  • 1
  • 10
  • 23
1

Try this solution

In manifest add this

 <uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" />

Then in your launcher activity or any base activity add these permissions

 private static final int CODE_DRAW_OVER_OTHER_APP_PERMISSION = 2084;

In onCreate() add this

if (SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
            Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
            startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
            //finish();
        }

Handle the request permission

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == CODE_DRAW_OVER_OTHER_APP_PERMISSION) {
            if (SDK_INT >= Build.VERSION_CODES.M && !Settings.canDrawOverlays(this)) {
                Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION, Uri.parse("package:" + getPackageName()));
                startActivityForResult(intent, CODE_DRAW_OVER_OTHER_APP_PERMISSION);
            } else {
               // do stuff here 
            }
        }
    }

Then Start your service and do this in service class , check the link below and do your stuff respectively https://medium.com/@kevalpatel2106/create-chat-heads-like-facebook-messenger-32f7f1a62064

Quick learner
  • 10,632
  • 4
  • 45
  • 55
  • Man just one more question if its not a trouble... can i add a feature like... The service should open when i touch the screen and should be open as long as my finger touches the screen and should stop once i leave it??? –  Sep 17 '18 at 11:48
  • i think you mean you want to show popup as long as the user touched the screen then make it invisible again ? – Quick learner Sep 17 '18 at 11:53
  • yeahhh... is there a way i can do that –  Sep 17 '18 at 11:54
  • according to me you can do that in your application only , like playing with touch up , touch down etc , not when the app is in background , i am not sure to be honest , wait i will check it for you – Quick learner Sep 17 '18 at 11:55
0

Create a style in your styles.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
  <style name="Theme.Transparent" parent="Theme.AppCompat.NoActionBar">
    <item name="android:windowIsTranslucent">true</item>
    <item name="android:windowBackground">@android:color/transparent</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowIsFloating">true</item>
    <item name="android:backgroundDimEnabled">false</item>
  </style>
</resources>

Set it on your activity tag in manifest.xml

<activity android:name=".YourActivity" android:theme="@style/Theme.Transparent">
...
</activity>

Don't set orientation on floating activity. As it will cause crash on Oreo devices.

Output

output

Update

Start your activity with clear top stack from service.

    Intent intent1 = new Intent(this, MainActivity.class);
    intent1.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
    startActivity(intent1);

Update 2

If you want put any overlay on android screen, then put SYSTEM_ALERT_WINDOW in manifest.

<uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW"/>
Khemraj Sharma
  • 57,232
  • 27
  • 203
  • 212
  • Edited answer, try `parent = "Theme.AppCompat.NoActionBar"` on theme. – Khemraj Sharma Sep 17 '18 at 10:39
  • this will not work in any case , he wants to show just some widgets using service class , and service class is not an activity class , you are asking to set style to an activity class which is considered as service class which will definitely not work – Quick learner Sep 17 '18 at 10:53
  • @Quicklearner Did you ever inflated a layout from service, it is done by an activity or dialog. So this solution will work. PO is starting Activity from Service. – Khemraj Sharma Sep 17 '18 at 10:56
  • @Quicklearner From the Question it clearly seems that He want to inflate an Activity(transparent) on the start of service. And also how service class will have layout? – Vir Rajpurohit Sep 17 '18 at 10:57
  • He wants to show some widget using service class , and activity wont work as a service class @VirRajpurohit – Quick learner Sep 17 '18 at 10:59
  • 1
    @Quicklearner Please tell me a way to show some layout from Service class, without using Dialog / Activity, perhaps I did not learn that. – Khemraj Sharma Sep 17 '18 at 11:03
  • @MichelleKinsten How are you showing your layout?, is that Activity or Dialog? – Khemraj Sharma Sep 17 '18 at 11:03
  • read more about this SYSTEM_ALERT_WINDOW , you will get a better way – Quick learner Sep 17 '18 at 11:07
  • will your code work when other application is running ? – Quick learner Sep 17 '18 at 11:08
  • 1
    the background is null and yeah that is fine... but i cant click anything in background cuz the activity stilll exists on top... just the color is transparent –  Sep 17 '18 at 11:15
  • sure i am waiting him to post appropriate code , and i am here to learn share knowledge not pointing out, if thats i wanted i would have down voted your answer :) – Quick learner Sep 17 '18 at 11:15
  • "the background is null and yeah that is fine... but i cant click anything in background cuz the activity stilll exists on top... just the color is transparent" See this i what i was talking , this wont work on any other application or in home screen coz activity is running and using all screen area :) – Quick learner Sep 17 '18 at 11:16
  • "we always ignore SYSTEM_ALERT_WINDOW because it is dangerous permission. Also when it can be done using Activity why we add dangerous permission. And SYSTEM_ALERT_WINDOW is not an class, which I asked for, in that we make Dialog, which I already told, that you can show layout from Service using Activity/ Dialog" - This is not wrong or any problem using it , Facebook messenger app is using System alert to show Chat Popup Just FYI :) – Quick learner Sep 17 '18 at 11:19
  • i will upload code on my git repo very soon for this requirement anyways – Quick learner Sep 17 '18 at 11:20
  • @Quicklearner He want to make floating widget service, you are right, @_Michelle see this https://stackoverflow.com/a/4482868/6891563 – Khemraj Sharma Sep 17 '18 at 11:30
  • @MichelleKinsten see updated answer. Please check this also https://www.simplifiedcoding.net/android-floating-widget-tutorial/ – Khemraj Sharma Sep 17 '18 at 11:34