3

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

H1991
  • 91
  • 1
  • 2
  • 7
  • This question would attract better answers if you made it clearer what the exact question is, and if you linked to the other threads you mention. – MikaelF Mar 14 '20 at 02:18

2 Answers2

0

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)
    
Bracadabra
  • 3,609
  • 3
  • 26
  • 46
  • Hello Bracadabra, thanks for taking the time! Actually now it does something,(small win) although is far from the desired result. I've tried to link everything that defines de button inside the style resources (layout restrictions included) but they do not work. As a result when I take the new button into scene its position is far off where it should be and also it seems that the only attribute that is followed is textColor which seems odd... – H1991 Mar 14 '20 at 15:20
  • Hello, it is hard to say where is the problem without code. – Bracadabra Mar 15 '20 at 09:20
0

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!

H1991
  • 91
  • 1
  • 2
  • 7