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

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);

Related

How to set the width and height of a layout in Android?

I have this ConstraintLayout:
lateinit var myConstraintLayout: ConstraintLayout
I try to set its width and height to match its parent via code, like this:
val params = ConstraintLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT
)
myConstraintLayout.setLayoutParams(params)
But I get error androidx.constraintlayout.widget.ConstraintLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams Why? How do I fix this?
The LayoutParams need to be the LayoutParams type of the parent ViewGroup. It is the parent ViewGroup that actually uses these params, after all. In this case, the parent of your ConstraintLayout is apparently a FrameLayout, so you need to use FrameLayout.LayoutParams.

Admob NativeExpressAdView only java

Any way to use NativeExpressAdView from admob without using a layout file (xml).
I use only java file to create views and am wondering on how to create a view from java file for NativeExpressAdView ?
Edit:
I tried this
NativeExpressAdView adView = new NativeExpressAdView(activity);
AdSize adSize = new AdSize(280, 80);
adView.setAdSize(adSize);
adView.setAdUnitId(getString(R.string.ad_unit_id));
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, adSize.getHeightInPixels(activity));
adView.setLayoutParams(layoutParams);
AdRequest request = new AdRequest.Builder().build();
adView.loadAd(request);
linear_layout.addView(adView);
this code give me a black empty view width:280dp, height: 80dp
Something like this should work.... First "reserve" an id for the view by creating a file /res/values/ids.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item name="nativeExpressAdId" type="id"/>
</resources>
On API versions of Android > 17 you can generate the ID on the fly, but then you lose backward compatibility.
Next add an entry to '/res/values/strings.xml`:
<string name="adUnitId">ca-app-pub-12345678901234567890etc</string>
Then in code:
NativeExpressAdView adView = new NativeExpressAdView(this);
adView.setId(R.id.nativeExpressAdId);
int height = 80; // or whatever is appropriate - make sure its >= ad minimum
// set the size to the width of the screen
adView.setAdSize(new AdSize((int) (AdSize.FULL_WIDTH, height));
adView.setAdUnitId(getString(R.string.adUnitId));
// assuming this goes in a linearlayout... you can also addRules to lp
LinearLayout.LayoutParams lp = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
// set margins manually (`.setMargins()`) or add rules with (`.addRule()`)
// or .setVisibility() etc... to lp
adView.setLayoutParams(lp);
Then use parent.addView(adView) to insert this to the parent container. This isn't exactly the code I've used (I was using a RelativeLayout), but it's pretty close, so should get you started.
This sort of code is kind of useful if you need to generate a new ad when rotating, since you can't resize an existing one. You can "borrow" the previous ID, visibility, layout, etc. from the previous one and just apply it to the new one. See here for a similar example.
Tip: Make sure to stop any running animations before removing a nativeexpressadview as this may cause crashes in older webviews like those found in kitkat.
This is pretty much simlar to other but the width and height matters also you need a AdRequest
NativeExpressAdView mNativeExpressAdView = new NativeExpressAdView(this;
mNativeExpressAdView.setLayoutParams(new NativeExpressAdView.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
mNativeExpressAdView.setAdSize(new AdSize(AdSize.FULL_WIDTH, 132)); // here 132 is the medium size
mNativeExpressAdView.setAdUnitId("YOUR AD_UNIT ID");
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
//adRequestBuilder.addTestDevice("28776EC697A5120CBA87CB573E26544A"); //if needed
<parent view>.adcontainer.addView(mNativeExpressAdView);
mNativeExpressAdView.loadAd(adRequestBuilder.build());

Cannot Programmatically Set EditText ID

I am creating a dynamic number of editTexts and want to eventually pull the ID for each to call .getText() on the editText.
However, I noticed that it is difficult to programmatically set the ID, so I am using the .setTag() method instead:
private void createAnswerChoice(int answerNumber) {
ViewGroup layout = (ViewGroup) mRootView.findViewById(R.id.create_poll_questions_answer_layout);
EditText editText = new EditText(getActivity());
editText.setHint(getResources().getString(R.string.answer_text) + " " + answerNumber);
editText.setSingleLine(true);
editText.setImeOptions(EditorInfo.IME_ACTION_DONE);
String editTextID = ((getResources().getString(R.string.created_answer_editText_id))+String.valueOf(answerNumber));
editText.setTag(editTextID);
Toast.makeText(getActivity().getApplicationContext(), editTextID, Toast.LENGTH_SHORT).show();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
editText.setLayoutParams(layoutParams);
TextInputLayout newAnswer = new TextInputLayout(getActivity());
newAnswer.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
newAnswer.addView(editText, layoutParams);
layout.addView(newAnswer);
}
How would I get the value of each editText if I know the tag and not the ID? Also, what is the purpose of the .setTag() method (how does it relate to .setID()?)
create resource file(id.xml) in res/values/id.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item
type="id"
name="edittext_hello" />
</resources>
and then set,
editText.setId(R.id.edittext_hello);
findViewById() and findViewWithTag() are methods intended to obtain a reference to a View that was inflated from XML.
If you are creating the Views, you have them already then there's no need to use those methods. Keep the reference to the View somewhere.
I do not recommend using tags to get views from your hierarchy but if you must this SO post describes how to do it: https://stackoverflow.com/a/16262479/6526330.
Tags should be used when you need to cache some data in a view to grab out later when you have the view again (yes I know very generic). Some examples would be if you are using a holder in a list view or if you have a lot of views on a screen and want a global click listener vs listeners for each view. I stole both of these examples from the answers on this post which do a hell of a better job at describing the use cases than I can.

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]);

what equals app:layout_anchorGravity="bottom|right|end" in 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.

Categories