Dynamic equivalent of these XML properties for a StackView - android

I am dynamically creating a StackView object in my Android project. How can I accomplish the following equivalent properties dynamically through code?
android:animateLayoutChanges="true"
android:loopViews="true"

Here's the first:
http://developer.android.com/reference/android/view/ViewGroup.html#setLayoutTransition
The second apparently has no related methods:
http://developer.android.com/reference/android/widget/AdapterViewAnimator.html#attr_android:loopViews

For animateLayoutChanges you can refer this.
And for the loopViews i am not getting any method but you can refer this.
Hope you got the Point. You can easilt search on developer site of Android for any other methods/function and its properties.

There is no way to change loopViews, but you can try to pass it through constructor using styles.

Related

how to set background of TextView to #android:drawable/dialog_holo_light_frame

i want to set the below xml code programmatically. i know about textView.setBackgroundResource(R.drawable...) but i am not able to set textView background to #android:drawable/dialog_holo_light_frame. Please suggest me suitable method.
android:background="#android:drawable/dialog_holo_light_frame"
Try to use the pattern of the code below:
textView.setBackgroundResource(android.R.drawable.dialog_holo_light_frame);
If you want to use and of the standard android resources you have to use android.R. folder rather than just R. . R. is for your own project. So do it like below -
textView.setBackgroundResource(android.R.drawable.dialog_holo_light_frame);
You can do like below:
yourTextView.setBackgroundResource(android.R.drawable.dialog_holo_light_frame);

Override layout xml from android framework

Problem
I want to override a layout file from android namespace, e.g. R.layout.popup_menu_item_layout (which is referenced from code as com.android.internal.R.layout.popup_menu_item_layout). By saying override, I assume declaring an xml file in the project which would be prioritized over the layout that framework owns.
Note, this is just an example layout, so the question concerns to each layout that's present in sdk/platforms/android-XX/data/res/layout directory.
What I've tried
tools:override
There's an undocumented tools:override tag available, which overrides specific resources. See this answer for an example, which overrides values from Design Support Library, not from Android framework.
Applying tools:override="true" to the root tag of the layout won't take effect.
XML layout references - refs.xml
As described in this post, declaring a refs.xml file in /values/ directory with following content:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="layout" name="activity_main">#layout/activity_second</item>
</resources>
will refer to activity_second.xml once activity_main.xml is used. There's an answer that suggests using this technique in order to substitute Snackbar's layout.
This also won't take effect.
Question
Is there any legitimate way to override/substitute a layout file from android package?
I know this is an old question but I also wanted to override a library layout with my own, here's how I did it.
The layout in question was called design_bottom_navigation_item
In refs.xml I added the following:
<resources xmlns:tools="http://schemas.android.com/tools">
<item name="design_bottom_navigation_item" type="layout" tools:override="true">#layout/bottom_navigation_item</item>
</resources>
There are 4 parts to this which I'll explain.
Name: This is the name of the layout you want to override
Type: The type of resource you are trying to override, in this case a layout.
tools:override: This is how you tell Android Studio to override the library layout with your own.
Value: This is where you specify what resource you want to use instead.
You can do this with any resource type this way.
What is that you're trying to do?
If the idea to only replace how the menu-item will look like, you can try the following:
Create a custom MyMenuAdapter extends MenuAdapter
Override the getView method to return the view from your adapter.
You are trying to customise your sdk on the application itself, at runtime.
That's just not how it works.
If you use an SDK on your project(on any technologies), and you need to modify some behavior, you will tweak this SDK and after that, compile your project with this news customized version.
Trying to modify it at runtime is not a good idea.
You will face multiple issues (retro compatibility, security trigger, TREBLE incompatibility , dependency issue, etc)
You have 4 possibilities to do what you want:
Make your own android rom where you will apply your modification
Copy the resources you need to modify on a fake xmlObject with the tag, after the onPostCreate of your application, you will be able to modify the when inflation. You can generalize this behavior and it will simulate an sdk overlay.
Make your own sdk :)
Multi-level reflection, but, no way you succeed with a stable version
Of course, none of this solutions is applicable for a public app.
don't know your issue have fixed or not but simple solution for this is create new layout that is same layout name of framework (in this case is popup_menu_item_layout). Then go to android google source to copy xml content popup_menu_item_layout
So you can custom anything u want. But remember don't change any id of views.

Change Android CheckBox box's color programmatically (support library)?

I'm trying to change the checkbox box's color programmatically to a different color than the theme's default. The problem is I was doing something like this:
checkbox.setSupportButtonTintList(ColorStateList);
It works but it seems, according its class documentation, this method has been restricted to be used only by classes from the same package (com.android.support). This is the warning I got from Android Studio:
AppCompatCheckBox.setSupportButtonTintList can only be called from within the same library group (groupId=com.android.support)
Is there a standard/correct way of doing this for all API levels?
Finally, found the answer here from one of the Google guys: https://code.google.com/p/android/issues/detail?id=202235. I was right about not using:
checkbox.setSupportButtonTintList(ColorStateList);
It seems is a private API. Instead, you have to use:
CompoundButtonCompat.setButtonTintList(checkbox, colorStateList);
Based on rylexr answer, you can specify the color in the following way:
CompoundButtonCompat.setButtonTintList(checkboxView, ColorStateList
.valueOf(getResources().getColor(R.color.red)));
chxAll.setButtonTintList(ColorStateList.valueOf(Color.parseColor("#CC0000")));
chxAll is a object of android.widget.CheckBox
replace the hexacolor code for desigred color

android - how can i use the “android:layoutDirection”? - again

can someone help me how can i use this attribute android:layoutDirection ,
and please don't refer me to this question What is the status of Right To Left languages on Android? because it doesn't answer my question!
layoutDirection is not be published in 4.0,so you cannot use the attr,but if you not be using sdk,but build with all code of android.
first get the Linearlayout in your code when it created,normally after setContent,and using #hide api setLayoutDiredtion(View.LAYOUT_DIRECTION_LOCALE),which define in View.java
,of course,after 4.2 you can use layoutDirection in xml directly

Building my custom compound view for Android. How to expose properties?

Doing something similar to this: http://developer.android.com/guide/topics/ui/custom-components.html
Now I want to do something like this:
<com.me.activities.MyCompoundControl android:id="#+id/someName" android:layout_height="wrap_content" android:layout_width="wrap_content" customproperty="12345"/>
So, my question is how do I declare/code
customproperty
So I can reference it in XML when insert my custom view?
See http://www.infidian.com/2008/05/02/android-tutorial-42-passing-custom-variables-via-xml-resource-files/ for how to use the /res/values/attrs.xml file to specify the custom attributes you want to attach to your custom view and then access in the component initialization.
You need to define custom attributes in res/values/attrs.xml and read their values in the attribute-aware constructor of your view. Then you refer to these attributes using your custom namespace.
You can see an example I have done in one f my libraries:
Attributes are defined here:
http://code.google.com/p/android-flip3d/source/browse/res/values/attrs.xml
And parsed here:
http://code.google.com/p/android-flip3d/source/browse/src/pl/polidea/androidflip3d/Flip3DView.java#117
And referred here:
http://code.google.com/p/android-flip3d/source/browse/res/layout/main.xml
There is no good documentation on available attribute types though and you need to browse android source code (look for attrs.xml there)

Categories

Resources