본문 바로가기
모바일개발(Mobile Dev)/안드로이드개발(Android)

About PreferenceFragment with Toolbar

by 테크한스 2016. 10. 1.
반응형
package uk.verscreative.materialsettings;

import android.media.Ringtone;
import android.media.RingtoneManager;
import android.preference.ListPreference;
import android.preference.PreferenceActivity;
import android.content.Context;
import android.content.pm.PackageInfo;
import android.preference.Preference;
import android.app.NotificationManager;

import android.preference.PreferenceManager;
import android.preference.RingtonePreference;
import android.support.design.widget.AppBarLayout;
import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.AppCompatCheckedTextView;
import android.support.v7.widget.AppCompatEditText;
import android.support.v7.widget.AppCompatRadioButton;
import android.support.v7.widget.AppCompatSpinner;
import android.text.TextUtils;
import android.util.Log;
import android.net.Uri;
import android.support.v7.app.NotificationCompat;
import android.graphics.Color;
import android.content.res.Configuration;
import android.util.AttributeSet;
import android.view.View;
import android.os.Build;
import android.os.Bundle;
import android.content.pm.PackageManager;
import android.view.Window;
import android.widget.LinearLayout;
import android.view.LayoutInflater;
import android.view.ViewGroup;
import android.support.v7.widget.Toolbar;
import android.widget.ListView;
import android.util.TypedValue;
import android.preference.PreferenceScreen;
import android.app.Dialog;


public class SettingsExampleActivity extends PreferenceActivity {
private static String appVersion;

protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);

AppBarLayout bar;

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
LinearLayout root = (LinearLayout) findViewById(android.R.id.list).getParent().getParent().getParent();
bar = (AppBarLayout) LayoutInflater.from(this).inflate(R.layout.toolbar_settings, root, false);
root.addView(bar, 0);
} else {
ViewGroup root = (ViewGroup) findViewById(android.R.id.content);
ListView content = (ListView) root.getChildAt(0);
root.removeAllViews();
bar = (AppBarLayout) LayoutInflater.from(this).inflate(R.layout.toolbar_settings, root, false);

int height;
TypedValue tv = new TypedValue();
if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
height = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
}else{
height = bar.getHeight();
}

content.setPadding(0, height, 0, 0);

root.addView(content);
root.addView(bar);
}

Toolbar Tbar = (Toolbar) bar.getChildAt(0);

Tbar.setNavigationOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});

try {
PackageInfo pInfo = getPackageManager().getPackageInfo(getPackageName(), 0);
appVersion = pInfo.versionName;
} catch(PackageManager.NameNotFoundException e) {
appVersion = "unknown";
}

setupSimplePreferencesScreen();
}

@SuppressWarnings("deprecation")
private void setupSimplePreferencesScreen() {
addPreferencesFromResource(R.xml.pref_general);
bindPreferenceSummaryToValue(findPreference("notifications_ringtone"));
Preference app_version = findPreference("application_version");
setPreferenceSummary(app_version, appVersion);
}

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 RingtonePreference) {
// For ringtone preferences, look up the correct display value
// using RingtoneManager.
if (TextUtils.isEmpty(stringValue)) {
// Empty values correspond to 'silent' (no ringtone).
preference.setSummary(R.string.pref_ringtone_silent);

} else {
Ringtone ringtone = RingtoneManager.getRingtone(
preference.getContext(), Uri.parse(stringValue));

if (ringtone == null) {
// Clear the summary if there was a lookup error.
preference.setSummary(null);
} else {
// Set the summary to reflect the new ringtone display
// name.
String name = ringtone.getTitle(preference.getContext());
preference.setSummary(name);
}
}

} 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) {
// 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(), ""));
}

private static void setPreferenceSummary(Preference preference, String value) {
preference.setSummary(value);
}

@Override
public View onCreateView(String name, Context context, AttributeSet attrs) {
// Allow super to try and create a view first
final View result = super.onCreateView(name, context, attrs);
if (result != null) {
return result;
}

if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// If we're running pre-L, we need to 'inject' our tint aware Views in place of the
// standard framework versions
switch (name) {
case "EditText":
return new AppCompatEditText(this, attrs);
case "Spinner":
return new AppCompatSpinner(this, attrs);
case "CheckBox":
return new AppCompatCheckBox(this, attrs);
case "RadioButton":
return new AppCompatRadioButton(this, attrs);
case "CheckedTextView":
return new AppCompatCheckedTextView(this, attrs);
}
}

return null;
}

public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}

@SuppressWarnings("deprecation")
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
super.onPreferenceTreeClick(preferenceScreen, preference);

if (preference!=null) {
if (preference instanceof PreferenceScreen) {
if (((PreferenceScreen) preference).getDialog() != null) {
((PreferenceScreen) preference).getDialog().getWindow().getDecorView().setBackgroundDrawable(this.getWindow().getDecorView().getBackground().getConstantState().newDrawable());
setUpNestedScreen((PreferenceScreen) preference);
}
}
}

return false;
}

public void setUpNestedScreen(PreferenceScreen preferenceScreen) {
final Dialog dialog = preferenceScreen.getDialog();

AppBarLayout appBar;

View listRoot = dialog.findViewById(android.R.id.list);
ViewGroup mRootView = (ViewGroup) dialog.findViewById(android.R.id.content);


if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
LinearLayout root = (LinearLayout) dialog.findViewById(android.R.id.list).getParent();
appBar = (AppBarLayout) LayoutInflater.from(this).inflate(R.layout.toolbar_settings, root, false);
root.addView(appBar, 0);
} else {
ListView content = (ListView) mRootView.getChildAt(0);
mRootView.removeAllViews();

LinearLayout LL = new LinearLayout(this);
LL.setOrientation(LinearLayout.VERTICAL);

ViewGroup.LayoutParams LLParams = new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
LL.setLayoutParams(LLParams);

appBar = (AppBarLayout) LayoutInflater.from(this).inflate(R.layout.toolbar_settings, mRootView, false);

LL.addView(appBar);
LL.addView(content);

mRootView.addView(LL);
}

if(listRoot != null){
listRoot.setPadding(0, listRoot.getPaddingTop(), 0, listRoot.getPaddingBottom());
}

Toolbar Tbar = (Toolbar) appBar.getChildAt(0);

Tbar.setTitle(preferenceScreen.getTitle());

Tbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
}
}


public class SettingsFragmentPhoto extends PreferenceActivity {
private Toolbar mToolBar;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/////////////// making toolbar ////////////////
LinearLayout root = (LinearLayout)findViewById(android.R.id.list).getParent().getParent().getParent();
mToolBar = (Toolbar) LayoutInflater.from(this).inflate(R.layout.settings_toolbar, root, false);

mToolBar.setTitleTextColor(Color.WHITE);

root.addView(mToolBar, 0); // insert at top
mToolBar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
/////////////// making toolbar ////////////////

getFragmentManager().beginTransaction().replace(android.R.id.content,new PrefsFragment()).commit();


// PreferenceManager.setDefaultValues(Preferences2.this, R.xml.preferences_t, false);

}


public static class PrefsFragment extends PreferenceFragment {

@Override
public void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

addPreferencesFromResource(R.xml.preferences3_t);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = super.onCreateView(inflater, container, savedInstanceState);
// calculate margins
int horizontalMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
int verticalMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 2, getResources().getDisplayMetrics());
int topMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (int) getResources().getDimension(R.dimen.activity_vertical_margin) + 30, getResources().getDisplayMetrics());

view.setPadding(horizontalMargin, topMargin, horizontalMargin, verticalMargin);
return view;
}




}



}


반응형