-1

1) Is it possible to create drawable, example

static Bitmap.Config conf = Bitmap.Config.ARGB_8888;
static Bitmap bmp = Bitmap.createBitmap(100, 100, conf);
// something draw
// eventually convert to DrawableBitmap

and then convert it / asiggn as / put in resource, to use in function with resource id param, like:

public void setWidgetLayoutResource (int widgetLayoutResId)

or
2) is it possible to dynamically draw to change image in R.drawable.something.bmp?

All this for change color of widget in setWidgetLayoutResource() to any color, not fixed as color of concrete resource

theWalker
  • 2,022
  • 2
  • 18
  • 27
  • 1
    can you comment this -1 or question? – theWalker Feb 06 '16 at 14:04
  • `widgetLayoutResId` is something like `R.layout.*`, so what does it have to do with a `Bitmap`? – pskink Feb 06 '16 at 15:58
  • Maybe exist some type conversion object to resource. I dont know and I ask about it. Question is 2 partial. You say 1) or 2) is obvious and certainly impossible? – theWalker Feb 06 '16 at 16:09
  • xml layouts define UI components and have nothing to do with Bitmaps – pskink Feb 06 '16 at 16:13
  • png in resources isn't xml layout – theWalker Feb 06 '16 at 16:14
  • no, it is not, it is something like R.drawable.* – pskink Feb 06 '16 at 16:15
  • in setWidgetLayoutResource (int widgetLayoutResId) is possible set id of drawable png from res. -1 for you. – theWalker Feb 06 '16 at 16:16
  • 1? what 1? what do you mean? – pskink Feb 06 '16 at 16:23
  • its joke:) in setWidgetLayoutResource (int widgetLayoutResId) argument may be R.drawable.* not must be R.layout.* – theWalker Feb 06 '16 at 16:26
  • no, it has to be R.layout.*, see http://androidxref.com/6.0.1_r10/xref/frameworks/base/core/java/android/preference/Preference.java#523 – pskink Feb 06 '16 at 16:27
  • I know what is in my code and on the device screen, realy – theWalker Feb 06 '16 at 16:31
  • well, my android studio marks it with lint error `"Expected resource of type layout less... (Ctrl+F1)"` and of course the app crashes but if you say so... – pskink Feb 06 '16 at 17:24
  • In my Eclipse no errors and image from R.drawable.* is shown as accesory of the preference screen line – theWalker Feb 06 '16 at 17:32
  • we are of course talking about `Preference#setWidgetLayoutResource` described [here](http://developer.android.com/reference/android/preference/Preference.html#setWidgetLayoutResource(int))? where it clealy states: `widgetLayoutResId The **layout resource ID** to be inflated into the main layout.` and the source code i posted above proves that it is true – pskink Feb 06 '16 at 17:38
  • then put drawable to simply layout only with it. all its out of my questions. – theWalker Feb 06 '16 at 17:46

1 Answers1

0

My own answer

This question is relative to my other: Android PreferenceScreen and I was do it in this way:

ColorLinesView.java

public class ColorLinesView extends View
{
    private static GradientDrawable gdDefault = new GradientDrawable();
    public static int fillColor;

    public ColorLinesView(Context context, AttributeSet attrs)
    {   super(context, attrs);
    }

    @Override protected void onDraw(Canvas cv) 
    {   
       gdDefault.setColor(fillColor);
       gdDefault.setCornerRadius(4);
       gdDefault.setStroke(2, 0xffbbbbbb);
       this.setBackgroundDrawable(gdDefault);
    }
}

color_lines_accesory.xml

<?xml version="1.0" encoding="utf-8"?>
<android.swp.ColorLinesView xmlns:android="http://schemas.android.com/apk/res/android"
 android:id="@+id/colorBoxLines"
 android:layout_width="52dp"
 android:layout_height="26dp"
 android:gravity="right"
 android:layout_marginRight="6dp" />

and finally while programmatically create PreferenceScreen, after add category preference "somePref":

ColorLinesView.fillColor = 0xff00ff00; // examply green
somePref.setWidgetLayoutResource(R.layout.color_lines_accesory);

and the same two lines (with new color) in OnPreferenceClickListener() for "somePref" category, after using color picker to change color.

result:

enter image description here

Community
  • 1
  • 1
theWalker
  • 2,022
  • 2
  • 18
  • 27
  • any reason for making that so complex instead of dreating a custom `Preference`? – pskink Feb 08 '16 at 13:38
  • What is "dreating", my english is very simply and I cant find it. You think "treating"? – theWalker Feb 08 '16 at 13:45
  • sorry, typo: "creating", why not to make a class that extends `Preference`? – pskink Feb 08 '16 at 13:48
  • In ColorLinesView.java I have full programmatically control & I may draw what I want. Rest of the preference (icon, summary, title) is controlled by system and the style for whole PreferenceScreen. – theWalker Feb 08 '16 at 13:50
  • Class extends Preference: I was probe and I have problems with this. – theWalker Feb 08 '16 at 13:53
  • and now try to use ColorLinesView twice in your PreferenceScreen... too bad as you are using a static field – pskink Feb 08 '16 at 13:54
  • yes I know:))) now I work about instantiate my class:). class and variables are the same, but parents not. – theWalker Feb 08 '16 at 13:55
  • just create a custom Preference, see how other Preferences do that (like CheckBoxPreference for example) – pskink Feb 08 '16 at 13:55
  • see http://androidxref.com/6.0.1_r10/xref/frameworks/base/core/java/android/preference/CheckBoxPreference.java#35 all you need to do your stuff in `onBindView` – pskink Feb 08 '16 at 13:59
  • Stuff with adding view to control color + extend preference code as I think is complicated like my ColorLinesView. Additionally in bindView are problems with get dimensions etc. For me is enough what I make. – theWalker Feb 08 '16 at 14:12
  • Temoporary because in all of my preference screens (more is this in app) not exist more like 2 color boxes in one window, I was made the same simply class code & xml layout 2 times with different names. In my country says: "Temporary sulutions are most durable" :))) – theWalker Feb 08 '16 at 14:25
  • and in practice those 20 lines of code make a custom preference: http://pastebin.com/GSTiYBWz – pskink Feb 08 '16 at 16:38
  • Pretty small code, but I don't understand how to later dynamically change color ( d.setColor(0xaaff0000)) to any other many times. Where is the trick? – theWalker Feb 08 '16 at 17:34
  • I work about it but v.setBackground(new InsetDrawable(gd, 0, 18, 0, 18)) or v.setBackground(gd) not work. – theWalker Feb 08 '16 at 19:06
  • I really can't do this. About my class instantation: it's simply. getId() is duplicated in other instances, but hashCode() is unique. Simply putInt and getInt (or other) from shared preferences for many paremeters without string coverting. Additionally parent of view (widget layout) reports rect of the widget field in preference line, parent of parent (LinearLayout) - rect on preference screen. It's flexible. – theWalker Feb 10 '16 at 07:52
  • sorry, i have no idea what you are talking about... you are talking about your or my code? – pskink Feb 10 '16 at 08:14
  • Yes, my first step is using getId(). Only one class *.java for many widgets, and multiple widget layout (on one preference screen, many screens may using the same layout). After creating preference: String sn = "ColorWidget_"+Integer.toHexString(R.id.colorBoxDots); SWP.preferencesEditorSwp.putInt(sn, SWP.PREF_LogDotsColor); =========== in class onDraw: String sn = "ColorWidget_"+Integer.toHexString(this.getId()); fillColor = SWP.preferencesSwp.getInt(sn, 0); =========== Now Im working about using hasCode() in place getId() to define only one widget layout. – theWalker Feb 10 '16 at 09:01
  • again... does my code work or not? if not, what doesn't work? – pskink Feb 10 '16 at 09:11
  • Not. I don't know why. I'm work about it independly. May it depend from styles.xml, I check it. – theWalker Feb 10 '16 at 09:13
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/103076/discussion-between-skippy-and-pskink). – theWalker Feb 10 '16 at 11:03
  • the color gradient is not visible in any xml style do you create preferences in xml or programatically? – theWalker Feb 10 '16 at 11:15
  • http://pastebin.com/JhqEsgVq see the bottom, there are tho `MyPref`s on the `Preferencescreen` (lines 48-63) – pskink Feb 10 '16 at 11:17
  • ok I check it later, I dont know what a different from programmatically creating, but all of the options must be checked – theWalker Feb 10 '16 at 11:21
  • it doesn't matter if you do that in xml or by `PreferenceScreen#addPreference`, both ways work, i just checked the second way – pskink Feb 10 '16 at 11:38
  • Well, still nothing, v.setBackground() not exist, I use v.setBackgroundDrawable(). As I think its only difference. It's mistake in youre code or something in my app? – theWalker Feb 10 '16 at 11:58
  • hmm, check `View#setBackground()` and `View#setBackgroundDrawable()` javadocs, i use API level 16+ – pskink Feb 10 '16 at 12:01
  • I havent info about using supress lint new api, but error. Do yoy use Eclipse? – theWalker Feb 10 '16 at 12:15
  • OK solved, ufff. I dont like unsolved problems:) after v.setVisibility I insert: v.setMinimumHeight(); v.setMinimumWidth(); and it work. – theWalker Feb 10 '16 at 14:56
  • if you need `setMinimumHeight` / `setMinimumWidth` then probably there is a better idea to provide a real widget layout by calling `setWidgetLayoutResource` which points to some `ImageView` – pskink Feb 10 '16 at 16:15
  • is it connected with `setMinimumHeight` / `setMinimumHeight`? if so, as i said, better provide a real widget layout by calling `setWidgetLayoutResource` which points to some `ImageView` - that way the code in `onBindView` will be just two lines – pskink Feb 10 '16 at 21:30
  • no, this is my class onDraw(canvas) and in widget layout "fill_parent" on both dimensions – theWalker Feb 10 '16 at 21:33
  • but in pref subclassing I think it may be the same – theWalker Feb 10 '16 at 21:35
  • what onDraw? Preference does not have such method – pskink Feb 10 '16 at 21:36
  • in my class "ColorLinesView extends View" in my post up, I not edit it – theWalker Feb 10 '16 at 21:40
  • You dont need it, just use an ImageView – pskink Feb 10 '16 at 21:42
  • your png with big dots is interesting design idea – theWalker Feb 10 '16 at 22:51
  • `my code is ready pastebin.com/SzPkd3zq` how can you manage that code? compare it to this: http://pastebin.com/BSzFcVKk, why dont you use a custom preference (a class extending `Preference`)? – pskink Feb 11 '16 at 07:26
  • I see, with simply color picker. I have more functionally and user friendly class over whole app http://imageshack.com/a/img923/6318/j72PnT.png . I don't understand question "how can you manage". You think about programm vs xml creating? "Why dont use" - if I fail to tackle full instantation problem (one widget xml over one preference screen), I do it. – theWalker Feb 11 '16 at 07:39
  • Have you seen MyPref? no id workarounds, everything in a one place, you want a custom picker? No problem extend just Preference, not DialogPreference as it adds its own dialog buttons – pskink Feb 11 '16 at 07:48
  • I dont want color picker, I have code for this screen shot. MyPref - as I wrote: if I fail to tackle full instantation problem (one widget xml over one preference screen), I do it. I want solve this problem in my way. – theWalker Feb 11 '16 at 07:51
  • `In my country says: "Temporary sulutions are most durable"` now it's clear why this happen: you have a right working solution, but still insist on using your temporary code, this is what i cannot undarstand – pskink Feb 11 '16 at 10:32
  • when I wrote this, solution contained 3 xmls and 3 classes, now its 3 short xml and 1 common class. temporary state chenged. – theWalker Feb 11 '16 at 11:25
  • it is still a completely bad practice: just take a look on other `Preference`s you can use: `CheckBoxPreference`, `EditTextPreference`, `ListPreference` etc, just see their sources and compare them with your code, your way is not a way you should write a custom `Preference`, just follow their way – pskink Feb 11 '16 at 11:29
  • That what you say, make setWidgetLayout() nonsense. – theWalker Feb 11 '16 at 14:26
  • in standard google's `Preference`s they do not call `setWidgetLayoutResource` anywhere, so what nonsense do you mean? – pskink Feb 12 '16 at 15:08
  • and if you really want to avoid calling `setWidgetLayoutResource`, [this](http://pastebin.com/Bjj3bY1Z) is a solution that doesn't use any xml files at all (except sample color picker layout) and still you can add your preference either from xml or java code (the complete code has only ~80 lines of java code) – pskink Feb 13 '16 at 10:53
  • your project uses api older than android 4.1 and i dont have such emulator so i was not able to run in in pre 4.1 environment, does it work at you? – pskink Feb 13 '16 at 14:36
  • I work only on real devices, never on emu. My app min API is 11. But runs without problems on A4 and A5 devices. BTW I dont understand why emu with 4.1 not work with backward compatibility. – theWalker Feb 13 '16 at 17:22
  • i know it works on A4.1+ (including A5) because i build it and run on those versions, i was asking about versions older than A4.1.. did it work on those? – pskink Feb 13 '16 at 17:37
  • Today I cant check it on older device like 4.1. In my code no @SuppressLint("NewApi") compiler commands. targetSdkVersion = 8, as I check now. What goes wrong? PrefrenceScreen creating or widget class? – theWalker Feb 13 '16 at 17:45
  • earlier i sent you some code (working at me) but you said that you had to add some `v.setMinimumHeight();` / `v.setMinimumWidth()` to make it work, that is why i am asking if the code i posted here: http://pastebin.com/Bjj3bY1Z works or not at you – pskink Feb 13 '16 at 17:49
  • Oh yes. In this code all is OK. Eclipse not alarm with "newApi" info. I cant check it on emu or device <4.1. One change are seekbars colors, all seekbars are blue, but img with destination color work properly. v.setMinimumWidth() I'v must insert in early version of code. In this version - not. I create myPref programmatically: myPref = new MyPref(this, null); myPref.setKey("myPref"); myCategory.addPreference(myPref); not in xml, this only one different. – theWalker Feb 13 '16 at 18:23
  • good, btw any reason for not defining preferences in xml? it is much easier than `myPref = new MyPref(this, null); myPref.setKey("myPref"); myPref.setTitle("my title"); myPref.setSummary("my summary"); myCategory.addPreference(myPref` and stuff like that – pskink Feb 13 '16 at 18:45
  • I always make it programmatically. Normally in own sub-method common for whole code, in one line create new, set key, title etc. This is faster in work for me. – theWalker Feb 13 '16 at 18:50
  • so if you dont want to use it inside xml, change the constructor and use like this: `myPref = new MyPref(this);` – pskink Feb 14 '16 at 07:37
  • please look at: http://stackoverflow.com/questions/35404956/how-to-get-preferencescreen-line-height – theWalker Feb 15 '16 at 09:05