Changing AppWidget's background dynamically with a 9 patch drawable - android

There is no setBackground() method in the RemoteViews class, so I've used the following workaround:
Created a FrameLayout for my app widget with an ImageView as the background view.
Changed the ImageView image using setImageViewResource() method.
Unfortunately, when it comes to 9-patch drawables this method does not work. Also when an ImageView's android:src attribute points to 9-patch - it doesn't work too. Is there any way to change the AppWidget's background image programatically using a 9-patch drawable? Thanks in advance.
EDIT
Settings the 9-patch as initial background in the XML:
<ImageView
android:id="#+id/small_widget_layout_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_lime" />
When I use android:src="#drawable/background_lime" it doesn't stretch the image properly, this code works fine. And the code to change the background from the AppWidgetProvider onUpdate method:
views.setImageViewResource(R.id.small_widget_layout_bg,
R.drawable.backgroung_lime);
This does not stretch the image as a 9-patch.

This answer was diagnosed in the above comments...
RemoteView doesn't allow access to View.setBackground(), so your workaround of using the android:src property of an ImageView is good, providing that the android:scaleType property is set to fitXY.
ImageView won't stretch it's foreground image unless you tell it to.

Please ignore if u find this trivial or irrelevant, but canT you try (assuming you are dealing with widgets):
Declaring different layouts (xml)for your widget.
Change the remoteView's source (layout.id) instead of trying to make alterations to the selected layout.
AFAIK, this is the most common approach to solving such problems. This is not perfect for two simple things I could note myself:
What do you do if you have n different "states" / "views" in your widget?
But as long as your 9-patch files are also static resources, n is painful but still theoretically manageable.
It s tedious to keep track of the changes in these parallel files.
I'd also love to find an easy way for this one...
This approach may not be an option for you also because it is basically the hard way. But it s an option nonetheless.
Suggestion #2
Have you tried using the method?
public void setInt (int viewId, String methodName, int value)
remoteView.setInt(R.id.viewid, "setBackgroundResource", R.drawable.backgroung_lime);
From another question: Change remoteView ImageView background

Related

What are insets in android?

I am beginner at android development. I was recently looking at someone else's code and found some functions view.onApplyWindowInsets(windowInsets), windowInsets.getSystemWindowInsetTop(). And this word was used frequently in the same app.
I tried googling it and found InsetDrwable class with explanation
A Drawable that insets another Drawable by a specified distance. This is used when a View needs a background that is smaller than the View's actual bounds.
Can some one explain me what is the meaning on Insets and what those piece of code up there meant?
A explanation with an example will be appreciated.
Suppose you have a TextView and you need to add a background to the TextView. But, on the other hand, you don't want the background to scan the entire View (TextView), therefore, you need to use Insets.
A Drawable that insets another Drawable by a specified distance or
fraction of the content bounds. This is used when a View needs a
background that is smaller than the View's actual bounds.
The background can be a Drawable. Hence you need to use the <inset> attribute inside your xml file (activity_main.xml) "For Example".
Then, after using the <inset> tag, you can specify some attributes such as:
<inset
android:drawable="#drawable/(Enter the file name under drawable)"
android:insetBottom="4dp"
android:insetTop="4dp"/>
For more information please have a look at InsetDrawable on Android developer.com
Hope this helps!

about semi-transparent image function in android

just a question wondering when I was working on my app
let say I have image which I want to make it semi-transparent but I don't want to add about image into the app (as in create another one and add it in)
is there a way to do it??
You have to set the alpha value of the image. ( If i understood you correctly)
If you load this Image in to a ImageView, you can set it from xml like this:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/your_image_in_res"
android:alpha="1"/>
where according to the developers site "alpha property of the view, as a value between 0 (completely transparent) and 1 (completely opaque). " Play with the value to get the desired effect.
If you have your image int the res/drawable folder, you can use it anywhere in your code. In xml almost all elements have the following parameter:
android:background="#drawable/im_your_image"
. You just have to set this in all places, where you want this to be your background, for the parent views background. (e.g. your MainActivitys RelativeLayout that you inflate in the onCreate funciton. You see the image is put only once in the package, but this way you can set it several times as a background.

Is it possible to use fonts for Images/icons in Android

I Just know that we can use fonts for text only by using the TypeFace class. But can anyone please confirm me about images.
I have added all my custom fonts(.ttf) inside asset/fonts folder. Placed some ImageViews in their appropriate place in my app. And trying to use fonts to set images on them.
I have googled for a long time, but unfortunately unable to find any satisfied hint or help.
Can any one please help me to go forward from the current step.
Placed some ImageViews in their appropriate place in my app. And trying to use fonts to set images on them.
ImageView accepts images. Whatever set of pixels or vectors are in a font, they are not images from the standpoint of ImageView. The simplest way to display material from a custom font is through TextView, though you are welcome to implement your own Drawable and attempt to use that with an ImageView, or draw straight to the Canvas (e.g., in a custom widget).
You can use font awesome. Import the android-iconify/1.0.6 jar library from http://repo1.maven.org/maven2/com/joanzapata/android/android-iconify/1.0.6/
and you can use IconTextView
<IconTextView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:padding="10dp"
android:text="{fa-mobile}"
android:textColor="#9fd46c"
android:textSize="30dp"
/>

How to make segmented seekbar/slider look like following?

I was wondering if anyone can provide hint or source to achieve following slider widget used in "Circle – Who's Around?" This is the first time I have ever came across this and I am not sure what to exactly name this widget.:
I was thinking of using custom seekbar background to do this but, I am not sure how do I figure out exact pixels that the seekbar will reach of next step. Since, that will be independent to devices. In my case I am planning to use images, rather than the indicators.
Please don't point to this link http://www.mokasocial.com/2011/02/create-a-custom-styled-ui-slider-seekbar-in-android/ because this is not what I want to achieve. They seem to have used static image footer to show D,W,K. I have tried that app and it doesn't even step to the exact dots or D,W,K. I have looked at AT&T Android Slider Controls but, they don't seem to provide any source for it. I have found some iOS devs achieving that but, I don't really understand obj C code in order to achieve that in Android.
This is just a seekbar with a custom thumb and background. You could use a 9patch for the background so it fills nicely and just set them in your styles
Following #Milanix answer using the library at https://github.com/karabaralex/android-comboseekbar here it is a minimum example code that worked for me:
<com.infteh.comboseekbar.ComboSeekBar
android:id="#+id/seekbar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
custom:color="#000"
custom:textSize="12sp"
custom:multiline="false"
/>
Then in the Activity
private ComboSeekBar mSeekBar;
List<String> seekBarStep = Arrays.asList("All","1","5","10","20");
mDistanceSeekBar.setAdapter(seekBarStep);
This will create a black segmented seekbar using default drawables. If you need to add some customization have a look at ComboSeekBar.onDraw(), CustomDrawable.draw() and CustomThumbDrawable.draw().
This project is all but finished but still a solid starting point.
#Giulio thank you for your post, I have the same problem as Ron Eskinder.
I heve fixed it by removing :"custom:color" , "custom:textsize" and "custom:multiline" in xml file. then in Java I put this:
mSeekBar = (ComboSeekBar) findViewById(R.id.seekbar);
List<String> seekBarStep = Arrays.asList("All","1","5","10","20");
mSeekBar.setAdapter(seekBarStep);
Hope this will help

how to give background color for button in android

i have three button which is arranged in table row .I gave background for the three button but it wraps the button .
For starters read my blog :-)
Now my suggestion is to use background definitions with gradients. They look nice and are simpler to create then background images. As I said in the blog you need three of them for the button to work as expected. I have a demo for you here: button_type_0.xml
You will also need to define the colours: colors.xml
and dimensions: dimens.xml
you might also want to consider different dimensions for various dpi values. for example I use half size corners and border for ldpi: ldpi/dimens.xml
Looks all very complicated at the beginning but it is worth it. In the end it will look like this:
declare new a new xml in the drawable folder with the image/color you can specify image for each event state.
and you can you can set this xml as you background
if your xml is 'res/drawable/abc.xml' then set background as
android:background="#drawable/abc"
You can also use ImageButton
<ImageButton
----------------------
android:src="#drawable/youimage"
/>
The benefit for ImageButton is that, no need of different image/color for highlight.

Categories

Resources