Last active
April 5, 2020 22:36
-
-
Save MohammadSamandari/b914d5220618058283c9a34f2453b1e5 to your computer and use it in GitHub Desktop.
Android-AppSetting
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mohammad.samandari.appwithsettings; | |
import android.content.Intent; | |
import android.content.SharedPreferences; | |
import android.os.Bundle; | |
import com.google.android.material.floatingactionbutton.FloatingActionButton; | |
import com.google.android.material.snackbar.Snackbar; | |
import androidx.appcompat.app.AppCompatActivity; | |
import androidx.appcompat.widget.Toolbar; | |
import androidx.preference.PreferenceManager; | |
import android.view.View; | |
import android.view.Menu; | |
import android.view.MenuItem; | |
import android.widget.Toast; | |
public class MainActivity extends AppCompatActivity { | |
@Override | |
protected void onCreate (Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
setContentView(R.layout.activity_main); | |
Toolbar toolbar = findViewById(R.id.toolbar); | |
setSupportActionBar(toolbar); | |
FloatingActionButton fab = findViewById(R.id.fab); | |
fab.setOnClickListener(new View.OnClickListener() { | |
@Override | |
public void onClick (View view) { | |
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG) | |
.setAction("Action", null).show(); | |
} | |
}); | |
PreferenceManager.setDefaultValues(this, R.xml.preferences, false); | |
SharedPreferences sharedPref = | |
PreferenceManager | |
.getDefaultSharedPreferences(this); | |
Boolean switchPref = sharedPref.getBoolean | |
(SettingsActivity.KEY_PREF_EXAMPLE_SWITCH, false); | |
Toast.makeText(this, switchPref.toString(), | |
Toast.LENGTH_SHORT).show(); | |
} | |
@Override | |
public boolean onCreateOptionsMenu (Menu menu) { | |
// Inflate the menu; this adds items to the action bar if it is present. | |
getMenuInflater().inflate(R.menu.menu_main, menu); | |
return true; | |
} | |
@Override | |
public boolean onOptionsItemSelected (MenuItem item) { | |
// Handle action bar item clicks here. The action bar will | |
// automatically handle clicks on the Home/Up button, so long | |
// as you specify a parent activity in AndroidManifest.xml. | |
int id = item.getItemId(); | |
//noinspection SimplifiableIfStatement | |
if (id == R.id.action_settings) { | |
Intent intent = new Intent(this, SettingsActivity.class); | |
startActivity(intent); | |
return true; | |
} | |
return super.onOptionsItemSelected(item); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?xml version="1.0" encoding="utf-8"?> | |
<PreferenceScreen xmlns:app="http://schemas.android.com/apk/res-auto" | |
xmlns:android="http://schemas.android.com/apk/res/android"> | |
<SwitchPreferenceCompat | |
android:layout_width="match_parent" | |
android:layout_height="wrap_content" | |
android:defaultValue="true" | |
android:key="example_switch" | |
android:summary="Turn this option on or off" | |
android:title="Settings option" | |
app:icon="@drawable/ic_launcher_foreground" /> | |
<EditTextPreference | |
android:defaultValue="Mohammad Samandari" | |
android:inputType="textCapWords" | |
android:key="example_text" | |
android:maxLines="1" | |
android:selectAllOnFocus="true" | |
android:singleLine="true" | |
android:summary="Your Name" | |
android:title="Display Name" /> | |
<ListPreference | |
android:accessibilityLiveRegion="assertive" | |
android:defaultValue="-1" | |
android:entries="@array/pref_example_list_titles" | |
android:entryValues="@array/pref_example_list_values" | |
android:key="example_list" | |
android:negativeButtonText="@null" | |
android:positiveButtonText="@null" | |
android:title="Add friends to order messages" | |
android:visibility="invisible" /> | |
</PreferenceScreen> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mohammad.samandari.appwithsettings; | |
import androidx.appcompat.app.AppCompatActivity; | |
import android.os.Bundle; | |
public class SettingsActivity extends AppCompatActivity { | |
public static final String | |
KEY_PREF_EXAMPLE_SWITCH = "example_switch"; | |
@Override | |
protected void onCreate (Bundle savedInstanceState) { | |
super.onCreate(savedInstanceState); | |
getSupportFragmentManager().beginTransaction() | |
.replace(android.R.id.content, new SettingsFragment()) | |
.commit(); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package mohammad.samandari.appwithsettings; | |
import android.os.Bundle; | |
import androidx.fragment.app.Fragment; | |
import androidx.preference.PreferenceFragmentCompat; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import android.widget.TextView; | |
/** | |
* A simple {@link Fragment} subclass. | |
*/ | |
public class SettingsFragment extends PreferenceFragmentCompat { | |
@Override | |
public void onCreatePreferences (Bundle savedInstanceState, String rootKey) { | |
setPreferencesFromResource(R.xml.preferences,rootKey); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment