How to set RelativeLayout layout params in code not in xml?
Just a basic example:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
Button button1;
button1.setLayoutParams(params);
params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, button1.getId());
Button button2;
button2.setLayoutParams(params);
As you can see, this is what you have to do:
- Create a
RelativeLayout.LayoutParams
object. - Use
addRule(int)
oraddRule(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).
Relative Layout Params does not work properly
Finally figured out what is my problem. My messagebubble's width was "match_parent" so i couldn't see the alignment.
I made it wrap_content and removed LayoutParams, they weren't working anyways.
this is my final code
for (Message m : chatList) {
LayoutInflater li = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View message = li.inflate(R.layout.msgbubble, null);
TextView messageText = message.getRootView().findViewById(R.id.messageText);
messageText.setText(m.messageText);
LinearLayout msgBubble = message.findViewById(R.id.bubbleLayout);
if (userID == m.sender.id) {
msgBubble.setGravity(Gravity.RIGHT);
messageText.setBackgroundColor(getResources().getColor(R.color.inBlue));
}
if (userID == m.recipient.id) {
msgBubble.setGravity(Gravity.LEFT);
messageText.setBackgroundColor(getResources().getColor(R.color.outBlue));
}
messageContainer.addView(message);
}
Thanks for any help, especially Tej for great and inspiring answer. Also thanks to others for pointing out possible performance-killer implementation!
How to set the align parent right attribute of TextViews in relative layout programmatically
You can access any LayoutParams from code using View.getLayoutParams. You just have to be very aware of what LayoutParams your accessing. This is normally achieved by checking the containing ViewGroup if it has a LayoutParams inner child then that's the one you should use. In your case it's RelativeLayout.LayoutParams. You'll be using
RelativeLayout.LayoutParams#addRule(int verb) and
RelativeLayout.LayoutParams#addRule(int verb, int anchor)
You can get to it via code:
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)textView.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
textView.setLayoutParams(params); //causes layout updat
Some RelativeLayout.LayoutParams doesn't work
you need to do this to get the LayoutParams
RelativeLayout.LayoutParams lp = (RelativeLayout.LayoutParams) getLayoutParams();
RelativeLayout LayoutParams incorrect scaling
The default ScaleType
for an ImageView
is FIT_CENTER
. What this means is that the source image will be scaled so that you can see the entire thing, regardless of the view's aspect ratio... so even though your ImageView is probably only changing its height, the image content is being scaled in both dimensions despite only having the view height change.
Try setting android:scaleType="centerCrop"
on your ImageView tag. This will allow you to resize the view without re-scaling the image contents.
More info on scale types: https://robots.thoughtbot.com/android-imageview-scaletype-a-visual-guide
Setting views dynamically with LayoutParams.addRule setting to the last position of the view
I think you should try simple solution - replace b.invalidate()
with b.requestLayout()
. For more background check this link. I didn't test it but I hope it will work. So the code should look like:
Buttons b = (Buttons) viewIndex;
positioningLayout = new RelativeLayout.LayoutParams(b.getLayoutParams());
positioningLayout.addRule(RelativeLayout.BELOW, viewIdFromList.intValue());
b.setLayoutParams(positioningLayout);
b.requestLayout();
Or maybe you can simplify this to:
Buttons b = (Buttons) viewIndex;
((RelativeLayout.LayoutParams) b.getLayoutParams()).addRule(RelativeLayout.BELOW, viewIdFromList.intValue());
b.requestLayout();
Set RelativeLayout layout params programmatically - throws ClassCastException
The layout params have to be of the type of the parent. So if you are creating a RelativeLayout and adding it to a ListView, the layout params must be of type ListView.LayoutParams, not RelativeLayout.LayoutParams. The correct way to do it is to call inflate this way:
inflate(R.layout.mylayout, theListView, false);
This version of inflate() will correctly inflate the layout parameters (android:layout_*) defined in XML.
Related Topics
How to Save State During Orientation Change in Android If the State Is Made of My Classes
How to Integrate Opencv Manager in Android App
Settext Fails to Show a Number as Text in a Textview
Android: Scrolling an Imageview
Setuservisiblehint Called Before Oncreateview in Fragment
How to Integrate Eclipse with Gradle in Android Project
How to Get City Name from Latitude and Longitude Coordinates in Google Maps
Edittext, Clear Focus on Touch Outside
Android Animation Is Not Finished in Onanimationend
How to Get the Correct Orientation of the Image Selected from the Default Image Gallery
Android - Application (Apk) Maximum Size
Location Service Gps Force Closed
Fragment Onresume() & Onpause() Is Not Called on Backstack
Listview with Add and Delete Buttons in Each Row in Android
How to Securely Store Credentials (Password) in Android Application
Detect the Status of Two Sim Cards in a Dual-Sim Android Phone