How to Change Text Style in Preference Fragment - android

I have created Preference Screen with Text View.User can change Text view style like font,color,size in Preference screen.so it will displayed on Textview which i have created in Preference Screen.but its not working .If its preference Activity ,we can use setContentview,In fragments i don't know how to pass the view..
public final class TestFragment2 extends PreferenceFragment {
static TextView textView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
LayoutInflater inflater = LayoutInflater.from(getActivity());
View view = inflater.inflate(R.layout.customprefernce, null);
textView = (TextView) view.findViewById(R.id.TextViewSample);
final CheckBoxPreference checkboxPref = (CheckBoxPreference) getPreferenceManager()
.findPreference("bold");
checkboxPref
.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
public boolean onPreferenceChange(Preference preference,
Object newValue) {
// Log.d("MyApp", "Pref " + preference.getKey() +
// " changed to " + newValue.toString());
textView.setTextColor(Color.BLUE);
// Color is not changing.
return true;
}
});
}
I have check box prefernce for changin the color ,but its not changing the color
textView.setTextColor(Color.BLUE);
Preferncescreen.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:holo="http://schemas.android.com/apk/res-auto">
<Preference
android:selectable="false"
android:summary="#string/preference_summary"
android:title="#string/preference_title" />
<Preference
android:layout="#layout/customprefernce" />
<PreferenceCategory android:title="#string/preference_base">
<CheckBoxPreference
android:id="#+id/bold"
android:defaultValue="false"
android:key="bold"
android:title="#string/preference_check_box_title" />
<CheckBoxPreference
android:id="#+id/italic"
android:key="italic"
android:defaultValue="false"
android:summaryOff="#string/preference_check_box_summary_off"
android:summaryOn="#string/preference_check_box_summary_on"
android:title="#string/preference_check_box_title" />
</PreferenceCategory>
</PreferenceScreen>
CustomPrefernce.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingRight="?android:attr/scrollbarSize">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<TextView android:id="#+id/TextViewSample"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Example Tex"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="#+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical" />
</LinearLayout>

Related

TextView gets overlaped(instead of Overwriting)in a Preference Screen

I am trying to build a preference screen which consists of a Layout with some TextViews and EditText Preference.
When I use textView.setText(inputString) in the Activity, it does not overwrite the existing TextView, instead it creates a new copy of TextView with inputString as text and it overlaps with the existing TextView. Is there any way that the setText overwrites the existing TextView itself.
This is the XML Layout with the TextView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:background="#color/blue"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/list"
></ListView>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:textColor="#color/pseudoBlack"
android:layout_height="wrap_content"
android:layout_marginStart="59dp"
android:layout_marginTop="100dp"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<TextView
android:text="TextView"
android:textColor="#color/pseudoBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView5"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_toEndOf="#+id/textView2"
android:layout_marginStart="100dp" />
<TextView
android:text="TextView"
android:textColor="#color/pseudoBlack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="43dp"
android:id="#+id/textView11"
android:layout_below="#+id/textView2"
android:layout_toEndOf="#+id/textView2"
android:layout_marginStart="21dp" />
</RelativeLayout>
And this is the Preference Screen I am using.
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
>
<Preference android:layout="#layout/inner" android:key="innerkey"
/>
<Preference android:key="mskey"
android:title="show"
/>
<EditTextPreference android:key="editpref"
android:title="Add"
/>
</PreferenceScreen>
This is the Activity Class in which I am trying to do setText().
package com.example.asus.layoutcheck;
import android.os.Bundle;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.widget.TextView;
public class SecondaryActivity extends PreferenceActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.inner);
TextView txt1 = (TextView)findViewById(R.id.textView2);
TextView txt2 = (TextView)findViewById(R.id.textView5);
TextView txt3 = (TextView)findViewById(R.id.textView11);
txt1.setText("First Text");
txt2.setText("Second Text");
txt3.setText("Third Text");
final String message = "This is the message" ;
addPreferencesFromResource(R.xml.pref);
final Preference msg = findPreference("mskey");
msg.setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() {
#Override
public boolean onPreferenceClick(Preference preference) {
msg.setSummary(message);
return true;
}
});
Preference edit = findPreference("editpref");
}
}
I have added the screenshot of the result.
How can I overcome this Issue?Thanks in advance
replace setContentView(R.layout.inner); with addPreferencesFromResource(//your preferenceScreen);
I've encountered this similar issue, and it happens with other view as well like a TextureView inside a FrameLayout. I resized my TextureView smaller and I can still see the previous TextureView in the back flickering.
What made it work is funny -> I just put a black background color (probably any color) to my layout (LinearLayout) and it is working fine now.
I added: android:background="#000000"
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical"
android:visibility="visible"
tools:context=".CameraActivity">
It seems the background is not cleared, if it doesn't know what color to clear it with.

how to get Android Custom Preference TextView OnClick?

I can't get custom preference TextView action. May I know how to get it.
Here my preference xml file user_setting.xml,
<PreferenceCategory android:title="#string/security_settings"
android:key="security">
<Preference
android:title="#string/change_pin"
android:summary="#string/change_pin_summary"
android:key="change_pin"
android:layout="#layout/test"/>
</PreferenceCategory>
Here my custom xml layout for preference test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingLeft="7dp"
>
<TextView android:id="#android:id/title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:fadingEdge="horizontal"
android:textSize="19dp"
/>
<TextView android:id="#android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignLeft="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:maxLines="2"
/>
</LinearLayout>
My activity is here,
public class AppPreference extends SherlockPreferenceActivity {
private boolean needResource=false;
#SuppressWarnings("deprecation")
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (needResource || Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) {
addPreferencesFromResource(R.xml.user_setting);
}
Initialize();
}
private void Initialize(){
Preference change_pin = findPreference("change_pin");
change_pin.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
#Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
Toast.makeText(getBaseContext(), "PIN",
Toast.LENGTH_SHORT).show();
/* Intent myIntent = new Intent(AppPreference.this, OriginalPinActivity.class);
AppPreference.this.startActivity(myIntent);*/
return true;
}
});
}
It's not work when click or touch on this preference. Please pointing to me how to do it.

Preference getView() android

I need to change the image of an ImageView placed inside a custom preference. I am doing this:
View thumb1 = findViewById(R.id.thumb_1);
ViewGroup vg = (ViewGroup) thumb1.getParent();
final Preference myPref = (Preference) findPreference("test02");
((ImageView) myPref.getView(thumb1, vg)).setImageResource(R.drawable.ic_chooser);
I get a null error. What am I doing wrong?
perference.getView(null,null).findViewById(R.id.xx);
Well, I did in this way in Kotlin.
This sample has custom layout for Preference and update it in any function.
Preference XML:
<Preference
android:key="preference_key"
android:layout="#layout/custom_layout_name"
android:summary="Summary"
android:title="Title" />
custom_layout_name.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeight"
android:paddingRight="?android:attr/scrollbarSize">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/preference_margin_top_bottom"
android:layout_marginLeft="#dimen/preference_margin_right"
android:layout_marginTop="#dimen/preference_margin_top_bottom"
android:orientation="vertical">
<TextView
android:id="#android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#android:color/black" />
<TextView
android:id="#android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true" />
</LinearLayout>
<View
android:id="#+id/colorView"
android:layout_width="#dimen/preference_color_size"
android:layout_height="#dimen/preference_color_size"
android:layout_alignParentEnd="true"
android:layout_marginRight="#dimen/preference_margin_top_bottom"
android:background="#color/colorPrimaryDark" />
</RelativeLayout>
Class:
private var preferenceColor: Preference? = null
preferenceColor = preferenceManager.findPreference("preference_key)
preferenceColor!!.onPreferenceClickListener = Preference.OnPreferenceClickListener {
anyFunction(Color.BLUE)
true
}
Function for updating the View:
private fun anyFunction(color: Int) {
val colorViewOrigin = findViewById<View>(R.id.colorView)
val colorViewParent: ViewGroup = colorViewOrigin.parent as ViewGroup
val preferenceManager = preferenceColor?.getPreferenceManager()
val colorView = preferenceManager?.findPreference("preference_key")!!.getView(colorViewOrigin, colorViewParent) as View
colorView.setBackgroundColor(color)
}
Hope it would be help you.

How to get EditTextPreference's layout items id

I have a EditPreferences.class which extends preferenceactivity and my preferences.xml layout in my xml folder
in the xml layout i used EditTextPreference control which have layout et_layout.xml
my question is how can i get my textview id in my EditPreferences.class
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:title="#string/app_name" >
<PreferenceCategory
android:icon="#drawable/batitle"
android:textColor="#000000"
android:title="كۆرۈنۈش" >
<ListPreference
android:defaultValue="#string/result_fontsize_default"
android:dialogTitle="تاللاڭ"
android:entries="#array/result_fontsize_keys"
android:entryValues="#array/result_fontsize_values"
android:key="result_fontsize"
android:summary="ئىزدەش نەتىجىسىنىڭ كۆرۈنۈش چوڭلۇقى"
android:textColor="#000000"
android:title="خەت چوڭلۇقى" />
<EditTextPreference
android:dialogIcon="#drawable/batitle"
android:dialogTitle="editText"
android:key="tel"
android:layout="#layout/layout" />
<Preference />
</PreferenceCategory>
</PreferenceScreen>
layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="right"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Inflate you xml
LayoutInflater inflater = LayoutInflater.from(this);
View view = inflater.inflate(R.layout.linear_layout, null);
textView2 = (TextView) view.findViewById(R.id.textView2);
Try this, hope it would help you out
Try this,
SharedPreferences prefs;
String x = prefs.getString("tel",defaultString value);
textView.setText(x);
The reason why a simple findViewById in your EditPreferences.onCreate doesn't work is because your layout with the TextView is created inside of a ListView and there is a delay before that actually happens when the activity is launched.
I'm not sure this is the best way, but one way to handle the event of ListView item population is to handle the OnHierarchyChange event for the ListView object. If you put this in your onCreate, after the preference layout has been loaded from what I assume is a call to addPreferencesFromResource, it should work.
ListView list = (ListView) findViewById(android.R.id.list);
list.setOnHierarchyChangeListener(new ViewGroup.OnHierarchyChangeListener() {
#Override
public void onChildViewAdded(View parent, View child) {
TextView text = (TextView) child.findViewById(R.id.textView2);
if (text != null) {
text.setText("Something else here");
}
}
#Override
public void onChildViewRemoved(View view, View view2) {
}
});

Add a Button into PrefrenceScreen : Android

Hi i want to add a button into prefrencescreen, i got success into add a button into the prefrence but i could not get onClick event. i have attached my code a pic of prefence screen
setting.xml
<PreferenceCategory android:title="Application Details">
<Preference android:key="type"
android:title="Type"
android:summary="" />
</PreferenceCategory>
<PreferenceCategory android:title="Notification Settings">
<ListPreference android:key="sendNotificationType"
android:title="Status Notification For"
android:dialogTitle="Status Notification For" />
</PreferenceCategory>
settingdetail.xml
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/textView2"
android:layout_marginLeft="15dp"
android:text="Type"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginLeft="15dp"
android:layout_toLeftOf="#+id/setFromTimeBtn"
android:text="Summary"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/buyItNowBtn"
android:layout_width="80dp"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_marginRight="15dp"
android:layout_centerVertical="true"
android:background="#drawable/button"
android:text="#string/buyItNowBtnTxt"
android:textColor="#color/white" />
and the onCreate Method of prefenceActivity Class
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.setting);
setContentView(R.layout.settingview);
Preference typePref = (Preference) findPreference("type");
typePref.setLayoutResource(R.layout.settingdetail);
typePref.setSelectable(true);
Button btn = (Button) findViewById(R.id.buyItNowBtn);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
Log.e(TAG,"TEST");
Toast.makeText(Setting.this, "TEST", Toast.LENGTH_SHORT).show();
}
});
}
screenshot
This thread is stale, but since there is no solution yet, here is how I succeeded:
1. Set the button click listener
There are two ways to set the click listener for a button, either programmatically or via XML.
Programmatically: In your custom preference, override onBindView and attach the listener there:
#Override
protected void onBindView(View view) {
View button = view.findViewById(R.id.myButton);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d(LOG_TAG, "button click listener works!");
}
});
super.onBindView(view);
}
Via XML: Add android:onClick="yourMethodName" to the layout definition of the Button element (which is part of the layout for your preference, see below) and implement the method in your PreferenceActivityas described here: Responding to Click Events
2. Fix OnPreferenceClickListener
Now the button click listener should work, but the Preference's OnPreferenceClickListener will not work anymore. In order to fix this, add
android:descendantFocusability="blocksDescendants"
to the top element in the layout file for your preference:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?android:attr/listPreferredItemHeight"
android:gravity="center_vertical"
android:paddingEnd="?android:attr/scrollbarSize"
android:background="?android:attr/selectableItemBackground"
android:descendantFocusability="blocksDescendants">
<ImageView
android:id="#android:id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dip"
android:layout_marginEnd="6dip"
android:layout_marginTop="6dip"
android:layout_marginBottom="6dip"
android:layout_weight="1">
<TextView android:id="#android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:ellipsize="marquee"
android:fadingEdge="horizontal" />
<TextView android:id="#android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/title"
android:layout_alignStart="#android:id/title"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="?android:attr/textColorSecondary"
android:maxLines="4" />
<Button android:id="#+id/myButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout android:id="#android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:orientation="vertical" />
</LinearLayout>
(No android:onClick="yourMethodName" here as I chose to do it programmatically.)
3. Attach the Layout
Finally you need to set this layout to your preference XML file:
Add android:layout="#layout/myLayoutName" or alternatively set it via setLayoutResource as you construct the preference in your PreferenceFragment.
Implement OnPreferenceClickListener in preferenceActivity
private Preference mBuyPreference = null;
mLogoutPreference = findPreference(yourkey);
mBuyPreference.setOnPreferenceClickListener(this);
#Override
public boolean onPreferenceClick(Preference preference) {
if (preference == mBuyPreference) {
try {
//Do Something
}
return false;
}
Instead of using the onClick() handler, override the onPreferenceTreeClick method in your PreferenceActivity. There you will get the clicked preference object and you can check its key.
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
if (preference.getKey().equals("key")) {
// do something
return true;
}
return false;
}
I've solved half of your problem by creating a custom preference like this -
In order to get to this, I created a custom preference like this -
public class CustomPreference extends Preference {
private LinearLayout mWidgetContainer;
private View mRowView;
public CustomPreference(Context context) {
super(context);
}
public CustomPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomPreference(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
#Override
protected View onCreateView(ViewGroup parent) {
LayoutInflater viewInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mRowView = viewInflater.inflate(R.layout.preferences_row_view, parent, false);
mWidgetContainer = (LinearLayout) mRowView.findViewById(android.R.id.widget_frame);
Button button = new Button(getContext());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
button.setBackgroundResource(R.drawable.listview_row_bg);
button.setTextSize(14);
button.setPadding(5, 5, 5, 5);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), BuyScreenActivity.class);
getContext().startActivity(intent);
}
});
button.setTypeface(null, Typeface.BOLD);
button.setText("Buy now");
mWidgetContainer.addView(button);
return mRowView;
}
}
Then in your settings xml file you can create a setting like
<package.of.your.java.file.CustomPreference
android:key="type"
android:title="Type"
android:summary="" />
And my preferences_row_view.xml is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:minHeight="50dip"
android:paddingLeft="5dip"
android:paddingRight="?android:attr/scrollbarSize" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dip"
android:layout_marginRight="6dip"
android:layout_marginTop="6dip"
android:layout_weight="1" >
<TextView
android:id="#+android:id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:lineSpacingMultiplier="1.3"
android:textSize="14sp" />
<TextView
android:id="#+android:id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#android:id/title"
android:layout_below="#android:id/title"
android:maxLines="4"
android:textSize="13sp" />
</RelativeLayout>
<!-- Preference should place its actual preference widget here. -->
<LinearLayout
android:id="#+android:id/widget_frame"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="horizontal" />
</LinearLayout>
That will do.
But there is one more issue I'm facing which I've posted here on SO.
Add a button in Preference Row
Instead of
Button btn = (Button) findViewById(R.id.buyItNowBtn);
do this
Button btn = (Button)typePref.getView(view, viewGroup );

Categories

Resources