I'm trying to put in RelativeLayout an image and text, and would like the text was down but over the image.
My code:
RelativeLayout rl;
//
rl = new RelativeLayout(mContext);
rl.setLayoutParams(new GridView.LayoutParams(85, 85));
rl.setGravity(Gravity.CENTER);
ImageView imageView = new ImageView(mContext);
imageView.setId(1);
TextView textView = new TextView(mContext);
textView.setId(2);
//
imageView.setImageResource(mThumbIds[position]);
textView.setText(mTextMenu[position]);
//
rl.addView(imageView);
//
RelativeLayout.LayoutParams mParams = new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
//
mParams.addRule(RelativeLayout.ALIGN_BOTTOM, imageView.getId());
rl.addView(textView, mParams);
Use linear layout. Thats more appropriate for your case.
I managed to solve by changing the layout of the TextView:
childParams = new RelativeLayout.LayoutParams(65, 25);
childParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
childParams.addRule(RelativeLayout.CENTER_HORIZONTAL);
rl.addView(textView, childParams);
Related
I have programmatically added Card View. I just want to make it clickable and show animation while it is clicked. Here is my Code
CardView cardView = new CardView(this);
LayoutParams layoutParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
cardView.setLayoutParams(layoutParams);
cardView.setRadius(15);
cardView.setPadding(25, 25, 25, 25);
cardView.setCardBackgroundColor(Color.MAGENTA);
TextView textView = new TextView(this);
textView.setLayoutParams(layoutParams);
textView.setText("Programmatically set");
textView.setGravity(Gravity.CENTER);
textView.setTextColor(Color.WHITE);
cardView.addView(textView);
LinearLayout linearLayout = findViewById(R.id.linearLayout1);
linearLayout.addView(cardView);
int[] attrs = new int[]{R.attr.selectableItemBackground};
TypedArray typedArray = this.obtainStyledAttributes(attrs);
int selectableItemBackground = typedArray.getResourceId(0, 0);
typedArray.recycle();
cardView.setForeground(this.getDrawable(selectableItemBackground));
cardView.setClickable(true);
With the Material Components Library just use:
cardView.setRippleColor(ContextCompat.getColorStateList(this,R.color.selector_card));
You can use this:
cardView.setClickable(true);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
TypedValue typedValue = new TypedValue();
getContext().getTheme().resolveAttribute(android.R.attr.selectableItemBackground, typedValue, true);
cardView.setBackgroundResource(typedValue.resourceId);
}
I want to make a customAlertDialog that shows the title, message and button centered, but I cannot make the size of the title and message of my customAlertDialog take the size of the parent
This is what my customAlertDialog currently looks like1
LinearLayout layout = new LinearLayout(context);
layout.setOrientation(LinearLayout.VERTICAL);
layout.setBackgroundColor(Color.RED);
layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
final TextView header = new TextView(context);
final TextView body = new TextView(context);
final SpannableString formatHeader = new SpannableString(title);
final StyleSpan negrita = new StyleSpan(android.graphics.Typeface.BOLD);
formatHeader.setSpan(negrita,0, title.length(), Spannable.SPAN_INCLUSIVE_INCLUSIVE);
header.setText(formatHeader);
header.setGravity(Gravity.CENTER);
header.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 20);
header.setTextColor(Color.BLACK);
body.setPadding(8, 0, 8, 10);
body.setText(message);
body.setGravity(Gravity.CENTER);
body.setPadding(8, 0, 8, 0);
body.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
body.setTextColor(Color.BLACK);
layout.addView(header);
layout.addView(body);
view = layout;
break;
You can change the default LayoutParams of Dialog to make your dialog full screen like this:
Window window = yourDialog.getWindow();
if (window != null) {
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.MATCH_PARENT;
window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
window.setAttributes(lp);
}
I am trying to create a method that will add a new LinearLayout to an existing LinearLayout. I would like to send a parameter to this method telling it whether the new LinearLayout should be VERTICAL or HORIZONTAL. This is what I have so far but 'LinearLayout.Orientation' is not the correct name for this parameter type.
private LinearLayout addLinearLayout(View linearLayout, LinearLayout.Orientation orientation)
{
//Create new horizontal linear layout and add to the specified linear layout
LinearLayout newLinearLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 10, 0, 10);
newLinearLayout.setOrientation(LinearLayout.orientation);
((LinearLayout) linearLayout).addView(newLinearLayout, layoutParams);
return newLinearLayout;
}
Is this possible? If so, what is the type of the parameter I am trying to pass?
Thanks for any help.
From the documentation:
public void setOrientation (int orientation)
(...)
orientation Pass HORIZONTAL or VERTICAL. Default value is HORIZONTAL.
private LinearLayout addLinearLayout(LinearLayout linearLayout, int orientation) {
//Create new horizontal linear layout and add to the specified linear layout
LinearLayout newLinearLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 10, 0, 10);
newLinearLayout.setOrientation(orientation);
linearLayout.addView(newLinearLayout, layoutParams);
return newLinearLayout;
}
Call this method like this:
addLinearLayout(yourLinearlayout, LinearLayout.HORIZONTAL);
The orientation of a Layout seems to be represented by an Integer...
private LinearLayout addLinearLayout(LinearLayout linearLayout, int orientation)
{
//Create new horizontal linear layout and add to the specified linear layout
LinearLayout newLinearLayout = new LinearLayout(context);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(20, 10, 0, 10);
newLinearLayout.setOrientation(orientation);
linearLayout.addView(newLinearLayout, layoutParams);
}
// Then you can call your function like this
addLinearLayout(layout, LinearLayout.VERTICAL);
As the title reads, I am getting this error in my LogCat when I run my app. This is the code written after which the error occured:
public void coinAnim1() {
RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainlayoutid);
ImageView coin1 = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
coin1 = (ImageView) findViewById(R.id.coinid);
Animation coinFall1 = AnimationUtils.loadAnimation(this,
R.anim.coinanimation);
coin1.startAnimation(coinFall1);
rl.addView(coin1, params);
}
public void coinAnim2() {
RelativeLayout rl2 = (RelativeLayout) findViewById(R.id.mainlayoutid);
ImageView coin2 = new ImageView(this);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(30, 40);
params2.leftMargin = 50;
coin2 = (ImageView) findViewById(R.id.coin2id);
Animation coinFall1 = AnimationUtils.loadAnimation(this,
R.anim.coinanimation);
coin2.startAnimation(coinFall1);
rl2.addView(coin2, params2);
}
LogCat says the line "rl.addView(coin1, params);" specifically causes the error.Does anyone know what I can do to fix this? There are similar questions, but as I am new to coding I don't know how to adapt the answers for my problem.
Any help is appreciated.
PROBLEM SOLVED:
Ashishduh solved the problem by replacing the line:
rl.addView(coin1, params);
With the line:
coin1.setLayoutParams(params);
Hope this helps someone! Thanks ashi <3
The View coin1 already has a parent (the RelativeLayout that you inflated). You can't assign it to another parent without removing it from that RelativeLayout.
I think what you're trying to do is modify the LayoutParams of coin1. Instead of doing this: rl.addView(coin1, params); you need to just set the layoutparams like this coin1.setLayoutParams(params);
From ur code snippet
RelativeLayout rl = (RelativeLayout) findViewById(R.id.mainlayoutid);
ImageView coin1 = new ImageView(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = 50;
coin1 = (ImageView) findViewById(R.id.coinid);
u r creating coin1 & coin2 ImageView programmatically as u have already added those in xml either create those ImageViews programmatically or in xml
I have the following code:
HashMap<String, String> tip = venue.tips.get(j);
TableRow tr = new TableRow(this);
TableRow.LayoutParams tableRowParams =
new TableRow.LayoutParams
(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
tr.setLayoutParams(tableRowParams);
LinearLayout ll = new LinearLayout(this);
ll.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
TextView tips = new TextView(this);
tips.setText(tip.get("text"));
Log.v("TIPS TEXT", tip.get("text"));
tips.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
ImageView avatar = new ImageView(this);
avatar.setImageBitmap(getBitmapFromURL(tip.get("photo")));
Log.v("PHOTO URL", tip.get("photo"));
avatar.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
ll.addView(tips);
ll.addView(avatar);
tr.addView(ll);
View v = new View(this);
v.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, 1));
v.setBackgroundColor(Color.rgb(51, 51, 51));
tipsTable.addView(tr,new TableLayout.LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
tipsTable.addView(v);
I think I did something wrong with setting up the layout, but the issue now is that I can't see the rows added to the tipsTable. Why is this?
You're not calling setContentView() anywhere. You create this layout but never assign it to the activity. I think what you want to do is after you've set up all your layout stuff:
setContentView(ll);
along the same line: tipsTable seems to be your top view so try setContentView(tipsTable)