Unable to scale Drawable with ScaleDrawable - java

Scaling with ScaleDrawable is not working for me.
The drawable is remained in the same size.
LayerDrawable layerDrawable = new LayerDrawable(layers);
Drawable d = layerDrawable.getCurrent();
ScaleDrawable sd = new ScaleDrawable(d, 0, 0.01f, 0.01f);
return sd.getDrawable();
What i need to do to fix it?
Thanks.

You need to set the level:
LayerDrawable layerDrawable = new LayerDrawable(layers);
Drawable d = layerDrawable.getCurrent();
ScaleDrawable sd = new ScaleDrawable(d, 0, 0.01f, 0.01f);
sd.setLevel(8000);
return sd;
The level ranges from 0 to 10000: at 10000 it is full size, at 0 it does not appear at all.

If you check the reference for ScaleDrawable, you will see that the getDrawable method returns the base drawable. That is, it returns d in your case. You should just return sd as it is already a Drawable.

Related

Android programmatically setting up layout for different screen sizes/densities

I am currently creating an Android app that I want to support multiple screen sizes/densities. When I set up layouts in xml, everything looks fine across different screen sizes. However, there are rows I need to add programatically.
Whenever I add the rows, I can't seem to get things to look consistent across different devices. I believe using the dp unit when settings up heights, widths in xml, along with wrap_content and match_parent when appropriate, have allowed things to easily translate between different devices.
Programatically, when I try to set a layout height/width, I have been trying to convert the anticipated dp value to a pixel value. I've done so using this:
public static int convertToPixels(int value) {
Resources resources = mContext.getResources();
int x = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,value,resources.getDisplayMetrics());
return x;
}
Overall, the heights look ok, but the widths don't look good. For instance, the width of the row will look such that on a smaller device, the information neatly displays across the row, which is what I want. However,when I try to run the app on a tablet, for instance, the information will only stretch to half of the row, which doesn't look good. I want the sizes to scale and neatly display just like on the smaller device.
If anyone has any idea what my problem might be, I would greatly appreciate it. Below is the source for adding a row using java:
LinearLayout row = new LinearLayout(mContext);
row.setOrientation(LinearLayout.HORIZONTAL);
row.setId(Integer.parseInt(txn.getId().toString()));
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(60));
params.setMargins(0, convertToPixels(1), 0, 0);
row.setLayoutParams(params);
row.setBackgroundColor(mContext.getResources().getColor(R.color.white));
LinearLayout imageLayout = new LinearLayout(mContext);
LinearLayout.LayoutParams imageParams = new LinearLayout.LayoutParams(convertToPixels(40), convertToPixels(40));
imageParams.gravity = Gravity.CENTER;
imageLayout.setLayoutParams(imageParams);
ImageView image = new ImageView(mContext);
if (txn.getTransactionStateID() == Constants.TXN_STATUS_OK) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.ok));
} else if (txn.getTransactionStateID() == Constants.TXN_STATUS_SUSPICIOUS) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.alert));
} else if (txn.getTransactionStateID() == Constants.TXN_STATUS_RED_FLAG) {
image.setImageDrawable(mContext.getResources().getDrawable(R.drawable.flag));
}
imageLayout.addView(image);
row.addView(imageLayout);
LinearLayout txnMiddleLayout = new LinearLayout(mContext);
txnMiddleLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams txnTopParams = new LinearLayout.LayoutParams(convertToPixels(400), convertToPixels(60));
txnTopParams.setMargins(convertToPixels(10), 0, 0, 0);
txnMiddleLayout.setLayoutParams(txnTopParams);
TextView txnTopContents = new TextView(mContext);
txnTopContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
txnTopContents.setText(txn.getTopLineContents());
txnTopContents.setTextColor(Color.BLACK);
// txnTopContents.setTextSize(convertToPixels(16));
TextView txnBottomContents = new TextView(mContext);
txnBottomContents.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
txnBottomContents.setText(txn.getBottomLineContents());
// txnBottomContents.setTextSize(convertToPixels(12));
txnMiddleLayout.addView(txnTopContents);
txnMiddleLayout.addView(txnBottomContents);
row.addView(txnMiddleLayout);
LinearLayout txnBottomLayout = new LinearLayout(mContext);
txnBottomLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams txnBottomParams = new LinearLayout.LayoutParams(convertToPixels(120), convertToPixels(60));
txnBottomLayout.setLayoutParams(txnBottomParams);
TextView amount = new TextView(mContext);
amount.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
amount.setText(txn.getAmountStr());
amount.setTextColor(Color.BLACK);
// amount.setTextSize(convertToPixels(16));
TextView date = new TextView(mContext);
date.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, convertToPixels(30)));
date.setText(txn.getDateStr());
// date.setTextSize(convertToPixels(12));
txnBottomLayout.addView(amount);
txnBottomLayout.addView(date);
row.addView(txnBottomLayout);
txnList.addView(row);
I eventually found a solution. Instead of trying to set an exact width value, I set the width of the layout or textview to 0, and used layout_weight instead.
https://stackoverflow.com/a/3996104/4056947

Adjusting height of linear layout programmatically

Whenever I try setting the height of a LinearLayout, I always get this exception:
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
Here is my code:
LinearLayout.LayoutParams hide = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 0);
LinearLayout.LayoutParams show = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 100);
driverMainOptions.setLayoutParams(hide);
mapDirections.setLayoutParams(show);
Is there a specific import statement I need for this to execute properly?
This should work
LinearLayout lLayout = new LineaLayout(context);
LayoutParams params = lLayout.getLayoutParams();
params.height = 200;
params.width = 200;
lLayout.setLayoutParams(params);
see the accepted answer at Android: How to Programmatically set the size of a Layout

Set Background Drawable Resource from String

I have looked everywhere but cannot find an example of programmatically setting a background resource from a string value?
As an example:
Drawable a = getResources().getDrawable( R.drawable.a );
Drawable b = getResources().getDrawable( R.drawable.b );
Drawable c = getResources().getDrawable( R.drawable.c );
abc.setBackgroundResource("b");
Is this possible or would I have to do it as a big switch statement?
you have getResources().getIdentifier for this purpose. It returns the id of the resources from its name.
E.g.:
int resId = getResources().getIdentifier("b", "drawable", getPackageName());
Here you can find the documentation.

Inline images in a TextView

I'm trying to put inline images in a TextView. Here goes my code:
ImageGetter imageGetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Drawable d = ctx.getResources().getDrawable(Integer.parseInt(source));
d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
return d;
}
};
CharSequence votesString="124 votes";
votesString=Html.fromHtml(votesString
+"(<img src='" + R.drawable.icon_upvotes + "'/>)", imageGetter, null);
labelVotes.setText(votesString);
It works (see the image below) but I would need the image to be vertically centered. How can I do that?
Thanks
Instead of use HTML, you can use
labelVotes.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, R.drawable.icon_upvotes);
and playing a little bit with padding, using setCompoundDrawablePadding
Why you don't use android:drawableRight ?

Android: What happens when you change the dimensions of a frameLayout that is set to wrap_content programmatically?

I have a FrameLayout in my application and in the xml file its width and height is set to wrap_content . then I want to change its dimensions programmatically like this :
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
ViewGroup.LayoutParams previewParam = preview.getLayoutParams() ;
previewParam.height =(int) (height * 0.86);
previewParam.width = (int)(previewParam.height/ratio) ;
preview.setLayoutParams(previewParam) ;
When I do this the program exits when I start this activity, is the problem with wrap_content or not ?
I believe its because previewParam is null at that point. Try doing it like this:
FrameLayout preview = (FrameLayout) findViewById(R.id.abs__action_bar);
ViewGroup.LayoutParams params = new ViewGroup.LayoutParams((height/ratio),
(int) (height * 0.86));
preview.setLayoutParams(params);
preview.requestLayout();

Categories

Resources