custom background button missing text - android

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

Related

GradientDrawable with OVAL shape not working inside programatically added Text View

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.

Set X and Y of a view onclick not changing position

I am trying to programatically change the position of a TextView. I create the TextView in code and display it. Everything is working however the TextView stay in the top left corner of the screen regardless of and changes to setX or setY on it.
This is my code.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
textView = new TextView(MainActivity.this);
textView.setText("Test");
textView.setPadding(10, 10, 10, 10);
RelativeLayout Screen = (RelativeLayout) findViewById(R.id.screenRelativeLayout);
Typeface face=Typeface.createFromAsset(getAssets(),"fonts/shablagooital.ttf");
textView.setTypeface(face);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int screenwidth = size.x;
int screenheight = size.y;
Random randdisp = new Random();
int randscreenwidth = randdisp.nextInt(screenwidth) + 1;
int randscreenheight = randdisp.nextInt(screenheight) + 1;
textView.setX(200);
textView.setY(200);
Screen.addView(textView);
Thanks for your help.
Nicholas
You can use LayoutParams:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.setMargins(200, 200, 0, 0);
Screen.addView(textView, params);
Maybe set X and Y AFTER adding the TextView to the Screen.

Add Button to LinearLayout - setText gives null

I am trying to create a button programmatically and add it to a LinearLayout.
I have the following code, and although all seems to work fine, I end up with a button with no text.
for (int i = 0; i < seed.dealer.size(); i++)
{
Button dealer = new Button(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, 40);
params.gravity= Gravity.CENTER;
params.setMargins(5, 5, 5, 5);
dealer.setLayoutParams(params);
dealer.setBackgroundResource(R.drawable.rounded_green);
dealer.setTypeface(Typeface.create("sans-serif-light", Typeface.BOLD));
dealer.setTextSize(22);
dealer.setTextColor(Color.parseColor("#F0F0F0"));
dealer.setText(seed.dealer.get(i).name);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
float logicalDensity = metrics.density;
int px = (int) (200 * logicalDensity + 0.5);
dealer.setMinWidth(px);
px = (int) (350 * logicalDensity + 0.5);
dealer.setMaxWidth(px);
LinearLayout ll = (LinearLayout) findViewById(R.id.dealerContainer);
ll.addView(dealer);
}
first check seed.dealer.get(i).name is giving a valid String (not null and not empty). if it is giving a valid String, I think the problem is with text color of a button (Color.parseColor("#F0F0F0") is giving color something like transparent(exactly I have not tested)). so try changing text color to other color like Color.BLACK or Color.RED something like...
The problem was with this line :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, 40);
I had to convert height to dp because the button height was so small that made the it look like there was no text.

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