Is there any way to programatically get the activity name in Android? - java

I have 100+ activities in my Android Studio project. I need to find and replace them with their activity name. For example, my 100+ activities have the same code as this
String activityName = "Example.this";
And now I need to search for and replace the Example part with their activity name.
For ActivityOne I need to achieve this
String activityName = "ActvitiyOne.this";
For ActivityTwo I need this
String activityName = "ActvitiyTwo.this";
I can replace all of them with one word by using Ctrl + Shift + R.
But is there any way to achieve the above or do I need to do it manually?

You can replace them all with ClassUtils.getSimpleName(this) or getClass().getSimpleName()
String activityName = ClassUtils.getSimpleName(this);
or
String activityName = getClass().getSimpleName();

Related

Confused regarding split in Java

I am trying to take a text from a file, and take the a's and b's out using split function.
String inStr = in.readLine();
// for example "a1a1a1a1b"
String lettersStr = letters.readLine();
// for example "ab"
Then i'm doing this trying to split all the letters i want.
Why is this not working?
String outFinal = "\"\\\\s*["+ lettersStr +"]\\\\s*\"";
String[] inSplit = inStr.split(outFinal);
What i'm trying to accomplish is
inStr.split("\\s*[ab]\\s*"));
Which works fine but the problem is that since i'm using a BufferedReader (fileread) the letters to cut out keep changing, hence why i can't just use the line above.
Thanks in advance
Regards
Change
String outFinal = "\"\\\\s*["+ lettersStr +"]\\\\s*\"";
to
String outFinal = "\\s*["+ lettersStr +"]\\s*";

Strings.xml and append

I am playing a bit around with Android after doing a number of tutorials.
I have a textview in which I want to print various lines of text that I have written in the strings.xml file:
<string name="welcome">Welcome! What is your name?</string>
<string name="welcome2">Welcome!</string>
In my main I have:
gamehistory = (TextView)findViewById(R.id.textView2);
gamehistory.setText(R.string.welcome);
This works fine and it displays the text. I want to use the textbox as a "log" so that a bit later in my code it prints welcome2 in the next line.
gamehistory.append("\n" + R.string.welcome2);
Unfortunately, when I use append, it turns the string into a number.
Is there a way to avoid this?
To append i think will not be there but yes you can concatenate like this
String outStr = getString(R.string.first) +
" " + getString(R.string.second);
For Refrence Link to refrence
String welcomestr = Context.getResources().getString(R.string.welcome2)
gamehistory = (TextView)findViewById(R.id.textView2);
gamehistory.setText(welcomestr);
Use the Context.getResources().getString(id) for that.
getReources().getString(R.string.welcome2); // Since you're calling this in your activity itself.
because R.string.welcome2 is an auto generated integer value for your string resource.
Try adding carriage return.
gamehistory.append("\r\n" + R.string.welcome2);
Or add these lines to TextView part of xml:
android:maxLines="2"
android:minLines="1"

Android - String

Probably a noob question but, after some search I could not find the answer.
I've receive this String like the following one: "Distribui\u00e7\u00e3o Alimentar", and I want to set it as text of a EditText. How can I "replace" the \u00e7 and \u00e3o to "ç" and "ã"?
Thanks in advance.
With the string replace function
String newString = stringName.replaceAll("\u007", "c');
String newString2 = stringName.replaceAll("\u00e7", "a");
and you could concatenate the two with the concat method

How do I extract a value from a string in java that has been retrieved from a C# REST service

My problem is when I get a string from the REST service and set it as a string in java. It looks like this
<string xmlns=\"http://schemas.microsoft.com/2003/10/Serialization/\">returnedValue</string>
How should I go about extracting the returnedValue from this?
Your string is just an XML fragment (or document by itself) so to get the value you will need to parse it. You can do so by many different means but for this specific case maybe the best option is to use something like:
String str = // Your string from the REST service
int start = str.indexOf("\">");
int end = str.indexOf("</string>", start);
String returnedValue = str.substring(start + "\">".length(), end);
Other options would be to use a regular expression or an XML parser.

How to change spaces to underscore and make string case insensitive?

I have following question. In my app there is a listview. I get itemname from listview and transfer it to the webview as a string. How to ignore case of this string and change spaces to underscores?
For example: String itemname = "First Topic". I transfer it to the next activity and want to ignore case and change space to underscore (I want to get first_topic in result).
I get "itemname" in webviewactivity and want to do what I've described for following code:
String filename = bundle.getString("itemname") + ".html";
Please, help.
use replaceAll and toLowerCase methods like this:
myString = myString.replaceAll(" ", "_").toLowerCase()
This works for me:
itemname = itemname.replaceAll("\\s+", "_").toLowerCase();
replaceAll("\\s+", "_") replaces consecutive whitespaces with a single underscore.
"first topic".replaceAll("\\s+", "_") -> first_topic
"first topic".replaceAll(" ", "_") -> first__topic
You can use the replaceAll & toLowerCase methods but keep in mind that they don't change the string (they just return a modified string) so you need to assign the back to the variable, eg.
String itemname = bundle.getString("itemname");
itemname = itemname.replaceAll(" ", "_").toLowerCase();
String filename = itemname + ".html";

Categories

Resources