title: Android TextView with Custom background?
body: <p>I need a "Button" that would have a texture background, and dynamic text. The end result should be like this, but the background is a textured picture.
  <a href="https://i.stack.imgur.com/sX5JU.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/sX5JU.png" alt="Custom Button with Text:"></a>
  I tried to use a <code>RelativeLayout</code> with a <code>TextView</code> inside, but the problem is that the result <code>CustomView</code> does not <code>wrap_content</code> in width/height.</p>
  
  <p>Here is the <code>custom_button</code><strong>XML</strong>:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:background="@drawable/custom_button_selector"
      android:layout_height="match_parent"&gt;
  
      &lt;TextView
          android:id="@+id/tv"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:gravity="center"
          android:layout_centerInParent="true"
          /&gt;
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Here is the <code>Code</code>:</p>
  
  <pre><code>public class CustomButton extends RelativeLayout {
  
  private LayoutInflater mInflater;
  
  private RelativeLayout mContainer;
  
  private TextView mTextView;
  
  public CustomButton(Context context) {
      super(context);
      mInflater = LayoutInflater.from(context);
      customInit(context);
  }
  
  public CustomButton(Context context, AttributeSet attrs) {
      super(context, attrs);
      mInflater = LayoutInflater.from(context);
      customInit(context);
  }
  
  public CustomButton(Context context, AttributeSet attrs, int defStyleAttr) {
      super(context, attrs, defStyleAttr);
      mInflater = LayoutInflater.from(context);
      customInit(context);
  }
  
  private void customInit(Context ctx) {
      mContainer = (RelativeLayout) mInflater.inflate(R.layout.cutom_button_view, this, true);
  
      mTextView = (TextView) mContainer.findViewById(R.id.tv);
  }
  
  public RelativeLayout getContainer() {
      return mContainer;
  }
  
  public TextView getTextView() {
      return mTextView;
  }
  }
  </code></pre>
  
  <p>This is how I use it at the moment, and it <strong>works</strong>:</p>
  
  <pre><code>&lt;com.myapp.views.CustomButton
                  android:id="@+id/btn"
                  android:layout_width="80dp"
                  android:layout_height="50dp"/&gt;
  </code></pre>
  
  <p>But I want to use it like this (for dynamic text), and it <strong>won't work</strong>:</p>
  
  <pre><code>&lt;com.myapp.views.CustomButton
                      android:id="@+id/btn"
                      android:layout_width="wrap_content"
                      android:layout_height="wrap_content"/&gt;
  </code></pre>
  
  <p>It will actually stretch across the whole screen, ignoring the layout rules.
  If this solution is a bad idea, could you recommend an alternative that would produce the same results?</p>