Getting a RotateDrawable from XML results in crash - android

I'm trying to retrieve a RotateDrawable defined in XML but it causes an excetion when I call getDrawable(). Here are my XML and java files:
square.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:id="#+id/mysquare">
<solid android:color="#0000f8"/>
<size android:height="200dp" android:width="200dp"></size>
<padding android:top="0dp"></padding>
</shape>
myrotate.xml
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://android.com/apk/res/android"
android:drawable="#drawable/square"
android:fromDegrees="0"
android:toDegrees="45"
android:pivotX="50%"
android:pivotY="50%">>
</rotate>
Indicator.java (only relevant code shown)
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setContentView(R.layout.drawablelayout);
Button b = (Button)findViewById(R.id.btnAnimate);
b.setOnClickListener(this);
r = getResources();
/* this is where the exception happens */
RotateDrawable rt = (RotateDrawable) r.getDrawable(R.drawable.myrotate);
}
Can anyone tell me how I'm supposed to get a RotateDrawable from XML?

You have an extra > in line 2 from the bottom of myrotate.xml
android:pivotY="50%">>
Remove it.

Related

how to add layer list on an user uploaded image?

i want to add effects from my layer-list on an user upload image.In this code i can only do effect on an image from drawable i want to change that to an user upload image
this is my xml file for effect
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#color/colorPrimary"/>
<item>
<bitmap android:src="#drawable/icn" android:gravity="center" android:alpha="0.1"/>
</item>
<item android:top="300dp"
android:left="0dp"
>
<rotate
android:fromDegrees="-12">
<shape
android:shape="rectangle">
<solid
android:color="?android:colorBackground"/>
</shape>
</rotate>
</item>
this is my xml file for displaying the final image
<ImageView
android:id="#+id/bgImage"
android:layout_width="match_parent"
android:gravity="center"
android:background="#drawable/background"
android:layout_height="290dp"/>
You can construct a LayerDrawable by code instead of xml.
Just new a LayerDrawable and call its addLayer method to add Drawables, and all the properties you set from xml can also be set in code, such as setLayerInset methods.
For example, define the drawable:
<?xml version="1.0" encoding="utf-8"?>
<rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="-12">
<shape
android:shape="rectangle">
<solid
android:color="?android:colorBackground"/>
</shape>
</rotate>
Get the drawable from code:
Drawable myDrawable;
Resources res = getResources();
try {
myDrawable = Drawable.createFromXml(res, res.getXml(R.xml.my_drawable));
} catch (Exception ex) {
Log.e("Error", "Exception loading drawable");
}
Create LayerDrawable and addLayer into it:
LayerDrawable layerDrawable = new LayerDrawable();
layerDrawable.addLayer(userDrawable); // userDrawable is from the user upload image
layerDrawable.addLayer(myDrawable);
You can do it by combine bitmaps of user photo and layer-list drawable.
Here is example:
Bitmap effects = BitmapFactory.decodeResource(context.getResources(), R.drawable.effect_name);
Bitmap[] parts = new Bitmap[2];
parts[0] = userImage; //Bitmap
parts[1] = effects; //Bitmap
Bitmap result = Bitmap.createBitmap(parts[0].getWidth() * 2, parts[0].getHeight() * 2, Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(result);
Paint paint = new Paint();
for (int i = 0; i < parts.length; i++) {
canvas.drawBitmap(parts[i], parts[i].getWidth() * (i % 2), parts[i].getHeight() * (i / 2), paint);
}
first of all Create a layer list like this
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/background" />
<item
<shape>
<solid/>
<stroke android:width="1dip" android:color="#225786" />
<corners android:radius="10dip"/>
<padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" />
</shape>
</item>
</layer-list>
just want a background:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/background"
android:tileMode="repeat" >
</bitmap>
And finally add background on ImageView
<ImageView
android:id="#+id/bgImage"
android:layout_width="match_parent"
android:gravity="center"
android:background="#drawable/background"
android:layout_height="290dp"/>

can't get reference to item inside of layer-list

I want to get a reference to item inside of the layer-list for changing some attributes programmatically, But I can't see any difference and seems it does not work.
this is my code:
logo.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/circle">
<shape android:shape="oval">
<solid android:color="#fff"/>
<size android:height="33px" android:width="33px"/>
<stroke android:color="#000" android:width="2px"/>
</shape>
</item>
</layer-list>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.shahin.testing2.MainActivity">
<Button
android:layout_width="33px"
android:id="#+id/button"
android:layout_height="33px"
android:background="#drawable/logo"/>
</LinearLayout>
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayerDrawable layerDrawable = (LayerDrawable)
ContextCompat.getDrawable(getApplicationContext(),R.drawable.logo);
GradientDrawable gradientDrawable = (GradientDrawable)
layerDrawable.findDrawableByLayerId(R.id.circle);
gradientDrawable.setStroke(10, Color.BLUE);
Button button = (Button)findViewById(R.id.button);
button.setBackgroundResource(R.drawable.logo);
}
in this case, when I changed the stroke color and width, no difference has appeared, what is the problem?

Android XML shape drawable is bogging down my UI

I'm using XML drawables and overlaying them ontop of eachother dynamically using layers. Everytime the user hits a button it overlays another image. The issue is that once I have five or ten layers of this particular XML drawable then the UI wants to just lock up. Here is the drawable:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:fromDegrees="247.5"
android:toDegrees="247.5"
android:pivotX="50%"
android:pivotY="50%">
<shape android:shape="oval">
<size
android:width="800dp"
android:height="800dp"/>
<stroke
android:dashWidth="235.6dp"
android:dashGap="5000.01dp"
android:width="200dp"
android:color="#c2272d" />
</shape>
</rotate>
</item>
<item android:top="0dp" android:bottom="0dp" android:left="0dp" android:right="0dp">
<rotate
android:fromDegrees="247.5"
android:toDegrees="247.5"
android:pivotX="50%"
android:pivotY="50%">
<shape android:shape="oval">
<size
android:width="800dp"
android:height="800dp"/>
<stroke
android:dashWidth="290.6dp"
android:dashGap="5000.01dp"
android:width="60dp"
android:color="#767171" />
</shape>
</rotate>
</item>
</layer-list>
And here is the code I am using to display and update:
public class ShotMagFragment extends Fragment implements DataDelegate, View.OnClickListener{
private ImageView mainImage;
private ArrayList<Drawable> layers = new ArrayList<>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_shot_mag, container, false);
View simulateBtn = view.findViewById(R.id.simulateBtn);
simulateBtn.setOnClickListener(this);
mainImage = (ImageView)view.findViewById(R.id.mainImage);
return view;
}
public void displayNewImage() {
Random r = new Random();
int number = 1 + (20 - 20) * r.nextInt();
Drawable d = getResources().getDrawable(getResources().getIdentifier("mag_shade" + number, "drawable", getContext().getPackageName()));
layers.add(d);
final LayerDrawable layerDrawable = new LayerDrawable(layers.toArray(new Drawable[layers.size()]));
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
mainImage.setImageDrawable(layerDrawable);
}
});
}
#Override
public void onClick(View v) {
displayNewImage();
}
}
But when I use a different XML drawable I don't get the same UI lock up problem:
<?xml version="1.0" encoding="UTF-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:top="130dp" android:bottom="130dp" android:left="130dp" android:right="130dp">
<rotate
android:fromDegrees="247.5"
android:toDegrees="247.5"
android:pivotX="50%"
android:pivotY="50%">
<shape android:shape="oval">
<size
android:width="540dp"
android:height="540dp"/>
<stroke
android:dashWidth="212.0dp"
android:dashGap="5000.01dp"
android:width="4dp"
android:color="#fff" />
</shape>
</rotate>
</item>
</layer-list>
EDIT
I also get a weird error icon next to my reference to my drawable. When I click on it, it just takes me to the drawable. I have no idea if that is related to my problem, if it is a real error, or if it is just a notice.

android custom searchView rounded corners

I want to create an Activity with a SearchView that is not in the ActionBar (in fact, I'm using the NoActionBar theme). Now I want this SearchView to have rounded corners (not the EditText inside the actual SearchView, but the SearchView itself),
like this: Mockup
All I can manage is this: actual SearchView.
Can somebody hint me to what I have to change?
XML of the Activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#color/theBlue">
<SearchView
android:id="#+id/search_view"
android:layout_width="1000dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:background="#drawable/bg_white_rounded"
android:clickable="true"/>
</RelativeLayout>
Code of drawable/bg_white_rounded.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"/>
<corners android:radius="10dp"/>
<padding android:left="0dp"
android:bottom="0dp"
android:right="0dp"
android:top="0dp"/>
</shape>
Code of the Activity:
public class MainActivity extends AppCompatActivity {
SearchView searchView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get search view and modify it to our needs
searchView = (SearchView) findViewById(R.id.search_view);
searchView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
searchView.onActionViewExpanded();
}
});
//int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
//int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_src_text", null, null);
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_badge", null, null);
View searchPlate = searchView.findViewById(searchPlateId);
//View searchPlate = searchView.findViewById(android.support.v7.appcompat.R.id.search_src_text);
searchPlate.setBackgroundResource(R.drawable.bg_white_rounded);
}
}
The commented lines are things I've already tried but didn't work.
Thanks!
Just give padding = corner radius,because corners are hidden by Actual SearchView Background will set it to back ,so padding makes the trick
drawable/bg_white_rounded.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"/>
<corners android:radius="8dp"/>
<padding android:left="8dp"
android:bottom="8dp"
android:right="8dp"
android:top="8dp"/>
</shape>
While rajan ks (Thanks!) answer works, it doesn't resemble the design mockup a 100% (the padding naturally increases the SearchView's size while not increasing the contained EditText's size).
The solution I found is this:
In the MainActivity.java, change search_edit_frame, search_plate and search_bar:
int searchFrameId = searchView.getContext().getResources().getIdentifier("android:id/search_edit_frame", null, null);
View searchFrame = searchView.findViewById(searchFrameId);
searchFrame.setBackgroundResource(R.drawable.bg_white_rounded);
int searchPlateId = searchView.getContext().getResources().getIdentifier("android:id/search_plate", null, null);
View searchPlate = findViewById(searchPlateId);
searchPlate.setBackgroundResource(R.drawable.bg_white_rounded);
int searchBarId = searchView.getContext().getResources().getIdentifier("android:id/search_bar", null, null);
View searchBar = findViewById(searchBarId);
searchBar.setBackgroundResource(R.drawable.bg_white_rounded);
That results in the SearchView looking exactly like the mockup.
I was able to achieve the desired result using only xml with androidx SearchView
<androidx.appcompat.widget.SearchView
...
app:queryBackground="#drawable/bg_white_rounded"
app:submitBackground="#drawable/bg_white_rounded"
android:background="#drawable/bg_white_rounded"/>
drawable/bg_white_rounded.xml:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#ffffff"/>
<corners android:radius="20dp"/>
<padding android:left="15dp"
android:bottom="15dp"
android:right="15dp"
android:top="15dp"/>
</shape>
and in .xml
<SearchView
...
android:submitBackground="#drawable/bg_white_rounded"
android:background="#drawable/bg_white_rounded"
NOTE: in my situation, solution is combined from above answers. Thanks to #rajan.kali and #A.Kazarovets

How to make blink effect on textview on click?

Hi I am new to android development. I want to create onclick effects to textview. When I click on the textview it will blink or something effects make. I tried it with change color, but it's not working. How can I make blink effect on textview onclick ??
please help me with example code. thanks in advance :)
The easiest way is to set this background in the TextView:
android:background="?attr/selectableItemBackground"
And if you want to set a different color for the background, set that attr as foreground instead of background.
try this. it worked for me.
android:clickable="true"
android:focusable="true"
android:background="?android:attr/selectableItemBackground"
create a xml with name something like txt_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/numpad_button_bg_selected" android:state_selected="true"></item>
<item android:drawable="#drawable/numpad_button_bg_pressed" android:state_pressed="true"></item>
<item android:drawable="#drawable/numpad_button_bg_normal"></item>
</selector>
then add in texview xml
android:background="#drawable/txt_bg"
android:clickable="true"
hope it will help.
try below code:-
<Button
android:id="#+id/action"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:layout_margin="5dp"
android:background="#drawable/btn_click"
android:gravity="center"
android:textColor="#color/white"
android:textSize="12sp" />
btn_click.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/button_hover" android:state_pressed="true"/>
<item android:drawable="#drawable/button"/>
</selector>
or below also
btn_hover.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="1dp"
android:color="#000000" />
<gradient
android:angle="270"
android:centerColor="#1a000000"
android:endColor="#33000000"
android:startColor="#android:color/transparent" >
</gradient>
<corners android:radius="5dp" />
</shape>
btn.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:color="#000000"
android:width="1dp"
/>
<gradient
android:angle="270"
android:centerColor="#android:color/transparent"
android:endColor="#android:color/transparent"
android:startColor="#android:color/transparent" >
</gradient>
<corners android:radius="5dp" />
</shape>
btn_click.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_hover" android:state_pressed="true"/>
<item android:drawable="#drawable/btn"/>
</selector>
public class TesteBlinkActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
blink();
}
private void blink(){
final Handler handler = new Handler();
new Thread(new Runnable() {
#Override
public void run() {
int timeToBlink = 1000; //in milissegunds
try{Thread.sleep(timeToBlink);}catch (Exception e) {}
handler.post(new Runnable() {
#Override
public void run() {
TextView txt = (TextView) findViewById(R.id.usage);
if(txt.getVisibility() == View.VISIBLE){
txt.setVisibility(View.INVISIBLE);
}else{
txt.setVisibility(View.VISIBLE);
}
blink();
}
});
}
}).start();
}

Categories

Resources