GradientDrawable with OVAL shape not working inside programatically added Text View - android

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.

Related

Round corner shape RelativeLayout

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);

Programmatic layout issue with incorrect height

I'm trying to create my own Toasts, and doing so by creating the layout programmatically and then setting the layout as the Toast's view to this layout.
This works absolutely fine for most of my code, but i've run into a quite specific issue i can't seem to fix.
I'm trying to add an image to the toast, and then display that image below or above the text.
This works 100% correctly when adding the image above the text. This is shown in the first image below:
http://i58.tinypic.com/zn2ag3.png
But when i try to add the image to the bottom, all hell breaks loose and i end up with this:
http://i59.tinypic.com/vdnyoj.png
Here's my code:
private void showToast() {
//Variables
Context context = getActivity();
int imageGravity = Gravity.BOTTOM;
CharSequence text = "Test";
//Create parent layout
RelativeLayout relativeLayout = new RelativeLayout(context);
RelativeLayout.LayoutParams relLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
relativeLayout.setLayoutParams(relLayoutParams);
//Set background color
relativeLayout.setBackgroundColor(0xFFF44336);
//Set padding
int padding = 10;
relativeLayout.setPadding(padding, padding, padding, padding);
//Initialize image
ImageView imageView = new ImageView(context);
int imageViewId = 0x100;
imageView.setId(imageViewId);
imageView.setAdjustViewBounds(true);
imageView.setImageResource(R.drawable.default_person_image);
RelativeLayout.LayoutParams imageViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
int margin = 30;
if(imageGravity == Gravity.TOP) {
imageViewParams.addRule(RelativeLayout.ALIGN_PARENT_TOP, RelativeLayout.TRUE);
imageViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
imageViewParams.setMargins(0, 0, 0, margin);
} else if(imageGravity == Gravity.BOTTOM) {
imageViewParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
imageViewParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
imageViewParams.setMargins(0, margin, 0, 0);
}
relativeLayout.addView(imageView, imageViewParams);
//Initialize textView
TextView textView = new TextView(context);
int textViewId = 0x101;
textView.setId(textViewId);
textView.setText(text);
textView.setTextColor(context.getResources().getColor(android.R.color.white));
RelativeLayout.LayoutParams textViewParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
if(imageGravity == Gravity.TOP) {
textViewParams.addRule(RelativeLayout.BELOW, imageViewId);
} else if(imageGravity == Gravity.BOTTOM) {
textViewParams.addRule(RelativeLayout.ABOVE, imageViewId);
}
relativeLayout.addView(textView, textViewParams);
//Setup the toast
Toast toast = new Toast(context);
toast.setView(relativeLayout);
toast.show();
}
I'm hoping someone here can spot what is wrong with my code, as i've been unable to find the issue myself.

custom background button missing text

Currently I am trying to create custom button with custom background, the point is that I want to create rounded edge for the button programmatically. This is the code that I create:
GradientDrawable gd = new GradientDrawable(Orientation.TOP_BOTTOM, new int[] {Color.parseColor("#ffffff"),Color.parseColor("#999999")});
gd.setCornerRadii(new float[] { 0, 0, 10, 10, 10, 10, 0, 0 });
gd.setStroke(1, Color.parseColor("#33364252"));
Button pillButtonRight = new Button(this);
pillButtonRight.setId(R.id.pillButtonRight);
pillButtonRight.setBackgroundDrawable(gd);
pillButtonRight.setWidth(ViewUtils.getScreenWidth(getBaseContext())/8);
pillButtonRight.setHeight(ViewUtils.getScreenHeight(getBaseContext())/40);
pillButtonRight.setText("English");
pillButtonRight.setTextColor(Color.parseColor("#000000"));
pillButtonRight.setTextSize(12);
The code behind getscreenwidth and getscreenheight is
WindowManager wm = (WindowManager) ctx.getSystemService(Context.WINDOW_SERVICE);
Display display = wm.getDefaultDisplay();
int screenWidthDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, display.getWidth(), ctx.getResources().getDisplayMetrics());
The result is the text is gone
Is there any work around on this?
change color of your text
pillButtonRight.setTextColor(Color.parseColor("#ff00ff00"));

How to place ImageButton at x, y location?

I want to place ImageButton at x, y location of my view.
The problem is that Android adds padding around image.
Because I don't know exact size of padding, I cannot place image button at exact location.
So, I want to remove padding.
How can I remove padding around image programmatically?
button.setPadding(0, 0, 0, 0) makes button width shorter and height longer than bitmap.
button.getLayoutParams().width gives minus value.
What I tried so far is like this.
protected class MyLayout extends RelativeLayout {
Bitmap img;
ImageButton button;
public MyLayout(Context context) {
button = new ImageButton(context);
img = BitmapFactory.decodeResource(getResources(), R.drawable.img);
button.setImageBitmap(img);
params = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
button.setLayoutParams(params);
params.setMargins(x, y, 0, 0);
button.setBackgroundDrawable(null);
addView(button, params);
}
}
EDIT
Use this...
MarginLayoutParams marginParams = new MarginLayoutParams(image.getLayoutParams());
int left = someValue;
int top = someValue;
marginParams.setMargins(left, top, 0, 0);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(marginParams);
image.setLayoutParams(layoutParams);

Android: create image button with programmatically drawn shape as its image

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!

Categories

Resources