Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to create button like AppCompat AlertDialog Button?

I want to overlay system window.

I'm trying to create button like AppCompat style.

I have 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's working in AppCompat but not in AppCompat.Light. I have the button from style AppCompat (dark). Screenshots:

Dark (works):

Dark

Dark focused (works):

Dark Focused

Light (works):

Light

Light focused (not working):

Light focused

In this image we can see all is ok. However in a phone it looks ugly.

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

Light focused working

I don't know why it's not working. How can I do it? The problem is with button background when focused. I don't know why the background is from dark style. I was tried to set AlertDialog.AppCompat.Light theme but not working.

like image 880
krystian71115 Avatar asked Apr 01 '26 04:04

krystian71115


1 Answers

I have 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's working.

like image 186
krystian71115 Avatar answered Apr 02 '26 20:04

krystian71115