I think the best is to first say, that I recently started Android programming. Despite the fact of me getting better now, I can't seem to find good ways to optimize my piece of code.
I've written this piece of code. It's a soundboard. And when you long click a button you're able to save it as a ringtone, as a alarm, as a notification or share it with your friends. For every method I've made a string.
This string is set by the corresponding button to "btn1" till "btn20". After this I open the method (in the example below showSelectedSaveDialog(). And in that method I've made a if or if else statement to open the correct sound.
This way of coding makes a very long code. Because for every button I have to make an if else statement. Is there a better way to code kind codes? Is there a good tutorial, or something like that? Or someone who can post an example?
Setting the string:
ringToneManager = "btn1";
showSelectSaveDialog();
Setting the correct sound:
if (str.equals("btn1")) {
fIn = getSherlockActivity().getBaseContext().getResources()
.openRawResource(R.raw.sound01);
Starting the method to share the sound file
shareButton("btn14");
Getting the corresponding sound file
private void shareButton(String str) {
// SAVE THE FILE
byte[] buffer = null;
if (str.equals("btn1")) {
fIn = getSherlockActivity().getBaseContext().getResources()
.openRawResource(R.raw.sound01);
[...] etc
Thanks in advance! :)
You can simplify your code by using the "tag" property, available on all Views and widgets. The tag property is a general purpose container.
Load the tag property for each button with the id of the sound file associated with the button, this can be done on an activities onCreate:
findViewById(R.id.btn1).setTag(R.raw.sound01);
findViewById(R.id.btn2).setTag(R.raw.sound02);
//etc.
findViewById(R.id.btn20).setTag(R.raw.sound20);
Each button can now share the same onClick handler and all run the same piece of code, no ifs required:
public void onClick(View arg0) {
fIn = getSherlockActivity().getBaseContext().getResources().openRawResource((Integer)arg0.getTag());
}
Likewise change the shareButton method to take an integer instead of a string:
shareButton((Integer)arg0.getTag());
private void shareButton(int soundID) {
// SAVE THE FILE
byte[] buffer = null;
fIn = getSherlockActivity().getBaseContext().getResources().openRawResource(soundID);
[...] etc
Related
How do I make my Text-To-Speech stop speaking? Right now, my TTS will start speaking when I launch my application, but I'm trying to make it stop speaking when a button on my Main Activity is pressed.
I have tried creating an onStop() method and using .stop, but to no avail.
May I please get some help on this issue?
You could do something along the lines of this ...
stopSpeakingButton.setOnClickListener(new
View.OnClickListener() {
String toSpeak = " ";
textToSpeech.speak(
toSpeaks,TextToSpeech.QUEUE_FLUSH,null);
});
As you gave us no code this a concept , not the most beautiful solution but will work if you adjust for your use case, the idea is to make it read string with onny a space in it!
Is it possible to focus and open up the information just like we do on click using magnet in cardboard android?
Like ray gaze in unity, is there a alternative for android?
I want to do something like the one shown in chromeexperiments
Finally solved!
In treasurehunt sample, you can find isLookingAtObject() method which detects where user is looking...
And another method called onNewFrame which performs some action in each frame...
My solution for our problem is:
in onNewFrame method I've added this snippet code:
if (isLookingAtObject()) {
selecting++; // selecting is an integer defined as a field with zero value!
} else {
selecting = 0;
}
if (selecting == 100) {
startYourFunction(); // edit it on your own
selecting = 0;
}
}
So when user gazes 100 frame at object, your function calls and if user's gaze finishes before selecting reaches 100, selecting will reset to zero.
Hope that this also works for you
Hope this helps. (Did small research, (fingers crossed) whether the link shared below directly answers your question)
You could check GazeInputModule.cs from GoogleSamples for
Cardboard-Unity from Github. As the documentation of that class says:
This script provides an implemention of Unity's BaseInputModule
class, so that Canvas-based UI elements (_uGUI_) can be selected by
looking at them and pulling the trigger or touching the screen.
This uses the player's gaze and the magnet trigger as a raycast
generator.
Please check some tutorial regarding Google Cardboard Unity here
Please check Google-Samples posted in Github here
I'm getting some weird effects after I force close my app. When the app is closed with finish(), everything is fine. I have some variables saved in a sharedPreferences so when the app is loaded again, it can restore those variables into the UI. However, if I force close the app and THEN try to continue where it had left off, some variables start "acting funny". By that I mean (in onCreate) I check to see if a string, loaded from the sharedPreferences, equals a value (crunched down version):
String namec;
private static final String TAG = "MyActivity";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//namec was set as "forest" in a previous activity
//which is bypassed if the user selects continue
//from the main menu
SharedPreferences pathtaken = getSharedPreferences("pathtakenpref", MODE_WORLD_READABLE);
namec = pathtaken.getString("namec", "Unknown");
ImageView v1 = (ImageView) findViewById(R.id.pathpic1);
RelativeLayout v2 = (RelativeLayout) findViewById(R.id.pathmain);
Log.i(TAG, "namec= " + namec);
if(namec == "forest"){
v1.setImageResource(R.drawable.forest);
v2.setBackgroundResource(R.drawable.forestrepeat);
}
}
What happens here is namec, does in fact, equal "forest". I send the value to the log and it shows the variable exactly as it should be ("forest"). Yet it won't run the code inside of the if{}. It's giving me nightmares. I've been stuck on this for a week!
In the same code, I load a different set of sharedPreferences (labeled as TRHprefs) and each one of those (6 integers and 3 strings) load up and display just fine. I even add an if{} to test 1 string and 1 integer from TRHprefs... they both came back true.
Q.1: Is there anything that can cause my sharedPreferences xml to become, somehow, corrupted on a force close?
Q.2: Is there a way for me to view the xml file before and after I use force close to help debug the situation. Thanks so much!
Its a String. Try this:
if("forest".equals(namec)){
If you want to compare two String you need to use this:
if(namec.equals("forest")){
v1.setImageResource(R.drawable.forest);
v2.setBackgroundResource(R.drawable.forestrepeat);
}
I have a audio file and I want to read this text file with highlight the text.Can I do this.Please help me.
You might begin by reading some introductory documentation on the Text to Speech the Android class android.speech.tts.TextToSpeech at http://developer.android.com/reference/android/speech/tts/TextToSpeech.html
With this class you may have your app to easily speak from text. You just create an instance of the class, wait for it to complete its initialization, and speak. Something along this, just to get you started.
import com.google.tts.TTS;
...
private TTS myTts;
...
myTts = new TTS(this, ttsInitListener, true);
...
private TTS.InitListener ttsInitListener = new TTS.InitListener() {
public void onInit(int version) {
myTts.speak("Hello world", 0, null);
}
};
Things will get more complex when you try to change the "Hello world" by the actual text read from your text file while highlighting the text on screen. I leave it as an exercise for you.
Can someone point me in the right direction - I'm trying to create an Android Activity that looks like a technical manual that ALSO can take some user input(I know how to do simple buttons etc.) and the user input part can wait until I have a few basic pages.
My goal (if possible) would be to create a text-heavy activity (like a technical manual) but I'm not sure what the best GENERAL method is for doing this.
To start - rather than having multiple activities I want one large activity that a User may be able to swipe through from left to right (Perhaps use ViewFlipper here??)
But how can I make an Activity that looks like a manual or is Text Heavy??
Thanks!
This is what I did with my application's instructions. I started with ViewFlipper that has next and back buttons. Then as the application grew in size, code got messier and there are a lot of formatting that I needed for the text. So I turned to use WebView and just store the text files as raw assets.
WebView manual=new WebView();
manual.loadData(Utilities.getData(this, R.raw.update),"text/html", "utf-8");
getData method:
public static String getData(Context c,int res) throws IOException{
InputStream ins = c.getResources().openRawResource(res);
byte[] buffer = new byte[ins.available()];
ins.read(buffer);
ins.close();
String html=new String(buffer);
StringBuilder buf = new StringBuilder(html.length());
for (char c1 : html.toCharArray()) {
switch (c1) {
case '#': buf.append("%23"); break;
case '%': buf.append("%25"); break;
case '\'': buf.append("%27"); break;
case '?': buf.append("%3f"); break;
default: buf.append(c1);
break;
}
}
return buf.toString();
}
Then you can attach buttons either to the webview or the application in response to user inputs and load new views.
Advantage:
1. Easy to format and integrate with Android. Very quick development.
2. No need to worry about the view flipper bugs that happens sometimes.
3. It will look like you want.
Disadvantage:
1. Doesn't look too much like an Android Application by itself.