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!
Related
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.
Here is the xml (activity_matches.xml) that displays the output in the screen shot below in an EditText object named txaMatches:
<EditText
android:id= "#+id/txaMatches"
android:text= ""
android:scrollbars= "vertical"
android:layout_below= "#+id/pattern"
android:textColor= "#color/yellow_foreground"
android:textSize= "#dimen/matches_text"
android:focusable="false"
android:inputType="textMultiLine"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd= "true"
tools:ignore= "LabelFor"
android:typeface="monospace"
android:focusableInTouchMode="false"
android:paddingEnd="#dimen/matches_padding"
android:paddingStart="#dimen/matches_padding"
/>
I would like the contents of this multiline EditText object to scroll smoothly with acceleration if the user swipes inside it.
Here's how I fill txaMatches (using doInBackground). This is the only place in Java code that I refer to it (of course I also define and initialize it in Java):
protected void onProgressUpdate(String... progress)
{
for (int i = 0; i < progress.length; i++)
if(progress[i].length() > 1) MatchesActivity.txaMatches.append("\n" + progress[i]);
else MatchesActivity.showLetter("Reading " + progress[i].charAt(0));
}
Is this an easy change to be made in xml?
Should I be using a ListView? Should the EditText be inside a ListView?
Am I going to have to write some Java code?
Given the small amount of code I've provided, I don't expect many details. Just an overview of what to Google would be a good start.
Pretty easy (thanks to Fllo's THIRD link). (The first was no help; the second focuses on Java code and the ScrollView class and is more involved than the third, which focuses on xml and was perfect for my situation (and required only changing the type of txaMatches to TextView):
start of new code
<ScrollView
android:layout_alignParentStart="true"
android:layout_alignParentEnd= "true"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
end of new code except for closing tag
<TextView
android:id= "#+id/txaMatches"
android:scrollbars= "vertical"
android:layout_width= "wrap_content"
android:layout_height= "wrap_content"
/>
</ScrollView>
Unsure of how to display text in the xml file, i have code written to output text in the java file but nothing happens when the application is run
TextView tv = (TextView) findViewById(R.id.my_text_view);
tv.setText("Welcome to the Dungeon");
*
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/my_text_view"
android:text="#string/my_text_view"/>
Use this:
<TextView android:id="#+id/my_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Example"
android:layout_gravity="center"
android:textColor="#656565"
android:textSize="20dp" />
And then setText
why are you setting text twice in xml and java, just add once as:
but whatever try to provide : fontsize, and gravity(center) of layout, either programmatically or in xml
I am using a built-in theme for my Android app:
<style name="AppTheme" parent="android:Theme.Black">
<!-- Customize your theme here. -->
</style>
I am happy with that theme, except I want to change the background color of a button. Here is how it looks by default:
Here's what happens when I add a background color to this button (android:background="#color/play_bg"):
Hey!? It basically changed all the button's size, padding and margins!
So I managed to get the expected result using the backgroundTint property (android:backgroundTint="#color/play_bg"):
Unfortunately, this is only supported since version 21 of the API, which is not acceptable for me.
So two questions:
Why does changing the background messes with the rest of the button's properties?
How do I get the expected result without backgroundTint?
And a bonus question: How can I get the expected result programmatically (I have dynamic buttons in my app, so this would be very useful)?
You can change this color in your Java File. When your main class loads you can take an object of this button and then change color.
Here is how you define this button in Manifest file :
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="PLAY"
android:id="#+id/btn1"
... />
Now in your Java file when you are adding this XML layout you need to
Button b = (Button)findViewByID(R.id.btn1);
b.getBackground().setColorFilter(0xFFFF0000,PorterDuff.Mode.MULTIPLY);
You may also use COLOR:
COLOR.RED
The code below sometimes does not work for me :-
b.setBackgroundColor(int color)
In my case I will be doing in this process
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#color/play_as"
android:padding="8dp"
android:text="Button" />
Or you can use this link which is more easy way of creating the buttons
I have an app that I have almost finished which is a scoreboard app. When the user does a LongClick on the score, currently a dialog box appears which shows an EditText field with an OK and CANCEL button. When the user clicks the EditText field a keypad appears (numbers only) in which the user types in the new score, clicks DONE and then the EditText field shows the new value in the dialog box and when the user clicks OK it saves the entered value to the TextView showing the current score.
The problem is that I feel it is cumbersome. Here is what I would like to happen:
User does a LongClick on score
Keypad appears
User enters new score
User clicks OK
Keypad disappears and value is saved to the TextView updating the current score.
In order to accomplish this, I thought of two things:
I could create a new class extending View -- I'm too new to android to really understand this option.
I could replace the EditText in the dialog with a new XML file which is designed like a keypad.
Option 2: I figured this would probably be the easiest so I have designed the XML Layout and called it keypad.xml. Now when the user does a LongClick the keypad.xml file inflates in a dialog with OK and CANCEL. So far so good. However, I don't know where I should put the code for the button actions.
At first thought, I figured that I could setup some onClick listener which is for ALL BUTTONS in the layout. I figured that all buttons are going to perform the exact same function. Basically, get the text from the button (in this case 0-9) and append it to the TextView which is just above the keypad. Then when the user clicks OK save the TextView in keypad.xml to the current score textview.
Can anyone help point me in the right direction to accomplish this please?
Here is the screenshot of keypad.xml (there is an empty TextView above the numbers):
Here is the XML code:
<?xml version="1.0" encoding="utf-8"?>
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:columnCount="3" >
<TextView
android:id="#+id/textView1"
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:gravity="right|center_vertical"
android:textSize="24sp" />
<Button android:text="1" />
<Button android:text="2" />
<Button android:text="3" />
<Button android:text="4" />
<Button android:text="5" />
<Button android:text="6" />
<Button android:text="7" />
<Button android:text="8" />
<Button android:text="9" />
<Button
android:layout_columnSpan="3"
android:layout_gravity="fill"
android:gravity="center"
android:text="0" />
This Question has not seemed to prompt a response. In case anyone is looking for an answer to this, I have come up with another solution which isn't exactly the same but works none the less.
Please see this question.