How do I create a button like AppCompat AlertDialog button?

I want to overlay a system window.

I am trying to create a button like AppCompat style.

I've tried this:

XML:

<LinearLayout 
    ...
    android:background="?android:attr/windowBackground"
/>

<!-- 
      My layout  ... 
      And button
      The buttonBarButtonStyle is in Theme.AppCompat and Theme.AppCompat.Light
 -->

 <Button 
      style="?attr/buttonBarButtonStyle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Close" />

      

Service:

LayoutInflater inflater;

public void setTheme(int theme)
{
    ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(this, theme);
    inflater = LayoutInflater.from(this).cloneInContext(contextThemeWrapper);
}

public void onCreate(){
    boolean themeDark = getThemeDark();
    setTheme(themeDark ? R.style.Theme_AppCompat : R.style.Theme_AppCompat_Light);
    super.onCreate();
    createView(); // in this method i'm only inflating view using: inflater.inflate(layoutId, null); and adding view to window
}

      

And it works in AppCompat but not AppCompat.Light. I have a button from AppCompat style (dark). Screenshots:

Dark (working):

Dark

Dark focusing (working):

Dark focused

Light (works):

Light

Focused light (not working):

Light focused

In this image, we can see that everything is in order. However, it looks ugly on the phone.

I want to make this button (I created in this same application this same AlertDialog theme using android.support.v7.app.AlertDialog) and it looks ok:

Light focused working

I don't know why it doesn't work. How should I do it? The problem is related to the background of the button when focusing. I don't know why the background is in dark style. I tried to install theme AlertDialog.AppCompat.Light but doesn't work.

+3


source to share


1 answer


I fixed it.

I replaced Button

with android.support.v7.widget.AppCompatButton

.



<android.support.v7.widget.AppCompatButton 
      style="?attr/buttonBarButtonStyle"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Close" />

      

Now it works.

+5


source







All Articles