How do I switch from alignParentLeft to alignParentRIght? - android

I want my chat bubble to move to the right side when a different user has sent a message but for some reason it stays on the same side. This is what I've tried:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.bubble.getLayoutParams();
if (message.getPersonId() == MainActivity.getUser().getUserId()) {
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
} else {
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, 0);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
}
holder.bubble.setLayoutParams(params);
Is there something I'm doing wrong? I also tried doing params.removeRule(int) but it didn't work either plus I prefer to stay away from that because I want to ensure as much usability as possible in terms of OS.

You can simply add parameters like this way-
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams
(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.MATCH_PARENT
);
------------------------------------
----------------------------------
// Creating a new TextView
TextView tv = new TextView(this);
tv.setText(d.getName());
tv.setTextSize(25);
// Defining the layout parameters of the TextView
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// Setting the parameters on the TextView
tv.setLayoutParams(lp);
b.setLayoutParams(lp);
// Adding the TextView to the RelativeLayout as a child
I think, if you follow this way, there will not be any problem.

It seems you are not using adRule() properly. Please change and try again:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) holder.bubble.getLayoutParams();
if (message.getPersonId() == MainActivity.getUser().getUserId()) {
params.removeRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
}
else {
params.removeRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
}
holder.bubble.setLayoutParams(params);

Related

Programmatically added Views not behaving

I've created many custom views and I am trying to add them to my fragment. They get added but I can't seem to get them to go where I want. There should be 2 columns and 3 rows but it ends up as 1 column with all of the custom views stacked on top of one another. Here is my code to add the views and set the layout params to the fragment layout:
RelativeLayout fm = (RelativeLayout) view.findViewById(R.id.fragmentLayout);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
CustomImages cs = new CustomImages(getActivity());
cs.setId(R.id.one);
cs.setLayoutParams(params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params.addRule(RelativeLayout.RIGHT_OF, cs.getId());
CustomImages2 cs2 = new CustomImages2(getActivity());
cs2.setId(R.id.two);
cs2.setLayoutParams(params);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params2.addRule(RelativeLayout.BELOW, cs2.getId());
CustomImages3 cs3 = new CustomImages3(getActivity());
cs3.setId(R.id.three);
cs3.setLayoutParams(params);
RelativeLayout.LayoutParams params3 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
params3.addRule(RelativeLayout.RIGHT_OF, cs3.getId());
CustomImages4 cs4 = new CustomImages4(getActivity());
cs4.setId(R.id.four);
cs4.setLayoutParams(params);
RelativeLayout.LayoutParams params4 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params4.addRule(RelativeLayout.BELOW, cs4.getId());
CustomImages5 cs5 = new CustomImages5(getActivity());
cs5.setId(R.id.five);
cs5.setLayoutParams(params);
cs3.setLayoutParams(params);
cs4.setLayoutParams(params);
cs5.setLayoutParams(params);
I believe params there should be replaced with params2, params3 and params4 respectively.
UPDATE:
Also, you should specify LAYOUT_BELOW for all views which are not on top, and do it correctly:
params2.addRule(RelativeLayout.BELOW, cs.getId()); // not cs2
params3.addRule(RelativeLayout.BELOW, cs2.getId()); // add this
params4.addRule(RelativeLayout.BELOW, cs3.getId()); // not cs4

How to implement Ads In AndEngine

I am trying to display an advertisement using startApp in AndEngine.
like this,
#Override
protected void onSetContentView()
{
StartAppAd.init(this, "apiID", "appID");
RelativeLayout relativeLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams layoutParams1 = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
layoutParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
BannerStandard startAppBanner = new BannerStandard(this);
relativeLayout.addView(startAppBanner, layoutParams);
this.addContentView(relativeLayout, layoutParams1);
}
but it isn't work, please help me. I almost done other setup, like adding jar and manifest file.

android creating a RelativeLayout programmatically

I'm having some troubles creating a relativeLayout programmatically.For better understanding I attached a picture below. The things with plus and minus are buttons, which should also be dynamically created and added to the layout. The values of tv4 and tv5 should increase/decrease accordingly to button presses.
What I have done so far:
1) creating the layout:
RelativeLayout rl = new RelativeLayout(this);
rl.setId(i);
rl.setBackgroundResource(R.drawable.bg);
RelativeLayout.LayoutParams Lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.BELOW, R.id.RL_default);
Lparams.setMargins(3, 5, 3, 0);
rl.setLayoutParams(Lparams);
2) adding the tv1:
Lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Lparams.setMargins(10, 0, 0, 0);
TextView txt = new TextView(this);
txt.setTextColor(Color.parseColor("#FFFFFF"));
txt.setId(x);
txt.setTextSize(25);
txt.setLayoutParams(Lparams);
txt.setText(name);
rl.addView(txt);
3) adding the tv2:
Lparams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Lparams.addRule(RelativeLayout.BELOW, txt.getId());
Lparams.setMargins(10, 0, 0, 0);
TextView txtS = new TextView(this);
txtS.setId(y);
txtS.setText("Test: ");
txtS.setTextSize(22);
txtS.setLayoutParams(Lparams);
txtS.setGravity(Gravity.BOTTOM);
txtS.setPadding(0, 0, 0, 20);
rl.addView(txtS);
4) now I want to create the first button:
Button btnSminus = new Button(this);
btnSminus.setId(btn1);
btnSminus.setText("<");
btnSminus.setTextSize(20);
Lparams= new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Lparams.addRule(RelativeLayout.RIGHT_OF, txtS.getId());
btnSminus.setLayoutParams(Lparams);
rl.addView(btnSminus);
The problem is, the the button View seems to just disappear from the screen when the line Lparams.addRule(RelativeLayout.RIGHT_OF, txtS.getId()); is executed. What can be the reason?
My guess would be that it's because you set the width of txtS to match_parent, so the button gets pushed off the screen. You can fix that by changing that to wrap_content.

Margin in Relative Layout

How to set margin in relative layout with code (not XML)?
I use this code but nothing happened:
RelativeLayout rlDetail = new RelativeLayout(context);
rlDetail.setBackgroundResource(R.drawable.bg_round);
RelativeLayout.LayoutParams rlDetailParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
rlDetailParams.setMargins(35, 10, 35, 0);
rlDetail.setLayoutParams(rlDetailParams);
try this answer set the absolute position of a view in Android
as mentioned in the link above you should use
TextView tv1 = new TextView(context);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(30, 40);
params.leftMargin = needed_margin;
params.topMargin = needed_margin;
// add your textview like this
rl.addView(tv1, params);

android:layout_alignParentBottom by code

How to set android:layout_alignParentBottom="true" for a videoview, by code and not from xml?
I assume you are trying to add your VideoView to a RelativeLayout ?
RelativeLayout relativeLayout = findViewById(R.id.your_relative_layout);
mVideo = new VideoView(this);
RelativeLayout.LayoutParams layoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); // or wrap_content
layoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
relativeLayout.addView(mVideo , layoutParams);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
YourVideoView.setLayoutParams(params);
get layout params from your view and add rule
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) view.getLayoutParams();
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
Some References are Here1 and Here2
this is what you have to do:
Create a RelativeLayout.LayoutParams object.
Use addRule(int) or addRule(int, int) to set the rules. The first method is used to add rules that don’t require values.
Set the parameters to the view (in this case, to each button).
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT,RelativeLayout.TRUE);
yourView.setLayoutParams(params);

Categories

Resources