Question: Can you change the android id and onClick tag dynamically that was originally set in xml?

Question

Can you change the android id and onClick tag dynamically that was originally set in xml?

Answers 5
Added at 2016-12-29 11:12
Tags
Question

Is it possible to change the id and onClick tag dynamically?

    <Button
        android:id="@+id/actionAudio"
        android:text="{faw-microphone}"
        android:layout_weight = "2"
        android:layout_width="80dp"
        android:layout_height="60dp"
        android:onClick="onRecordAudio" />

For example, I'm trying to record an audio clicking a single button with id and onClick tag. That button will serve as the stop button also. To be able to do that, I'm thinking of dynamically changing those tags to manipulate it visually and also the onclick event.

Is this approach okay or are there better ways to do such thing. Thanks a lot!

Answers to

Can you change the android id and onClick tag dynamically that was originally set in xml?

nr: #1 dodano: 2016-12-29 11:12

You can set Id of the any View using this method

yourView.setId(int);

and set on click listener like this

View v = findViewById(id);
v.setOnClickListener(new View.OnClickListener() {
     public void onClick(View v) {
         // Perform action on click
     }
 });
nr: #2 dodano: 2016-12-29 11:12

I understood your scenario as I have implemented the Recording sound and playing Audio functionality just two days ago, However my answer is not a solution to your question, but it is a better and clean approach compared to yours,

  1. Create two different buttons in xml, one for record and one for stop.
  2. Specify different methods for onClick to both of them.
  3. Hide one of the button and display the other one according to the user click events in code(i.e change the visibility accordingly).

This way your logic is completely decoupled and separated, and you dont have to switch between button images, click event checks etc.

  • When user presses Record, its registered onClick method will be called, hide the clicked button in the method by setVisibility(View.GONE) and make the Stop button visible again by setVisibility(View.VISIBLE) and vice versa

Toggle Button can also get things done for you, but later when you will decide to add new buttons like playing, pausing sound, the above approach will work out well for you.

nr: #3 dodano: 2016-12-29 11:12

You can do this in many ways but the simple solution is for you use Toggle Button instead of normal Button.

<ToggleButton  
    android:id="@+id/toggleButton"  
    android:layout_width="wrap_content"  
    android:layout_height="wrap_content"  
    android:layout_alignParentLeft="true"  
    android:layout_alignParentTop="true"  
    android:layout_marginLeft="60dp"  
    android:layout_marginTop="18dp"  
    android:text="ToggleButton1"  
    android:textOn="Record"  
    android:textOff="Stop" /> 

and..

ToggleButton toggle = (ToggleButton) findViewById(R.id.togglebutton);
toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
    if (isChecked) {
        // The toggle is enabled
    } else {
        // The toggle is disabled
    }
}
});

Hope this will help you.

nr: #4 dodano: 2016-12-29 12:12
public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    Button button;
    boolean f=true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
// code
        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.button:
                if(f) {
                    button.setText("Stop");
                    f=false;
                    // your code
                }else{
                    Toast.makeText(this, "Stroped", Toast.LENGTH_SHORT).show();
                    button.setText("Activate");
                    f=true;
                }
        }
    }

The easiest way to do this using the if/else and you dont need to reset id

don't forget to implement View.OnClickListner

nr: #5 dodano: 2016-12-29 12:12

One more way to do that

button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            // Perform action on click
            if (((String) v.getTag()) != null && ((String) v.getTag()).equalsIgnoreCase("STOP")) {
                v.setTag("START");
                // start
                 // you can also use boolean like v.setTag(true)
            } else {
                v.setTag("STOP");
                // stop
            }
        }
    });
Source Show
◀ Wstecz