editText only writes on one line, scrolls right? - java

I have an editText that fills half the screens width, and all of it's height. When I append text to it the text always starts halfway down the edit text height and when it gets to the edge to the editText it keeps writing, scrolling to the right. I want it to go to a new line, why isn't it? and why does it start half way down? At the moment the text on the left should be replicated on the right. AppendToEMulator writes to the terminal fine, but when i'm ssetting the text in the editText on the right there are no newlines from either the bytes received, probably as I convert it to a string and also none from when the end of the editText is reached, just keeps going right.
<jackpal.androidterm.emulatorview.EmulatorView
android:id="#+id/emulatorView"
android:layout_width="500dp"
android:layout_height="wrap_content"
android:layout_above="#+id/term_entry"
android:layout_below="#+id/deviceConnect"
android:layout_toRightOf="#+id/scrllyout"
android:focusable="true"
android:focusableInTouchMode="true" />
<EditText android:id="#+id/outputBox"
android:layout_toRightOf="#+id/emulatorView"
android:layout_alignParentRight="true"
android:layout_width="200dp"
android:layout_height="fill_parent"
android:background="#ffffff"
android:textColor="#FF043241"
android:inputType="text|textImeMultiLine" />
In Java:
public void onDataReceived(int id, byte[] data) {
dataReceived = new String(data);
dataReceivedByte = dataReceived.getBytes();
statusBool = true;
((MyBAIsWrapper) bis).renew(data);
runOnUiThread(new Runnable(){
#Override
public void run() {
mSession.appendToEmulator(dataReceivedByte, 0, dataReceivedByte.length);
}});
final String ReceivedText = mReceiveBox.getText().toString() + " "
+ new String(data);
runOnUiThread(new Runnable() {
public void run() {
mReceiveBox.setText(ReceivedText);
mReceiveBox.setSelection(ReceivedText.length());
}
});
viewHandler.post(updateView);
}

Answer was that setting an inputType disables wordwrap via:
android:singleLine="true"
Even if I specify
android:singleLine="false"
it was still getting overridden.
As I do not need to type in the editText I simply removed the input type.

Related

TextView in Android doesn't show any text after '/' character

I have a TextView showing the name of a parameter, the value of the parameter and a unit. (Like "speed 5 m/s")
Since I need the value to change it's color i am using a Handler to switch between two Strings. Both of these strings have html-code injected, so i can change the color without the need to have more than one TextView.
My problem now is that if i have a dash ('/') inside my string, all characters after it will not be shown.
If i replace the slash inside my string it works. But that's not really solution.
private void setBlinkText(){
try{
strBlinkOff = "speed <font color='#fafafa'>12</font> m/s";
strBlinkOn = "speed <font color='#212121'>12</font> m/s";
m_displayLine.setText(strBlinkOff.substring(0, strBlinkOff.indexOf('<')));
m_displayLine.append(Html.fromHtml(strBlinkOff.substring(strBlinkOff.indexOf('<'), strBlinkOff.indexOf("font>")), Html.FROM_HTML_MODE_LEGACY));
m_displayLine.append(strBlinkOff.substring(strBlinkOff.indexOf("font>") + 5));
m_blinkHandler = new Handler();
final String strFinalBlinkOn = strBlinkOn;
m_blinkHandler.postDelayed(new Runnable()
{
#Override
public void run()
{
m_displayLine.setText(strFinalBlinkOn.substring(0, strFinalBlinkOn.indexOf('<')));
m_displayLine.append(Html.fromHtml(strFinalBlinkOn.substring(strFinalBlinkOn.indexOf('<'), strFinalBlinkOn.indexOf("font>")), Html.FROM_HTML_MODE_LEGACY));
m_displayLine.append(strFinalBlinkOn.substring(strFinalBlinkOn.indexOf("font>") + 5));
m_blinkHandler.postDelayed(new Runnable()
{
#Override
public void run()
{
setBlinkText(p_strMessage);
}
}, 800);
}
}, 300);
} catch(Exception e)
{
Toast.makeText(getContext(), "Error", Toast.LENGTH_SHORT).show();
}
}
m_displayLine should show "speed 12 m/s" but instead just shows "speed 12 m/"
Edit: The TextView looks like this:
<TextView
android:id="#+id/ToolTextLine2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="12dp"
android:layout_marginEnd="12dp"
android:background="#color/accentGreenLight"
android:fontFamily="#font/lordmeg09"
android:maxLength="16"
android:maxLines="1"
android:paddingStart="12dp"
android:paddingEnd="12dp"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:textColor="#2d373c"
android:textSize="28sp"
app:autoSizeTextType="uniform"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/guidelineTool2" />
It works without the autosizing and a smaller textsize. But why does it get cut off?
Looks like the maxLines attribute was getting ignored cause i was appending the text to my TextView after already setting it. I fixed it by setting
m_displayLine2.setLines(1);
again after appending all parts of my string.
Try using &sol; instead of /

EditText Crashes App When It Set to Empty ( Android )

I have an EditText and when I set it to Empty and Click on my Button, my App crashes.
When I view it in Android Monitor it points to the line:
final int addTm = Integer.parseInt(Teaching);
Here is my code:
LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="2dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_weight="1"
android:text="" />
<EditText
android:id="#+id/tM"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="24dp"
android:layout_marginRight="50dp"
android:ems="1"
android:inputType="number"
android:maxLength="1"
android:text="0" />
<Button
android:id="#+id/submitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit" />
</LinearLayout>
And my Java Code:
Submit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String Teaching = Tm.getText().toString();
final int addTm = Integer.parseInt(Teaching);
FirebaseDatabase database = FirebaseDatabase.getInstance();
DatabaseReference myRef = database.getReference("sub").child("TM");
myRef.runTransaction(new Transaction.Handler() {
#Override
public Transaction.Result doTransaction(MutableData mutableData) {
Integer currentValue = mutableData.getValue(Integer.class);
if (currentValue == null) {
mutableData.setValue(0);
} else {
mutableData.setValue(currentValue + addTm);
}
return Transaction.success(mutableData);
}
#Override
public void onComplete(DatabaseError databaseError, boolean committed, DataSnapshot dataSnapshot) {
System.out.println("Transaction completed");
}
});
}
});
The other answers are good, but I'd recommend wrapping with a try/catch for the NumberFormatException. I know you have the input set to accept numbers only, but always better safe than sorry.
Lowercase the String variable Teaching. In Java we only upper case Type names. (classes, interfaces, etc.) Notice how StackOverflow is highlighting the variable Teaching blue, a bit disorienting no?
Do this for your member fields as well Tm and Submit. They should be written tm and submit. Also, Tm is not a very descriptive name for a variable either. Imagine another programmer coming in and looking at your code, and wondering what a tm is. What is the context of this tm, where does it come from... what does it do? Is it a Teenage Mutant?
Regardless when using Integer.parseInt wrap it in a try/catch:
#Override
public void onClick(View view){
final int addTm;
try {
String teaching = Tm.getText().toString();
addTm = Integer.parseInt(teaching);
} catch (NumberFormatException e) {
addTm = 0;
}
// ...
}
Why should you do this? What if I enter a decimal number into your number input?
Using your accepted answer you will still crash. Integer.parseInt does not parse decimal numbers.
Or how about, if I switch the locale of the device and enter a number with odd characters that Integer.parseInt won't expect.
Gotta catch that exception to be full proof.
When executing Integer.parseInt() on an empty string it throws an NumberFormatException.
First, check the value of Teaching, and verify it's not empty string, or - try/catch for NumberFormatException, and set the value you want for Teaching in that case.

Android TextView with limited amount of items and special functionality

I want to have a multiline text view and adding text to it,
After adding five lines to the TextView I want to start to add to the beginning of the text view.
For example: (numbers as text), 1,2,3,4,5 -> 6,2,3,4,5 -> 6,7,3,4,5 and so on.
I thought about using StringBuilder but I don't see an efficient way to implement this.
The delimiter of each row is "\n\n" maybe it will help to solve the problem.
Or maybe should I just do 5 textView's and have some switch case between them?
Button xml:
<TextView
android:id="#+id/searchesInputTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollHorizontally="false"
android:width="300dp"
android:height="200dp"
android:background="#drawable/border_style"
android:maxLines="5"
android:layout_marginTop="24dp"
/>
in MainActivity.java:
public class MainActivity extends AppCompatActivity {
private TextView searchesTextView;
private ImageButton refreshCurrentLocationButton;
private String myText = "";
refreshCurrentLocationButton = (ImageButton) findViewById(R.id.currentLocationRefreshImageButton);
searchesTextView = (TextView) findViewById(R.id.searchesInputTextView);
refreshCurrentLocationButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Actual code
myText += location.ToString() + "\n\n";
searchesTextView.setText(myText);
});
}

TextView attribute in android for displaying intergers?

public void onClick(View v) {
if (v == button1){
counter++;
textView2.setText(Integer.toString(counter));
textView2.setText(counter);
}
}
I have tried to create a program that counts the number of clicks on a button, but for some reason it is not displaying it. Here is my textview component.
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="180dp"
android:layout_alignParentTop="true"
android:layout_above="#+id/button1"
android:layout_toRightOf="#+id/textView1"
android:layout_centerHorizontal="true"
android:textColor="#000000"/>
I can't seem to find an attribute for display the number of clicks. Is this because it interprets it as an interger instead of a string?
textView2.setText(String.valueOf(counter));
This takes your Integer and converts it to its String value, and sets it to your TextView.
try casting your counter variable to string variable and then try assigning it to button text as:
public void onClick(View v) {
if (v == button1){
counter++;
String s=counter.toString();
textView2.setText(S);
}
}
what your code might be doing as of what you have posted you are trying to pass an integer which is creating problem here..

Android Application Closing upon Button Click (on custom registration page)

Need some help debugging my code. I am very new to the Android SDK and am working on learning it. From what I can glean from several posts on SO and other google search results... I formulated this code.
public class MainMenu extends Activity {
private int str = 8, dex = 8, inte = 8, luk = 8, stats = 20;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
setContentView(R.layout.activity_main_menu);
}
As you can see, I have created a few private variables my app is going to use to store data. In order to interact with these values, I will redraw the TextView each time they are modified. *Feel free to correct me here if this is not an ideal strategy.
public void strup(View view) {
if(stats > 0) {
TextView tv = (TextView)findViewById(R.id.textView4);
TextView st = (TextView)findViewById(R.id.textView18);
str++;
stats--;
tv.setText(str);
st.setText(stats);
} else {
Toast.makeText(this, "Out of stats!", Toast.LENGTH_SHORT).show();
}
I use a button with the following format.
Str [X] [+1]
Where Str [X] is TextView with X a dynamic value.
Also where [+1] is a button with an Onclick function preset by the XML file.
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Str [" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="]" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="strup"
android:text="+1" />
Now for simplicity, I have altered the "android:text="" " lines to reflect their actual values instead of linking to #string/... I don't think this makes of much difference but I wanted to acknowledge it.
So my question is why does my Application crash when I click the "+1" button? All my app is trying to do is to redraw the TextView with a higher value (under str) and a lower value (under stats).
Thanks for any and all help!
You are passing integer value in settext. Either cast integer to string or you can try changing settext like this:
tv.setText(""+str);
st.setText(""+stats);
You tried to create this object:
TextView st = (TextView)findViewById(R.id.textView18);
where is textView18 in the xml?
u can caste using toString method like this:
tv.setText(str.toString());
st.setText(stats.toString());
Try this change the position of your setContentView(R.layout.activity_main_menu);
public class MainMenu extends Activity {
private int str = 8, dex = 8, inte = 8, luk = 8, stats = 20;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setDisplayShowTitleEnabled(false);
}

Categories

Resources