Note that there are some explanatory texts on larger screens.

plurals
  1. POToggle button doesn't work at the first time on Android
    primarykey
    data
    text
    <p>I wrote an Android app with toggle button but toggle button does not work well at the first time. But after unchecking, if you try again, it is working properly after that.</p> <p>I listed below my codes.</p> <p>Which codes I have to add to activate it for the first time?</p> <pre><code>public class MainActivity extends Activity{ private ToggleButton togg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); togg = (ToggleButton) findViewById(R.id.toggleButton1); } public void nameOfMethod(View v){ togg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (togg.isChecked()) { //Toast.makeText(MainActivity.this, "Servise bağlanılıyor...", Toast.LENGTH_SHORT).show(); new Thread(new ClientThread()).start(); } else { Toast.makeText(MainActivity.this, "Bağlantı sonlandırılıyor...", Toast.LENGTH_SHORT).show(); } } }); } </code></pre> <p>in .xml file:</p> <pre><code>&lt;ToggleButton android:id="@+id/toggleButton1" android:layout_width="120dp" android:layout_height="60dp" android:layout_below="@+id/textView1" android:layout_centerHorizontal="true" android:layout_marginTop="20dp" android:onClick="nameOfMethod" android:textOn="Bağlantıyı bitir" android:textOff="Bağlantıyı başlat" /&gt; </code></pre> <p>EDIT: It works just fine like that:</p> <pre><code>public class MainActivity extends Activity{ private ToggleButton togg; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); togg = (ToggleButton) findViewById(R.id.toggleButton1); togg.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (togg.isChecked()) { //Toast.makeText(MainActivity.this, "Servise bağlanılıyor...", Toast.LENGTH_SHORT).show(); new Thread(new ClientThread()).start(); } else { Toast.makeText(MainActivity.this, "Bağlantı sonlandırılıyor...", Toast.LENGTH_SHORT).show(); } } }); } </code></pre> <p>with moving setOnClickListener method to onCreate(Bundle savedInstanceState) and deleting android:OnClick method in .xml file...</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload