Skip to content

Instantly share code, notes, and snippets.

@Dzakus
Last active December 30, 2015 15:29
Show Gist options
  • Save Dzakus/7848837 to your computer and use it in GitHub Desktop.
Save Dzakus/7848837 to your computer and use it in GitHub Desktop.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Ustawienia" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.intent"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="18" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name="com.example.intent.MainActivity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name="com.example.intent.SettingsActivity"
android:label="@string/title_activity_settings" >
</activity>
</application>
</manifest>
package com.example.intent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private Button mButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mButton = (Button) findViewById(R.id.button1);
mButton.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if(v == mButton){
Intent i = new Intent(this, SettingsActivity.class);
startActivity(i);
}
}
}
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="@string/pref_header_general" >
<EditTextPreference
android:defaultValue="@string/pref_default_host_ip"
android:key="host_ip"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_host_ip" />
<EditTextPreference
android:defaultValue="@string/pref_default_port"
android:key="port"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_port" />
<EditTextPreference
android:defaultValue="@string/pref_default_user"
android:inputType="textCapWords"
android:key="user"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_user" />
<EditTextPreference
android:defaultValue="@string/pref_default_password"
android:inputType="textPassword"
android:key="password"
android:maxLines="1"
android:selectAllOnFocus="true"
android:singleLine="true"
android:title="@string/pref_title_password" />
</PreferenceCategory>
<PreferenceCategory android:title="@string/pref_header_hardware" >
<ListPreference
android:defaultValue="-1"
android:entries="@array/pref_rev_list_titles"
android:entryValues="@array/pref_rev_list_values"
android:key="example_list"
android:negativeButtonText="@null"
android:positiveButtonText="@null"
android:title="@string/pref_title_rev" />
<Preference
android:selectable="false"
android:summary="@string/pref_note_summary_drgpio"
android:title="@string/pref_note_title_drgpio" />
</PreferenceCategory>
</PreferenceScreen>
package com.example.intent;
import android.os.Bundle;
import android.preference.EditTextPreference;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.text.InputType;
public class SettingsActivity extends PreferenceActivity {
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
setupSimplePreferencesScreen();
}
@SuppressWarnings("deprecation")
private void setupSimplePreferencesScreen() {
addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference("host_ip"));
bindPreferenceSummaryToValue(findPreference("port"));
bindPreferenceSummaryToValue(findPreference("user"));
bindPreferenceSummaryToValue(findPreference("password"));
bindPreferenceSummaryToValue(findPreference("example_list"));
}
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object value) {
String stringValue = value.toString();
if (preference instanceof ListPreference) {
// For list preferences, look up the correct display value in
// the preference's 'entries' list.
ListPreference listPreference = (ListPreference) preference;
int index = listPreference.findIndexOfValue(stringValue);
// Set the summary to reflect the new value.
preference.setSummary(index >= 0 ? listPreference.getEntries()[index] : null);
} else if (preference instanceof EditTextPreference) {
EditTextPreference editTextPreference = (EditTextPreference) preference;
if ((editTextPreference.getEditText().getInputType() & InputType.TYPE_TEXT_VARIATION_PASSWORD) != 0) {
//TODO Make it better
preference.setSummary("****");
} else {
preference.setSummary(stringValue);
}
} else {
// For all other preferences, set the summary to the value's
// simple string representation.
preference.setSummary(stringValue);
}
return true;
}
};
private static void bindPreferenceSummaryToValue(Preference preference) {
if (preference == null)
return;
// Set the listener to watch for value changes.
preference.setOnPreferenceChangeListener(sBindPreferenceSummaryToValueListener);
// Trigger the listener immediately with the preference's
// current value.
sBindPreferenceSummaryToValueListener.onPreferenceChange(preference, PreferenceManager
.getDefaultSharedPreferences(preference.getContext()).getString(preference.getKey(), ""));
}
}
<resources>
<string name="title_activity_settings">Settings</string>
<!-- Strings related to Settings -->
<!-- Authoriasdasdasd settings -->
<string name="pref_header_general">Autoryzacja</string>
<string name="pref_title_host_ip">Hostname or IP address</string>
<string name="pref_default_host_ip">192.168.0.3</string>
<string name="pref_title_port">Port</string>
<string name="pref_default_port">8080</string>
<string name="pref_title_user">Username</string>
<string name="pref_default_user">webiopi</string>
<string name="pref_title_password">Password</string>
<string name="pref_default_password">raspberry</string>
<!-- Hardware settings -->
<string name="pref_header_hardware">Hardware</string>
<string name="pref_title_rev">Rasperry PI revision:</string>
<string name="pref_summary_rev">To long to rewrite</string>
<string-array name="pref_rev_list_titles">
<item>Rev1 &#8211; 256 MB of RAM</item>
<item>Rev2 &#8211; 512 MB of RAM</item>
</string-array>
<string-array name="pref_rev_list_values">
<item>1</item>
<item>2</item>
</string-array>
<string name="pref_note_title_drgpio">Note:</string>
<string name="pref_note_summary_drgpio">To long to rewrite\nTo long to rewrite\nTo long to rewrite\nTo long to rewrite\nTo long to rewrite\nTo long to rewrite\nTo long to rewrite\nTo long to rewrite\nTo long to rewrite\nTo long to rewrite\n</string>
</resources>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment