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>
I hope to execute a function when I check or uncheck CheckBoxPreference in PreferenceScreen, how can I do ? Thanks!
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="AppPreference"
android:summary="#string/PreferenceSummary"
android:title="#string/Preference" >
<CheckBoxPreference
android:defaultValue="true"
android:key="AlwaysHideIcon"
android:title="#string/AlwaysHideIconTitle"
android:summary="#string/AlwaysHideIconSummary"
android:layout="#layout/sms_custom_preference_layout"
/>
</PreferenceScreen>
public class SmsPreference extends PreferenceActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.smspreference);
setContentView(R.layout.sms_custom_preference);
// Look up the AdView as a resource and load a request.
if (PublicPar.IsRegistered==false) {
AdView adView = (AdView)this.findViewById(R.id.adView);
adView.loadAd(new AdRequest());
}
Button btnClose=(Button)findViewById(R.id.btnClosePreference);
btnClose.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
finish();
}
});
}
}
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/border_ui"
android:orientation="vertical">
<ListView android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/adView"
android:layout_above="#+id/linearLayout1"
>
</ListView>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center_vertical"
android:gravity="center"
android:orientation="horizontal"
android:weightSum="4" >
<Button
android:id="#+id/btnClosePreference"
style="#style/myTextAppearance"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:text="#string/BtnReturn" >
</Button>
</LinearLayout>
</RelativeLayout>
CheckBoxPreference preference = (CheckBoxPreference) findPreference("preference");
preference.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener()
{
public boolean onPreferenceChange(Preference preference, Object newValue)
{
Log.d("MyApp", "Pref " + preference.getKey() + " changed to " + newValue.toString());
if(newValue.toString().equalsIgnoreCase("true"))
//Here manage your event for checked preference
else
//Here manage your event for Unchecked preference
return true;
}
});
Hi im trying to add a simple overlay to my app to highlight bits and bobs to the user i am currently following this short guide
http://www.christianpeeters.com/android-tutorials/android-tutorial-overlay-with-user-instructions/
but i get an error using android studio cannot resolve variable topLeveLayout im a complete beginner and have tried alsorts to get this moving any suggestions?
here is my xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_layout" >
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="match_parent"
android:layout_height="55dp"
android:text="#string/bath2"
android:id="#+id/bath_text_View"
android:textSize="23sp"
android:gravity="center"
android:textStyle="bold|italic" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/bath_text_View"
android:contentDescription="#string/bathable2"
android:src="#drawable/pic5"
android:layout_above="#+id/nextbutton"
android:id="#+id/imageView"
android:scaleType="centerCrop"
android:cropToPadding="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/next"
android:id="#+id/nextbutton"
android:textSize="20sp"
android:textStyle="bold|italic"
android:layout_gravity="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:onClick="nextImage2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button2"
android:background="#android:color/transparent"
android:layout_above="#+id/button4"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/button4"
android:background="#android:color/transparent"
android:layout_above="#+id/button"
android:layout_alignParentLeft="true"
android:clickable="false" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#88666666"
android:id="#+id/top_layout">
<ImageView
android:id="#+id/ivInstruction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:paddingTop="25dp"
android:layout_marginRight="15dp"
android:clickable="false"
android:paddingLeft="20dip"
android:scaleType="center"
android:contentDescription="#string/overlay"
android:src="#drawable/hint_menu" />
</RelativeLayout>
</FrameLayout>
So i have 2 relative layouts in a frame layout the first being my app and the second underneath being the overlay
here is my .java
public class bathactivity2 extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bath_2);
topLevelLayout = findViewById(R.id.top_layout);
if (isFirstTime()) {
topLevelLayout.setVisibility(View.INVISIBLE);
}
Button button2 = (Button) findViewById(R.id.button2);
Button button4 = (Button) findViewById(R.id.button4);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(bathactivity2.this, R.raw.rubber_duck);
mp.start();
}
});
button4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MediaPlayer mp = MediaPlayer.create(bathactivity2.this, R.raw.bubbles);
mp.start();
}
});
}
public void nextImage2(View view){
Intent intent = new Intent(this, BathActivity3.class);
startActivity(intent);
}
private boolean isFirstTime()
{
SharedPreferences preferences = getPreferences(MODE_PRIVATE);
boolean ranBefore = preferences.getBoolean("RanBefore", false);
if (!ranBefore) {
SharedPreferences.Editor editor = preferences.edit();
editor.putBoolean("RanBefore", true);
editor.commit();
topLevelLayout.setVisibility(View.VISIBLE);
topLevelLayout.setOnTouchListener(new View.OnTouchListener(){
#Override
public boolean onTouch(View v, MotionEvent event) {
topLevelLayout.setVisibility(View.INVISIBLE);
return false;
}
});
}
return ranBefore;
}
}
it is just an image with a couple of hidden buttons on it hence the reason i would like an overlay to point out where the buttons are, i get errors cannot resolve topLevelLayout cannot resolve motion event and others, is it just a case of declaring these first and if so how? is it something like
Layout topLevelLayout;
or is there something worse going on here thank you for any help
View topLevelLayout;
this solved it
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 );
Hey guys im making an app using zxing integrater I have the scanner working propelry and show the results fine but would like the reslts to showon a separate class activity any ideas?
JavaActivity
public class QRGOLFActivity extends Activity {
TextView contents = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
contents = (TextView) findViewById(R.id.contents);
}
public void doScan(View v) {
IntentIntegrator.initiateScan(this);
}
public void onActivityResult(int request, int result, Intent i) {
IntentResult scan = IntentIntegrator.parseActivityResult(request,
result, i);
if (scan != null) {
contents.setText(scan.getContents());
}
}
#Override
public void onSaveInstanceState(Bundle state) {
state.putString("contents", contents.getText().toString());
}
#Override
public void onRestoreInstanceState(Bundle state) {
contents.setText(state.getString("contents"));
}
}
and the Xml file
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:onClick="doScan"
android:text="Scan!" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/contents"
android:layout_width="fill_parent"
android:layout_height="116dp"
android:textSize="20dp" />
</ScrollView>
<Button
android:id="#+id/score"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="100dp"
android:text="Scorecard" />
<Button
android:id="#+id/about"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="About Us" />
<Button
android:id="#+id/contact"
android:layout_width="230dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Contact Us" />
Call startActivity() on "a separate class activity", passing whatever data you want (such as the ZXing results) via extras on the Intent you supply to startActivity(). Your "separate class activity" can then use getIntent() to retrieve the Intent and the various get...Extra() methods to retrieve those values.