Suppose I have button1 in the markup of the first fragment, and button2 in the second fragment. I want by clicking on button2 to change the parameters of button1 . And more specifically:

 android:text="" android:background="" 

and it would be very good if there is such an opportunity to change and id .

  android:id="@+id/" 

Why change the text and background understandable, not discussed. But why change the id now tell. The fact is that in the first fragment not one button button1, but two: button0 and button1 . In addition to the buttons in this markup, there is a TextView with certain text. Those. no button was pressed. Let's call this virgin state TextView - TX-0. When you click on the button0 text in the TextView changes. By pressing button1 text in the TextView changes again. Now let's go back to the moment when button0 was pressed and we should return to the TextView - TX-0 virgin state. For this, I want to change the text on the button button0 - since it has already been pressed and the sense from it is now no more than from an empty can of canned food. Suppose text and background tell me how to do it and I will register it in the case of the pressed button0.

  switch (view.getId()) { case R.id.button0: mSelectedItemView2.setText(Html.fromHtml(string3, null, new MyHtmlTagHandler())); Вот сюда пропишу break; case R.id.button1: mSelectedItemView2.setText(Html.fromHtml(string4, null, new MyHtmlTagHandler())); break; } 

But how to make it so that by pressing a second time on the same button you go back? It is clear that this will not work:

 case R.id.button0: mSelectedItemView2.setText(Html.fromHtml(string3, null, new MyHtmlTagHandler())); switch (view.getId()) { case R.id.button0: mSelectedItemView2.setText(Html.fromHtml(string2, null, new MyHtmlTagHandler())); - (это та самая TX-0) break; } break; 

Then I had an idea to change the id and in the second switch I’ll not apply to button0, but to the new id, which I will manage to assign in the case. Or can it somehow be done differently? Thanks for the help!

ps why did I initially talk about the second fragment and button2? It also has its own idea, so not superfluous.

  • myView.setId (); - not suitable? - Kirill Stoianov
  • 2
    what epic bike size did you come up with here ... Create a boolean isPressed field for boolean isPressed , and write to it whether the button was pressed or not. And learn from him when you need it. - Vladyslav Matviienko
  • thanks, boolean helped - Alexey

1 answer 1

The answer is about how to change the parameters of the View element from Java code.

YoursViewElement.setText("Текст"); - how to change the text on the button YoursViewElement.setBackgroundResource(R.drawable.название рисунка/картинка); - How to change the background , if it is not about color ( color ), but about a drawing / picture from drawable .