title: How to wrap content a button on this code? (Window manager) (Android)
body: <p>How can I make this button, a button created by code wrap its contents? This is a service using window manager to display the view. The Button is called mainButton. The problem is that that button filled the parent.</p>
  
  <pre><code>package com.toksis.pvscreen;
  
  import android.app.Service;
  import android.content.Context;
  import android.content.Intent;
  import android.content.SharedPreferences;
  import android.graphics.PixelFormat;
  import android.os.IBinder;
  import android.preference.PreferenceManager;
  import android.util.Log;
  import android.view.DragEvent;
  import android.view.Gravity;
  import android.view.LayoutInflater;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.WindowManager;
  import android.widget.Button;
  import android.widget.FrameLayout;
  import android.widget.LinearLayout;
  import java.io.FileInputStream;
  import java.io.FileNotFoundException;
  import java.io.FileOutputStream;
  import java.io.IOException;
  import java.io.ObjectInputStream;
  import java.io.ObjectOutputStream;
  import java.io.Serializable;
  
  /**
   * Created by god-gavedmework on 23/10/13.
   */
  public class DragDropButtonMainService extends Service{
      private LinearLayout main;
      private LinearLayout firstLayer;
      private LinearLayout secondLayer;
      private FrameLayout  vTopLeft;
      private FrameLayout  vTopRight;
      private FrameLayout  vBottomLeft;
      private FrameLayout  vBottomRight;
      private Integer gravity;
  
  
      private Button mainButton;
      private ViewGroup mView;
      private LayoutInflater inflater;
  
      public IBinder onBind(Intent intent) {
          return null;
      }
  
  
      public void onCreate()  {
          super.onCreate();
  
  
          createDragDropLayout();
  
  
  
      }
  
         void createDragDropLayout() {
  
             mainButton = new Button(this);
             mainButton.setText("Main");
  
             inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
             WindowManager wm = (WindowManager) getSystemService(WINDOW_SERVICE);
  
             WindowManager.LayoutParams params = new WindowManager.LayoutParams(
  
                     WindowManager.LayoutParams.TYPE_PHONE,
                     WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                             | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
                             | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
                     PixelFormat.TRANSLUCENT
             );
  
  
             params.gravity = Gravity.RIGHT | Gravity.TOP;
  
  
             LocationSerializable ls = new LocationSerializable();
  
  
  
             mView =  (ViewGroup) inflater.inflate(R.layout.dragdroplayout, null);
  
  
             vTopLeft   = (FrameLayout) mView.findViewById(R.id.topLeft);
             vTopRight  = (FrameLayout)  mView.findViewById(R.id.topRight);
             vBottomLeft = (FrameLayout) mView.findViewById(R.id.bottomLeft);
             vBottomRight= (FrameLayout) mView.findViewById(R.id.bottomRight);
             firstLayer = (LinearLayout) mView.findViewById(R.id.top);
             secondLayer = (LinearLayout) mView.findViewById(R.id.bottom);
  
  
  
  
             gravity = ls.getGravity(getApplicationContext());
  
  
             if (gravity  == (Gravity.LEFT | Gravity.TOP)){
  
                 vTopLeft.addView(mainButton);
  
  
             }
  
  
  
             if (gravity == (Gravity.RIGHT | Gravity.TOP)){
  
  
                 vTopRight.addView(mainButton);
  
  
  
  
             }
  
  
             if (gravity == (Gravity.RIGHT | Gravity.BOTTOM)){
  
  
                 vBottomRight.addView(mainButton);
  
  
  
  
             }
  
  
             if (gravity == (Gravity.LEFT | Gravity.BOTTOM)){
  
                 vBottomLeft.addView(mainButton);
  
  
  
             }
  
  
           //  mainButton.setLayoutParams(new WindowManager.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
  
          //   mainButton.setGravity(ls.getGravity(getApplicationContext()));
  
  
             wm.addView(mView,params);
  
             mainButton.setOnTouchListener(new MyTouchListener() );
             vTopLeft.setOnDragListener(new MyDragListener());
             vTopRight.setOnDragListener(new MyDragListener());
             vBottomLeft.setOnDragListener(new MyDragListener());
             vBottomRight.setOnDragListener(new MyDragListener());
  
  
             Log.d("tok", "add mview");
  
  
  
         }
  
  
      public void onDestroy() {
          super.onDestroy();
  
  
      }
  
  
  
  }
  </code></pre>
  
  <p>Container XML Layout.</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  
  &lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent" android:layout_height="match_parent"
      android:orientation="vertical"
      android:id="@+id/main"
      &gt;
          &lt;LinearLayout
              android:id="@+id/top"
              android:layout_height="match_parent"
              android:layout_width="match_parent"
              android:layout_weight="1"
              android:orientation="horizontal"
              &gt;
  
  
          &lt;FrameLayout
              android:id="@+id/topLeft"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
  
              android:tag="topLeft"
              android:layout_weight="1.0"
              android:background="#ff3d38"&gt;
  
  
          &lt;/FrameLayout&gt;
  
  
          &lt;FrameLayout
              android:id="@+id/topRight"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
              android:tag="topRight"
  
          android:layout_weight="1.0"
              android:background="#fff839"&gt;&lt;/FrameLayout&gt;
  
          &lt;/LinearLayout&gt;
  
      &lt;LinearLayout
          android:layout_height="match_parent"
          android:layout_width="match_parent"
          android:layout_weight="1"
          android:orientation="horizontal"
          android:id="@+id/bottom"
          &gt;
  
      &lt;FrameLayout
              android:id="@+id/bottomLeft"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
  
              android:layout_weight="1.0"
              android:background="#1e22ff"&gt;
  
          &lt;/FrameLayout&gt;
  
          &lt;FrameLayout
              android:id="@+id/bottomRight"
              android:layout_width="match_parent"
              android:layout_height="match_parent"
  
              android:layout_weight="1.0"
              android:background="#32ff1f"&gt;&lt;/FrameLayout&gt;
  
  
  &lt;/LinearLayout&gt;
  
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>The main button should wrap the MAIN text and not fill the parent view.</p>
  
  <p><img src="https://i.stack.imgur.com/8A8HA.png" alt="enter image description here"></p>