0

I need to get a real reference to the components inside PreferenceActivity.

CheckBoxPreference myPref = (CheckBoxPreference) findPreference("setting1");
View v = (View)myPref.getView(null, getListView());

This code returns the view I want, but it is actually a copy of what I want and changing the properties there, doesn't change the real objects.

Please note that I need to change some properties (such as color or alignment) of the components there. And Also I know that I can use Layout, but because of some reasons, I want to do it just programmatically

mehrdad seyrafi
  • 3,084
  • 2
  • 19
  • 16
  • you can declare a global variable of "View" , before the onCreate() method, and change its properties wherever in your code. – Farhad Jun 17 '14 at 07:04

1 Answers1

0

Figured out: this.getListView() returns a ViewGroup which contains everything in the ListView. By traversing the children, everything is accessible.

There is just one issue that in the onCreate event, the ListView will be filled, so one solution is to poll and check if it is filled (for example bt defining an AsyncTask.

public class AsyncChecker extends AsyncTask <Void, Void, Boolean> {
public interface AsyncCheckerCallbackInterface {

    boolean checkFunction();
    void checkDone(Boolean result);
}

private int period;
private int maxTime;
final AsyncCheckerCallbackInterface callback;

public static void Start(int period, int maxTime, AsyncCheckerCallbackInterface callback){
    new AsyncChecker(period, maxTime, callback).execute();
}

public AsyncChecker(int period, int maxTime, AsyncCheckerCallbackInterface callback) {
    this.period = period;
    this.maxTime = maxTime;
    this.callback = callback;
}

@Override
protected Boolean doInBackground(Void... arg0) {
    long startTime = System.currentTimeMillis();
    while(System.currentTimeMillis() - startTime < maxTime){
        if (callback.checkFunction())
            return true;
        try {
            Thread.sleep(period);
        } catch (InterruptedException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
    return false;
}

@Override
protected void onPostExecute(Boolean result) {
    callback.checkDone(result);

}

}

public static void DoIt(final PreferenceActivity pref){
    AsyncChecker.Start(10, 1000, new AsyncChecker.AsyncCheckerCallbackInterface() {
        public boolean checkFunction() {
            int childCount = pref.getListView().getChildCount();
            if (childCount > 0)
                return true;
            return false;
        }

        public void checkDone(Boolean result) {
            if (result)
                new PreferencesRightAligner().Recursive(pref.getListView());
        }
    });
}

private void Recursive(View v){
    if (v instanceof CheckBox){
        TextView chk = ((TextView) v);
        ViewGroup father = (ViewGroup)chk.getParent();
        ViewGroup grandpa = (ViewGroup)father.getParent();
        //if (grandpa.indexOfChild(father) != 1){
            grandpa.removeView(father);
            grandpa.addView(father, 0);
        //}
    }
    else if (v instanceof TextView){
        TextView txt = ((TextView) v);
    }
    else if (v instanceof RelativeLayout){
        RelativeLayout lay = ((RelativeLayout) v);
        ((RelativeLayout) v).setGravity(Gravity.RIGHT);
    }
    if (count!=0 && v instanceof LinearLayout){
        LinearLayout lay = ((LinearLayout) v);
        ((LinearLayout) v).setGravity(Gravity.LEFT);
    }
    if (v instanceof ViewGroup){
        int childCount = ((ViewGroup)v).getChildCount();
        for(int i=0; i<childCount; i++) {
            View c = ((ViewGroup)v).getChildAt(i);
            Recursive(c);
        }
    }
}

And in the onCreate:

DoIt(this);
mehrdad seyrafi
  • 3,084
  • 2
  • 19
  • 16