how to make a part of textview editable - java

First I'm using Anko library, Kotlin, Java in my project.
I have an android app that test skills in French so I want to make a part of Textview editable its something like this (Solo Learn):
https://i.stack.imgur.com/bfric.png
I try :
je mang$ une pomme
then split String with $ and add editText in place of $ but it's not a good idea
using web view also it's not a good idea even its work
thanks in advance.

You can add addTextChangedListener to EditText view and add prefix or suffix, you need to be non-editable.
Check this previous question also ->
Put constant text inside EditText which should be non-editable - Android

Related

Underline text while typing in EditText - Android

I am new to android development and was working on a project which involved in underlining specific words in the text field while typing I found an example on dribble which achieves the same thing:
Here I have the index of the word in the string which I want to underline. Is there any way to achieve this? Thank you
Please Note: I don't want to know about how to find the substrings as mentioned in the video but I want to achieve the same underline animation as shown in the video
Is there any way that we can have a canvas behind the EditText and then have the underline animation there??

How to format xml and java code as text inside TextView

I am creating an Android Tutorial Application. I am planning to use Tabs to display the java and the xml code used for that topic.
I thought of using TextView and applying the Java and XML code as text to it.
However I am failing to format the Java code, plus I am not able to add xml as text at all as it has tags which are not supported.
Below are snaps from another App and the desired output is what I am looking for.
Any help or suggestions are welcomed.
You could display your Code in your TextView with HTML and style/format it with CSS.
Try this,
Suppose you have HTML code like this
<h2>Title</h2><br>
<p>description here</p>
Then in android you can use below code
TextView tvTrial=(TextView)findViewById(R.id.tvTrial);
String html="<h2>Title</h2><br> <p>description here</p>";
tvTrial.setText(Html.fromHtml("<h2>Title</h2><br><p>Description here</p>"));
Now any thing you want to display in you view you can store it the string and set it.

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.

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

How to translate ListView texts?

My android application includes ListView binded with SimpleCursorAdapter to the database. This database contains column, which contains data like
somenameofrow
anothername
thirdname
However instead of directly displaying these texts in the ListView, I would like to read according texts (actually - translations) from the Android resources.
How should I do it?
I think that I can use setViewValue for the same, name in the code below is TextView item, which displays my text:
case R.id.name:
TextView elName = (TextView) view;
elName.setText(getApplicationContext().getString(R.string.));
return true;
I am not sure how to understand what is correct id of the resource I am looking for (probably, cursor.getString(columnIndex) or elName.getText()) and how to get the value?
Upd. What if I exclude names from the database and will use ids instead. Will it help?
If you read the android guide ListView tutorial, you will find:
Note that using a hard-coded string array is not the best design practice. One is used in this tutorial for simplicity, in order to demonstrate the ListView widget. The better practice is to reference a string array defined by an external resource, such as with a resource in your project res/values/strings.xml file
Simply create different string-arrays items in res/values-XX, where XX are the locales you want to translate.
See the dev guide for more details about resources and i18n: http://developer.android.com/guide/topics/resources/localization.html
No you can't, since id's are generated in some basically sem-random way during compilation, so you can't be sure which id will have specific String resource.
I would suggest to use translation from DB or you can read translations from assets

Categories