How to transform XML layouts uniformly across Android apps? - java

I am looking into code injection for Android apps (written in Java). Let's say I have a set of ImageViews declared similarly as such within separate layout files in .../layout/ :
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#color/colorPrimary"
tools:layout_editor_absoluteX="70dp"
tools:layout_editor_absoluteY="38dp" />
Now let's say I have the following attribute:
android:rotation="180"
My goal is to add this attribute to every single ImageView in every .xml file in the layout directory on run-time, so that I do not have to edit the source code of these .xml files. How would I go about doing this?

You can do this dynamically. What you need to do is to find the image view on activity and call set rotation method and give angle as you are giving in xml file.
And second way of doing the same thing is you can provide customize style for image view in styles folder of the project.And this will be very simple and far more effective way of doing so. To achieve this via style, you can use below code.
<style name="TitleBarImage">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:src">#drawable/lock</item>
<item name="android:rotation">180</item>
</style>
and in every image view, you need to use this below code to get the reflection of style
<ImageView style="#style/TitleBarImage" />
Hope this will help you.

Related

Why does ImageButton quality blurs or reduces to worst?

My app has a ImageButton whose quality is too bad. The actual image dimensions are 4176*4176 (4176pixels) which looks completely fine when opened separately. But when I import this to android studio and assign it to a ImageButton, the quality of the image downgrades. I have been trying a number of ways to fix this but haven't succeeded yet. I'm completely new to Android Programming.I go to res/drawable then right click to create a new Image Asset and then specify the location which imports the png file.
Actual image:
Image inside studio:
Here is the code in activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center"
android:orientation="vertical">
<ImageButton
android:id="#+id/police"
android:layout_width="317dp"
android:layout_height="295dp"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:background="#drawable/custom_button"
/>
</LinearLayout>
Custom button code which calls the image:
<item
android:state_pressed="true"
android:drawable="#mipmap/pressed" />
<item
android:drawable="#mipmap/defaultt" />
</selector>
You are actually referencing the image like Launcher icon in /mipmap directory.
I suggest reducing the image size which you are only using it for ImageButton and placing it inside /drawable with the quality you want or maybe smaller than the current one instead of placing it inside /mipmap directory.
Also, I don’t actually see the reason of how scaleType helps in ImageButton. You may wanna remove that line too.
I'd start by reviewing density independent pixels on Android. This is a good blog that covers the concept: https://www.captechconsulting.com/blogs/understanding-density-independence-in-android
The main issue is that your image (which is very high res) is getting compressed to fit inside of the image button. Typically, you would want to include image assets for different densities via different drawable resource directories (e.g., drawable-hdpi, drawable-xhdpi, drawable-xxhdpi).
However, these days, you can use vector drawables, which will reduce the need for having multiple assets for each density bucket, assuming you have the ability to generate SVG asset files: https://developer.android.com/guide/topics/graphics/vector-drawable-resources

Using ScaleType enum values by its integer reference

To make my question way clear, I want to use ScaleType in layout xml with different values according to layout width (e.g sw-600, sw-720, ...etc), and as it doesn't has a clear constant values like layout weights for example I don't know if it is possible to achieve that, so I had a look about ImageView class and found that the ScaleType values are enum with assigned values, so is it possible to use the enum its assigned values? so when use it in the xml it will be
android:scaleType="2" // 2 will be an Integer value assigned differently across different layouts
instead of:
android:scaleType="FIT_START"
This is what I've done and so far so good. I have one resource that contains different values depending on the res folder type.
w720dp\sizes.xml
<integer name="advert_scaletype">3</integer>
values\sizes.xml
<integer name="advert_scaletype">6</integer>
By doing this I get different scale type on the image if I'm on tablet or phone.
The way I use it:
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/advert_image"
android:layout_width="match_parent"
android:layout_height="230dp"
android:layout_centerVertical="true"
android:scaleType="#integer/advert_scaletype"
android:transitionName="photo"/>
I haven't found any issues so far so not sure why this is not working for other people.
In the case of the value you want to use is float, you can store it inside dimens.xml in the following format:
<item name="advert_scaletype" format="float" type="dimen">7.6</item>
and then it will be like:
<com.android.volley.toolbox.NetworkImageView
...
android:scaleType="#dimens/advert_scaletype" />
I had the same problem before and I'm posting here what I think is the best approach so far:
values\style.xml
<style name="my_imageview_parent_style">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_weight">match_parent</item>
</style>
<style name="my_imageview_child_style" parent="my_imageview_parent_style">
<item name="android:scaleType">centerInside</item>
</style>
values-XXX\style.xml
<style name="my_imageview_child_style">
<item name="android:scaleType">fitXY</item>
</style>
values-YYY\style.xml
<style name="my_imageview_child_style">
<item name="android:scaleType">fitXY</item>
</style>

Different textColor for TextView with custom theme

I want to create a custom theme for my application, but I have a small problem. In my TextViews I use different textColors so for my custom theme i put
<item name="android:textColor">#color/myColor</item>
The problem is: How can I set different textColors for different TextViews? Thanks in advance.
Don't define it in theme then, create a style and apply it to different textviews.
You could use TextView#setTextColor:
text.setTextColor(Color.rgb(250,20,250));
Here are some examples where you can also get the text color from resources:
text.setTextColor(getResources().getColor(R.color.myColor));
If you absolutely want to use Theme/Styles you can set a custom style for each TextView:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Sample Text"
style="#style/my_style" />
Here is a good explanation about creating styles!

can I force android (API8) seekbar to have a specific color?

I'm using android SeekpBar in my android app (API8)
I want to change it's color to be always blue.
but nowadays the color depends on the device model.
is there a way to make it always blue?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/settingsSeekbarMain">
<SeekBar
android:id="#+id/settingsSeekbarBar"
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_gravity="center_vertical|right"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
/>
</LinearLayout>
You need to create a custom style for your SeekBar which has a different progress drawable.
Copy the file android-sdk-root\platforms\android-8\data\res\drawable\progress_horizontal.xml into your drawable-folder. Alter the colors as you see fit.
In your styles.xml, create a custom style which uses your custom background drawable:
<style name="CustomSeekbarStyle" parent="#android:style/Widget.SeekBar">
<item name="android:progressDrawable">#drawable/custom_progress_horizontal</item>
</style>
To use your custom style, either set it explicitly for every SeekBar in your application like this:
<SeekBar
android:layout_width="match_parent"
style="#style/CustomSeekbarStyle"
android:progress="50"
android:layout_height="wrap_content" />
Or set the style globally in your application theme:
<style name="AppBaseTheme" parent="android:Theme.Light">
<item name="android:seekBarStyle">#style/CustomSeekbarStyle</item>
</style>
Edit 1: If your application uses newer themes on newer devices (i.e. holo-theme on ICS devices), you should consider overriding the progress_horizontal file for all relevant API levels. Create multiple drawable-vXX folders (i.e. drawable-v10 for GB, drawable-v14 for ICS) and put modified progress_horizontal.xml-files in those folders. You'll find those files in your android SDK as well.
Edit 2: This solution however does not come without a somewhat serious flaw: Device vendors like to create custom themes for their android firmwares, often involving different drawables and colors. Using the approach described above, those custom styles are overridden, possibly resulting in an inconsistent user interface.

Android spinnerStyle not working

First excuse me for my english.
Im designing a theme (With HoloEveryWhere) and im trying to style default spinner.
The problem is that the spinnerStyle is not working. I'm testing with this layout.
<Spinner
android:id="#+id/spCountry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/etPhone" />
And im using this for my theme.xml..
<item name="android:spinnerStyle">#style/Coloquium.Spinner.Ruby</item>
and in styles.xml
<style name="Coloquium.Spinner.Ruby" parent="#style/Holo.Spinner.Light">
<item name="android:background">#drawable/spinner_holo_white</item>
</style>
The problem is that the spinner style is never applied (Others styles like edittextstyle or buttonstyle works fine), the background never change, But i tried this style as spinnerItemStyle and work fine, but spinnerItemStyle is the inner view, and i want to style the outer, the spinner.
If i set background directly in the layout node, the background change fine, but i need to set spinnerStyle for all spinners in the theme, not in layout.
I test a lot of post looking for a solution without success.
The solution is:
HoloEveryWhere appear use "spinnerStyle" and not "android:spinnerStyle".
But really... I can't find the HoloEveryWhere documentation about this. :S

Categories