I want to show a simple java program in my app
when i put the below code in my textview it simply shows it like a paragraph.
How to show a program in my textview like the format given below
public class Main {
public static void main(String[] args) {
System.out.println("Hello, World!");
}
}
Help me :-) please
I dont have any idea how you are trying but refer my answer i think you will get some idea
String firststring = "public class Main {";
String secondstring = "public static void main(String[] args) {";
String thirdstring = "System.out.println("Hello, World!");";
textview.setText(Html.fromHtml(firststring + "<br>" + secondstring+"<br>"+thirdstring));//Your textview
I finally got the answer.
Place texts which you want to look like a program in the Syntax highlighter save it as html and put it on assets folder load the url in webview!
That's what I found on matter:
Syntax Highlighter Link
Related
I have used refactor for copying, moving and renaming but I want to expand my horizon for feature use so I wanted to know what Extract feature does and how it helps to increase productivity and code cleanliness.
"Extract Method" can help you reduce the size of your methods. For example,
public static void main(String[] args) {
System.out.println("Hello!");
System.out.println("My name is Sweeper!");
System.out.println("I write code in Java.");
}
If I select the three lines of System.out.println, and do "Extract Method", and give it a name of selfIntro, I get the following:
public static void main(String[] args) {
selfIntro();
}
private static void selfIntro() {
System.out.println("Hello!");
System.out.println("My name is Sweeper!");
System.out.println("I write code in Java.");
}
Is it necessary to write URL of web-service in each page in android application or any other way where i can save URL once for the whole application.
I have a android application where i am calling web-service . I am new to android so i don't have idea of saving the URL globally. How i can save the URL once in the whole application.
private final String URL = "http://192.1.1.1/Service1.asmx";
Instead of writing in every .java file what else i can do.
Create a class in java and declare a String field like the following in that class,
public class MyConstants {
public static final String Url = "http://192.1.1.1/Service1.asmx";
}
then you can access it globally from any class like the following
String url = MyConstants.Url;
As Url is a static final field of MyConstants class, so you can access it just with the name of the class(without creating the object of the class with new Operator). i.e MyConstants in this case.
For Learning more about static and to see how this thing works, Please refer to this link
final means that the value cannot be changed after initialization, that's what makes it a constant. static means that instead of having space allocated for the field in each object, only one instance is created for the class.
So, static final means only one instance of the variable no matter how many objects are created and the value of that variable can never change.
If you don't want to create a class you can just put it on res/values/strings
<string name="URLWebService">http://192.1.1.1/Service1.asmx</string>
Then in each class you need it do this :
getResources().getString(R.string.URLWebService);
And you can do it directly or just put a
public static final String URL = getResources().getString(R.string.URLWebService);
You can use what you want all are going to work fine.
You can also use the string file in res/values.
You can you like following:
<string name="ws_url">http://192.1.1.1/Service1.asmx</string>
and use it so: String wsUrl = getString(R.string.ws_url)
create a global constant class and save constants there :
public class Constants {
public static final String Url = "http://192.1.1.1/Service1.asmx";
}
And access it anywhere in application by just calling
Constants.Url
Create common class YourGlobalClass:
public class YourGlobalClass{
public static final String URL = "192.1.1.1/Service1.asmx";
}
And wherever you want just call the class name. your variable name
ex. YourGlobalClass.URL;
I can see this is common practice among Android developers.
public final class TasksSample extends ListActivity {
private static final String TAG = "TasksSample";
private void method() {
Log.i(TAG, "message");
}
}
Will it be easier, if I do it this way? I need not to declare TAG for every new class.
public final class TasksSample extends ListActivity {
private void method() {
Log.i(getClass().getName(), "message");
}
}
Rather than writing getClass().getName() at each place where a log is placed in a particular activity, it is always preferred to have a TAG that would represent the name of the activity class.
Why use TAG?
When you are running your application there might be more than one Activity class in it. To distinguish which activity class has logged the information in logcat we use a TAG which of course represents the name of the class.
And the proper way (I am not saying what you have written is wrong) of writing the TAG is:
private static final String TAG = TasksSample.class.getSimpleName(); // and not "TasksSample"
Every previous answer is right, but I just wanna add a little comment.
private static final String TAG = TasksSample.class.getSimpleName();
or
private static final String TAG = "TasksSample"
The latter is used when you use proguard. As you know, proguard obfuscates class names and it affects logs too.
calling a function every time has it's toll and getClass().getName() is calling 2 functions every time you log something into the system (an already long process).
Therefor, it's better to save the tag is a final static String instead of calling the same function over and over again.
Yes its a common practice, and is supported by Google for logging & debugging. If you use getClass().getName() then you have to call getClass().getName() every time, so its a better approach use TAG.
Actually getClass().getName() returns the class name, where TAG represents easy understandable name/identification of your class.
I'm trying to store a create database script (a rather lengthy one) in the strings.xml file (noobie here, haven't figured out a better place to put it yet) it does show up in the generated R class:
public static final class string {
public static final int app_name=0x7f040001;
public static final int create_database=0x7f040002; //this one here
public static final int hello=0x7f040000;
}
but when I try this in the code:
DATABASE_CREATE = R.string.create_database;
'create_database' is not available. Ti's simply not there, I get an error if I try to use it. Any ideas why this is so? Do those strings have length limitations? Can they only consist of a single line?
If that's the case, what's the right place to put my SQL create script?
Thanks for your answers.
R.string.create_database in the generated R is an integer (see your line with the comment). In order to get the string value, you need to call getString(R.string.create_database). See getString(int)
whats the deal with
CharSequence contentTitle = R.string.value;
Error cannot convert from int to CharSequence. Is there a way around this or am i missing something?
i tried
String s = R.string.value + "";
CharSequence contentTitle = s;
it returns integers values.
Any help?
R.string.value is a call to the static field in the class R, which is auto generated by Eclipse and which does a kind of summary of all your resources. To retrieve the string, you need to use :
CharSequence contentTitle = getString(R.string.value);
If you open the R class you will see that it contains only numbers that are references to the compiled resources of your project.
To retrieve the string, you need to use getString(),
but getString() is a method from Context class.
If you want to use this method outside your Activity class, you should get link to your context first and then call:
String s = mContext.getString(R.string.somestring)
R.string.value returns the reference ID number of the resource 'value'. If you look at your R class it will appear as something like this:
public static final class string {
public static final int value=0x7f040007;
}
I've been experiencing issues with referencing the getString() method. The exact error that Eclipse spits at me is:
The method getString(int) is undefined for the type DatabaseHelper.MainDatabaseHelper
After reading for awhile I've figured out that you must reference your application's context to get access to the getString() method. I was trying to create a private SQLDatabase helper class in a content provider, however, that was not allowing me to reference the getString() method. My solution so far is to do something like this:
private class MainDatabaseHelper extends SQLiteOpenHelper {
MainDatabaseHelper(Context context) {
super(context, context.getString(R.string.createRoutesTable), null, 1);
}
public void onCreate(SQLiteDatabase db) {
db.execSQL((getContext()).getString(R.string.createRoutesTable));
}
}
Notice these two context references:
context.getString()
(getContext()).getString()
I don't know if this is the optimal long-term solution but it seems to work for the moment. Hope this helps.
You could use String s = getResources().getString(R.string.value); also.