[XPOSED] HTC Motion Media Gestures - HTC 10 Themes, Apps, and Mods

Hey everyone. I got sick of not having double tap to wake enabled when using the asop lockscreen and created this xposed module to fix that and change all of the other functions to control media.
After you install double check the motion launch settings are on by going into settings -> Display, gestures & buttons -> Motion Launch gestures.
Swiping up will pull google play music up (I use this in conjunction with quick access which lets it draw over the lock screen)
Swiping Down is play/pause music
Swiping Right is next
Swiping left is previous
Double tap is wake device
I was originally going to try and do more with this, but I'm gonna shift focus to modifying the kernel to see if it's possible to add even more custom gestures. However if you run into any issues let me know and I'll try to figure out what's going on.
Here's the source code, do what you want:
Code:
import android.content.Context;
import android.content.Intent;
import android.os.PowerManager;
import android.view.KeyEvent;
import java.lang.Object;
import de.robv.android.xposed.IXposedHookLoadPackage;
import de.robv.android.xposed.XC_MethodHook;
import de.robv.android.xposed.XposedBridge;
import de.robv.android.xposed.XposedHelpers;
import de.robv.android.xposed.callbacks.XC_LoadPackage.LoadPackageParam;
import de.robv.android.xposed.XC_MethodReplacement;
public class XposedMod implements IXposedHookLoadPackage {
Context context;
XC_MethodHook.MethodHookParam temp;
@Override
public void handleLoadPackage(LoadPackageParam lpparam) throws Throwable {
if (!lpparam.packageName.equals("com.htc.sense.easyaccessservice"))
return;
Class<?> EasyAction =
XposedHelpers.findClass("com.htc.sense.easyaccessservice.easy.sense81.EasySensorAction81", lpparam.classLoader);
XposedHelpers.findAndHookMethod(EasyAction, "triggerMotionAction", int.class, new XC_MethodReplacement() {
@Override
protected Object replaceHookedMethod(MethodHookParam param) throws Throwable {
XposedBridge.log("BEFORE EXECUTE!");
temp = param;
context = (Context) XposedHelpers.getObjectField(param.thisObject, "mContext");
int gesture = (Integer) param.args[0];
XposedBridge.log(String.format("%1$d",gesture));
performAction(gesture, context);
param.setResult(null);
return null;
}
});
}
private void performAction(int action, Context context) {
switch(action){
case GestureType.SWIPE_LEFT:
sendMediaButton(context, KeyEvent.KEYCODE_MEDIA_PREVIOUS);
XposedHelpers.callMethod(temp.thisObject, "doOnResetAction");
XposedBridge.log("LEFT");
break;
case GestureType.SWIPE_RIGHT:
sendMediaButton(context, KeyEvent.KEYCODE_MEDIA_NEXT);
XposedHelpers.callMethod(temp.thisObject, "doOnResetAction");
XposedBridge.log("RIGHT");
break;
case GestureType.CAMERA_ACTION:
sendMediaButton(context, KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE);
XposedHelpers.callMethod(temp.thisObject, "doOnResetAction");
break;
case GestureType.SWIPE_UP:
Context ctx=context;
try {
Intent i = ctx.getPackageManager().getLaunchIntentForPackage("com.google.android.music");
ctx.startActivity(i);
} catch (Exception e) {
XposedBridge.log(e);
}
wakeUpDevice();
break;
case GestureType.DOUBLE_TAP_ACTION:
wakeUpDevice();
break;
default: break;
}
}
private void wakeUpDevice() {
PowerManager pm = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
PowerManager.WakeLock wakeLock = pm.newWakeLock(PowerManager.FULL_WAKE_LOCK | PowerManager.ACQUIRE_CAUSES_WAKEUP
| PowerManager.ON_AFTER_RELEASE, "MyWakeLock");
wakeLock.acquire();
wakeLock.release();
XposedBridge.log("WTFISGOINGON");
}
private void sendMediaButton(Context context, int keyCode) {
KeyEvent keyEvent = new KeyEvent(KeyEvent.ACTION_DOWN, keyCode);
Intent intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
context.sendOrderedBroadcast(intent, null);
keyEvent = new KeyEvent(KeyEvent.ACTION_UP, keyCode);
intent = new Intent(Intent.ACTION_MEDIA_BUTTON);
intent.putExtra(Intent.EXTRA_KEY_EVENT, keyEvent);
context.sendOrderedBroadcast(intent, null);
}
public class GestureType {
public static final int SWIPE_LEFT = 3;
public static final int SWIPE_RIGHT = 2;
public static final int SWIPE_UP = 1;
public static final int CAMERA_ACTION = 6;
public static final int DOUBLE_TAP_ACTION = 5;
}
}

Cool, thanks for sharing. Is there any module out there that let us put our own commands to shortcuts - as example, open torch or xda app when swiping on screen up while phone is off?
That would be insane!

Would love this with an interface to customize shortcuts. Nice work!
Sent from my HTC6545LVW using Tapatalk

Hi. Thanks for this. Could you please also make vibration feedback and flip to mute for calls to work with Google dialler?

Works as advertised!
A user interface that can be customized would be great but good job! :good:

This mod working with Play Music app only?

JR 2007 said:
This mod working with Play Music app only?
Click to expand...
Click to collapse
It will only launch the play music app by swiping up, but it should control any media player.
EDIT:
To anyone asking for features, i'm not gonna be adding them. I upgraded to Nougat and I'm probably not gonna be using xposed anymore considering the big reason why I did was for android n-ify and a few other things that are now just built into the system.

Sivat said:
It will only launch the play music app by swiping up, but it should control any media player.
EDIT:
To anyone asking for features, i'm not gonna be adding them. I upgraded to Nougat and I'm probably not gonna be using xposed anymore considering the big reason why I did was for android n-ify and a few other things that are now just built into the system.
Click to expand...
Click to collapse
I know you are done with xposed territory but I have a question. Is there a way to make this work with magisk? I'm on nougat and there is no xposed yet for nougat.. Not a request, just a question. Thank you.

Great mode. I inow that there is some time since the first release, but awesome!

In case anyone is wondering, this works on Oreo with Leedroid's ROM and AOSP lockscreen activated.

Broken Link
Could you reupload the .APK file? It doesn't seem to be working anymore.

kilaxsith said:
Could you reupload the .APK file? It doesn't seem to be working anymore.
Click to expand...
Click to collapse
this post is from 2016 . that time android 6 was boarded on htc 10 . not sure if it is going to work on Oreo or android 7. are you using xposed ?

Related

Help writing something with setOnItemClickListener

In my Android app, I have a sound that I want to play when a certain selection has been made from a spinner, but I want it to play the when the user actually makes the proper selection (or just after). My problem is that although the sound does play when they make the correct selection, as long as that selection stays chosen, it also plays every time the app starts up, when it should ONLY play at the time it's chosen. I think I need to change my setOnItemSelectedListener to setOnItemClickListener, but I'm not sure how (still pretty new to java). Can any generous soul out there show me how to change this up (assuming that's how to best solve this problem)?
Here is the code I have now:
Code:
fitnessSpinner = (Spinner) findViewById(R.id.fitness_spinner);
ArrayAdapter adapter4 = ArrayAdapter.createFromResource(
this, R.array.fitness_array, android.R.layout.simple_spinner_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fitnessSpinner.setAdapter(adapter4);
fitnessSpinner.setOnItemSelectedListener(new OnItemSelectedListener()
{
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long i) {
Log.d("test", "p: " + position + " " + i);
if(position == 0) {
//First Entry
MediaPlayer mp = MediaPlayer.create(mContext, R.raw.bowchica);
mp.start();
} if(position == 4) {
MediaPlayer mp = MediaPlayer.create(mContext, R.raw.debbie2);
mp.start();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
I haven't try the below code but you can try it on your own and tell us.
In onCreate() declare MediaPlayer mp;
In every if statement that you use for check insert this code:
Code:
if(mp!=null){mp.release();}
int resid = R.raw.yoursound;
mp = MediaPlayer.create(this, resid);
After that override the methods onPause() and onResume() and insert this:
if(mp!=null){mp.release();}
If it is still playing a sound when you start your app, then you should check your code again if you have set as default option any of your selection options.
I would LOVE to try this out...Unfortunately, I'm way too dumb at this point point ot figure out exactly where those code snippets would go inside of what I already have.
Does anyone have a couple of minutes to show me where it would go?
Below is a sample code. Since i don't know your code I give you a snippet that you should adjust it to your code.
Code:
public class SampleSound extends Activity{
private Spinner fitnessSpinner;
private MediaPlayer mp;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);//here goes your layout
setViews();//here you will set all your views(spinners buttons textviews etc..)
setAdapters();//set your adapters here
setListeners();//
}
private void setListeners() {
fitnessSpinner.setOnItemSelectedListener(new OnItemSelectedListener(){
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long i) {
Log.d("test", "p: " + position + " " + i);
if(position == 0) {
//First Entry
if(mp!=null){mp.release();}
int resid = R.raw.bowchica;
mp = MediaPlayer.create(this, resid);
mp.start();
} if(position == 4) {
if(mp!=null){mp.release();}
int resid = R.raw.debbie2;
mp = MediaPlayer.create(this, resid);
mp.start();
}
}
@Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
}
private void setAdapters() {
ArrayAdapter adapter4 = ArrayAdapter.createFromResource(this, R.array.fitness_array, android.R.layout.simple_spinner_item);
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
fitnessSpinner.setAdapter(adapter4);
}
private void setViews() {
fitnessSpinner = (Spinner) findViewById(R.id.fitness_spinner);
}
public void onResume(){
super.onResume();
if(mp!=null){mp.release();}
}
public void onPause(){
super.onPause();
if(mp!=null){mp.release();}
}
}
I really appreciate the help. I put the code in my routine, but it still plays the sound every time the activity is loaded (as long as the selection in the spinner is correct). It should only play the sound when the correct selection is made.
Any other ideas?
I am sure that your Spinner is set to some value (since you have values to display). Because your Spinner points to a selection (doesn't matter if you have selected or it is selected by default) your sound plays (even when you start the app).
A way to stop the sound playing at start is to declare and an other Item like you did with the previous 4 and set it as default selection of your Spinner.
To sum up:
1.You have to append in R.array.fitness_array an Item (like you did with the previous Items) and give it a name.
2.At the end of method setAdapters() insert this:
Code:
fitnessSpiner.setSelection(5);// or whatever is your selection number
Now it should work but you should know that this is not a good practice and you should try make a ListView or something else.
I'd be happy to change this out to a listview, or whatever would work. I just have to give my user a choice of 4 or 5 items, from which they can choose only one. Something like a drop down box, but in Android, I thought my only option was a spinner. But whatever I use, I have to be able to play a sound when certain items are chosen, but ONLY when those items are chosen, NOT whenever the activity is called up.
Any specific ideas of what I might change to?
What if I had another control like a textview or an edittext (with it's visibility property set to false) that I programatically populated with the users selection (when it's the selection that I want) and then have an OnItemClcickListener set to play the sound?
Could that work?
I will answer from the last to the top of your questions.
1.You can do whatever you want with android. You want TextViews and EditTexts with complex and nested Layouts you can do it. Write services that will communicate with your contacts through a content provider? You can do it.
Write, read and test code. Only this way you will actually learn.
2.Read developer.android.com. Read the android tutorials from there and specifically the Notepad example. You will learn a lot.
A good resource with small examples for ListViews is this.
3.Have you tried the changes I told you from the last post? Did it worked?
Since you just started with android and programming you must first be happy if you have the expected result and then read more to make your code better
Your suggested changes (fitnessSpiner.setSelection(5);// or whatever is your selection number) would stop the sound from playing, but defeat the apps purpose. Every time this activity is loaded, the spinners hit preferences to load the previously stored data. So if I force the spinner to a different selection to NOT play sound when the activity loads, then I would be displaying the wrong data for the user.
Yes you are right. So it is better to make a ListActivity. Read developer.android.com and the link i gave you before. You will be ok with this!
You're using "setOnItemSelectedListener", which sounds like when the app starts, its getting "selected" again.
Have you tried using "setOnItemClickListener" instead?
fitnessSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener () {
public void onItemClicked() {}
};
Lakers16 said:
You're using "setOnItemSelectedListener", which sounds like when the app starts, its getting "selected" again.
Have you tried using "setOnItemClickListener" instead?
fitnessSpinner.setOnItemClickListener(new AdapterView.OnItemClickListener () {
public void onItemClicked() {}
};
Click to expand...
Click to collapse
onClickListener doesn't work for the spinner...I wish it did.
I REALLY need the drop down functionality of teh spinner, so I guess I'm going to try and figure out a way to have an invisible edittext that I set to the spinner selection and then use onClickListener or onChange...

SoundPool Only plays sounds twice and then quits?

For whatever reason, when loading sounds via soundpool, I can get it to work but each sound only plays exactly two times, then it stops playing. I'm using a simple onClickListener so that whenever I hit a button, a sound is played (I am making a drum pad just for practice). Everything seems to run fine, I don't get any force closes or anything, but after I hit each sound twice, it won't play a third time. This is really frustrating me, and I have searched all over the internet and can't find any answers. I am using OGG files fwiw, cause I heard that they work best.
Here is my code
package com.tst.tanner;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
public class sp extends Activity {
private SoundPool soundpsd, soundpbd;
private int soundID, sdsound;
boolean loaded = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View view = findViewById(R.id.bBdrum);
soundpsd = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundID = soundpsd.load(this, R.raw.kickdrum1, 1);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
soundID= soundpsd.play(soundID, 1, 1, 1, 0, 1);
}
});
View sdv = findViewById(R.id.bSD);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
soundpbd = new SoundPool(10, AudioManager.STREAM_MUSIC, 1);
sdsound = soundpbd.load(sp.this, R.raw.sd, 1);
sdv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
sdsound= soundpbd.play(sdsound, 1, 1, 1, 0, 1);
}
});
}
}
Hey
You don't need a new SoundPool object to load different sound files. You can use the same SoundPool object to load multiple small sound files.
First you declare a SoundPool field for your class along with an AudioManager field
private SoundPool soundPool;
private AudioManager audioManager;
Then in the onCreate() method:
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
audioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
You could then make a loadSFX method like this:
public int loadSFX(int raw)
{
return soundPool.load(this,raw,1);
}
The load method returns an int which is kind of like an id for that sound file. You use this id to tell SoundPool to play that sound file. Make sure you keep a seperate int variable to store the ID of each individual sound file.
you could then make a method like below to play your sound file
public int playSFX(int sfxID, int loop, float rate)
{
float v = (float) audioManager.getStreamVolume(AudioManager.STREAM_MUSIC) / (float) audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
return soundPool.play(sfxID, v, v, 1, loop, rate);
}
Note: the int returned by the play method is a streamID( it is NOT the same as your soundID), which you can use in case you want to stop/pause the sound effect while its still playing. Ideally sfx files are too short for you to want to stop and pause them, but if you do need this then you'd have to keep a seperate int for each stream.
What you are doing in your code is:
storing the int returned from the soundPool.load in an int named "sdsound"
and you use this to play it like this
sdsound = soundPool.play(sdsound,...);
so basically you are overwriting the variable that is storing the soundID for your sound file with the streamID. Hence you have lost the ID for your sound file and you wont be able to play it again when you pass it to soundPool.play...
I use a very similar code to what i have shown above to play sound effects in my games. And it has worked well enough for me till now. Please let me know if this works for you.
Thanks for the reply! I'll try to apply what you explained and I'll definitely keep you posted. I am very new to android programming and I think I am jumping ahead of myself trying to develop apps already, when I don't have a great understanding of even basic concepts. I'm going to try to re-learn java/android programming from the start and read this book from cover to cover so I have a clearer understanding of how everything works.
(http://www.amazon.com/Programming-Android-Zigurd-Mednieks/dp/1449389694/ref=pd_sim_b_1)
It's hard not to get excited and start developing an app you've thought about for a long time, but I am getting ahead of myself and it might cause a LOT more frustration in the long run . Anyways, I'm very glad you responded and am confident your method will work, so I can already say thanks in advance .
Edit.
P.S. I was wondering if there is a better website to ask these questions? I asked the same question at stackoverflow but I didn't get an answer; I'm just wondering what the best place is. Thanks again
Well Stack Overflow is actually a good place to ask questions. Maybe you just needed to wait more, I guess?
The first thing to do before asking on a public forum is check the online reference for the class you are having trouble with:
http://developer.android.com/reference/android/media/SoundPool.html
Its also a good place to learn new stuff you did not think of using before.
If you have any other programming related quesiton you could just send me a PM. I'll do my best to explain it if I can.
Thanks man!
Well, after reading that, it makes no sense at all. I am just in over my head
The codes not working for you?
Did you try the code yet?
Edit:
Oops...Double post...
Hmmm... So this is weird. What I decided to do, was take a look back at your code, and realized an obvious thing that you said. There is no need to have more than one sounPool variable(?). So, thats exactly what I did. Cut out the extra unneeded soundpool variable, and it works! It doesn't crash anymore. I'm not sure why, but I don't understand how your code works, yet I re-did m,y code, applying what you said, and it works all of the sudden. Here is my code if you are curious.
package com.tst.tanner;
import android.app.Activity;
import android.media.AudioManager;
import android.media.SoundPool;
import android.os.Bundle;
import android.view.View;
public class sp extends Activity {
private SoundPool soundPool;
private int soundID, sdsound;
boolean loaded = false;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
View view = findViewById(R.id.bBdrum);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
soundPool = new SoundPool(10, AudioManager.STREAM_MUSIC, 0);
soundID = soundPool.load(this, R.raw.kickdrum1, 1);
view.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
soundPool.play(soundID, 1, 1, 1, 0, 1);
}
});
View sdv = findViewById(R.id.bSD);
// Set the hardware buttons to control the music
this.setVolumeControlStream(AudioManager.STREAM_MUSIC);
// Load the sound
sdsound = soundPool.load(sp.this, R.raw.sd, 1);
sdv.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
soundPool.play(sdsound, 1, 1, 1, 0, 1);
}
});
}
}
Looking back to my first way of doing things :
soundID= soundpsd.play(soundID, 1, 1, 1, 0, 1);
,I don't know how I got any sound to play. I think I was trying to define the variable when it was already defined, which doesn't sound like anything would work.
All I did was delete the "sound ID=" part, and removed the portion where I loaded up soundPool twice. And wahla!
Thanks a lot!
tannerw_2010 said:
Looking back to my first way of doing things :
soundID= soundpsd.play(soundID, 1, 1, 1, 0, 1);
,I don't know how I got any sound to play. I think I was trying to define the variable when it was already defined, which doesn't sound like anything would work.
All I did was delete the "sound ID=" part, and removed the portion where I loaded up soundPool twice. And wahla!
Thanks a lot!
Click to expand...
Click to collapse
I had already pointed this mistake of yours. You weren't redefining a variable, you were only overwriting it with a value you weren't supposed to.
The_R said:
What you are doing in your code is:
storing the int returned from the soundPool.load in an int named "sdsound"
and you use this to play it like this
sdsound = soundPool.play(sdsound,...);
so basically you are overwriting the variable that is storing the soundID for your sound file with the streamID. Hence you have lost the ID for your sound file and you wont be able to play it again when you pass it to soundPool.play...
Click to expand...
Click to collapse
Anyways its great that you got it to work
Edit:
When I said you only need one SoundPool, I did not mean it as a reason for your code not working, but was only pointing out that you were doing something unnecessary

Android/Java Newbie

Hi all, im having a go at developing a simple app. i have little experience with Java and Android development. i have a little test app at the moment and have created a new class, im trying to create a new instance of this class on a button click. it fails to do so, i cant for the life of me see why so.. can someone shed any light on this?
Thanks
Debuging this shows it hitting the "LocationFactory locationf = new LocationFactory();" line and throwing an exception-
"java.lang.NullPointerException"
Main
Code:
package com.example.testapp;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.Menu;
import android.view.View;
import android.widget.Toast;
import java.io.IOException;
public class MainActivity extends Activity {
private static final Context Context = null;
protected static final String TAG = null;
[user=439709]@override[/user]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
[user=439709]@override[/user]
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;
}
public void mainButton(View view) throws IOException {
try {
LocationFactory locationf = new LocationFactory();
Toast.makeText(this, locationf.getAddress(),Toast.LENGTH_LONG).show();
} catch (Exception e)
{
Toast.makeText(this, e.toString(), Toast.LENGTH_LONG).show();
}
}
}
Class
Code:
package com.example.testapp;
import android.content.Context;
import android.location.Address;
import android.location.Geocoder;
import android.location.Location;
import android.location.LocationManager;
import java.io.IOException;
import java.util.Locale;
import java.util.List;
public class LocationFactory
{
private static final Context Context = null;
Geocoder geocoder = new Geocoder(Context, Locale.getDefault());
LocationManager manager = (LocationManager) Context.getSystemService(android.content.Context.LOCATION_SERVICE);
public double Latitude = 0.0;
public double Longitude = 0.0;
public LocationFactory()
{
}
public String getAddress() throws IOException
{
String ReturnAddress = "";
String Address = "", City = "", Country = "";
List<Address> addresses = null;
if(manager.isProviderEnabled(LocationManager.GPS_PROVIDER))
{
// Use GPS Radio Location
Location GPSlocation = manager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
Latitude = GPSlocation.getLatitude();
Longitude = GPSlocation.getLongitude();
}
else
{
// Use Cell Tower Location
Location NETlocation = manager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
Latitude = NETlocation.getLatitude();
Longitude = NETlocation.getLongitude();
}
if(Latitude > 0 && Longitude > 0)
{
addresses = geocoder.getFromLocation(Latitude, Longitude, 1);
if(!addresses.isEmpty())
{
Address = addresses.get(0).getAddressLine(0);
City = addresses.get(0).getAddressLine(1);
Country = addresses.get(0).getAddressLine(2);
}
}
ReturnAddress = Address + " " + City + " " + Country;
return ReturnAddress;
}
}
I don't see anywhere in your code where you are calling the mainButton(View view) method. In the Android lifecycle, the onCreate method is the equivalent of a normal Java program's main() method, which means that code execution begins with the first line of onCreate(). Not knowing what you're trying to do, a good start would be to call your mainButton() method AFTER setContentView() in onCreate().
Side note: your mainButton() method has a View parameter that is never used. Is there a reason for that?
Android activity lifecycle: http://developer.android.com/reference/android/app/Activity.html#ActivityLifecycle
You have to use an intent on that button click, use the method onClickListener and define the intent in the androidmanifest.xml
e.g
Code:
Button button = (Button) findViewById(R.id.[B]button[/B]) // replace latter button with actual id defined in main xml.
button.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View arg0) {
// TODO Auto-generated method stub
startActivity(new Intent("[B]com.example.packagename.CLASSNAME[/B]")); // this should be your own package name.
}
});
Also define this in android manifest under the <application> and </application>
Code:
<activity
android:name=".[B]CLASSNAME[/B]"
android:label="@string/app_name"
>
<intent-filter>
<action android:name="[B]com.example.packagename.CLASSNAME[/B]" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
Change the values of BOLD text according to your own values.
I tried to help you as far as I understood your question. Please let me know if you face any problem I would be more than happy to help you. Rest I am also in the learning phase so you can always PM me if you face any problem.
Hit thanks if I have helped you in any way.
coolbud012 said:
You have to use an intent on that button click, use the method onClickListener and define the intent in the androidmanifest.xml
Click to expand...
Click to collapse
Nope! He didn't say that he wanted to launch a new Activity when the button is clicked. He wants to create a new instance of his LocationFactory Class.
jpepin said:
Nope! He didn't say that he wanted to launch a new Activity when the button is clicked. He wants to create a new instance of his LocationFactory Class.
Click to expand...
Click to collapse
Oops yeah right read that now...I thought he want to start an activity... Anyways tried to delete my reply but not getting an option to delete.
There are many flaws in his code. And the other thing is if its his first app and if he has low level of programming experience then according to me it would be a next to impossible app for him, as per his code and what he is trying to implement.
I think he should rather start up with small apps, understand things and then move on to complex apps.
P.S - its just my opinion
Click to expand...
Click to collapse
Agreed that he should start small...which is exactly why your suggestion for creating and handling Intents makes no sense. Before that, he should first understand the activity lifecycle. Until then, he can just stick to trivial single-activity apps to gain experience.
OP: This code should be placed in the onCreate method:
Code:
Button button = (Button) findViewById(R.id.your_button_ID_here)
button.setOnClickListener(new View.OnClickListener() {
[user=439709]@override[/user]
public void onClick(View arg0) {
mainButton(); // get rid of the View parameter in this method...it's not needed
}
});
This will cause a new instance of your LocationFactory to be created, and will also cause your Toast message to be displayed.
thanks for the replies. yes you are right in that i am inexperienced, but this is just a test app for me to play around with and learn on. i tend to learn better by doing rather than constantly reading. thanks for your suggestions ill look into them
osmorgan said:
thanks for the replies. yes you are right in that i am inexperienced, but this is just a test app for me to play around with and learn on. i tend to learn better by doing rather than constantly reading. thanks for your suggestions ill look into them
Click to expand...
Click to collapse
I also believe in the same, I also keep on doing experiments and testing things out.
What I would suggest is that start with a small app and understand the insights on how android works and all...
Thanks
Potential Solution
Alright, I think I've found your problem. Have a look at where you define your variables in your LocationManager class:
Code:
private static final Context Context = null;
Geocoder geocoder = new Geocoder(Context, Locale.getDefault());
LocationManager manager = (LocationManager) Context.getSystemService(android.content.Context.LOCATION_SERVICE);
This is your problem:
Code:
Context Context = null;
If your context is null, and you use it to create a geocoder and call Context.getSystemService, you'll hit a null pointer. You're trying to access an object (the Context) that doesn't even exist
I'd recommend you pass the context in the LocationManager constructor and then instantiate your objects there. That's standard java procedure.
Code:
private Context mContext = null;
Geocoder geocoder = null;
LocationManager manager = null;
public double Latitude = 0.0;
public double Longitude = 0.0;
public LocationFactory(Context context)
{
this.mContext = context;
this.geocoder = new Geocoder(context, Locale.getDefault());
this.manager = (LocationManager) Context.getSystemService(android.content.Context.LOCATION_SERVICE);
}
I also renamed Context to mContext - it's generally a good idea to keep the instance's name separate from the class name.
Try that - it should work. Please feel free to ask any more questions - this is how I learned, and I think it's the best way!

Is my Dialogbox correct?

Hey guys, im on the process of building my app. Basically, so far, this is what should happen:
The user presses a button and a dialogbox appears. In the dialogbox, the user selects the days in which the reminder shall remind him. (If that makes sense...).
This is my Java code:
Code:
package yCM.medireminder;
import java.util.ArrayList;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class ADD extends Activity implements OnClickListener{
private AlertDialog.Builder dialogBuilder;
private void days()
{
//Declaring the Variables
final ArrayList weekdays = new ArrayList();
dialogBuilder = new AlertDialog.Builder(this);
final String[] dayss = {"Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday", "Sunday"};
//Starting the Process
dialogBuilder.setTitle("Check the Days");
dialogBuilder.setMessage("Check the days when you will shall be reminded");
dialogBuilder.setMultiChoiceItems(dayss, null, new DialogInterface.OnMultiChoiceClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface dialog, int which, boolean isChecked) {
// TODO Auto-generated method stub
if(isChecked){
weekdays.add(which);
}else if(weekdays.contains(which))
{
weekdays.remove(Integer.valueOf(which));
}
}
});
dialogBuilder.setPositiveButton("OK", new DialogInterface.OnClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Your days have been set", Toast.LENGTH_LONG);
}
});
dialogBuilder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
[user=439709]@override[/user]
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "Your days have not been set", Toast.LENGTH_LONG);
}
});
// OUTPUT
AlertDialog dayDialog = dialogBuilder.create();
dayDialog.show();
}
Does everything look alright, the emulator isn't working for my and I have misplaced my phone (its on silent) so I have no way of checking whether it will work. I have linked the dialog box to a button so Im sure that when the button is clicked, the dialog box should appear.
It is recommended to use DialogFragments instead of simple dialogs. That way the system would orientation changes etc. for you.
However, you would need to convert your Activity into a FragmentActivity.
nikwen said:
It is recommended to use DialogFragments instead of simple dialogs. That way the system would orientation changes etc. for you.
However, you would need to convert your Activity into a FragmentActivity.
Click to expand...
Click to collapse
I do plan on making the app on permenantly on portrait mode so I don't think the orientation would be any problem even when the user switches to landscape.
Why would I need to convert my activity to fragmentactivity? What does this do? Also my min sdk is set to api level 8 (2.2 i believe).
Other than that, does the code seem alright?
TwilightLoz said:
I do plan on making the app on permenantly on portrait mode so I don't think the orientation would be any problem even when the user switches to landscape.
Why would I need to convert my activity to fragmentactivity? What does this do? Also my min sdk is set to api level 8 (2.2 i believe).
Other than that, does the code seem alright?
Click to expand...
Click to collapse
You just need to do this if you want to use the DialogFragment. For the normal dialog you don't need it.
You can use fragments on Android 1.6+ if you include the supportv4 library.
Try it. That's all I can say. There might be other things that make it crash, apart from that code. Maybe related to the Android lifecycle.
thanks for the info. Just runned it, everythings working smooth for now. Any idea on how to make the android system recognise/link the days of the week on my string? I did have a read through this but didn't quite understand it. I have a feeling that I should be using something got to do with the calender though...
http://developer.android.com/reference/java/util/Date.html#getDay()
TwilightLoz said:
thanks for the info. Just runned it, everythings working smooth for now. Any idea on how to make the android system recognise/link the days of the week on my string? I did have a read through this but didn't quite understand it. I have a feeling that I should be using something got to do with the calender though...
http://developer.android.com/reference/java/util/Date.html#getDay()
Click to expand...
Click to collapse
You can get the current time using
Code:
System.currentTimeMillis()
Then create a Date from it:
Code:
Date myDate = new Date(System.currentTimeMillis();
Then you can format it using the SimpleDateFormat class: http://developer.android.com/reference/java/text/SimpleDateFormat.html
For some things the Calendar class might be better: http://developer.android.com/reference/java/util/Calendar.html
However, I have not worked with it yet.

[Tutorial] Learn to create a Matrix Effect

Hello,
I create that thread to present you a tutorial learning you how to create a Matrix Effect, also known as Digital Rain Effect, on Android. The tutorial is also available as a Youtube video :
Create a Matrix Effect on Android
Also know as Digital Rain Effect, the Matrix Effect is a classic effect featured in the Matrix series films. A green code is falling and represents the activity of the virtual reality environment of the Matrix on screen. On Android, Matrix Effect has been implemented in various applications often as a Live Wallpaper. In that tutorial, you are going to learn how to create a simple Matrix Effect.
1. Create a Matrix Effect custom View
First, you need to create a custom view named MatrixEffect :
Code:
package com.ssaurel.digitalraineffect;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.view.View;
import java.util.Random;
/**
* Created by ssaurel on 22/09/2016.
*/
public class MatrixEffect extends View {
private static final Random RANDOM = new Random();
private int width, height;
private Canvas canvas;
private Bitmap canvasBmp;
private int fontSize = 40;
private int columnSize;
private char[] cars = "+-*/!^'([])#@&?,=$€°|%".toCharArray();
private int[] txtPosByColumn;
private Paint paintTxt, paintBg, paintBgBmp, paintInitBg;
public MatrixEffect(Context context, AttributeSet attrs) {
super(context, attrs);
paintTxt = new Paint();
paintTxt.setStyle(Paint.Style.FILL);
paintTxt.setColor(Color.GREEN);
paintTxt.setTextSize(fontSize);
paintBg = new Paint();
paintBg.setColor(Color.BLACK);
paintBg.setAlpha(5);
paintBg.setStyle(Paint.Style.FILL);
paintBgBmp = new Paint();
paintBgBmp.setColor(Color.BLACK);
paintInitBg = new Paint();
paintInitBg.setColor(Color.BLACK);
paintInitBg.setAlpha(255);
paintInitBg.setStyle(Paint.Style.FILL);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
width = w;
height = h;
canvasBmp = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
canvas = new Canvas(canvasBmp);
canvas.drawRect(0, 0, width, height, paintInitBg);
columnSize = width / fontSize;
txtPosByColumn = new int[columnSize + 1];
for (int x = 0; x < columnSize; x++) {
txtPosByColumn[x] = RANDOM.nextInt(height / 2) + 1;
}
}
private void drawText() {
for (int i = 0; i < txtPosByColumn.length; i++) {
canvas.drawText("" + cars[RANDOM.nextInt(cars.length)], i * fontSize, txtPosByColumn[i] * fontSize, paintTxt);
if (txtPosByColumn[i] * fontSize > height && Math.random() > 0.975) {
txtPosByColumn[i] = 0;
}
txtPosByColumn[i]++;
}
}
private void drawCanvas() {
canvas.drawRect(0, 0, width, height, paintBg);
drawText();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.drawBitmap(canvasBmp, 0, 0, paintBgBmp);
drawCanvas();
invalidate();
}
}
First, we define the variables for the Matrix Effect with the size of the code, the size of a column, the position of the bottom text for each column and then the caracters that will be used for the code. Note that you can put the caracters you want here or why not a custom font.
In the constructor, we initialize the paint objects. To get view width and height, we override the onSizeChanged method of the View. We initialize the position of the first caracter for each column. We use a random position between the top of the screen and the middle of the screen.
Then, we define a draw text method used to draw a random caracter for each column at the position indicated by txtPosByColumn variable.
The drawCanvas method is used to draw the background of our view and then the code.
Finally, we override the onDraw method of our custom view to call the drawCanvas method and invalidate the view to force a redraw. With that call, the Matrix Effect could progress from top to bottom in infinite mode. Note that it is not the most optimized way to manage a Matrix Effect, but for a tutorial it's sufficient.
2. Define the MatrixEffect View on the Layout
Now, we can define the MatrixEffect View on the layout of the Main Activity.
Code:
<?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"
tools:context="com.ssaurel.digitalraineffect.MainActivity">
<com.ssaurel.digitalraineffect.MatrixEffect
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
3. Set the layout on the Main Activity
Last step is to set the layout as content view for the Main Activity.
Code:
package com.ssaurel.digitalraineffect;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
4. Demo
To enjoy the Matrix Effect, you have just to launch the application on an Android device and you sould see the following effect :
Don't hesitate to give it a try and give me your feedbacks.
Thanks.
Sylvain
Nice work mate, will give it a try.
Sent from my SM-G935F using XDA-Developers mobile app
silverrum said:
Nice work mate, will give it a try.
Sent from my SM-G935F using XDA-Developers mobile app
Click to expand...
Click to collapse
Thanks
I really like this animated wallpaper app.
Thanks for the detailed tutorial, it's always nice to see actually what the code does. :highfive:
In theory, would it be possible to take smali from the app you create and use it to create this effect on the notification screen (for example)?
Seeing the code I’m sure this app drain a lot of battery but it's very nice.
Ticklefish said:
Thanks for the detailed tutorial, it's always nice to see actually what the code does. :highfive:
In theory, would it be possible to take smali from the app you create and use it to create this effect on the notification screen (for example)?
Click to expand...
Click to collapse
Thanks.
Did you mean put the effect on the lock screen ? I thought it was not possible now to put live wallpaper on lock screen. If it's possible it could be great.
slnit said:
Seeing the code I’m sure this app drain a lot of battery but it's very nice.
Click to expand...
Click to collapse
For sure . But life is a choice, like in Matrix films, cool effect on your screen or save your battery lol
sylsau said:
Thanks.
Did you mean put the effect on the lock screen ? I thought it was not possible now to put live wallpaper on lock screen. If it's possible it could be great.
Click to expand...
Click to collapse
Should be possible to pull the AOSP lockscreen source (or the source from OEM variants that are actually available...) and replace the wallpaper code, right?
Interesting , definitely going to try it on Samsung TW later , great idea mate ..
thereassaad said:
Interesting , definitely going to try it on Samsung TW later , great idea mate ..
Click to expand...
Click to collapse
Thanks
Thanks for this tutorial With this I've started learning to work with canvas Nice job!
thereassaad said:
Interesting , definitely going to try it on Samsung TW later , great idea mate ..
Click to expand...
Click to collapse
Me too. hope it works
Excellent work.
rejm0901 said:
Me too. hope it works
Click to expand...
Click to collapse
It works mate ?
Hello! nice tutorial, everything works fine. But I would like to add buttons to change the colors of chars but i'm not able to call the method from the main activity.
I've tried this but giving errors
Code:
MatrixEffect matrixEffect = new MatrixEffect(); // this gives an error : MatrixEffect(Context, AttributSet) in MatrixEffect cannot be applied to ()
final Paint paintTxt = effetMatrix.peindreTxt;
paintTxt.setColor(Color.BLUE);
Is there a way that I can call paintTxt.setColor() to change color?
PS: Im just a noob on programming
esQmo said:
Hello! nice tutorial, everything works fine. But I would like to add buttons to change the colors of chars but i'm not able to call the method from the main activity.
I've tried this but giving errors
Code:
MatrixEffect matrixEffect = new MatrixEffect(); // this gives an error : MatrixEffect(Context, AttributSet) in MatrixEffect cannot be applied to ()
final Paint paintTxt = effetMatrix.peindreTxt;
paintTxt.setColor(Color.BLUE);
Is there a way that I can call paintTxt.setColor() to change color?
PS: Im just a noob on programming
Click to expand...
Click to collapse
If you want to add buttons to change the color of the Matrix Effect, you should work on the layout and add two buttons at the bottom of the screen under the MatrixView. The, you have to change the colors of the Matrix Effect when a user click on one of these buttons.
esQmo said:
Hello! nice tutorial, everything works fine. But I would like to add buttons to change the colors of chars but i'm not able to call the method from the main activity.
I've tried this but giving errors
Code:
MatrixEffect matrixEffect = new MatrixEffect(); // this gives an error : MatrixEffect(Context, AttributSet) in MatrixEffect cannot be applied to ()
final Paint paintTxt = effetMatrix.peindreTxt;
paintTxt.setColor(Color.BLUE);
Is there a way that I can call paintTxt.setColor() to change color?
PS: Im just a noob on programming
Click to expand...
Click to collapse
SOLVED!
---------- Post added at 11:13 PM ---------- Previous post was at 11:05 PM ----------
sylsau said:
If you want to add buttons to change the color of the Matrix Effect, you should work on the layout and add two buttons at the bottom of the screen under the MatrixView. The, you have to change the colors of the Matrix Effect when a user click on one of these buttons.
Click to expand...
Click to collapse
Already solved. I added 4 buttons for 4 colors and i used switch statement
1st, created a custom text color inside MAtrixEffect
Code:
public void setCustomTextColor(int color){
peindreTxt.setColor(color);
invalidate();
then, in mainactivity :
Code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
MatrixEffect matrixEffect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
matrixEffect = (MatrixEffect) findViewById(R.id.arrPlan);
final Button boutton_blanc = (Button) findViewById(R.id.b_bl);
final Button boutton_bleu = (Button) findViewById(R.id.b_b);
final Button boutton_rouge = (Button) findViewById(R.id.b_r);
final Button boutton_rose = (Button) findViewById(R.id.b_ro);
if (boutton_bleu != null) {
boutton_bleu.setOnClickListener(this);
}
if (boutton_blanc != null) {
boutton_blanc.setOnClickListener(this);
}
if (boutton_rouge != null) {
boutton_rouge.setOnClickListener(this);
}
if (boutton_rose != null) {
boutton_rose.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.b_bl:
matrixEffect.setCustomTextColor(Color.WHITE);
break;
case R.id.b_b:
matrixEffect.setCustomTextColor(Color.BLUE);
break;
case R.id.b_ro:
matrixEffectsetCustomTextColor(Color.MAGENTA);
break;
case R.id.b_r:
matrixEffect.setCustomTextColor(Color.RED);
break;
case R.id.b_def:
matrixEffect.setCustomTextColor(Color.GREEN);
break;
}
}
}
Now it works fine and i can change color while a button is pressed
esQmo said:
SOLVED!
---------- Post added at 11:13 PM ---------- Previous post was at 11:05 PM ----------
Already solved. I added 4 buttons for 4 colors and i used switch statement
1st, created a custom text color inside MAtrixEffect
Code:
public void setCustomTextColor(int color){
peindreTxt.setColor(color);
invalidate();
then, in mainactivity :
Code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
MatrixEffect matrixEffect;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
matrixEffect = (MatrixEffect) findViewById(R.id.arrPlan);
final Button boutton_blanc = (Button) findViewById(R.id.b_bl);
final Button boutton_bleu = (Button) findViewById(R.id.b_b);
final Button boutton_rouge = (Button) findViewById(R.id.b_r);
final Button boutton_rose = (Button) findViewById(R.id.b_ro);
if (boutton_bleu != null) {
boutton_bleu.setOnClickListener(this);
}
if (boutton_blanc != null) {
boutton_blanc.setOnClickListener(this);
}
if (boutton_rouge != null) {
boutton_rouge.setOnClickListener(this);
}
if (boutton_rose != null) {
boutton_rose.setOnClickListener(this);
}
}
@Override
public void onClick(View v) {
switch(v.getId()){
case R.id.b_bl:
matrixEffect.setCustomTextColor(Color.WHITE);
break;
case R.id.b_b:
matrixEffect.setCustomTextColor(Color.BLUE);
break;
case R.id.b_ro:
matrixEffectsetCustomTextColor(Color.MAGENTA);
break;
case R.id.b_r:
matrixEffect.setCustomTextColor(Color.RED);
break;
case R.id.b_def:
matrixEffect.setCustomTextColor(Color.GREEN);
break;
}
}
}
Now it works fine and i can change color while a button is pressed
Click to expand...
Click to collapse
Great
This is just the kind of guide that I have been waiting for! I am gonna implement this as a hidden feature in my Rom Control apk. Good lookin on the guide brother

Categories

Resources