Setting Layout params programmatically recyclerview's item - android

I am using RecyclerView in which I have a RelativeLayout. In that RelativeLayout, I have a 2 TextView. If the first TextView has some value which decides if the second TextView should be aligned to right or left.
I have set the LayoutParams in RecyclerView Adapter using the below code:
if (listItem.getmLikeCount() > 0) {
RelativeLayout.LayoutParams feedCommentParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
feedCommentParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
listItemHolder.mCommentCount.setLayoutParams(feedCommentParams);
} else {
listItemHolder.mLikeCount.setVisibility(View.GONE);
RelativeLayout.LayoutParams feedCommentParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
feedCommentParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
listItemHolder.mCommentCount.setLayoutParams(feedCommentParams);
}
But the items are getting recycled i.e. if any of the RecyclerView items is not having any count then all other items are also aligned to the left.
I want to show the comment count to Right if like count > 0 else show comment count to left.

The problem can be easily solved with two TextView aligned separately. So that based on comments count you could set their visibility to GONE or VISIBLE.
Anyway, in your case you might consider calling the requestLayout inside the if and else statement like this.
if (listItem.getmLikeCount() > 0) {
RelativeLayout.LayoutParams feedCommentParams = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
feedCommentParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
listItemHolder.mCommentCount.setLayoutParams(feedCommentParams);
// Call requestLayout here
} else {
listItemHolder.mLikeCount.setVisibility(View.GONE);
RelativeLayout.LayoutParams feedCommentParams = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
feedCommentParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, 0);
listItemHolder.mCommentCount.setLayoutParams(feedCommentParams);
// Call requestLayout here for the else part.
}

Related

Android - positioning TextView in layout programmatically

I don't know how I can set the TextView on the right side in my RelativeLayout. Adding gravity to the TextView didn't work. Thanks for helping me.
RelativeLayout relativeLayout = new RelativeLayout(context);
relativeLayout.setGravity(Gravity.CENTER_HORIZONTAL); // center in linearLayout_power_stations_bar
textView = new TextView(context);
textView.setText(String.valueOf(nuke_count));
textView.setTextColor(activity.getResources().getColor(R.color.game_powerStationsBar_textOverIcon));
textView.setTextSize(16);
relativeLayout.addView(textView); // TextView over image
relativeLayout.addView(imageView); // adding image
linearLayout_power_stations_bar.addView(relativeLayout); // adding to other layout
You can set layout parameters to TextView and also set alignment of parent right.
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
textViewsetLayoutParams(params);
Now You can Use this code :)

How to align in RelativeLayout properly?

Hi I am trying to design a layout programatically where I have a textview and a edittext and a button underneath. Two rows. First row with TextView. Second row with Edittext and button. Edittext should extend from left to the button. Right now, the Textview appears in the first row, but the second row (with Edittext and Button) is messed. Right now, the EditText doesnt extend all the way to the send button. I tried using Wrap_Content, Fill_parent, Match_Parent but no luck. Here is what I have so far:
RelativeLayout layout = new RelativeLayout(this);
EditText myText = new EditText(this);
Button myBtn = new Button(this);
parameters_layout = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
myText.setId(1);
myText.setLayoutParams(parameters_layout);
layout.addView(myText,parameters_layout);
parameters_layout = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.RIGHT_OF, myText.getId());
myBtn.setLayoutParams(parameters_layout);
layout.addView(myBtn,parameters_layout);
Thank you any help is appreciated.
EDIT: Clarification I know
parameters_layout = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
can be replaced with numbers. For example:
parameters_layout = new
RelativeLayout.LayoutParams(350,
RelativeLayout.LayoutParams.WRAP_CONTENT);
Is that 350 just pixels, dp, sp? What is it?
What I understand is what you want to implement is one Button on the bottom-right corner of the screen and one extended EditText just left of the button. Am I correct?
If that, try this. I tested this code in on my galaxy nexus and it worked fine.
RelativeLayout layout = new RelativeLayout(this);
EditText myText = new EditText(this);
Button myBtn = new Button(this);
myBtn.setId(1);
RelativeLayout.LayoutParams parameters_layout = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.LEFT_OF, myBtn.getId());
myText.setLayoutParams(parameters_layout);
layout.addView(myText,parameters_layout);
parameters_layout = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
parameters_layout.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
myBtn.setLayoutParams(parameters_layout);
layout.addView(myBtn,parameters_layout);
One tip is when you use RelativeLayout, first locate the view that the position is fixed (in your case bottom-right cornered button) then locate other views by using relative layout specific parameters. (In your case LEFT_OF).
Another tip is whenever if you need to assign a ID value in programmatically, don't assign any specific value (in your case '1'), instead of it you can define ID as a resource, and then use it w/o any worries about id conflict or some strange behaviors.
There is a document about ID resource, so check it - http://developer.android.com/guide/topics/resources/more-resources.html#Id

Put Items in RelativeLayout dynamically

I have done some research, but the answer i found does not work for me. Here is some part of my code. the R.id.relative is the id of the relativelayout in the xml file
RelativeLayout RL = (RelativeLayout) findViewById(R.id.relative);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
TextView title = new TextView(this);
title.setText(" History ");
title.setId(99099);
title.setTextSize(30);
title.setGravity(Gravity.CENTER);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
title.setLayoutParams(params);
RL.addView(title);
RelativeLayout.LayoutParams test_params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test = new Button(this);
test.setText(" Back ");
test_params.addRule(RelativeLayout.BELOW,99099);
test.setId(199291);
test.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params);
RL.addView(test);
RelativeLayout.LayoutParams test_params2 = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
Button test2 = new Button(this);
test2.setText(" Clear ");
test_params2.addRule(RelativeLayout.BELOW,test.getId());
test2.setGravity(Gravity.CENTER);
test.setLayoutParams(test_params2);
RL.addView(test2);
all 3 items did show up, but they stack together. I can't get them below another.
Could anyone help ?
From what I've been able to find out, you have to add the view using LayoutParams. Here's an example:
LinearLayout linearLayout = new LinearLayout(this);
RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
relativeParams.addRule(RelativeLayout.ALIGN_PARENT_TOP);
parentView.addView(linearLayout, relativeParams);
And to relatively position your items programmatically you have to assign ids to them, this stops them from 'overlapping'.
TextView tv1 = new TextView(this);
tv1.setId(1);
TextView tv2 = new TextView(this);
tv2.setId(2);
Then addRule(RelativeLayout.RIGHT_OF, tv1.getId());
Change test.setLayoutParams(test_params2); to test2.setLayoutParams(test_params2);
Explanation: You have inadvertently set the layout params of test a second time, with params that instruct it to be below itself, which I guess just puts it top-left (default placement in a RelativeLayout). Since you never give test2 any layout params, it also gets default placement. So everything is at the top and thus appear atop each other.
Incidentally, if you just want them arranged linearly, why not use a vertical LinearLayout?
The rule you have added is causing your view to be stacked together.If you need to add it should be used by the following rule.
test_params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 1);
Next, add the view to your RelativeLayout with your LayoutParams:
RL.addView(yourAdView, rLParams);
same goes for the each cases.try to run.It will solve your problem

Edit Android SFGameView layout programmatically

I'd like to insert a pause button and a Highscore text for my game with the pause button at the upper left of the gameview and the highscore text at the upper right. I'd like to code it programmaticaly, but the highscore and button are not showing up. Here's the code :
gameView = new SFGameView(this);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
TextView textBox = new TextView(SFEngine.context);
textBox.setText("HIGH SCORE");
textBox.setId(1);
Button pauseButton = new Button(SFEngine.context);
pauseButton.setText("PAUSE");
pauseButton.setGravity(Gravity.RIGHT);
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.RIGHT_OF, textBox.getId());
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
layout.addView(textBox);
layout.addView(pauseButton,lp);
layout.addView(gameView,new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
setContentView(layout,rlp);
here's the button listener, the button won't respond to the touch event
pauseButton.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e) {
try{
Thread.sleep(1000);
}
catch(InterruptedException ex){
}
return true;
}
});
You need to add the LayoutParams correctly. Each View needs LayoutParams of the type of its parent. E.g. your TextView and Button need RelativeLayout.LayoutParams, and your RelativeLayout needs FrameLayout.LayoutParams (as this is the built-in container for anything you add in setContentView).
For the pauseButton set the RelativeLayout.LayoutParams to WRAP_CONTENT & WRAP_CONTENT, with rules ALIGN_PARENT_TOP & ALIGN_PARENT_RIGHT.
For the textBox add another RelativeLayout.LayoutParams with WRAP_CONTENT & WRAP_CONTENT and rules ALIGN_PARENT_TOP & ALIGN_PARENT_LEFT.
Then the RelativeLayout named layout needs FrameLayout.LayoutParams with MATCH_PARENT & MATCH_PARENT.
Example code:
RelativeLayout layout = new RelativeLayout(this);
layout.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
SFGameView game = new SFGameView(this);
game.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
Button btn = new Button(this);
btn.setText("||");
RelativeLayout.LayoutParams btnLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
btnLP.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
btnLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
btn.setLayoutParams(btnLP);
TextView tv = new TextView(this);
tv.setText("Some Text");
RelativeLayout.LayoutParams textLP = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
textLP.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
textLP.addRule(RelativeLayout.ALIGN_PARENT_TOP);
tv.setLayoutParams(textLP);
layout.addView(game);
layout.addView(btn);
layout.addView(tv);
setContentView(layout);
As for the listener. Use setOnClickListener (not onTouch).

Programmatically Can't Align ImageView in RelativeLayout

I'm having trouble adding an imageview to a relative layout. I would like to add an image to a list of menu items that I am creating dynamically using RelativeLayout. All of my menu items show up just fine and in order but when I try to add an image to each of the items I only get one arrow and it is not centered vertically. Below is my code.
In my XML file
<RelativeLayout
android:id="#+id/pMenu"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</RelativeLayout>
In my code:
private void buildMenu(String name, int id) {
String[] menuItems = getResources().getStringArray(pMenus[id]);
// Get the rel layout from xml
RelativeLayout container = (RelativeLayout) findViewById(R.id.pMenu);
int imageId=1;
Typeface tf = Typeface.createFromAsset(this.getAssets(),"mreavesmodot-reg.otf");
for(String menuItem: menuItems) {
// Defining the layout parameters
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
StyledButton menuImage = new StyledButton(this);
menuImage.setBackgroundResource(R.drawable.menu_button);
menuImage.setText(menuItem);
menuImage.setTypeface(tf);
menuImage.setTextSize(19);
menuImage.setPadding(20, 0, 0, 0);
menuImage.setTextColor(Color.WHITE);
menuImage.setGravity(Gravity.LEFT | Gravity.CENTER_VERTICAL);
menuImage.setOnClickListener(getOnClickListener(menuImage, name));
menuImage.setId(imageId);
if(imageId==1) {
lp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
} else {
lp.addRule(RelativeLayout.BELOW ,imageId-1);
}
menuImage.setLayoutParams(lp);
ImageView arrow = new ImageView(this);
arrow.setImageResource(R.drawable.arrow_menu);
arrow.setPadding(0, 0, 15, 0);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams( RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT );
params.addRule(RelativeLayout.ALIGN_RIGHT,menuImage.getId());
params.addRule(RelativeLayout.CENTER_VERTICAL);
arrow.setLayoutParams(params);
container.addView(menuImage);
container.addView(arrow);
imageId++;
}
}
I think the line below is your problem
params.addRule(RelativeLayout.CENTER_VERTICAL);
YES, you are most likely adding multiple arrows, they are just simply one on top of each other ALL aligned to the vertical center of the full relative layout. That command is not performing a vertical centering against your button item, but agains the parent RelativeLayout.

Categories

Resources