I'm currently creating a soundboard app. I have about 100+ sound files to import.
I have code lines (android:onClick="song1") and (MediaPlayer mysound1).
Just wondering if there is a way to copy+paste these lines and have android studio auto change the line to "song2" and "song3" all the way to "song100"? Same goes for the "mysound1" all the way to "mysound100". I hope I do not have to do it manually :(
Thank you!
The approach you're using seems like a brute force method. There are much more elegant ways to approach this. For one, I would recommend creating dynamic Android components programmatically instead of in XML. Then you could hold all of your elements in a List or a Map, and you could tie them to a generic onClick listener. I'd recommend taking a look around online to figure out how to do some of these things.
But, if you would like to stick with you original method, I don't believe that Android has a way to auto number your click events. You could, however, write code to write your code. Here's a simple example using Java - since you're writing an Android app (utilizing the Eclipse console to print out to):
public class WriteMyCode {
public static void main(String[] args) {
StringBuffer sb = new StringBuffer();
for (int i = 1; i <= 100; i++) {
sb.append("<Button android:id=\"#+id/mybutton\"\n");
sb.append(" android:layout_width=\"wrap_content\"\n");
sb.append(" android:layout_height=\"wrap_content\"\n");
sb.append(" android:text=\"Click me!\"\n");
sb.append(" android:onClick=\"" + "song" + i + "\" />\n\n");
}
System.out.println(sb.toString());
}
}
You can then copy the output from the Eclipse console and paste it into your Android XML resource file.
I went to use Microsoft Excel to auto fill the numbers all the way to 100. Then CONCATENATE the ; to the end of "Mediaplayer mysound1".
Related
Tried to make it short but have to explain in detail. I have one small project where I have one activity and multiple fragments. While launching, first fragment added and works fine. After that when I navigated to another fragment by using replace, another fragment opens but it does not display any UI at all. When i debugged it, then while inflating it says Resource not found without any extra details. While in log it's Invalid ID XXXXXXXXX where XXXXXXXXX is the id number but I am not able to find which id it is because as there is no R file in android studio.
Also tried to use analyze apk but did not find any id with that number there as well with no luck.
Also tried clean build, with no luck.
Please do help here.
I've seen this kind of errors many times here in Stack Overflow. It may caused by passing an invalid/wrong resource id e.g. R.id instead of R.layout but most the time it because of non-existing ids like following example:
int value = 999;
Toast.makeText(context, value, Toast.LENGTH_SHORT).show();
Even though the code compiles just fine, but as you already know it will throw a RuntimeException.
Anyway, you can check your ids with the following code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
StringBuilder sb = new StringBuilder();
Class clazz = R.class;
Class[] classes = clazz.getDeclaredClasses();
for(int m=0; m < classes.length; ++m) {
sb.append("Class name: " + classes[m].getSimpleName() + "\n");
Field[] fields = classes[m].getDeclaredFields();
sb.append("Number of fields: " + fields.length + "\n");
try {
for(int n=0; n < fields.length; ++n)
sb.append(String.format("Field[%d]: %s=0x%08x\n", n+1, fields[n].getName(), fields[n].getInt(fields[n])));
} catch(Exception e) {}
}
Log.d("TAG", sb.toString());
//Toast.makeText(this, sb.toString(), Toast.LENGTH_LONG).show();
}
looks for me like your app doesn't have a layout file. It only shows something if you have a Layout file that tells Android Studio how the fragment is supposed to look like. Make in the Layout folder a document for it and connect it to the fragment.
you can see I have for every single of the classes/Fragnments a Layout file that tells Android Studio how to show every part of the class, shape and color of buttons, TextViews and more.
You then connect it to the class so the class knows where it's layout is
I hope that is going to help you
I am developing an Android Application and I need to display on a TextView special UTF-16LE characters like æ or Œ, or the one in the picture I posted.
I can't understand why everything is displayed correctly on Nougat Emulator and also on an Lollipop and Marshmallow Smartphone while on my Personal Android Nougat I can't see those characters, I can only see ": :" characters instead of the right ones.
(Careful, In the left picture, that ' character is not a normal accent and that I is not a normal I)
Why can't I display those special characters on my Smartphone? How should I proceed with that?
I debugged my application and characters are properly set on my TextView; but still, I can't read them correctly.
It seems that my Smartphone (OnePlus3 on latest Software Updates) can't read UTF-16LE characters, but still, It's really strange for me to face something like that.
Any help would be very appreciated,
Have a nice day.
UPDATE: I downloaded https://play.google.com/store/apps/details?id=org.madore.android.unicodeMap on my Smartphone and it seems that I don't have many of those unicode characters on my Smartphone, is there a way to fix it? Do I just need to install a better font?
I solved my issue, that's what I did. (As usual, thanks also to #Claffolo, my teammate)
I imported a free copyrighted font like Gentium in my android assets.
/assets/fonts/
On my project, I imported the new font and init a variable like that
Typeface plain = Typeface.createFromAsset(getActivity().getAssets(), "fonts/GentiumPlus-I.ttf");
final TextView text_view = (TextView)view.findViewById(R.id.text_view);
text_view.setTypeface(plain);
text_view.setText("[String with special characters]");
When I loaded my ArrayList with all the special characters, let's say my wordlist.
I did it in the following way:
//Loading my Wordlist
BufferedReader line_reader = new BufferedReader(new InputStreamReader(activity.getResources().openRawResource(R.raw.ipautf16le), Charset.forName("UTF-16LE")));
String line;
try {
while ((line = line_reader.readLine()) != null) {
WordList.add(line);
}
} catch (IOException e) {
e.printStackTrace();
}
Collections.sort(WordList);
That's all.
I have a webview in an ios app that basically has no id or class. (I know, right?)
But it does have a textContent field that I would like to use to select elements.
This is the element I want to find:
{"rect"=>
{"center_x"=>307.5,
"left"=>295,
"bottom"=>136,
"right"=>320,
"top"=>93,
"width"=>25,
"height"=>43,
"center_y"=>178.5},
"nodeName"=>"LI",
"id"=>"",
"textContent"=>"!!! I WANT TO FIND IT BY THIS !!!",
"center"=>{"X"=>307.5, "Y"=>178.5},
"nodeType"=>"ELEMENT_NODE",
"webView"=>
"<UIWebView: 0xe2e1400; frame = (0 0; 320 504); clipsToBounds = YES; autoresize = W+H
"class"=>"arrow",
"html"=>"<div class=\"arrow\"></div>"}
So I was able to find this using css pseudo-selectors alá
query("webView css:'el:first-child'")
I can find it by using the hashes in the results array alá
query("webView css:'li'").select {|element| element["textContent"] == "!!! I WANT TO FIND IT BY THIS !!!}
And I can refactor it a bit to use a regex alá
query("webView css:'li'").select {|element| element["textContent"] =~ /I WANT/}
But all this feels really dirty. Very un-Calabashy. Is there a better way to write this?
I have not tried your exact setup. But I do often use queries with the LIKE comparison on label.
Would that solve your problem?
ex.
element_exists("label {text LIKE 'I WANT TO FIND'}")
I wound up going with this:
query("webView css:'TITLE'{textContent CONTAINS ’I WANT’}")
It tends to work more consistently with these particular webviews (given a lack of accessibility labels in the code).
I decided to learn a little bit android progrramming and by using Eclipse + android plug-in I made an android "hello world" application. After that I tryed something simple and used 3 TextViews:
ID:TextView1 String-Value:A=5
ID:TextView2 String-Value:B=10
ID:TextView3 String-Value:A+B=C=
I created them in main.xml menu by drag and drop so, to show them I'm using the flowing line:
setContentView(R.layout.main);
The Question is: I want to get string values of A and B and append the result of 5+10 to the string value of C actualy I did it but it take too much codes just for get str value of a TextView and editing it. Is there any short way like in .Net?
string Str = This.Label1.Text();
And is eclipse is the best IDE or am I should use something else?
When I have several textviews etc, Why am I should write unnecessary lines if there is a short way to do it.
Another Question is I get str-value of B which is 10 but when I tryed the same thing for A it says there is nothing inside VarAStr
*My Codes are:*
setContentView(R.layout.main);
TextView VarAStr = (TextView)findViewById(R.id.VarA);
TextView VarBStr = (TextView)findViewById(R.id.VarB);
TextView VarC = (TextView) findViewById(R.id.VarC);
try{
int VarA=Integer.parseInt(VarAStr.getText().toString().substring(2));
int VarB=Integer.parseInt(VarBStr.getText().toString().substring(2));
VarC.append(String.format("%d", (VarA+VarB)));
}
catch(Exception e){
Toast.makeText(this, e.toString() + " -" + VarAStr.getText().toString() + "-", Toast.LENGTH_LONG).show();//Show();
}
For the last question I found a bad code line in main.xml :) wich has no android:text="#string/VarA" I added this and problem solved.
Thank you for any respond.
You can always put the repeated code on a function that returns a string.
Something like:
public String getTextOfView(int id) {
TextView tmp = (TextView)findViewById(id);
return tmp.getText.toString();
}
Since LogCat truncates long strings, I work around this using FileOutputStream to inspect the contents of very long strings.
It works but it forces me to 'adb pull' that file, which is not very convenient, compare to watching it on LogCat.
Is there any other way in Eclipse to watch very long strings?
For the record, the answer was found here.
when you stop in debug on the variable
do the following and past it to a text file
I think it will be easier to use the eclipse debugging view.
Set a break point at the line where you call Log.* and run your app in debug mode.
When execution reaches the break point the app will ... yes, it will halt.
In the Variables window you may now browse your data and display whatever you need. Copy and paste it at a safe place as well and don't forget to smile :)
What I do, is in "Variables->Change value".
That will show you a Windows with the full text. Then just Copy&paste to notepad.
try this:
public void logLargeString(String str) {
if(str.length() > 3000) {
Log.i(TAG, str.substring(0, 3000));
logLargeString(str.substring(3000));
} else
Log.i(TAG, str);
}
}