How to customize a vector in Android Studio? - java

I am working with an Android app (using API 21) in which I can change the color of a vector manually depends on previous Activity, but I have a problem, I can not access the 'Path' object in my xml file
<!-- drawable/chevron_left.xml -->
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:name="left"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24"
android:viewportWidth="24">
<path
android:id="#+id/chervon_left"
android:fillColor="#000"
android:pathData="M15.41,16.58L10.83,12L15.41,7.41L14,6L8,12L14,18L15.41,16.58Z" />
</vector>
So how can I customize this vector in MainActivity.java file ?
I read this but it did not work: Working with Drawables

basically, use #color.
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:name="left"
android:width="24dp"
android:height="24dp"
android:viewportHeight="24"
android:viewportWidth="24">
<path
android:id="#+id/chervon_left"
android:fillColor="#color/purple_200"
android:pathData="M15.41,16.58L10.83,12L15.41,7.41L14,6L8,12L14,18L15.41,16.58Z" />

Related

Glide error with ?attr/color use in drawable

I have ic_test.xml in drawable folder.
ic_test.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="?attr/black"
android:pathData="M19,13H13V19H11V13H5V11H11V5H13V11H19V13Z" />
</vector>
when I use Glide for load, Glide not work because I use "?attr/black" for "fillColor", if I use "#color/black", Glide is work. anyWay can fix this?
Glide.with(getContext())
.load(R.drawable.ic_test)
.apply(new RequestOptions().transforms(new CenterCrop()))
.into(this);
Add this to your styles.xml:
<item name="black">#000000</item>
? is only for accessing styles.xml not colors.xml.
use #android:color/black
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24">
<path
android:fillColor="#android:color/black"
android:pathData="M19,13H13V19H11V13H5V11H11V5H13V11H19V13Z" />

How to Set fillColor, StrokeWidth and StrokeColor of vector drawable programatically in Android

I have searched a lot about changing fillColor , strokeColor and strokeWidth of a vector drawable but didn't found anything.
This is the vector drawable:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:viewportWidth="300"
android:viewportHeight="300"
android:width="300dp"
android:height="300dp">
<path
android:pathData="M150.412,10.422999999999973,C227.47,10.422999999999973,289.93100000000004,72.81,289.93100000000004,149.771,C289.93100000000004,226.73,227.47,289.119,150.412,289.119,C73.36,289.119,10.893,226.73,10.893,149.771,C10.893,72.81,73.36,10.422999999999973,150.412,10.422999999999973z"
android:strokeLineJoin="round"
android:strokeWidth="4"
android:fillColor="#00000000"
android:strokeColor="#010101"
android:strokeMiterLimit="10" />
If I want to Change its FillColor, StrokeColor and StrokeWidth using Xml then code for that will be the following one:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:viewportWidth="300"
android:viewportHeight="300"
android:width="300dp"
android:height="300dp">
<path
android:pathData="M150.412,10.422999999999973,C227.47,10.422999999999973,289.93100000000004,72.81,289.93100000000004,149.771,C289.93100000000004,226.73,227.47,289.119,150.412,289.119,C73.36,289.119,10.893,226.73,10.893,149.771,C10.893,72.81,73.36,10.422999999999973,150.412,10.422999999999973z"
android:strokeLineJoin="round"
android:strokeWidth="10"
android:fillColor="#f50d0d"
android:strokeColor="#f1e904"
android:strokeMiterLimit="10" />
How can I achieve this programmatically via Java code?

FloatingActionButton black box

I'm using this https://github.com/makovkastar/FloatingActionButton library for FloatingActionButtons in my project.
On some smaller devices, for example the Acer Liquid Z4 (Android version 4.2.2, screen 480x800) or the Genymotion Galaxy S2 image (Android version 4.1.1, screen 480x800) the FABs have a black box around them:
I tried to narrow this down, removed any special stuff from my theme making it look like this:
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
and also put the buttons in the most simple layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="de.immowelt.android.immobiliensuche.ui.FabTestActivity">
<com.melnykov.fab.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
but the box remains. On the other hand I tried to recreate the effect with a new project but couldn't do it.
The box disappears when using the mini size version or disabling the shadow.
Any ideas on what my problem is?
PS: I tried cleaning the project ;)
I was using that library but then decided to make my own, set this as the background to an imageButton or something. I used a textview because all I needed was a +. It looks pretty good in my app.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- Drop Shadow Stack -->
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
            <padding android:right="1dp" android:bottom="1dp" />
            <solid android:color="#00262626" />
        </shape>
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
            <padding android:right="1dp" android:bottom="1dp" />
            <solid android:color="#0D262626" />
        </shape>
    </item>
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
            <padding android:right="1dp" android:bottom="1dp" />
            <solid android:color="#26262626" />
        </shape>
    </item>
    <!-- Background -->
    <item>
        <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="oval" >
            <solid android:color="#448AFF"/>
        </shape>
    </item>
</layer-list>
The problem was that the FloatingActionButton class makes use of a drawable called shadow.9.png for the shadow. I also had a shadow.9.png in my drawables and the library class obviously picked up the wrong one (mine).
Renaming my drawable solved the problem.

Android OpenCV : No resource identifier found for attribute 'camera_id' in package

I have OpenCV and Android set up in my Eclipse. The following is one of my layout files:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:opencv="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<org.opencv.android.JavaCameraView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:visibility="gone"
android:id="#+id/hello"
opencv:show_fps="true"
opencv:camera_id="any" />
</LinearLayout>
The Eclipse compiler complains about:
No resource identifier found for attribute 'show_fps' in package
No resource identifier found for attribute 'camera_id' in package
Please add following resource file in your project's values directory :
attrs.xml
with following content :
<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name = "CameraBridgeViewBase" >
<attr name="show_fps" format="boolean"/>
<attr name="camera_id" format="integer" >
<enum name="any" value="-1" />
<enum name="back" value="0" />
<enum name="front" value="1" />
</attr>
</declare-styleable>
</resources>
The two previously given answers to this question, in my opinion, are bandaids to the actual problem. When I encountered this error message, I needed to change some project properties.
Right click project and select Properties
Select 'Android' in the tree control
Make sure the OpenCV library is present and has a green check mark next to it in the 'Library' section (seen in the image below)
If the OpenCV library is not present or has a red X next to it, you need to fix the Library dependency. To do this:
Remove broken library (if necessary)
Click Add and select OpenCV Library
If OpenCV library is not present, you need to add the library to the project
you did not given value for this variables or not declare in opencv class..
opencv {
show_fps="true"
camera_id="any"
}
First assign the those two variables globally with necessary values....

How to create XML in Android for several images?

How to create an xml with several images? I presume that this file should be saved in drawable folder? I mean, what should be the structure of that xml?
To describe in more detail, for example I have a bitmap XML:
<?xml version="1.0" encoding="UTF-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/logo_top"
android:dither="true" />
And it works good, untill I need to add several images, I understand that there is no way to add more bitmaps to 1 XML file, so I search the way to do so.
PS I need this technique to add images dynamically and programmatically.
Also I've tried to do that this way:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/logo_top"
android:dither="true" />
</item>
<item>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/image2"
android:dither="true" />
</item>
<item>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/image3"
android:dither="true" />
</item>
</layer-list>
But the picture didn't show up.
Check following Tutorial to see how we can define a selector and apply on a view:
http://www.gersic.com/blog.php?id=56

Categories