Make EditText field uneditable - java

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 :(

Related

How do you display a string on Android?

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.

Android - Formatting text (textView vs button)

I'll try to keep it simple, but it's two part.
In my Android app, I have a textview anf 4 buttons (question and 4 answers). I'm attempting to utilize the html <sub>subscript</sub> and <sup>superscript</sup>. It's working on the textivew (ie. the text is formatted the way I want it to be), but when I try to do the same thing on the buttons in the exact same way, it doesn't format them. X2 just becomes "X2" without superscript.
Why is this the case and how can I get it to work on the buttons like it works on the textview?
If it helps, I noticed something. If I pull the same string to the textview and to the buttons (keep in mind I'm pulling the strings from a variable set in a java structure I made... questions/answers I coded into arrays), the textview will read "Question # 1" and the button will read "QUESTION # 1". So, somehow the button is formatting the text and I wasn't aware of that. Could this be the reason the <sub> html code isn't working on the buttons? What's causing this and how can I fix it?
Thank you.

Changing the font of a text in a JTextPane dynamically

I want to change the certain texts written on a JTextPane dynamically. I have a String array containing the words which should be changed
String ListMethod [] = {"forward", "backward", "left", "right"};
I've gone through some posts and many suggest to use JTextPane or JEditorPane to edit text but most of the answers given work on static text. I want to do it in a way such that as I type "forward" or "backward", etc... in the textpane, it detects this words and changes the color. How can I go about it?
Thanks for your help.
See here how to implement a DocumentListener Value Change Listener to JTextField. The have a look at javax.swing.text.Highlighter and javax.swing.text.HighlightPainter.
You have to capture the appropriate event and perform actions. For example in your case you can create an ActionListener that changes color and use registerKeyBoardAction to attach it on your JTextPane.
Oracle has a good tutorial: http://docs.oracle.com/javase/tutorial/uiswing/events/index.html on event listeners. I suggest you start getting yourself familiar there

Get input values from JComboBox

How can I get the input for an editable JComboBox. When user gives an input to the combo how I can get the input text from it?
You need to get the edited text from the combobox editor via combo.getEditor().getItem().
If you need the text that is selected on a JComboBox and you are sure it's a String and not any other object, just use something like String text = (String)myCombobox.getSelectedItem().
If the thing you have in your Model is other than a String, then you need to cast it to the appropriate class, and then use the toString() method of that object.
If you need more help, you should paste a bit of your code, at least declaration and inicialization of your JComboBox...
Just have a look at the oracle tutorial. They do explain how to handle the common swing components http://docs.oracle.com/javase/tutorial/uiswing/components/combobox.html

How do I add a Button to JxTable?

I guess I would have to use a Highlighter, but I cannot figure it out.
Table Button Column shows one way to do it with a regular JTable. I would assume it also works for a JXTable.

Categories