Android: Short way to call widgets? Is eclipse the best? - android

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();
}

Related

Invalid ID XXXXXXXXX Android studio

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

Possible to auto fill code statements in android studio

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".

Android - Shortname to Unicode from Emojione doesn't work

First of all, I don't know whether I should ask this here or in the repo, but as I saw other questions for other libraries like.. Picasso, so here goes :
I'm trying to implement Emojione into my Android app. I've downloaded a small static class to convert all short name to unicode from Emojione Github Repo.
The problem is, when I try to use it to convert :smile: to unicode..
Emojione.shortnameToUnicode(postMessageText, true); // postMessageText is ":smile:"
It always return.. A square, as if it failed to convert. I tried other short names too like :smiley:, :grinning:, but they also failed. :(something): is really a short name right?
The answer is actually in front of my eyes all the time. It really did convert to unicode but, I might be wrong here, in a font unsupported by Android. So I tried using SpannableStringBuilder to span the emoji part to use emojione-android.ttf
Here's how :
int firstEq = sb.length();
Typeface font = FontCache.getTypeface("emojione-android.ttf", context);
String convertPart = Emojione.shortnameToUnicode(part, true);
sb.append(convertPart + " ");
int lastEq = sb.length();
sb.setSpan(new CustomTypefaceSpan("", font), firstEq, lastEq, Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
And that's how I got the TextView to show emojis.. Just use the font!

text widget integration in android

I want to integrate text widget in my application in android studio. I tried running the samples and they are working fine. Now i want to toast the digital text that appears after user input. I want to save that text somehere. I used this code in OnRecognitionEnd() function
#Override
public void onRecognitionEnd() {
String Result = mEditText.getText().toString();
Log.d(TAG, "result is: " +Result);
}
Now can you kindly tell me I want to display the text that is being stored in “Result” I used multiple ways but it says UNFORTUNETLY THE APPLICATION HAS STOPPED. For Example i simply did this
TextView textElement = null;
textElement.setText(Result);
Please tell me what I am doing wrong what should i do to display it ?
If you do this :
TextView textElement = null;
textElement.setText(Result);
You will try to setText on null. I suppose the error in your logcat is NullPointerException.
You have to instantiate your textElement.
TextView textElement=(TextView)findViewById(R.id.myid);
Where "myid" is the id in your layout.xml.

Robotium for Android - solo.searchText () not working

I'm having a problem with searchText() function used with Robotium.
I'm searching for this string:
<string name="provisioning_wizard_title_2">Service Activation Wizard (Step 2)</string>
I defined it in string xml, and I'm searching it this way:
Activity act=solo.getCurrentActivity();
String string = solo.getString(act.getResources().getIdentifier("provisioning_wizard_title_2", "string", act.getPackageName()));
It fails when I call
assertTrue(solo.searchText(string));
and it fails also if i call:
assertTrue(solo.searchText("Service Activation Wizard (Step 2)"));
even if that is the string I'm seeing in the focused screen activity.
The strange thing is that it works if I use the same string without last character:
assertTrue(solo.searchText("Service Activation Wizard (Step 2"));
and it works if I use
assertTrue(solo.searchText(string.substring(0, string.length()-1)));
I hope someone could help me.
Sorry for my english!
Thank you.
PROBLEM SOLVED
I solved problem thanks to Renas Reda (Founder and maintainer of Robotium).
The problem is due to regex support in searchText(); some characters will trigger it.
Use Pattern.quote(string)
Example: Pattern.qoute("provisioning_wizard_title_2") and eveything works good!
I usually use Solo#waitForText(...) so I make sure I'm not losing some race condition.
Try this:
assertTrue(solo.waitForText( solo.getString(R.string. provisioning_wizard_title_2) );
Make sure you are importing the correct R.java file from your production project (not the test project).
I'm not sure, what string you are fetching. Try to log it:
Log.d("Debug", "String is: " + string);
you should rather call, here may be another issue related to package as activity may be in another package than main package of application:
String string = solo.getString(act.getResources().getIdentifier(act.getPackageName()+":string/provisioning_wizard_title_2"), null, null));
You can also get all visible texts, to make sure, what is on the screen:
for(TextView textView : solo.getCurrentViews(TextView.class)) {
Log.d("DEBUG", "Text on the screen: " + textView.getText().toString());
}

Categories

Resources