Malo
Malo

Reputation: 1606

click toggle button programmatically in android

hello i have a toggle button like this :

  <RadioGroup android:layout_height="wrap_content"
  android:layout_width="fill_parent"
  android:orientation="horizontal">


    <ToggleButton android:id="@+id/detailed_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
        android:text="@string/detailed_view"
        android:textOff="@string/detailed_view"
        android:textOn="@string/detailed_view" 
   android:onClick="viewChange"  />

     <ToggleButton android:id="@+id/main_view"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
        android:text="@string/main_view"
        android:textOff="@string/main_view"
        android:textOn="@string/main_view" 
        android:checked="true"
        android:onClick="viewChange"
    />
  </RadioGroup>

and i have the function viewChange like this:

    public void viewChange(View v){
    int id=v.getId();
    if(id==R.id.main_view){
        //do some thing with main view

    }else if(id==R.id.detailed_view){
    do something with detailed view
    }
}

Now i need to call this function ViewChange(View v) and set the parameter v programatically, i means that i want to click this toggle button programatically and specify which view i want to click.

Upvotes: 0

Views: 2106

Answers (2)

Willis
Willis

Reputation: 5336

You can programmatically change the state of the ToggleButton by using the setChecked method:

toggleButton.setChecked(true or false)

More information on the ToggleButton may be found in the API reference: http://developer.android.com/guide/topics/ui/controls/togglebutton.html

Upvotes: 3

Related Questions