Başka bir değişle android switch butonu tanımlama yapacağız arkadaşlar ilk olarak yapmamız gerekenleri listemek istiyorum
1. Makalem de android studio yoksa eğer ilk olarak https://mdsal.com/android-studio-indir.htmlAndroid Studio İndir adlı makalem de android studio yu indirelim ve https://mdsal.com/android-studio-kurulum.htmlAndroid Studio Kurulum adlı makalem deki kurulum işlemin yapalım.
ardından android studio yu açalım ve build.gradle ekleyelim ve ardından projenizi senkronize edin
compile ‘com.github.zcweng:switch-button:0.0.3@aar’
Ardından, XML Dosyanıza aşağıdaki kodu ekleyin
android:id=“@+id/swithButton”
android:layout_width=“100dp”
android:layout_height=“60dp”
android:layout_marginLeft=“30dp”
android:layout_below=“@+id/textView”
android:layout_margin=“10dp”
android:tint=“@color/colorAccent”
android:layout_centerHorizontal=“true”
android:paddingRight=“16dp”
android:layout_gravity=“center”
app:sb_button_color=“@color/white”
app:sb_shadow_color=“#A36F95”
app:sb_background=“#FFF”
app:sb_checkline_color=“@color/green”
app:sb_checked_color=“@color/green”
app:sb_uncheck_color=“@color/red”
app:sb_uncheckcircle_color=“@color/red”/>
Java Sınıfı dosyanızda Aşağıya kod ekleyin
package com.androidmkab.swich;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.Toast;
import com.suke.widget.SwitchButton;
public class MainActivity extends AppCompatActivity {
SwitchButton swithButton;
SharedPreferences.Editor prefEditor;
SharedPreferences prefs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
swithButton = findViewById(R.id.swithButton);
prefEditor = PreferenceManager.getDefaultSharedPreferences(getBaseContext()).edit();
prefs = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
swithButton.setOnCheckedChangeListener(new SwitchButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(SwitchButton view, boolean isChecked) {
if (isChecked){
// DO what ever you want here when button is enabled
Toast.makeText(MainActivity.this, “Enabled”, Toast.LENGTH_SHORT).show();
prefEditor.putString(“checked”,”yes”);
prefEditor.apply();
}else {
// DO what ever you want here when button is disabled
Toast.makeText(MainActivity.this, “Disabled”, Toast.LENGTH_SHORT).show();
prefEditor.putString(“checked”,”false”);
prefEditor.apply();
}
}
});
if (prefs.getString(“checked”,”no”).equals(“yes”)){
swithButton.setChecked(true);
}else {
swithButton.setChecked(false);
}
}
}
Bu işlem sonucunda geçiş butonu artık aktiftir.
Makalem için https://mdsal.com/android-gecis-dugmesi-nasil-eklenir.html