Android bug with buttons and editText - android

I have some android code (textView, editText, button) and I add all strings in string.xml. I call these data in this way:
Button vibro;
setVibro((Button) findViewById(R.id.vibro));
vibro.setText(getString(R.string.vibro_button));
vibro = (Button)findViewById(R.id.vibro);
and also I create set and get method for it. This is one part of my main.xml:
<EditText
android:id="#+id/editme"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/username"
/>
<Button
android:id="#+id/buttons"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/bfirst"
/>
and this is one part of my string.xml:
<string name="bfirst">Register</string>
The problem is that I see the buttons and edit text and everything in all my pages. I am sure that I call them in a correct way. But what can cause a problem?

Is setVibro a method?
Also, try to set this line:
vibro = (Button)findViewById(R.id.vibro);
before this line:
vibro.setText(getString(R.string.vibro_button));

Related

Android XML - EditText word-wrapping without newlines

I have this stupid and seemingly trivial problem with the properties of an EditText.
The properties I am trying to achieve for the EditText, are the following:
The contents of the view should NOT contain newlines. It can be text, numbers, symbols, but no newlines.
The soft keyboard should NOT display the enter button because of the above. It should instead display something like "Send" or "Done".
The contents of the view should NOT continue horizontally when reaching the edge of the screen. Instead I want to wrap the text, displaying it on multiple lines.
I have tried many different combinations, but I can not achieve this combination.
What I currently have is this, which is inside a RelativeLayout:
<EditText
android:id="#+id/comment_box"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_below="#id/preparation_text"
android:hint="#string/comment_hint"
android:inputType="textCapSentences|textAutoCorrect|text"
android:scrollHorizontally="false"
android:maxLength="400"
android:imeOptions="actionSend"/>
It achieves 2 of 3. No newlines possible, keyboard displays "Send" rather than the enter-key for me, but the text continues on one line.
Changing inputType="text" to "textMultiLine" wraps text correctly on multiple lines, but also overrides the keyboard to always display the enter button.
I have tried different solutions around the Internet, including setting the properties maxLines="4", singleLine="true" and possible others that I have forgotten again.
I can not find a combination that works.
Output:
Exp:
The op is on right track. I did some research found that some options gets ignored which are specified in XML. but if the same options are set in code then it should do the trick. I used the same XML fragment specified in the question.
<EditText
android:id="#+id/edit_text"
android:layout_width="match_parent"
android:layout_height="80dp"
android:hint="hint"
android:inputType="textCapSentences|textAutoCorrect|text"
android:scrollHorizontally="false"
android:maxLength="400"
android:imeOptions="actionSend"
/>
And by adding the following lines in the code, it helped in achieving what you want.
edit_text.setMaxLines(Integer.MAX_VALUE);
edit_text.setHorizontallyScrolling(false);
textShortMessage is the inputType that you are looking for:
<EditText
android:id="#+id/commentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="100dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:gravity="left"
android:text=""
android:hint="hint"
android:textSize="16dp"
android:textColor="#android:color/black"
android:lineSpacingExtra="0dp"
android:lineSpacingMultiplier="1"
android:background="#android:color/white"
android:selectAllOnFocus="true"
android:inputType="textShortMessage|textMultiLine|textCapSentences"
android:singleLine="false" />
Refere to this to prevent paste function. To prevent pasting a new line.
Please use android:imeOptions="actionSend"
to solve your problem.
I have the same issue a long time ago, you have to programmatically change Edittext property on OnCreate().
So in XML, create your Edittext like this
<EditText
android:id="#+id/comment_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="80dp"
android:hint="Comment here"
android:maxLength="400"
android:inputType="textMultiLine" />
And on onCreate() (I wrote in kotlin not Java)
comment_box.imeOptions = EditorInfo.IME_ACTION_SEND
comment_box.setRawInputType(InputType.TYPE_CLASS_TEXT)

Android TextView doesn't do linebreak in ListView (multiline text)

I have created a 3-level ExpandableListView and have the problem that the TextViews which are used for the 2nd and 3rd level do not support line-breaks if the content is too long. It should be dynamically over more than one line, if needed. The 1st level TextView does it well (automatically) and I actually had the same settings in the xml for all three TextViews. Followed are the layout xmls, the one TextView with the id groupname is for the 2nd level (e.g. the first red X in the picture below) and the one with id childname is for the 3rd level (e.g. the second and third red X in the picture below). It should all be like at the green hook in the picture.
"singleLine=false" seems not to work. Also tried some different options found in other SO posts, but what I've testet haven't worked for me. Like ellipsize, scroll horizontale, different layout_width and so on. The only thing worked is to set a fixed layout_width on x hundred dp, but this is not dynamically, I'm right?
Would be great if anybody could help me with this. Lot of thanks!
Here's a screenshot:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<TextView
android:id="#+id/childname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="60dp"
android:layout_marginLeft="60dp"
android:textColor="#AAAAAA"
android:singleLine="false"
android:text=""
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/groupname"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="45dp"
android:layout_marginRight="60dp"
android:textColor="#555555"
android:singleLine="false"
android:gravity="center_vertical"
android:text=""
android:textAppearance="?android:attr/textAppearanceMedium" />
Add this line in your xml
android:inputType="textMultiLine"
or
Add text using coding like this, where you can add line break using '\n'(But here you have to manually add breaks where you want them)
TextView txt1 = (TextView) findViewById(R.id.childname);
txt1.setText("Hi \nHello \nHow are You");
Results will be
Hi
Hello
How are You
Edit
Accepted Answer - removing the line 'android:layout_alignParentLeft="true"
try using LinearLayout instead of RelativeLayout as parent for the TextView
I had add this attribute to my TextView inside ListView, and makes it do line break correct.
android:maxWidth="xxxdp"
F.Y.R.

onclicklistener textView in android

I have one TextView with two different colors. Here if I click mini it should redirect to one activity, if I click metro redirect to another activity. How can I achieve this?
TextView t = (TextView)v.findViewById(R.id.text);
String text = "<font color=#000000><b>"+"mini"+"</b></font><font color=#000000> added </font><font color=#1569C7>"+"Metro"+"</font><font color=#000000> as a favourite.</font>";
t.setText(Html.fromHtml(text));
What you can do is create an custom class that extends ClickableSpan and manage your clickable text. I had answered the same here.
On your click listener of textview you have to find its text and you can use getText property just compare the text and opens activity as per the text.
I think you can use two textview in linear layout with two different colours and text. Then you can implement onClicklistener() for two text view. You will achieve what you want and it looks same like what you expecting.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/settings"
android:layout_marginTop = "10dip"
android:padding="3dip"
android:text="#string/txt1"
android:textColor="#000000"
android:textSize="25sp"
android:textStyle="bold" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/settings"
android:layout_marginTop = "10dip"
android:padding="3dip"
android:text="#string/txt2"
android:textColor="#1569C7"
android:textSize="25sp"
android:textStyle="bold" />
</LinearLayout>
Hope it helps
In an onclick listener I'd try this:
if (text.equalsIgnoreCase("mini")) {
// call your next activity here
} else if(text.equalsIgnoreCase("metro")) {
// call your next activity here
}
Don't forget to set android:clickable="true" in your XML file of the TextView.
This answers what you want, though keep in mind that it's very hard to get the usability of such a component right. It's very likely that someone somewhere will have trouble clicking the links - especially if the text is on multiple lines and the linked words are close to each other vertically.
I would advice against such a solution.

How to remove the underline from the EditText field in Android?

Whenever a word is typed in the EditText box, I always see an underline under the word being typed. But when I press a space after that word I no longer see the underline.
My reqirement is to remove that underline when the user is typing the message.
Added is the screenshot and we see that Smith is underlined. But I don't want this to happen.
Below is the xml that I use for the AlertDialog box.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/name_view"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:text="#string/alert_dialog_name"
android:gravity="left"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/username_edit"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:scrollHorizontally="true"
android:autoText="false"
android:inputType="textPersonName"
android:capitalize="none"
android:gravity="fill_horizontal"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
android:inputType="textNoSuggestions"
There is a function that removes any composing state. I think if you call it after every time a user types, you will not see the underline. I use it after the user finishes typing, to get the drawing cache of the entire textView (which I need without underline). It's
someTextView.clearComposingText();
You don't have to use any logic to remove the underlines -- just call getText().toString() when you want to use the value. It won't include special formatting or anything.
accepted answer is not given solution, and some other user given answer but no one given full anser, so that's why i write here working solution.
If you want to remove underline then just add below code.
XML
android:inputType="textNoSuggestions"
Java
EditText ed;
ed = findViewById(yourId);
ed.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
If you want to set more than one input type then,
ed.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
May be above information is helpful to others.
The best solution for this is to add on your EditText:
android:inputType="textMultiLine|textVisiblePassword"
Use this from your class, if EditText view is dynamic (created from class file):
EditText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
OR include android:inputType="textNoSuggestions" with your EditText in XML.
Read this if you want to keep emojis and textNoSuggestions doesn't work for you.
textNoSuggestions does not work in every keyboard.
inputType="textVisiblePassword" works but it removes emoji's from most keyboards.
I found that setting inputType="textUri" works and keeps the ability to add emojis.
try:
android:inputType= InputTypes.TextVariationVisiblePassword;
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#null"
android:imeOptions="actionDone"
android:inputType="textNoSuggestions"
android:maxLines="1"
/>

Accessing a TextView from a resource file

Let me first explain what my Androïd application is made of :
- a class extended from an Activity. In the OnCreate member of my class, I try to access a TextView described in my main.xml file by using
"MyTextView=(TextView)findViewById(R.id.myTextView);".
- an xml file where the TextView is described as follows :
<TextView
android:name="#+id/myTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/another TextView"/>
In the R.java file, I can see that my TextView is registered.
My problem is that, when I try to get a handle on the TextView with the findViewById function, I get a null pointer.
It seems a mystery to me because I wrote another application where I was able to access TextViews. And I can't see any difference beetween both applications!!!
Hello,
Here is my complete layout file :
<TextView
android:name="#+id/Titre"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Application GPS :"/>
<TextView
android:name="#+id/NombreMaxSatellites"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:name="#+id/NombreSatellites"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello Android from NetBeans"/>
<TextView
android:name="#+id/TempsAcquisition"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello Android from NetBeans"/>
<EditText
android:name="#+id/Texte"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Le texte"/>
<Button android:id="#+id/BoutonTexte"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dip"
android:layout_marginTop="10dip"
android:text="Terminer"/>
I can access the Button but neither the TextView nor the EditText.
Any idea?
I am sure there are some errors in the layout, due to which your changes for layout were not saved. And then it is not reflecting in R.Java. Look for any red marks in your layout. Resolve the errors before your proceed, then it should work fine. Post your complete layout file.
Make sure that any calls to findViewById() occur after setting the layout with setContentView().
A good practice is to call setContentView() as the first line in your onCreate() method.
Make sure about 2 things
the ID of your textview is not repeated in the same layout
make sure that you are setting the content view (setContentView()) to the correct layout.
The problem is id is assigned to name android:name="#+id/Titre", it should be android:id="#+id/Titre".

Categories

Resources