title: Inflate Floating Action Button from android.support.design.widget.FloatingActionButton
body: <p><em>(Project overview part)</em></p>
  
  <p>I would like to do flying button which is over all the other apps and system icons. Once you press it, kind of menu pops up where you have several options to choose from. Once you press and move the button, you can move it to any place on a screen.</p>
  
  <p>So far, I have both flying button on a screen (actually flying bmp file) in one project and floating button with popping up menu in second project. </p>
  
  <p>What I want to do now is to replace bmp file with real floating button with menu. </p>
  
  <p>The way I'm trying to do that:</p>
  
  <p><em>(The most important starts here) :)</em></p>
  
  <p>I add flying button to windowManager by addView method. Flyingbutton is an View object. This is the point where I came a cropper. </p>
  
  <p>I was trying to carry out how to find view by id in Service class and I did that by inflating  android.support.design.widget.FloatingActionButton class
  but now I see the inflate method doesn't go well with that class.</p>
  
  <p>I saw few threads on Stack Overflow but none of them helped me.</p>
  
  <p>Any idea?</p>
  
  <p>Perhaps you will see that the background so far is not transparent but this not the problem at this moment. :) </p>
  
  <p><strong>logcat says...</strong></p>
  
  <pre><code>FATAL EXCEPTION: main
  
  Process: com.example.komp.floatingbuttonpowtorka, PID: 32452
  
  java.lang.RuntimeException: Unable to create service com.example.komp.floatingbuttonpowtorka.FlyingButton: android.view.InflateException: Binary XML file line #8: Error inflating class &lt;unknown&gt;
      at android.app.ActivityThread.handleCreateService(ActivityThread.java:2780)
      at android.app.ActivityThread.access$1800(ActivityThread.java:151)
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424)
      at android.os.Handler.dispatchMessage(Handler.java:110)
      at android.os.Looper.loop(Looper.java:193)
      at android.app.ActivityThread.main(ActivityThread.java:5333)
      at java.lang.reflect.Method.invokeNative(Native Method)
      at java.lang.reflect.Method.invoke(Method.java:515)
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828)
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644)
      at dalvik.system.NativeStart.main(Native Method)
  Caused by: android.view.InflateException: Binary XML file line #8: Error inflating class &lt;unknown&gt;
      at android.view.LayoutInflater.createView(LayoutInflater.java:620)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
      at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
      at com.example.komp.floatingbuttonpowtorka.FlyingButton.onCreate(FlyingButton.java:63)
      at android.app.ActivityThread.handleCreateService(ActivityThread.java:2770)
      at android.app.ActivityThread.access$1800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424) 
      at android.os.Handler.dispatchMessage(Handler.java:110) 
      at android.os.Looper.loop(Looper.java:193) 
      at android.app.ActivityThread.main(ActivityThread.java:5333) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
      at dalvik.system.NativeStart.main(Native Method) 
  Caused by: java.lang.reflect.InvocationTargetException
      at java.lang.reflect.Constructor.constructNative(Native Method)
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
      at android.view.LayoutInflater.createView(LayoutInflater.java:594)
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
      at com.example.komp.floatingbuttonpowtorka.FlyingButton.onCreate(FlyingButton.java:63) 
      at android.app.ActivityThread.handleCreateService(ActivityThread.java:2770) 
      at android.app.ActivityThread.access$1800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424) 
      at android.os.Handler.dispatchMessage(Handler.java:110) 
      at android.os.Looper.loop(Looper.java:193) 
      at android.app.ActivityThread.main(ActivityThread.java:5333) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
      at dalvik.system.NativeStart.main(Native Method) 
  Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
      at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:34)
      at android.support.design.widget.FloatingActionButton.&lt;init&gt;(FloatingActionButton.java:110)
      at android.support.design.widget.FloatingActionButton.&lt;init&gt;(FloatingActionButton.java:104)
      at java.lang.reflect.Constructor.constructNative(Native Method) 
      at java.lang.reflect.Constructor.newInstance(Constructor.java:423) 
      at android.view.LayoutInflater.createView(LayoutInflater.java:594) 
      at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696) 
      at android.view.LayoutInflater.rInflate(LayoutInflater.java:755) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:492) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:397) 
      at android.view.LayoutInflater.inflate(LayoutInflater.java:353) 
      at com.example.komp.floatingbuttonpowtorka.FlyingButton.onCreate(FlyingButton.java:63) 
      at android.app.ActivityThread.handleCreateService(ActivityThread.java:2770) 
      at android.app.ActivityThread.access$1800(ActivityThread.java:151) 
      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1424) 
      at android.os.Handler.dispatchMessage(Handler.java:110) 
      at android.os.Looper.loop(Looper.java:193) 
      at android.app.ActivityThread.main(ActivityThread.java:5333) 
      at java.lang.reflect.Method.invokeNative(Native Method) 
      at java.lang.reflect.Method.invoke(Method.java:515) 
      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:828) 
      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:644) 
      at dalvik.system.NativeStart.main(Native Method)
  </code></pre>
  
  <p><strong>MainActivity.java</strong></p>
  
  <pre><code>public class MainActivity extends AppCompatActivity {
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
  
          startService(new Intent(MainActivity.this, FlyingButton.class));
          finish();
  }
  
  @Override
  protected void onResume() {
      Bundle bundle = getIntent().getExtras();
  
      if(bundle != null &amp;&amp; bundle.getString("LAUNCH").equals("YES")) {
          startService(new Intent(MainActivity.this, FlyingButton.class));
      }
      super.onResume();
  }
  }
  </code></pre>
  
  <p><strong>FlyingButton.java</strong></p>
  
  <pre><code>public class FlyingButton extends Service {
  
  private WindowManager windowManager;
  private ImageView chatHead;
  
  @Override
  public IBinder onBind(Intent intent) {
      // TODO Auto-generated method stub
      return null;
  }
  
  @Override
  public void onCreate() {
      super.onCreate();
  
      windowManager = (WindowManager) getSystemService(WINDOW_SERVICE);
  
      chatHead = new ImageView(this);
  
      chatHead.setImageResource(R.drawable.floating);
  
  
      LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
      View layout = inflater.inflate(R.layout.activity_main, null);
  
      FloatingActionButton fab = (FloatingActionButton) layout.findViewById(R.id.fab);
  
      final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
              WindowManager.LayoutParams.WRAP_CONTENT,
              WindowManager.LayoutParams.WRAP_CONTENT,
              WindowManager.LayoutParams.TYPE_PHONE,
              WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
              PixelFormat.TRANSLUCENT);
  
      params.gravity = Gravity.TOP | Gravity.LEFT;
      params.x = 0;
      params.y = 100;
  
      windowManager.addView(fab, params);
  
      try {
          chatHead.setOnTouchListener(new View.OnTouchListener() {
              private WindowManager.LayoutParams paramsF = params;
              private int initialX;
              private int initialY;
              private float initialTouchX;
              private float initialTouchY;
  
              @Override public boolean onTouch(View v, MotionEvent event) {
                  switch (event.getAction()) {
                      case MotionEvent.ACTION_DOWN:
  
                          // Get current time in nano seconds.
  
                          initialX = paramsF.x;
                          initialY = paramsF.y;
                          initialTouchX = event.getRawX();
                          initialTouchY = event.getRawY();
                          break;
                      case MotionEvent.ACTION_UP:
                          break;
                      case MotionEvent.ACTION_MOVE:
                          paramsF.x = initialX + (int) (event.getRawX() - initialTouchX);
                          paramsF.y = initialY + (int) (event.getRawY() - initialTouchY);
                          windowManager.updateViewLayout(chatHead, paramsF);
                          break;
                  }
                  return false;
              }
          });
      } catch (Exception e) {
          // TODO: handle exception
      }
  
  }
  
  @Override
  public void onDestroy() {
      super.onDestroy();
      if (chatHead != null) windowManager.removeView(chatHead);
  }
  
  }
  </code></pre>
  
  <p><strong>activity_main.xml</strong></p>
  
  <pre><code> &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:layout_width="match_parent"
  android:layout_height="match_parent"
  xmlns:app="http://schemas.android.com/apk/res-auto"
  tools:context=".MainActivity"&gt;
  
  &lt;android.support.design.widget.FloatingActionButton
      android:id="@+id/fab"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:layout_gravity="bottom|end"
      android:layout_margin="16dp"
      android:src="@drawable/filter"
      app:backgroundTint="@color/colorAccent"
      xmlns:android="http://schemas.android.com/apk/res/android" /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p><strong>styles.xml</strong></p>
  
  <pre><code>&lt;resources&gt;
  &lt;!-- Base application theme. --&gt;
  &lt;style name="AppTheme" parent="Theme.AppCompat"&gt;
      &lt;!-- Customize your theme here. --&gt;
      &lt;item name="colorPrimary"&gt;@color/colorPrimary&lt;/item&gt;
      &lt;item name="colorPrimaryDark"&gt;@color/colorPrimaryDark&lt;/item&gt;
      &lt;item name="colorAccent"&gt;@color/colorAccent&lt;/item&gt;
  &lt;/style&gt;
  </code></pre>
  
  <p></p>
  
  <p><strong>androidmanifest.xml</strong></p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;manifest xmlns:android="http://schemas.android.com/apk/res/android"
  package="com.example.komp.floatingbuttonpowtorka"
  android:versionCode="1"
  android:versionName="1.0" &gt;
  
  &lt;uses-sdk
  android:minSdkVersion="10"
  android:targetSdkVersion="18" /&gt;
  
  &lt;uses-permission android:name="android.permission.SYSTEM_ALERT_WINDOW" /&gt;
  
  &lt;application
  android:allowBackup="true"
  android:icon="@mipmap/ic_launcher"
  android:label="@string/app_name"
  android:theme="@style/Theme.AppCompat" &gt;
  
  &lt;activity
      android:name=".MainActivity"
      android:label="@string/app_name"
      android:screenOrientation="portrait"
      android:theme="@style/Theme.AppCompat"&gt;
      &lt;application android:name=".ApplicationContextProvider"
          android:label="@string/app_name"/&gt;
      &lt;intent-filter&gt;
          &lt;action android:name="android.intent.action.MAIN" /&gt;
  
          &lt;category android:name="android.intent.category.LAUNCHER" /&gt;
      &lt;/intent-filter&gt;
  &lt;/activity&gt;
  &lt;service
      android:name="com.example.komp.floatingbuttonpowtorka.FlyingButton"
      android:exported="true" /&gt;
  &lt;/application&gt;
  
  &lt;/manifest&gt;
  </code></pre>
  
  <p><strong>gradle</strong>
      apply plugin: 'com.android.application'</p>
  
  <p>android {
      compileSdkVersion 23
      buildToolsVersion "23.0.3"</p>
  
  <pre><code>defaultConfig {
      applicationId "com.example.komp.floatingbuttonpowtorka"
      minSdkVersion 15
      targetSdkVersion 23
      versionCode 1
      versionName "1.0"
  }
  buildTypes {
      release {
          minifyEnabled false
          proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
      }
  }
  }
  
  dependencies {
  compile fileTree(dir: 'libs', include: ['*.jar'])
  testCompile 'junit:junit:4.12'
  compile 'com.android.support:appcompat-v7:23.1.1'
  compile 'com.android.support:design:23.1.1'
  }
  </code></pre>