what equals app:layout_anchorGravity="bottom|right|end" in Java? - java

In xml, I can use app:layout_anchorGravity="bottom|right|end" as an attribute of a view. But now I want to create a view programmatically. What I know is like this:
CoordinatorLayout.LayoutParams params = new CoordinatorLayout.LayoutParams(CoordinatorLayout.LayoutParams.WRAP_CONTENT, CoordinatorLayout.LayoutParams.WRAP_CONTENT);
params.anchorGravity = Gravity.BOTTOM;
But how can I add right and end ?

You may change to this
params.anchorGravity = Gravity.BOTTOM|Gravity.RIGHT|Gravity.END;

Fully answer:
CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams)findViewById(R.id.floatingActionButton).getLayoutParams();
params.anchorGravity = Gravity.BOTTOM|Gravity.RIGHT|Gravity.END;
findViewById(R.id.floatingActionButtonFlip).setLayoutParams(params);
This allow use another existing settings of LayoutParams and work fine for me.

Related

Can't access child by id on custom view

I have created a custom rotary know view, which I need to attach a listener to from my fragment.
Accessing all other children in the view by id (or by kotlinx.android.synthetic id) works perfectly.
However, this one is null, and I can't figure out why.
When looking at the fragment's view in debug I see it in mChildren, but it's mKey is -1 while all other views have realisting ids.
An ugly workaround was to find it directly like this:
val c = layout.getChildAt(layout.childCount - 1) as RotaryKnobView
c.listener = this
but accessing it directly by id - rotaryKnob.listener - doesn't work.
Of course this approach won't work once I update the fragment to include more views.
In the layout's xml it is defined like this.
<geva.oren.android_kotlin_metronome.views.RotaryKnobView
android:id="#+id/rotaryKnob"
class="geva.oren.android_kotlin_metronome.views.RotaryKnobView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="32dp"
app:initialValue="130"
app:knobDrawable="#drawable/ic_rotary_knob"
app:layout_constraintStart_toStartOf="#+id/toneButton"
app:layout_constraintTop_toBottomOf="#+id/toneButton"
app:maxValue="220"
app:minValue="40" />
Also - if I set an id explicitly it works, not that it's needed this way.
For example:
val knob = layout.getChildAt(layout.childCount - 1) as RotaryKnobView
knob.id = 12345
val rotaryKnob = layout.findViewById<RotaryKnobView>(12345)
rotaryKnob.listener = this
setBpmText(rotaryKnob.value)
What am I missing?

Changing an ImageResource (drawable) programmatically

I am currently using the #android:drawable/presence_online as the srcCompat for a ImageView.
I would like to change it programmatically to #android:drawable/presence_offline.
I tried the code below but it didn't work.
final ImageView imgPresence = (ImageView) findViewById(R.id.imgPresence);
imgPresence.setImageResource(R.drawable.presence_offline);
Any ideas?
You missed android in the resource id.
Instead of
imgPresence.setImageResource(R.drawable.presence_offline);
use this:
imgPresence.setImageResource(android.R.drawable.presence_offline);
Though commented by #Srijith, I am putting as an answer here:
Change the below line
setImageResource(R.drawable.presence_offline);
to
setImageResource(android.R.drawable.presence_offline);
try this:
imgPresence.setImageResource(getResources().getDrawable(R.drawable.presence_offline));

Is it possible to modify xml in java programatically. specifically layout_alignBottom, layout_alignLeft and layout_alignStart

A few ImageViews have been already created in xml
eg:
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/blue_dot1"
android:src="#drawable/checkers_blue1"
android:layout_alignBottom="#+id/y1"
android:layout_alignLeft="#+id/x1"
android:layout_alignStart="#+id/x1"/>
Is it possible to update the xml later on programatically in Java for the alignbottom, left and start variables (so that the blue_dot1 image can be moved along the x/y grid of images)
For example I am going to make a method which has a X and Y arg to allow movement in a simple grid, its just the xml part i cant figure out.
yes this is very much possible. but you are not modifying the XML instead you are modifying properties of a specific view:
check this question
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params); //causes layout update
You can not change the xml-file. But you can set LayoutParams of your view programatically.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.removeRule(RelativeLayout.ALIGN_BOTTOM);
No, modifying the XML is not possible to do from Java code. However, you can modify the Views layout properties programatically even after the XML properties have been declared and the view has been drawn. Its fairly simple to do. You just get a reference to that image view:
ImageView myImg = (ImageView)findViewById(R.id.myImg);
myImg.setLayoutParams([Your params here]);

Liferay - change portlet column programmatically

I'm doing a Liferay portal with different portlets on it in different columns created in a Liferay Layout.
My question is: How can I change programmatically (in java) the column to which a portlet belongs?
I've tried with this:
long userId = themeDisplay.getUserId();
long groupId = themeDisplay.getLayout().getGroupId();
Layout layout = LayoutLocalServiceUtil.getFriendlyURLLayout(groupId, true, currentFriendlyURL);
LayoutTypePortlet layoutTypePortlet = (LayoutTypePortlet) layout.getLayoutType();
layoutTypePortlet.removePortletId(userId, iniPortletName);
String portletInstanceId = layoutTypePortlet.addPortletId(userId, iniPortletName, newColumn, position, true);
LayoutLocalServiceUtil.updateLayout(layout.getGroupId(), layout.isPrivateLayout(), layout.getLayoutId(), layout.getTypeSettings());
It works, but with this code, I delete the portlet and then, I create a new instance, and I don't want that.
How can I update the position of same instance of the portlet?
Thank you.
Ok, I found it.
If you want to keep the same instance, you can use the movePortletId(userId, portletId, newColumn, position) method.
String portletId = (String) request.getAttribute(WebKeys.PORTLET_ID);
layoutTypePortlet.movePortletId(userId, portletId, finColumn, position);
You need to use the portletId instead of portletName, that identifies all the portlets with the same name.
https://docs.liferay.com/portal/6.2/javadocs/src-html/com/liferay/portal/model/LayoutTypePortlet.html
Thank you.

How to set android view's layout_alignParentEnd property by code?

How can I set the alignParentEnd property programmatically?
I searched all over the Android Developer website, but I couldn't find any reference to setting properties like alignParentEnd in code. It is only explained how to set them in XML. Where could I find documentation for something like this in the future?
LayoutParams are used to set layout properties in code. alignParentEnd is a property of RelativeLayout so you have to use RelativeLayout.LayoutParams. You can find the documentation here.
So to set the property in code you can use the addRule() method of the RelativeLayout.LayoutParams. For example you can do this:
// Create the LayoutParams
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
// Add all the rules you need
params.addRule(RelativeLayout.ALIGN_PARENT_END);
...
// Once you are done set the LayoutParams to the layout
relativeLayout.setLayoutParams(params);
You can find a list of all rules which you can set with addRule() here.
Parameter are added in View and ViewGroup using LayoutParam.
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) my_child_view.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_END);
my_child_view.setLayoutParams(params);

Categories