Android multi stroke gesture not working - android

I modified an existing app,GestureBuilder,to accept multiple strokes.Hence to accept the letter A,D, F and so on. I created a simple app which recognises if the letter is correct or not.My problem is:
1.I have successfully recorded multi stroke letter in a file called gesture with gestureBuilder.
2.My application does not accept multi stroke gesture.But i have set fadeoffset to 1000 and gesturestroketype as multiple.I do not know why this is happening.The moment I enter next xtroke to write capital A my previous stroke dissapears.I cant write multi stroke letters/symbols.
Code:
xml file:
<LinearLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<android.gesture.GestureOverlayView
android:id="#+id/gestures_overlay"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:fadeOffset="5000"
android:eventsInterceptionEnabled="true"
android:gestureStrokeType="multiple" />
</LinearLayout>
java file:
package com.example.gesture_letter;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity implements OnGesturePerformedListener {
GestureLibrary mLibrary;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GestureOverlayView gestureoverlayview=new GestureOverlayView(this);
View inflate=getLayoutInflater().inflate(R.layout.activity_main,null);
gestureoverlayview.setGestureColor(Color.rgb(0, 255, 0));
gestureoverlayview.setGestureVisible(true);
gestureoverlayview.setUncertainGestureColor(Color.rgb(0,0,255));
gestureoverlayview.addView(inflate);
gestureoverlayview.addOnGesturePerformedListener(this);
mLibrary=GestureLibraries.fromRawResource(this,R.raw.gestures);
if(!mLibrary.load())
{
Log.i("debug","FAILED");
}
else
{
Log.i("debug","LOADED");
}
Log.i("debug","OnCreate");
setContentView(gestureoverlayview);
}
#Override
public void onGesturePerformed(GestureOverlayView arg0, Gesture arg1) {
// TODO Auto-generated method stub
ArrayList<Prediction> predictions=mLibrary.recognize(arg1);
if(predictions.size()>0)
{
Prediction prediction=predictions.get(0);
if(prediction.score>1.0)
{
String s=prediction.name;
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_LONG).show();
}
else
if(prediction.score<1.0)
{
Toast.makeText(getApplicationContext(), "None",Toast.LENGTH_LONG).show();
}
}
}
}
Thank you.
I also would like a good resource for gesture application building.thank you

I do not know why, android:GestureStrokeType="multiple" did not work. It struck me to try it from the java side. I set :
gestureoverlayview.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
gestureoverlayview is an object of GestureOverlayView(you can name it anything)
(in main_activity.java file for newbies)
Then you'll see it works.
I think its a bug, I'm not sure.I spent weeks hovering on this :(

The problem is that you create a new GestureOverlayView :
GestureOverlayView gestureoverlayview=new GestureOverlayView(this);
You should call the view defined in the xml with findViewById(R.id.gesture_overlay);

Related

Android application that downloads images from bing image search not having the correct image

I have an android application that has a button and an image view
when clicking the button the application goes to bing image search and search for images then get the url of the first image and make it the source of the image view so that it has the first image of the search
When I do the image search using the explorer (I use chrome) I find that the first image displayed in my browser is not the same displayed in the image view
when I search for (facebook.com logo) or (google.com logo) the image view is the same of the first image shown in the search result but if I change the search to (yahoo.com logo) the image view is the second
If I change the search to (outlook.com logo) it takes the 10th image of the search and if I search for (hotmail.com logo) it does not show an image related to hotmail at all
I use JSOUP library to parse the HTML of the web page of the search results
here is the xml of the project:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="arb.myapplication.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:layout_alignParentBottom="true"
android:layout_marginBottom="131dp"
android:id="#+id/imageView" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="39dp"
android:layout_marginEnd="39dp"
android:layout_marginTop="11dp"
android:id="#+id/button3"
android:elevation="0dp" />
</RelativeLayout>
and this is the Java file of the project:
package arb.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
class DownloadIcon extends AsyncTask<Object, Object, Integer>
{
#Override
protected Integer doInBackground(Object... objects) {
Connection connecion= Jsoup.connect("http://www.bing.com/images/search?q=outlook.com+logo");
try {
Document document=connecion.get();
Elements element=document.select("img.mimg");
String url=element.get(0).absUrl("src");
InputStream inputStream= new URL(url).openStream();
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
publishProgress(bitmap);
} catch (IOException e1) {
publishProgress(-1);
}
return 0;
}
#Override
protected void onProgressUpdate(Object... values) {
ImageView imageView=(ImageView) findViewById(R.id.imageView);
Bitmap bitmap=(Bitmap) values[0];
imageView.setImageBitmap(bitmap);
}
#Override
protected void onPostExecute(Integer integer) {
Toast.makeText(getBaseContext(),"done",Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button3=(Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DownloadIcon().execute();
}
});
}
}
Help me please

Android: ImageView Rotate Animation working only once

I am facing strange Issue
If I click on the image it is rotating for first time,
if you click it again it is not rotating.
I have used toast to check if control is going inside the function, but toast is getting printed all the time.
Why Image is not rotating for second time?
here is my code..
MainActivity:
package com.example.sayantan.myapp1;
import android.annotation.TargetApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
public void rotateImage(View view) {
Toast.makeText(getApplicationContext(), "in rotateImage()...", Toast.LENGTH_SHORT).show();
imageView.animate().rotation(1800f).setDuration(1500);
// Toast.makeText(getApplicationContext(), "End of rotateImage()", Toast.LENGTH_SHORT).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
}
}
Layout XML
<?xml version="1.0" encoding="utf-8"?>
<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="com.example.sayantan.myapp1.MainActivity">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView"
android:layout_below="#+id/blue"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:src="#drawable/bart"
android:onClick="rotateImage" />
I am pretty sure that the rotation is kept at the end of the animation. So the second time you try to animate the image it doesn't move because it already is rotated to 1800.
So either reset your image to 0 or rotate it to currentRotation + 1800f.
Use rotationBy() instead of rotation();
Have you tried to call Start()?
imageView.animate().rotation(1800f).setDuration(1500).start();
hi you can try with this,
imageView.animate().rotation(1800f).setDuration(1500);
imageView.getAnimation().setRepeatCount(Animation.INFINITE);
Thanks hope this will help you.

Android Studio, adding multiple images on click

JAVA:
package com.example.scott.testapplication;
import android.app.Activity;
import android.media.Image;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class HomeScreen extends Activity {
RelativeLayout Backround;
Button InitButton;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Backround = (RelativeLayout) findViewById(R.id.Backround);
InitButton = (Button) findViewById(R.id.InitButton);
image = (ImageView) findViewById(R.id.image);
InitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*insert code here*/
}
});
}
}
In order to insert the image, i'm thinking that I need to use a command like ImageView image = new (image), and then use another command to draw it on the display. I dont know which commands I should use.
XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Backround"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<ImageView
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:scaleType="fitXY"
android:src="#drawable/DESERT2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Initiate"
android:id="#+id/InitButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical" />
</RelativeLayout>
I would like to create the image on the event of the click of a button, I also created the ImageView in the xml file. Not sure if I should change something there.
you can use
image.setImageResource(R.drawable.yourDesiredImageName);

Android-background change

So basically what I want to do is to change the background of the application when integer A value is greater than 50. I've tried using:
setBackgroundResource(R.drawable.imagename);
but it doesn't work.
I would really appreciate your help.
UPDATE:
My code:
package com.example.highhope;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
runOnUiThread(new Runnable() {
#Override
public void run() {
view.setBackgroundResource(R.drawable.i0);
}
});
}
});
}
}
My 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.highhope.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="90dp"
android:text="Button" />
So for starters I'm just trying to change application background on button click and I've tried everything and nothing seems to work.
You can make that with a Selector, Check this tutorial.
it's pretty easy, you just have to:
Add the images to your app resources
Create your background_button.xml (selector in the tutorial) in the drawable carpet
Set the background_button as your button background in your layout (android:background="#drawable/background_button")
Set an onClickListener to your button
Test it!.
When you do it this way, the background of your button will change with no need of handle it programmatically.
Are you making the background change in the UI thread? Try setBackgroundResource in the UI thread like below.
runOnUiThread(new Runnable() {
#Override
public void run() {
view.setBackgroundResource(R.drawable.bg);
}
});
Also, if you are using a Fragment. you would have to put getActivity(). in front of runOnUiThread.

How to implement click the certain area to do to action?

This is an example of facebook sliding menu.
When sliding, there is 20% space user can see which is similar with facebook. Facebook implements it with clicking anywhere of this 20% and the menu is slide back.
How to implement this?
One way of doing that is as following with OnTouchListener on your activity. You can detect exactly where is touched on the screen.
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.Toast;
public class AndroidTestActivity extends Activity implements OnTouchListener {
LinearLayout main;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
main = (LinearLayout) findViewById(R.id.main_layout);
main.setOnTouchListener(this); // you need to set the touch listener for your view. And every element around the detection area.
}
public boolean onTouch(View v, MotionEvent e) {
if(e.getX() <= main.getWidth() / 5) {
Toast.makeText(this, "In the %20..", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
You also need to id your main layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

Categories

Resources