i wish to implement the GestureDetector using service instead of activity. the code works on activity but not on service. In Service i am not able to #Override the onTouchEvent(MotionEvent e) method - is this the cause of the problem?
public class GestureService extends Service implements OnGestureListener {
private GestureDetector gestureScanner;
#Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
#Override
public void onCreate() {
super.onCreate();
Toast.makeText(getBaseContext(),"onCreate", Toast.LENGTH_SHORT).show();
gestureScanner = new GestureDetector(this);
}
#Override
public int onStartCommand(Intent intent, int flags, int startId) {
Toast.makeText(getBaseContext(),"onStartCommand", Toast.LENGTH_SHORT).show();
return START_STICKY;
}
public boolean onTouchEvent(MotionEvent me) {
return gestureScanner.onTouchEvent(me);
}
#Override
public boolean onDown(MotionEvent e) {
// TODO Auto-generated method stub
Toast.makeText(getBaseContext(),"onDown", Toast.LENGTH_SHORT).show();
return false;
}
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onLongPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
// TODO Auto-generated method stub
return false;
}
#Override
public void onShowPress(MotionEvent e) {
// TODO Auto-generated method stub
}
#Override
public boolean onSingleTapUp(MotionEvent e) {
Toast.makeText(getBaseContext(),"onSingleTapUp", Toast.LENGTH_SHORT).show();
// TODO Auto-generated method stub
return false;
}
}
i started the service in an activity class using startService(new Intent(this,GestureService.class));
I'm guessing you want to make it a Service so that you can use it for all applications. But from reading this article, it's really not the way it works: gestures are application specific.
Related
this is my code....
StudentLogin.java class
public class StudentLogin extends Activity implements OnClickListener, OnItemSelectedListener{
Spinner sp1,sp2;
EditText reg;
Button b1,b2,b3;
String a1,a2,studreg,studdept,studyr;
SQLiteDatabase db1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_student_login);
reg=(EditText) findViewById(R.id.editText1);
sp1=(Spinner)findViewById(R.id.spinner1);
sp2=(Spinner)findViewById(R.id.spinner2);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Cursor c=db1.rawQuery("Select * from StudReg", null); // Im getting an error here
int count=c.getCount();
int flag=0;
c.moveToFirst();
String regno=reg.getText().toString();
String yr=a1;
String dept=a2;
Toast.makeText(getApplicationContext(), regno, Toast.LENGTH_LONG).show();
if(regno.trim().length()>0&&!yr.equals("Select Year")&&!dept.equals("Select Dept"))
{
for(int i=0;i<count;i++)
{
studreg=c.getString(c.getColumnIndex("register"));
studdept=c.getString(c.getColumnIndex("deptmt"));
studyr=c.getString(c.getColumnIndex("year"));
if(regno.equals(studreg)&&yr.equals(studyr)&&dept.equals(studdept))
{
flag=1;
break;
}
c.moveToNext();
}
c.close();
if(flag==1)
{
Intent inn=new Intent(StudentLogin.this,StudyMaterials.class);
startActivity(inn);
}
else
{
Toast.makeText(getApplicationContext(), "Invalid User!\nKindly try again..", Toast.LENGTH_SHORT).show();
}
}
else
{
Toast.makeText(getApplicationContext(), "Kindly fill all the fields..", Toast.LENGTH_SHORT).show();
}
}
});
b2=(Button)findViewById(R.id.button2);
b2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intt=new Intent(StudentLogin.this,StudentRegister.class);
startActivity(intt);
}
});
b3=(Button)findViewById(R.id.button3);
b3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent int1=new Intent(StudentLogin.this,Update.class);
startActivity(int1);
}
});
sp1.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> p, View v,
int a, long l) {
// TODO Auto-generated method stub
a1=p.getItemAtPosition(a).toString();
//Toast.makeText(getApplicationContext(), a1, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
sp2.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> p, View v,
int a, long l) {
// TODO Auto-generated method stub
a2=p.getItemAtPosition(a).toString();
//Toast.makeText(getApplicationContext(), a2, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
List<String> categories = new ArrayList<String>();
categories.add("Select Department");
categories.add("CSE");
categories.add("IT");
categories.add("CIVIL");
categories.add("MECH");
categories.add("ECE");
categories.add("EEE");
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, categories);
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp2.setAdapter(dataAdapter);
List<String> categories1 = new ArrayList<String>();
categories1.add("Select Year");
categories1.add("I");
categories1.add("II");
categories1.add("III");
categories1.add("IV");
ArrayAdapter<String> dataAdapter1 = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, categories1);
dataAdapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp1.setAdapter(dataAdapter1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.student_login, menu);
return true;
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
#Override
public void onItemSelected(AdapterView<?> p, View v, int it,
long l) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
This code validates whether the user is already registered or not..
Please help me solve this error...
you need to initialize database
//dbHelper its a helper class for database
database = dbHelper.getWritableDatabase();
You have given the table name but you are not saying it on which db the table exists(as db1 is not initialized).So follow #Madhur answer to solve this null pointer exception.Also a suggestion when working with db(better to use loaders instead of working on MainThread).
I tried placing the interface in my Activity, my fragment and my FragmentStatePagerAdapter. But in all of them the code never got there. this is what my FragmentStatePagerAdapter looks like since this is my latest try.
FragmentStatePagerAdapter :
public static class AppSectionsPagerAdapter extends FragmentStatePagerAdapter implements OnPageChangeListener
{
......
......
#Override
public void onPageScrollStateChanged(int arg0)
{
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int arg0)
{
// TODO Auto-generated method stub
Log.w("onpage", "onpage executed");
currentSelectedFragmentPosition = arg0;
}
public int getCurrentSelectedFragmentPosition()
{
return currentSelectedFragmentPosition;
}
}
Fragment would look like this :
public class SectionFragment extends ListFragment implements OnPageChangeListener
{
......
......
#Override
public void onPageScrollStateChanged(int arg0)
{
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2)
{
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int arg0)
{
// TODO Auto-generated method stub
Log.w("onpage", "onpage executed");
currentSelectedFragmentPosition = arg0;
}
public int getCurrentSelectedFragmentPosition()
{
return currentSelectedFragmentPosition;
}
}
But both didnt work, it would be most usefull to me to use it in a fragment but if that is not possible then the activity or statepager will work too
In AppSectionsPagerAdapter constructor: add this
mViewPager.setOnPageChangeListener(this);
(this assume the ViewPager is an instance variable named mViewPager)
Or, as an alternative in the MainActivity.onCreate() :
mViewPager.setOnPageChangeListener(mAppSectionPageAdapter);
(this assume the AppSectionPageAdapter is in a variable named mAppSectionPageAdapter)
How to store a number from edittext in a variable (integer) each time when user enter a number and to use the variable in 'onSensorChanged'? Like here: if (event.values[0] == (1 - UserInputVariable)) {acceleration.setText(UserInputVariable);}
My code is:
Sensor accelerometer;
SensorManager sm;
TextView acceleration;
EditText mEdit;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sm=(SensorManager)getSystemService(Context.SENSOR_SERVICE);
accelerometer=sm.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
sm.registerListener(this, accelerometer, SensorManager.SENSOR_DELAY_NORMAL);
acceleration=(TextView)findViewById(R.id.acceleration);
mEdit = (EditText)findViewById(R.id.editText1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public void onAccuracyChanged(Sensor sensor, int accuracy) {
// TODO Auto-generated method stub
}
#Override
public void onSensorChanged(SensorEvent event) {
if (event.values[0] == 1) {
acceleration.setText(mEdit);
}
else{ acceleration.setText("X: "+event.values[0]+
"\nY:"+event.values[1]);
}
}
}
mEdit.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
doSomething();
}
});
you can use edit text change listener to handle whenever user enter something.
How do I get values from a SeekBar?
I have a code of a class that has three SeekBars (PRICEbar)
I want to pass the values of these SeekBars to next Activity (screen)
as Intents
I know how toimplement OnClickListener but how can I extract the
values from the SeekBars?
ex: if PRICEbar is pointing to value 10 ... I need to get the value of 10
Filters.java
public class Filters extends Activity implements OnSeekBarChangeListener{
// declare text objects variables
private SeekBar PRICEbar;
private TextView PRICEtextProgress,DISTANCEtextProgress, RATINGtextProgress;
Button back;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// load the layout
setContentView(R.layout.filters);
PRICEbar = (SeekBar)findViewById(R.id.PRICEseekBarID); // make seekbar object
PRICEbar.setOnSeekBarChangeListener(this);
PRICEbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress,
boolean fromUser) {
// TODO Auto-generated method stub
PRICEtextProgress = (TextView)findViewById(R.id.PRICEtextViewProgressID);
PRICEtextProgress.setText("Price:: Rs "+progress);
seekBar.setMax(100);
}
});
}
#Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (seekBar == PRICEbar)
PRICEtextProgress.setText("Price:: Rs "+progress);
}
#Override
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
#Override
public void onStopTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
}
The SeekBar class is a subclass of ProgressBar, which contains a getProgress() method.
Calling PRICEbar.getProgress() will return the value you are looking for.
I want to call MyView.circle() method when I click on menu Circle but when I'm clicking on menu it is throwing a NullPointerException, which means that the object is not initialized as I expected.
Here is my code. Where am I going wrong?
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(new MyView(this));
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// TODO Auto-generated method stub
menu.add(0,1,1,"Circle");
menu.add(0,1,2,"Rect");
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// TODO Auto-generated method stub
switch(item.getItemId())
{
case 1:
MyView.circle();
break;
}
return super.onOptionsItemSelected(item);
}
public static class MyView extends View {
private Context context;
static Canvas can=null;
public MyView(Context c) {
super(c);
context=c;
}
#Override
protected void onDraw(Canvas canvas) {
mPaint=new Paint();
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(Color.BLUE);
mPaint.setAntiAlias(true);
//circle(canvas);
}
public static void circle() {
// TODO Auto-generated method stub
can.drawCircle(50, 50, 50, mPaint);
}
}
}
public void static circle(c) {
needs to be changed to
public void static circle(Canvas c) {
Might I recommend spending some time learning Java before writing Android apps?