ContextThemeWrapper to change styles in android

Viewed 590

I've seen several threads related to a similar question but I haven't been able to successfully apply it to my case. Actually I'm trying to change the style of a button when it's clicked by the user using a ContextThemeWrapper. So initially the button is set to a certain style that I linked inside the XML document, and I include this code to try to make it change in appearance by using a ContextThemeWrapper and according to what was advised in the first link below. The problem (I think) is that I don't know how to link that newly created button to the physical object that I see on the screen. Any suggestion is welcome! Thanks!

    public class MainActivity extends AppCompatActivity {

    private Button TestButton;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TestButton = findViewById(R.id.button);

        TestButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ContextThemeWrapper newContext = new ContextThemeWrapper(getApplicationContext(), R.style.custom);

                 TestButton = new TextView(newContext);

            }
        });
    }
}

Here are the links I'm referrring to: ContexThemeWrapper to change style General question about different contexts

2 Answers

You have several options to change the style of the button dynamically.

  1. Change every attribute by hands:

    TestButton.setTextColor(Color.RED);
    TestButton.setTextSize(24);
    // etc.
    
  2. Create a new button and replace the old one in layout:

    ContextThemeWrapper newContext = new ContextThemeWrapper(MainActivity.this, R.style.custom);
    NewTestButton = new NewTestButton(newContext);
    Container.removeView(TestButton)
    Container.addView(NewTestButton)
    

Agree, difficult without the code... Attach what I've done so far:

public class MainActivity extends AppCompatActivity {
    private Button TestButton;
    private Button NewTestButton;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        TestButton = (Button) findViewById(R.id.button);

    }

    public void doSomething(View view) {
        ViewGroup parent = (ViewGroup) view.getParent();

        parent.removeView(view);
        ContextThemeWrapper newContext = new ContextThemeWrapper(MainActivity.this, R.style.custom);
        NewTestButton = new Button(newContext);
        parent.removeView(view);
        parent.addView(NewTestButton);

    }
}

This is the XML concerning for the main_activity

<Button
        android:id="@+id/button"
        style="@style/original"
        android:onClick="doSomething"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

And both styles applied before and after the click...

<style name="custom">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginTop">200dp</item>
        <item name="android:background">@android:color/black</item>
        <item name="android:text">Custom Button</item>
        <item name="android:textColor">@android:color/white</item>

    </style>
    <style name="original">
        <item name="android:layout_width">wrap_content</item>
        <item name="android:layout_height">wrap_content</item>
        <item name="android:layout_marginTop">200dp</item>
        <item name="android:background">@android:color/white</item>
        <item name="android:text">Original Button</item>
        <item name="android:textColor">@android:color/black</item>

    </style>

The problem is that within the style I cannot include data regarding constraints, so once I remove the view and create a new one, this one is not properly positioned. Also, the effects of background/text color are not achieved... I only was able to get a successful result resorting to LayoutInflater... although I'd love to get to grips with this method.... Thanks for the help!

Related