How to reach the elements of the layout using PreferenceFragment? - android

I've tried to make a custom preference layout, because I wanted to make a color picker with it.
I used the solution here:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content, new PrefFragment()).commit();
}
I wanted to edit the value of the TextView on the layout, that's why I added the lines with the TextView in it.
The problem is that it threw a NullPointerException on the setText line, because I think the findViewById() method returned null.
PrefFragment.java:
public class PrefFragment extends PreferenceFragment {
private TextView titleTV;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
titleTV = (TextView) getActivity().findViewById(R.id.title);
titleTV.setText(R.string.pref_bgcolor);
}
}
preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<Preference
android:title="#string/pref_bgcolor"
android:summary="#string/pref_bgcolor_sum"
android:key="hu.atsoft.myip.color"
android:widgetLayout="#layout/color_pref"/>
</PreferenceScreen>
color_pref.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/color_preference">
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:paddingLeft="5dip"
android:paddingRight="5dip"
android:paddingTop="5dip"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black"/>
<TextView
android:id="#+id/summary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dip"
android:layout_alignParentLeft="true"
android:layout_below="#id/title"
android:singleLine="true"
android:textAppearance="?android:attr/textAppearanceSmall"/>
<ImageView
android:id="#+id/color_preview"
android:layout_width="20dip"
android:layout_height="20dip"
android:background="#android:color/darker_gray"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_margin="5dip"/>
</RelativeLayout>
What should I use to reach the elements instead of the findViewById() method?

You should use custom Preference:
PreferenceWithTip.class
public class PreferenceWithTip extends Preference {
private static final String TAG = "PreferenceWithTip";
String pTitle = null;
String tipstring = null;
#SuppressLint("Recycle")
public PreferenceWithTip(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
Log.i(TAG,"PreferenceWithTip invoked");
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.PreferenceWithTip);
tipstring = ta.getString(R.styleable.PreferenceWithTip_tipstring);
pTitle = ta.getString(R.styleable.PreferenceWithTip_titlestring);
ta.recycle();
}
public PreferenceWithTip(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
#Override
protected void onBindView(View view) {
super.onBindView(view);
TextView pTitleView = (TextView)view.findViewById(R.id.title);
pTitleView.setText(pTitle);
TextView pTipView = (TextView)view.findViewById(R.id.summary);
pTipView.setText(tipstring);
}
#Override
protected View onCreateView(ViewGroup parent) {
return LayoutInflater.from(getContext()).inflate(R.layout.color_pref,
parent, false);
}
}
attr.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="PreferenceWithTip">
<attr name="tipstring" format="string"></attr>
<attr name="titlestring" format="string"></attr>
</declare-styleable>
</resources>
preferences.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<com.test.mytest.PreferenceWithTip
preference:tipstring="a"
preference:titlestring="title" >
</com.test.mytest.PreferenceWithTip>
</PreferenceScreen>

Related

How do I change the progress value of a progress bar programmatically?

I'm trying to create a custom view of some components because I need this combination a lot in my application. Everything in the code works except the progress of my progressBar. I think that it doesn't get the value correctly of the XML file.
In my init void I call the setProgressBar(progress) to provide the current value to my xml file. When I enter for example 'setProgressBar(88)' it works perfectly but not with a value that it has to find in the xml file.
CustomProgressbar.java
public class CustomProgressbar extends RelativeLayout {
#StyleableRes
int index0 = 0;
#StyleableRes
int index1 = 1;
#StyleableRes
int index2 = 2;
TextView titleText;
TextView valueText;
ProgressBar progressBar;
public CustomProgressbar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
inflate(context, R.layout.custom_progressbar_layout, this);
int[] sets = {R.attr.title, R.attr.value, R.attr.progress};
TypedArray typedArray = context.obtainStyledAttributes(attrs, sets);
CharSequence title = typedArray.getText(index0);
CharSequence value = typedArray.getText(index1);
int progress = typedArray.getInteger(index2,0);
typedArray.recycle();
titleText = findViewById(R.id.title_text);
valueText = findViewById(R.id.value_text);
progressBar = findViewById(R.id.progressbar_attr);
setTitleText(title);
setValueText(value);
setProgressBar(progress);
}
public CharSequence getTitleText() {
return titleText.getText();
}
public CharSequence getValueText() {
return valueText.getText();
}
public int getProgressBar() {
return progressBar.getProgress();
}
public void setTitleText(CharSequence value) {
titleText.setText(value);
}
public void setValueText(CharSequence value) {
valueText.setText(value);
}
public void setProgressBar(int value) {
progressBar.setProgress(value);
}
}
custom_progressbar_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#android:color/black" />
<TextView
android:id="#+id/value_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:textAppearance="#style/Base.TextAppearance.AppCompat.Medium"
android:textColor="#android:color/black" />
<ProgressBar
android:id="#+id/progressbar_attr"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_below="#id/value_text"
android:layout_alignParentStart="true"
android:layout_marginTop="3dp"
android:layout_marginBottom="10dp"
android:max="100"
android:maxHeight="10dip"
android:minHeight="10dip"
android:progressTint="#android:color/holo_green_dark"
android:progressDrawable="#drawable/progressbars" />
</RelativeLayout>
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context=".MainClass"
android:background="#android:color/background_dark">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
app:cardCornerRadius="8dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<com.x.customprogressbar.CustomProgressbar
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:title="ThisIsTheTitle"
app:value="€ 28,30"
app:progress="77" />
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
And my attributes
<resources>
<declare-styleable name="CustomProgressbar">
<attr name="title" format="string"/>
<attr name="value" format="string"/>
<attr name="progress" format="integer"/>
</declare-styleable>
</resources>
Current results:
The XML file, as it should look. There it works perfectly.
Link1
When I run the app.
Link2
(I'm not allowed yet to add images so I got links.)
try this, styles attributes are getting in the other way
public class CustomProgressbar extends RelativeLayout {
private TextView titleText;
private TextView valueText;
private ProgressBar progressBar;
public CustomProgressbar(Context context, AttributeSet attrs) {
super(context, attrs);
init(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
inflate(context, R.layout.custom_progressbar_layout, this);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CustomProgressbar);
CharSequence title = typedArray.getText(R.styleable.CustomProgressbar_title);
CharSequence value = typedArray.getText(R.styleable.CustomProgressbar_value);
int progress = typedArray.getInteger(R.styleable.CustomProgressbar_progress, 0);
typedArray.recycle();
titleText = findViewById(R.id.title_text);
valueText = findViewById(R.id.value_text);
progressBar = findViewById(R.id.progressbar_attr);
setTitleText(title);
setValueText(value);
setProgressBar(progress);
}
public CharSequence getTitleText() {
return titleText.getText();
}
public CharSequence getValueText() {
return valueText.getText();
}
public int getProgressBar() {
return progressBar.getProgress();
}
public void setTitleText(CharSequence value) {
titleText.setText(value);
}
public void setValueText(CharSequence value) {
valueText.setText(value);
}
public void setProgressBar(int value) {
progressBar.setProgress(value);
}
}

How to set Typeface on ListView which has an ArrayList placed through ArrayAdapter

I want to set the typeface to the listview in the following code. All the related files are attached.
SearchClient.java
Toolbar mToolbar;
EditText search;
ListView listView;
ArrayAdapter<String> adapter;
ArrayList<HashMap<String, String>> productList;
Typeface typeface;
DatabaseHelper databaseHelper;
//Clients List
List<String> client_name = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search_client);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
mToolbar = (Toolbar) findViewById(R.id.toolbar_search_client);
setSupportActionBar(mToolbar);
typeface = Typeface.createFromAsset(getAssets(),"fonts/ProductSans-Regular.ttf");
init();
search.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
search.setBackgroundTintList(ColorStateList.valueOf(Color.parseColor("#ffffff")));
}
});
databaseHelper = new DatabaseHelper(this);
Cursor res = databaseHelper.TABLE_CLIENTS_view();
while (res.moveToNext()) {
client_name.add(res.getString(1));
}
adapter = new ArrayAdapter<String>(this, R.layout.search_list_item, R.id.search_client_name, client_name);
listView.setAdapter(adapter);
search.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence cs, int arg1, int arg2, int arg3) {
// When user changed the Text
SearchClient.this.adapter.getFilter().filter(cs);
}
#Override
public void beforeTextChanged(CharSequence arg0, int arg1, int arg2,
int arg3) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable arg0) {
// TODO Auto-generated method stub
}
});
}
public void init(){
search = (EditText) findViewById(R.id.search_client);
search.setTypeface(typeface);
listView = (ListView) findViewById(R.id.listView_search_client);
}
activity_search_client.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
android:focusable="true"
android:id="#+id/rel_lay_client_search"
tools:context="com.example.monilandharia.invoice.SearchClient">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/container_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:id="#+id/toolbar_search_client"
layout="#layout/toolbar_search" />
</LinearLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:id="#+id/listView_search_client" />
</LinearLayout>
</RelativeLayout>
search_list_item.xml
<?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:orientation="vertical" >
<TextView android:id="#+id/search_client_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
</LinearLayout>
And thanks a lot in advance for help!!
You can create a custom textview like below and set the desired typeface in the custom textview class as shown below.
public class CustomTextView extends TextView {
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/ProductSans-Regular.ttf");
this.setTypeface(typeface);
}
}
and in your xml (search_list_item.xml) file kindly do the following change.
<?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:orientation="vertical" >
<com.example.CustomTextView android:id="#+id/search_client_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold"/>
</LinearLayout>
Now the type face would be applied to the textview.
1- make a directory assets inside the main directory
2- download the required typeface from internet and copy the file to the assets
3- in your adapter
Typeface name_For_Typeface;
name_For_Typeface= Typeface.createFromAsset(getContext().getAssets(), "yourfont.ttf");
textview.setTypeface(name_For_Typeface);
Oh thanks but you made a mistake but I changed it. Check this out:
Your code:
/* Constructor */ public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
private void init(Context context, AttributeSet attrs) {
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/ProductSans-Regular.ttf");
this.setTypeface(typeface);
}
You forgot to call the init() so it didn't make any change. You could have passed the context and attrs in the init like init(context,attrs). Also you could have done as below, instead I did:
/* Constructor */ public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
Typeface typeface = Typeface.createFromAsset(context.getAssets(), "fonts/ProductSans-Regular.ttf");
this.setTypeface(typeface);
}

Custom component Android LinearLayout not visible

Custom LinearLayout component view not displaying content
Hi i have created a custom component view that extends LinearLayout and when i use this view on my xml, it doesnt become visible on the screen.
Here is the main view that is using this custom component
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/contentLinearLayout"
android:layout_width="#dimen/width"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ABB0B6" />
<com.jon.ui.EnableView
android:id="#+id/enableContainter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Here is the EnableView custom component code:
package com.jon.ui;
/**
*/
public class EnableView extends LinearLayout {
private static View enableView;
private static Button btnContactlessInfoAction;
private static LinearLayout btnMakeContactlessAction;
private static View makeContactlessView;
private static View.OnClickListener enableContaclessBtnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
};
private static View.OnClickListener contactlessHelpBtnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
}
};
private static ViewGroup viewContainer;
private final Context mContext;
public EnableContactlessView(Context context) {
super(context);
mContext = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_component, this);
setOrientation(LinearLayout.VERTICAL);
}
public EnableContactlessView(Context context, AttributeSet attrs) {
super(context, attrs);
mContext = context;
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.custom_component, this);
setOrientation(LinearLayout.VERTICAL);
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
public void initiate() {
btnMakeContactlessAction = (LinearLayout) this.findViewById(R.id.btn);
btnContactlessInfoAction = (Button) this.findViewById(R.id.info_btn);
btnContactlessInfoAction.setOnClickListener(contactlessHelpBtnClick);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
}
}
Below is the
View's layout xml i use to inflate from EnableView class called custom_component.xml
<?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="wrap_content"
android:layout_marginTop="#dimen/top_margin"
android:gravity="center_horizontal"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="9dp"
android:layout_weight="3"
android:background="#color/button_colour"
android:gravity="center"
android:paddingBottom="9dp"
android:paddingTop="9dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="15dp"
android:src="#drawable/icon_symbol" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="activate and enable"
android:textColor="#android:color/white" />
</LinearLayout>
<Button
android:id="#+id/info_btn"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_gravity="center_vertical|end"
android:background="#drawable/icon" />
</LinearLayout>
Do not override onLayout if you're leaving it empty, remove that function. onLayout is used to measure the parent and its children, if you leave it empty nothing is getting measured.

Android Spinner is unable to scroll

I use Spinner in my app, with keyboard opened.
This Spinner has 9 items (from 1 to 9).
However if keyboard is opened, spinner cannot be scrolled!
Thanks to it, some of items are out of screen, and I cannot select them.
Dialog Layout here:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.material.widget.FloatingEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fet_productName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="#dimen/floating_edittext_margin"
android:layout_marginRight="#dimen/floating_edittext_margin"
android:hint="#string/product_name"
android:inputType="text" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fet_productName"
android:layout_alignLeft="#+id/fet_productName"
android:layout_alignStart="#+id/fet_productName"
android:layout_alignRight="#+id/fet_productName"
android:layout_alignEnd="#+id/fet_productName"
android:layout_marginTop="#dimen/space_20dp"
android:id="#+id/linearLayout2">
<com.material.widget.FloatingEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fet_productUnit"
android:layout_weight="1"
android:hint="#string/product_unit"
android:inputType="text"
android:layout_marginRight="#dimen/space_6dp" />
<Spinner
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="#+id/sP_dialog_productNumber"
android:entries="#array/spinner_cart_item_number"
android:layout_marginLeft="#dimen/space_6dp" />
</LinearLayout>
</RelativeLayout>
Java code here:
public class CartFragment extends Fragment {
private Spinner spNum;
private MaterialDialog dialog;
private static String[] msITEMS;
private ArrayList<CartItemData> itemDatas;
private ArrayAdapter<String> strAdapter;
public CartFragment(){}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_placeholder_cart, container, false);
msITEMS = rootView.getContext().getResources().getStringArray(R.array.spinner_dialog_item_number);
strAdapter = new ArrayAdapter<String>(rootView.getContext(), R.layout.support_simple_spinner_dropdown_item, msITEMS);
strAdapter.setDropDownViewResource(R.layout.support_simple_spinner_dropdown_item);
final FloatingActionButton fabAdd = (FloatingActionButton)rootView.findViewById(R.id.fabAdd);
fabAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog = new MaterialDialog.Builder(getActivity())
.title(R.string.product_title)
.customView(R.layout.dialog_add_cartitem, false)
.positiveText(R.string.dialog_positive_add_cartitem)
.negativeText(R.string.dialog_negative_add_cartitem)
.show();
View view = dialog.getCustomView();
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
spNum = (Spinner)view.findViewById(R.id.sP_dialog_productNumber);
fetName = (FloatingEditText)view.findViewById(R.id.fet_productName);
fetUnit = (FloatingEditText)view.findViewById(R.id.fet_productUnit);
spNum.setAdapter(strAdapter);
dialog.getActionButton(DialogAction.POSITIVE).setOnClickListener(onPositiveClick());
}
});
return rootView;
}
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
}
}
Create a custom scroll:
import java.lang.reflect.Field;
import java.lang.reflect.Method;
import org.holoeverywhere.widget.ListPopupWindow;
import org.holoeverywhere.widget.ListView;
import org.holoeverywhere.widget.Spinner;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;
public class CustomSpinner extends Spinner
{
public CustomSpinner(Context context)
{
super(context);
}
public CustomSpinner(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public CustomSpinner(Context context, AttributeSet attrs, int defStyle, int mode)
{
super(context, attrs, defStyle, mode);
}
public CustomSpinner(Context context, int mode)
{
super(context, mode);
}
#Override
public boolean performClick()
{
boolean bClicked = super.performClick();
try
{
Field mPopupField = Spinner.class.getDeclaredField("mPopup");
mPopupField.setAccessible(true);
ListPopupWindow pop = (ListPopupWindow) mPopupField.get(this);
ListView listview = pop.getListView();
Field mScrollCacheField = View.class.getDeclaredField("mScrollCache");
mScrollCacheField.setAccessible(true);
Object mScrollCache = mScrollCacheField.get(listview);
Field scrollBarField = mScrollCache.getClass().getDeclaredField("scrollBar");
scrollBarField.setAccessible(true);
Object scrollBar = scrollBarField.get(mScrollCache);
Method method = scrollBar.getClass().getDeclaredMethod("setVerticalThumbDrawable", Drawable.class);
method.setAccessible(true);
method.invoke(scrollBar, getResources().getDrawable(R.drawable.scrollbar_style));
if(VERSION.SDK_INT >= VERSION_CODES.HONEYCOMB)
{
Field mVerticalScrollbarPositionField = View.class.getDeclaredField("mVerticalScrollbarPosition");
mVerticalScrollbarPositionField.setAccessible(true);
mVerticalScrollbarPositionField.set(listview, SCROLLBAR_POSITION_LEFT);
}
}
catch(Exception e)
{
e.printStackTrace();
}
return bClicked;
}
}
As shown on this other post by End.Fouad
I found one solution but it makes UX worse.
When the activity is appeared I focus a view which isn't a input view.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true" >
<com.material.widget.FloatingEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/fet_productName"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="#dimen/floating_edittext_margin"
android:layout_marginRight="#dimen/floating_edittext_margin"
android:hint="#string/product_name"
android:inputType="text" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/fet_productName"
android:layout_alignLeft="#+id/fet_productName"
android:layout_alignStart="#+id/fet_productName"
android:layout_alignRight="#+id/fet_productName"
android:layout_alignEnd="#+id/fet_productName"
android:layout_marginTop="#dimen/space_20dp"
android:id="#+id/linearLayout2">
<com.material.widget.FloatingEditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fet_productUnit"
android:layout_weight="1"
android:hint="#string/product_unit"
android:inputType="text"
android:layout_marginRight="#dimen/space_6dp" />
<Spinner
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="#+id/sP_dialog_productNumber"
android:entries="#array/spinner_cart_item_number"
android:layout_marginLeft="#dimen/space_6dp" >
</Spinner>
</LinearLayout>
<requestFocus/>
</RelativeLayout>
android:focusable="true"
android:focusableInTouchMode="true"
<requestFocus/>
Delete the following line in your 'Activity':
getWindow().setFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS,
WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
or if you are using any such flags...

Custom Preference is recreated when I click to some another (standard) preference (Android)

I have a big problem! I defined a custom Preference in my android application (this preference is called SliderPreference that has a SeekBar inside) and this works fine. But then I use some other element (in my case this is CheckBox) my preference is recreated! How can I escape this nasty effect?
There is the code:
public class SliderPreference extends Preference {
private int value;
private SeekBar seekBar;
private TextView textView;
public SliderPreference(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected View onCreateView(ViewGroup parent) {
LayoutInflater li = (LayoutInflater)getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE );
View root = li.inflate( R.layout.preference_seekbar, parent, false);
seekBar = (SeekBar)root.findViewById(R.id.pref_seekbar_seekbar);
textView = (TextView)root.findViewById(R.id.pref_seekbar_percent);
seekBar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) { }
#Override
public void onStartTrackingTouch(SeekBar seekBar) { }
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
if(progress == 0) { seekBar.setProgress(1); progress = 1; }
double current = (((double)progress/(double)seekBar.getMax())*100f);
value = (int) Math.round(current);
textView.setText(Integer.toString(value)+"%");
}
});
return root;
}
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference android:title="hello"/>
<PreferenceCategory
android:title="Donut settings">
<com.example.SliderPreference
android:key=""
android:title="number"/>
</PreferenceCategory>
</PreferenceScreen>
<?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:orientation="horizontal">
<SeekBar
android:id="#+id/pref_seekbar_seekbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="5"
android:padding="2dp"
android:max="100"
android:progress="50"/>
<TextView
android:id="#+id/pref_seekbar_percent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"/>
</LinearLayout>
public class AndroidPrefs extends PreferenceActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.preferences);
}
}

Categories

Resources