I am working on layout on Android project.
Where the layout parameters are coming programmically. My current layout is coming -
Code-
mRedBackground=initBackground(getResources().getColor(R.color.transparent_red_80), this.mWidthScreen, (int)(0.0984375F * this.mWidthScreen), 0, 0);
And initBackground looks like-
public RelativeLayout initBackground(int intColor, int intWidthscreen, int intHeight, int intMerginLeft, int intMerginTop)
{
RelativeLayout localRelativeLayout = new RelativeLayout(this.mContext);
if (intColor != 0)
localRelativeLayout.setBackgroundColor(intColor);
RelativeLayout.LayoutParams localLayoutParams = new RelativeLayout.LayoutParams(intWidthscreen, intHeight);
localLayoutParams.setMargins(intMerginLeft, intMerginTop, 0, 0);
localRelativeLayout.setLayoutParams(localLayoutParams);
return localRelativeLayout;
}
But I need it in rounded shape. something like-
I can not use other than RelativeLayout due to project restriction. I also can not use Background XML also. All I need to set from code. I am sorry for that. Any Help will be appreciated.
GradientDrawable gd = new GradientDrawable();
gd.setColor(Color.RED);
gd.setCornerRadius(10);
gd.setStroke(2, Color.WHITE);
view.setBackground(gd);
Related
I have programmatically added Text View and Button.
Code
final TextView test = new TextView(getApplicationContext());
test.setText(String.valueOf(counterQuantity));
test.setTextColor(getResources().getColor(R.color.black));
test.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 10);
GradientDrawable border = new GradientDrawable();
border.setShape(GradientDrawable.OVAL);
border.setStroke(1, getResources().getColor(R.color.black));
border.setCornerRadius(2);
border.setColor(getResources().getColor(R.color.colorPrimaryDark)); //white background
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
test.setBackgroundDrawable(border);
} else {
test.setBackground(border);
}
Button articleButtonId = (Button) v;
int articleButton = articleButtonId.getId();
final String articleButtonText = articleButtonId.getText().toString();
Log.w("articleButton", "" + articleButton);
final Button button = new Button(getApplicationContext());
button.setText(test.getText() + " " + " " + articleButtonText);
button.setGravity(Gravity.CENTER);
button.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 12);
button.setTextColor(getResources().getColor(R.color.black));
button.setBackgroundColor(getResources().getColor(R.color.article_button_group));
button.setBackground(getResources().getDrawable(R.drawable.order_button_group_bg));
Here in above code i have a Text View and Button added programatically.
The text of Text Views added inside Button setText method using concatination.
Now i want to make only Text View in Circle View.
I have tried with GradientDrawable but the effect on Text View is remains same no any effects taken by Text View.
In other scenarios GradientDrawable is working fine but in this scenario is not getting what i want.
Here is image what i want exactly.
Any help will be highly appreciated.
below is a snippet for your problem and I've tested on kitkat and marshmallow devices, it is showing properly, So give it a try...
activity_main.xml
<LinearLayout
android:id="#+id/lv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"/>
MainActivity.java
1- find id of linearlayout
2 - Then create dynamic view with TextView or Button etc...
private void createDynamicView() {
TextView tvDigit = new TextView(this);
tvDigit.setText("1");
tvDigit.setTextColor(ContextCompat.getColor(this, R.color.white));
tvDigit.setGravity(Gravity.CENTER);
tvDigit.setTextSize(18);
customViewOval(tvDigit, ContextCompat.getColor(this, R.color.teal_500), ContextCompat.getColor(this, R.color.transparant), 100, 100);
TextView tvName = new TextView(this);
tvName.setText("Richard");
tvName.setGravity(Gravity.CENTER);
tvName.setTextColor(ContextCompat.getColor(this, R.color.black_80));
tvName.setPadding(10, 0, 0, 0);
tvName.setTextSize(18);
lv.addView(tvDigit);
lv.addView(tvName);
customViewWithRadius(lv, ContextCompat.getColor(this, R.color.lightgrey), ContextCompat.getColor(this, R.color.transparant), 90);
}
custom view for digit within oval
public static void customViewOval(View view, int backgroundColor, int borderColor, int height, int width) {
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.OVAL);
shape.setColor(backgroundColor);
shape.setStroke(1, borderColor);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(height, width);
view.setLayoutParams(layoutParams);
view.setBackgroundDrawable(shape);
}
custom view for main background with radius
private static void customViewWithRadius(View view, int backgroundColor, int borderColor, float radius) {
GradientDrawable shape = new GradientDrawable();
shape.setShape(GradientDrawable.RECTANGLE);
shape.setCornerRadii(new float[]{radius, radius, radius, radius, radius, radius, radius, radius});
shape.setColor(backgroundColor);
shape.setStroke(1, borderColor);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(layoutParams);
view.setPadding(10, 10, 40, 10);
view.setBackgroundDrawable(shape);
}
Hope it'll help to solve your problem.
As you can see on the picture below, I put a GradientDrawable as background of a textview.
The result is weird : the text is croped.
Result
And there is the code :
GradientDrawable border = new GradientDrawable();
border.setColor(0x00FFFFFF);
border.setStroke(1,Color.GRAY);
border.setCornerRadius(size);
EditText surname = new EditText(this);
surname.setGravity(Gravity.CENTER_HORIZONTAL | Gravity.BOTTOM);
surname.setBackground(border);
surname.setTypeface(Stats.fontRegular);
FrameLayout.LayoutParams lps = new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, size_x);
surname.setLayoutParams(lps);
view.addView(surname);
If you know a way to uncrop the text it will be very usefull for me :)
You can change the gravity with this XML code:
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="#string/**yourstringname**"
/>
I found something that works :
When you use a EditText, it seems that there are some paddings inside the view. So you can 'disable' them using
EditText.setPadding(int left, int top, int right, int bottom)
Try this:
GradientDrawable border = new GradientDrawable();
border.setColor(0x00FFFFFF);
border.setStroke(1,Color.GRAY);
border.setCornerRadius(size);
border.setSize(450, 150);
Hi I wanted to change height and width property of image view inside my activity
I tried it in following way but its not working for me ...
View card_view = getLayoutInflater().inflate(R.layout.card_details1,null);
coupon_img = (ImageView) card_view.findViewById(R.id.coupon_image);
// I tried this ////////
coupon_img.getLayoutParams().height = 20;
coupon_img.getLayoutParams().width = 20;
// I also tried this ////
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(100, 100);
coupon_img.setLayoutParams(layoutParams);
// also this one ////
coupon_img.setMaxHeight(10);
But I am not able to change height and width of imageview src. Is there any mistake I am doing?
How to do this?
Need help...
Thank you...
In this piece of code, I am creating a new instance of an ImageView at runtime and setting dimensions to it:
// SET THE IMAGEVIEW DIMENSIONS
int dimens = 120;
float density = activity.getResources().getDisplayMetrics().density;
int finalDimens = (int)(dimens * density);
LinearLayout.LayoutParams imgvwDimens =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgAlbumPhoto.setLayoutParams(imgvwDimens);
// SET SCALETYPE
imgAlbumPhoto.setScaleType(ScaleType.CENTER_CROP);
// SET THE MARGIN
int dimensMargin = 5;
float densityMargin = activity.getResources().getDisplayMetrics().density;
int finalDimensMargin = (int)(dimensMargin * densityMargin);
LinearLayout.LayoutParams imgvwMargin =
new LinearLayout.LayoutParams(finalDimens, finalDimens);
imgvwMargin.setMargins
(finalDimensMargin, finalDimensMargin, finalDimensMargin, finalDimensMargin);
This will set the dimensions of the ImageView. They will, however, be in px. Use the code from here if you need dp values: https://stackoverflow.com/a/9563438/450534
UPDATED:
To change the dimensions of an existing ImageView that has already been defined in the XML, use this:
coupon_img.setLayoutParams(new LayoutParams(100, 100));
try some thing like this...
LayoutParams params = new LayoutParams(100, 100);
parantlayout.addView(coupon_img, params);
I think this will help you.
I think you are not adding the changed image to the layout..
LinearLayout ll = (LinearLayout)findViewById(R.layout.yourlinearlayout);
image.setLayoutParams(
new LinearLayout.LayoutParams(
bmp.getWidth(),
bmp.getHeight()));
ll.addView(image);// Then add the image to linear layout
I have to make a seekbar where i can change almost everything programmatically. And now I'm having a problem to change the thumb and the bar size. Everything works great if the bar is larger or the same size as the thumb, but if the thumb is larger than the bar, I can't get it to show the entire thumb with the correct size of the bar.
Here's my code to create the thumb:
public void setSeekBarThumb(int width, int height, int rColor, int gColor, int bColor){
ShapeDrawable thumb = new ShapeDrawable( new OvalShape() );
thumb.setIntrinsicHeight( height );
thumb.setIntrinsicWidth( width );
thumb.setBounds(new Rect(0, 0, width, height));
StateListDrawable states = new StateListDrawable();
states.addState(new int[] {android.R.attr.state_pressed},thumb );//add to the state list with the state pressed
thumb = new ShapeDrawable( new OvalShape() );
thumb.setIntrinsicHeight( height );
thumb.setIntrinsicWidth( width );
thumb.setBounds(new Rect(0, 0, width, height));
states.addState(new int[] { },thumb );//add to the state list with no state
seekbar.setThumb(states);
seekbar.setThumbOffset(0);
}
heres my code to create the bar:
public void setSeekBarProgress(int width, int height, int rColor, int gColor, int bColor){
GradientDrawable shape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE,Color.rgb(rColor, gColor, bColor)});
shape.setCornerRadius(50);
shape.setSize(width, height);
shape.setBounds(0, 0, width, height);
ClipDrawable clip = new ClipDrawable(shape, Gravity.LEFT,ClipDrawable.HORIZONTAL);
shape = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE, getResources().getColor(R.color.DimGray)});
shape.setCornerRadius(50);//change the corners of the rectangle
shape.setSize(width, height);
shape.setBounds(0, 0, width, height);
LayerDrawable mylayer= new LayerDrawable(new Drawable[]{shape, clip,});
seekbar.setProgressDrawable(mylayer);
seekbar.setProgress(50);
}
Here is my code to put everything together, where I set the height of the seek bar:
public MySeekBar(Activity activity, AbsoluteLayout ParentLayout, SeekBarParameters parameters)
{
super(activity.getApplicationContext());
seekbar =(SeekBar)activity.getLayoutInflater().inflate(R.layout.horizontal_seek_bar, ParentLayout,false);
seekbar.setMax(100);
setSeekBarThumb(parameters.widthThumb, parameters.heightThumb,0,100,0);
setSeekBarProgress(parameters.width, parameters.height,255,69,0);
int drawHeight;
if(parameters.height>parameters.heightThumb) drawHeight=parameters.height;
else drawHeight=parameters.heightThumb;
AbsoluteLayout.LayoutParams params = new AsoluteLayout.LayoutParams(parameters.width,drawHeight,parameters.xPosition, parameters.yPosition);
ParentLayout.addView(seekbar, params);
}
The XML code where I inflate the bar from:
<SeekBar xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:max="100"
android:progress="0"
android:secondaryProgress="0"
android:maxHeight="10000dip"
android:id="#+id/slider"
/>
To change the size of the seek bar I use ParentLayout.addView(seekbar, params).
When adding the view, if I use the height that I want to the bar, the thumb gets cutted. If I use the height of the thumb, the bar gets to the same height of the thumb. Android resizes the bar to the size I use to add the view.
I tryed setting the size of the GradientDrawable that is the bar with shape.setSize(width, height), I also tried shape.setBounds(0, 0, width, height), and I tried creating another image with the right size and adding to the LayerDrawable mylayer. But nothing worked.
When creating this through XML it's possible to use paddingTop and paddingBottom to get the bar to the right size. But I don't know how to do this to the GradientDrawable programmatically.
well, too bad there were no responses but i was able to answer the question myself. The answer is to use InsetDrawable. You have to put the one of the drawables of the bar into a InsetDrawable.
Thats the code:
public void setSeekBarProgress(int width, int height, int rColor, int gColor, int bColor){
GradientDrawable shape2 = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE,Color.rgb(rColor, gColor, bColor)});
shape2.setCornerRadius(50);
ClipDrawable clip = new ClipDrawable(shape2, Gravity.LEFT,ClipDrawable.HORIZONTAL);
GradientDrawable shape1 = new GradientDrawable(Orientation.BOTTOM_TOP, new int[]{Color.WHITE, getResources().getColor(R.color.DimGray)});
shape1.setCornerRadius(50);//change the corners of the rectangle
InsetDrawable d1= new InsetDrawable(shape1,5,5,5,5);//the padding u want to use
LayerDrawable mylayer = new LayerDrawable(new Drawable[]{d1,clip});
seekbar.setProgressDrawable(mylayer);
seekbar.setProgress(50);
}
I hope this can help someone else with the same problem.
I'd like to programmatically create a button with a custom drawn shape as the image. In my activity's onCreate, I have:
final FrameLayout contentContainer = new FrameLayout(this);
final FrameLayout.LayoutParams contentLayoutParams = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.FILL_PARENT,
FrameLayout.LayoutParams.FILL_PARENT);
contentContainer.setLayoutParams(contentLayoutParams);
contentContainer.setBackgroundColor(Color.BLUE);
int size = 50;
final ShapeDrawable drawable = new ShapeDrawable(new OvalShape());
drawable.getPaint().setColor(Color.RED);
drawable.setBounds(0, 0, size, size);
final ImageButton leftArrow = new ImageButton(this);
leftArrow.setImageDrawable(drawable);
final FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(size, size);
leftArrow.setLayoutParams(lp);
contentContainer.addView(leftArrow);
When I run the app, I see a blue screen with a plain white button. I has hoping to see a red oval on the button, but it seems I'm not understanding the setImageDrawable call.
Any suggestions how to achieve this? Thanks!