I have set up some text in a textView to explain the commands that the app responds to but the code gets formated (flattened) no matter what i type for gravity. The text is supposed to look like this :
Commands and functions:
"left"- Sets tv gravity to left and changes text.
"right"- Sets tv gravity to right and changes text.
"center"- Sets tv gravity to center and changes text.
"blue"- sets tColor to BLUE.
"reset"- resets all tv properties to default
"WTF"- randomises tSize and tColour
But instead turns out like this :
And here is my code(TextView is in a linear layout) :
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/tvTextPlayExplain"
android:typeface="serif"
/>
Remove the line android:gravity="center", and for the text string that you input, you should use the character "\n" to make a newline instead of Enter. For example, the string "This is line 1 \n This is line two" will give you output
This is line 1
This is line two
What you can do is declare the text in the string.xml file like this and you will be able to use html tags to make a list and keep the format :
<string name="tvTextPlayExplain">
<![CDATA[
<p>Commands and functions:</p><br/>
<ul>
<li>"left"- Sets tv gravity to left and changes text.</li>
.... and so on
<li>"WTF"- randomises tSize and tColour</li>
</ul>
]]>
</string>
and then use :
myTextView.setText(Html.fromHtml(getString(R.string.tvTextPlayExplain)));
Related
I am trying to display a textual histogram into a TextView in Android Studio but I don't want it to break the line of characters and separate them onto two lines, because it obviously ruins the histogram. I have the text set to resize itself in order to fit into the text box when I have more and more lines, but I want to make the size scale to the longest line specifically, that way they are all on a single line.
Here is what the histogram is showing, rather than adjusting the line size
<TextView
android:id="#+id/histogramTextbox"
android:layout_width="0dp"
android:layout_height="453dp"
android:layout_marginStart="16dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="16dp"
android:autoSizeMaxTextSize="80sp"
android:autoSizeMinTextSize="12sp"
android:autoSizeStepGranularity="2sp"
android:autoSizeTextType="uniform"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/totalRollsText" />
Here is what I have in my textview XML in order to scale the font size.
For whatever reason, you have to set android:maxLines too in order to get the autosizing to work properly (and it's a good idea to use app:autoSize* instead of android:autoSize* to support API levels < 26 or if you are using AppCompat components).
There are a lot of details about getting this right here - some key takeaways in addition to using maxLines are: do not use android:singleLine, and do not use wrap_content for either width or height.
Demo Use
<TextView
android:id="#+id/histogram_text"
android:layout_width="0dp"
android:layout_height="400dp"
android:layout_marginStart="36dp"
android:layout_marginEnd="36dp"
android:maxLines="5"
app:autoSizeMaxTextSize="20sp"
app:autoSizeMinTextSize="4dp"
app:autoSizeStepGranularity="1sp"
app:autoSizeTextType="uniform"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/get_text"/>
If the max lines changes dynamically, you can set it in code instead, like this
private fun expandHistogram() {
var demoHist = "(01): #\n"
demoHist += "(02): ##\n"
demoHist += "(03): ##############\n"
demoHist += "(04): " + "#".repeat(h) + "\n"
demoHist += "(05): ##############\n"
binding.histogramText.text = demoHist
binding.histogramText.maxLines = 5
h += 5
}
Create constraintlayout as parent of a textview. Set your textview width to match constraint. You are good to go.
When adding a field for entering a number(Number widget), the error "No speakable text present at Android Studio" takes off
enter image description here
content_main.xml: enter image description here
activity_main.xml: enter image description here
The problem is you are missing content labeling for the view, you should add content description so the user could simply understand what data he should enter into the view
for example, if you want the user to enter the number of cookies he wants you should add a content description as seen below:
android:contentDescription="Enter How Much Cookies You Want"
You should also add an android:hint so the user would have it in front of them an example of the data you want inputted for example:
android:hint="e.g 5"
So your views XML code should look as follows
<EditText
android:id="#+id/editTextNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number"
android:minHeight="48dp"
android:contentDescription="Enter How Much Cookies You Want"
android:hint="e.g 8" />
The solution is simple you just need to add the text into the hint part.
search hint in search-bar ant type something in hint block.
and hit Enter.enter image description here
The problem is missing constraints. Any view you add in Constraint layout, you must set the margins otherwise you will get those errors and even if your app managed to run, your edit text will not be place properly.
Add this to your editText;
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
Let me know if it worked.
Remember you can twick this to your desired position.
I have a few questions about react-native. I'm developing a calculator. In the calculator there are 2 text-view, responsible for entering the input-line (input is from the buttons) and for displaying the result. I need to press the "=" button to transfer the input-line to the android-module, parse this string and return the numeric result to text-view with the result in result-view. How can I do that?
I read that I need to create a native module, but did not understand how to get the text from the js. And as I still understood, I need a callback to set the text in the view for the result.
My code:
<View style={{backgroundColor:'#282828',height:50}}>
<View style={{flex:1,justifyContent:'center'}}>
<Text style={[styles.resultText,{fontSize:(30-(this.state.result.toString().length))}]}>
{this.state.result}
</Text> //It's a result text-view
</View>
</View>
<View style={{flex:1,flexDirection:'column',justifyContent:'flex-end'}}>
<View style={{flex:1,backgroundColor:'#494949'}}>
<View style={{flex:1,alignItems:'center',flexDirection:'row'}}>
<Text style={styles.formulaText}>
{this.state.formula}
</Text> //It's a input text-view
</View>
</View>//Below is another code
<Button style={styles.equalButton} titleStyle = {styles.titleOperationStyle} onPress={this.onPressSubmitResult} title="="/>//Equals button
Thank you in advance.
P.S. Sorry for my English.
You dont have to call getText setText from Native. All of what you want can be done by React-Native. Code in Js files itself, do your calculations there, update the state and show you result in a Text!
I want show image in ImageView, but don't show it. I use XML code and Java code but don't show. show in simulator but in real devices don't show. tested on LG G2, HTC One X, Samsung Galaxy S3.
my xml code :
<ImageView
android:id="#+id/sms_dialog_header"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:background="#drawable/show_sms_header" />
my java code :
Dialog_Header_Img = (ImageView) findViewById(R.id.sms_dialog_header);
Dialog_Header_Img.setImageResource(R.drawable.show_sms_header);
I used to be separated from each.
Please tell me the solution
First of all you should set image in xml by code:
android:src="#drawable/show_sms_header"
not by android:background, well you can set background to color or other image but your main image you should set by android:src
If you changing something about your image, leave first line in your code that you show, and delete second, if setting image is only thing you set, then you can delete both, because you set source of image in xml.
The "src" attribute should be set to your drawable if you want it to be "centerCrop" correctly.
Check this out :
<ImageView
android:id="#+id/sms_dialog_header"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerCrop"
android:src="#drawable/show_sms_header" />
Make sure that your file, called "show_sms_header", is in your drawable ( /res/drawable) directory.
I have a strange behavior with the use of singleLine=true and ellipsize=start with a Button.
First of all, the declaration of my button :
<Button
android:id="#+id/enterDeparture"
android:layout_width="175dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:background="#drawable/field_button"
android:text="#string/research_enterDeparture"
android:textColor="#drawable/field_button_textcolor"
android:paddingLeft="3dp"
android:paddingRight="3dp"
android:ellipsize="start"
android:singleLine="true" />
With this declaration, no text is displayed inside the button. But if I write Log.d(TAG, "the text is : " + findViewById(R.id.enterDeparture));, the LogCat gives me the correct value...
I tried to set the text programatically, either in the onCreateView() and in the onResume() methods : same behavior. But if I set the text really later of if I put an AlertDialog over my screen, the content come back immediatly...
To finish, if I remove the two lines android:ellipsize="start" and android:singleLine="true", everything is normal : my text is displayed in the first time.
EDIT
I tried to remove the singleLine=true line : the initial content is actually displayed but the ellipsize behavior doesn't work anymore...
So I tried with maxLines=1 : the initial content is displayed, but the "..." are not shown anymore (the content is just truncated).
Try changing the height value:
android:layout_height="wrap_content"
You could be elipsising your text to ... and because the button padding it could be being hidden.
Also you want to try this instead:
Log.d(TAG, "the text is : " + ((Button)findViewById(R.id.enterDeparture)).getText())