How do you display a string on Android? - java

Edit: it turned out the XML Viewer wasn't the emulator, and does not actually run code.
So, I'm starting my first app in Android Studio 1.1.0, and I realized that I can't even manage to display a string (created in the java file).
I did all research I could, but no matter what snippets of code I copy/paste, it never works.
I tried copy/pasting the snippet of code from the first answer here: Android Eclipse: Change the text in the app to a string created withing the program and it doesn't work. The findViewById method is in red, and so is my_text_view :
String displaythisgoddammit = "display this goddammit";
private TextView text = (TextView) findViewById(R.id.my_text_view);
text.setText(displaythisgoddammit);
And my textview (in activity_main.xml) is:
<TextView
android:id="#+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/displaythisgoddammit"/>
It can't find the string, no matter what I do. Apparently I must put it in the strings.xml file, but then how do I edit it...? In the question I linked, the snippet of code apparently worked for a string in the java file.
Here is my main file (I'm not sure if it's really relevant though, it's just snippets of code I copy/pasted): http://pastebin.com/7uuHyJWP I put the string at line 144, should I put it elsewhere?
Some help please? I've been researching for 5 hours but none of the solutions I tried work...

The issue with your code is on the line when you use
TextView text = (TextView) findViewById(id.my_text_view);
It is occuring because when you define a TextView or any other object by it's Id you need to use R.id.my_text_view.
So, to fix it simply change it to
TextView text = (TextView) findViewById(R.id.my_text_view);

In your code please check the reference to the TextView that you are making.
It should be findViewById(R.id.my_text_view);

A few things you should check:
Inside a method, you should not be using the word private, as you are not defining a class level variable.
You need to use R. before the id, to signify you are retrieving a resource.
You need to make sure that the Text View is inside activity_main.xml.
In addition to all of that, you have a lot of things wrong in your pastebin file, I highly recommend you avoid just copy and pasting code, because it is never that simple.
You need to remove all access modifiers that are not at the class level, and clean up snippets like this:
String contentAsString;
public void onCreate;(Bundle savedInstanceState;) {
setContentView(R.layout.main);
super.onCreate(savedInstanceState);
txtView=(TextView)findViewById(R.id.my_text_view);
txtwidth = (TextView)findViewById(R.id.viewwidth);
hello="This is my first project";
mybtn.setOnClickListener(this);
txtView.setText(hello);}
There are brackets and semi colons all over the place, and not to mention this code is written after a return statement, so you won't even hit this line of code.
NOTE
Other things that I've found that are wrong:
You have an extra closing bracket after the DownloadWebpageTask which will cause some compilation errors.
You override onCreate twice in HTTPExample
It looks like the code you're having trouble with should be inside onCreate.
In other words, try something like this.
Also, inside your XML where you have the line android:text="#string/displayThis, the #string is basically saying you want to get the string 'displayThis' from strings.xml. If you don't have, or don't want to put 'displayThis' in strings.xml, you need to remove that attribute and just set the text programatically.

Related

Is it possible to program setText from another Java file in android?

Please understand me and my problem first.(i explain very badly so)
My files are ActivityMain.java And activity_main.xml which are connected to each other (as far as i know), means if I am calling
final TextView helloTextView = (TextView) findViewById(R.id.text_view_id);
helloTextView.setText(R.string.user_greeting);
in (R.id.text_view_id), text_view_id can only be connect to android:id="#+id/text_view_id" which only present it activity_main.xml (As i understand). And correct me if i am wrong if the text_view_id is not present in the activity_main.xml it ll throw an error "NullPointerException"
Now My Question is - there is anyway that i can interchange data without getting NullPointerException ?
What i mean is want to create new ActivityMain2.java, write the javacode in ActivityMain2.java but android:id="#+id/text_view_id" ll be present on activity_main.java.
I tried several ways searched stack overflow but failed. Either i am getting NullPointerException or the text field do not get updated from code.
My skill level : Just a beginer
you'll have to create another
activity2.xml
file in the layout folder, then you put another
android:id="#+id/text_view_id"
file in the layout.
and since you are a beginner i recommend the beginner's course by Google here

Paste into text area

I tried to make my day easier making a program that will register my self into card game tournaments via selenium webdriver. I am beginner so I was thrilled even though it was just basics aka click here, confirm this, write that but at the end I ran into a problem that I am having trouble solving.
At the end you need to submit decks via deck code. The sendKeys function? (not sure if thats the correct wording) does not work since it actually needs to be pasted into the text area. Whenever you try to "just" write into the box it says its invalid deck code.
driver.findElement(By.xpath("//*[#id=\"react-root\"]/div/div/div[2]/div/div[2]/div/div[4]/div[1]/div/textarea")).click();
driver.findElement(By.xpath("//*[#id=\\\"react-root\\\"]/div/div/div[2]/div/div[2]/div/div[4]/div[1]/div/textarea")).sendKeys(Keys.chord(Keys.CONTROL + "v"));
I went with this, first line just clicking in to the text area and then trying to paste it in (while I copied the deck code manually, not sure how I will solve this since I need to paste 3 different ones, but that is issue for the future) but the code just wont paste and I have no idea where is the catch. I tried the paste function in different text area with the same settings meaning I have something copied in my clipboard and it worked just fine.
Example of the deck code is
" AAECAZICCiT3A94FrtICv/IC9fwC2KAD+KED9KID/KMDCkBWX/4BxAapogPIogPcogPvogPZqQMA "
I hope I provided everything I should have otherwise let me know and I will gladly post more :) Thanks in advance and I hope someone can help me
This might not work because I don't see you explicitly copying the deck code, as you mentioned you did it manually. I also modified your example to not use Keys.chord, as you might not need them here.
To copy the element:
deckCodeWebElement = driver.findElement(locatorToFindDeckCode);
deckCodeWebElement.send_keys(Keys.CONTROL+'a')
time.sleep(1)
deckCodeWebElement.send_keys(Keys.CONTROL+'c')
Then, to paste:
deckCodeWebElementToPaste = driver.findElement(locatorToFindDeckCodeToPaste);
deckCodeWebElementToPaste.send_keys(Keys.CONTROL+'v')
On another note, I recommend shortening your XPaths to relative notation, as the absolute notation you are using is very brittle and breaks very easy. You want to find elements irrespective of their location in the DOM tree.
You can change this:
//*[#id=\\\"react-root\\\"]/div/div/div[2]/div/div[2]/div/div[4]/div[1]/div/textarea
to just this:
//textarea
You may need to query on something like ID, class, or name if there are multiple textarea elements, but there is no need to start at the root node and use div[2] and div[4] unless absolutely necessary.

How to set masked input to edittext in android? [duplicate]

How to use inputfilter for CNIC i.e #####-#######-# ? Please help me.. I am new to Android and I dont know how to use it?
How to mask input? I went through various sites but couldn't find solution. I am so confused.
Have you tried using Masked EditText ? This is what you want ... :)
If you want a numeric keyboard instead of alphabetical, then comment the following lineof code:
this.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
in the file MaskedEditText.java. Now setting android:inputType = "numeric" will work.
) You can also try the fork of Masked EditText. It's derived but with works with Gradle, has some bugfixes, an example project, and a little bit another behavior :-)
The behavior is also changed:
You can set the flag and the hint will be always visible.
Your pattern is invisible initially and grows automatically as soon, as you type. For instance, in the beginning, it is: "+7(", then you type '999' and it shows you "+7(999)" with another brace. In the original library, AFAIK it works like: "+7( ) - - ", then you type '999' and it transforms into "+7(999) - - " and so on.

How to change text color dynamically matching user input string

I started android apps development today and wanted to make a small app that takes some text as input (a color) and returns the text input with the color chosen by the user.
I started by creating a colors.xml in this fashion:
<color name="white">#FFFFFF</color>
<color name="yellow">#FFFF00</color>
...
Then I created an EditText in the main activity, and an activity for displaying the colored text, which included in the onCreate:
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Create the text view
TextView textView = new TextView(this);
textView.setTextSize(70);
textView.setText(message);
Which worked fine, but I could not manage a way to retreive the text string and match it with the color in colors.xml. I was thinking something on the lines of parameter substitution in BASH, like:
textView.setBackgroundColor(getResources().getColor(R.color.XXXXXX));
But I do not know what to put instead of "XXXXXX", as putting "message" would not work, but putting the direct color name (e.g. red) works, but only for one color.
I am trying to avoid case statements, also because I would like to make it as general as possible for many colors. Can anyone suggest if this is possible, in this way or in another way. I am sorry if this is trivial, but I do not have much confidence with Java.
Thank you in advance.
Look at the documentation of the Color class here.
Basically, if you want to set the color from the message string, you could do:
textView.setBackgroundColor(Color.parseColor(message));
I haven't tested it but it should work. The colors.xml file isn't needed for this.
Read the documentation of the Color class and don't forget to use a try-catch for the IllegalArgumentException that the parseColor() method can throw.
this R.color.XXXXXX is a integer. You need to grab the whole parameter instead of just XXX, basically you can store the colors in a static integer variables and then use them depending on your condition. Hope this helps.

Make EditText field uneditable

How to make EditText Field uneditable over property inputType when
inputType="textPassword"
My problem is that following code line makes the password visible.
password.setInputType(InputType.TYPE_NULL);
Already tried following, but remains editable
password.setEnabled(false)
If you play with :
edittext.setFocusable();
edittext.setFocusableInTouchMode();
You should be able to make the EditText editable/uneditable when you want.
Using this: How to make EditText not editable through XML in Android?
You said that you wanted your field to be editable after some logic. Do this:
KeyListener keyLis = textView.getKeyListener();
textView.setKeyListener(null);
Then, when you want your TextView to be editable again, do this:
textView.setKeyListener(keyLis);
Documentation for TextView class:
Two methods:
getKeyListener()
setKeyListener(KeyListener keyLis)
EDIT
I didn't find any beautiful answers, but I did find a few alternative ways to do this:
To make uneditable
valueText.setEnabled(false);
valueText.setClickable(false);
valueText.setFocusable(false);
And then,
valueText.setEnabled(true);
valueText.setClickable(true);
valueText.setFocusable(true);
You can also change what happens when the text is edited to make nothing happen: Can we have uneditable text in edittext
There are some other very wild solutions, out there. They're not beautiful at all, though.
I recommend using the initial solution, unless somebody finds a quicker way.
Sorry I couldn't find a better way to do this :(

Categories