Android Calculator using textview - android

I am trying to build a calculator app. i use textview to recieve the value from button, whenever user click on button. but when user click on the button second time then the value in textview is overwritten by the value of the previous button.
But i want to get the whole value in textview.
Ex: Suppose user click on the 1 then 2,then 3 so number appear in textview will be 123.
So please guide me.

when user click on the button second time then the value in textview is overwritten by the value of the previous button
Use append() instead of setText() to add on to the existing string in your TextView.

To do this you need to add the number to the string in your textview, instead of replacing it. Put the following in you onClick method for each button
String s = textview.getText().toString();
textview.setText(s + "1")

Related

Selecting text in textview from some start to some end

I have an object which stores start and end of the selection made on the textview earlier. I do it by textview.getSelectionStart() and textview.getSelectionEnd().
Now I am in another activity and I have this object and the same textview, I need to programmatically select the text again in order to edit the selection.(I have start and end of the previous selection).
Please help!

How to make a TextView editable at the click of a button?

I have an app that displays certain content from a database and lets users edit it. I have an activity that displays those details in some textviews. Now, there is an "EDIT" button at the bottom of the screen. When this button is clicked, I want to make the textview's editable (which are initially read-only) so that the database can be updated. Also, I want the "EDIT" button to turn into a "SAVE" button. I understand I can just use another activity for this, but is there any way to do this while in the same activity?
EditText is a TextView, what it can do TextView can do better
so suppose you have a TextView
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="i am a text"
android:textColor="#243b03" />
and you want it to be editable by a click of a button; in your onclick add this
TextView.setCursorVisible(true);
TextView.setFocusableInTouchMode(true);
TextView.setInputType(InputType.TYPE_CLASS_TEXT);
TextView.requestFocus(); //to trigger the soft input
they are not static, but i made it static so you know which view object's methods are those. About your Button after the text has been inputted then you change the text to "done" or whatever, Button extends from TextView so treat it as if it was-research on TextWatcher
You'd be better off to switch the TextView to an EditText and toggle it with editText.setEnabled(boolean) to get the effect you want
Making a textview editable has its limitations, such as not being able to modify the data already in it (Check this answer)
You can just use an EditText and use android:inputType="none" in xml or use <your_editText>.setEnabled(false) in your activity/fragment to make it read-only. After the user clicks EDIT, you can make the editText editable by putting the below statements inside the button's onClick method.
<your_editText>.setEnabled(true)
<your_editText>.requestFocus();
Regarding making the EDIT button turn into SAVE, you can put a counter inside your onClick method which will keep track of the order of clicks. After the first click you can change the button text using
<your_button>.setText("SAVE").
On the next click you can execute whatever statements you're using to save data.

Passing text value from one Button to another Button

My application is having two activities. In first activity, I have one button and in second activity I have 3 buttons and wheel view.
If I click the button from first activity it moves to second activity and if it selects any value from wheel view,that current value should be displayed in first button.
Using stored Preferences, it works fine, then if again when I select any value from wheel view that first selected value is displayed in second button and current selected item should be displayed in first button.
Can anyone help?
here,
i am using getter and setter for store the first selected value in setter and get the value into second button, here my code
setSecondBrandValue(btnCurrentSelection.getText().toString());
PreferenceConnector.writeString(WheelActivity.this, itemKeyName,
wheelMenu1[getWheel(R.id.p1).getCurrentItem()]);
and here i set the value to second button
btnSecondSelection.setText(getSecondBrandValue());

Textview Click Issue Android

I have a textview as "emy wattson is a student" and I used marquee property for this textview. Now, it is sliding text. Now, I want to know the clicked word. For example, now I can click the all text and when I want to write the clicked text, I see "emy wattson is a student". But I want to know "emy" , "wattson" , "is" ... seperatly. So, how can I click just word???? Please,help me.
Hmmm...I'm not sure how this would be done. Since a TextView is just an instance of a String, the only way(that I know of) to extract specific values and indexes from String is to use indexOf() and subString() methods. What are you trying to do after you've determined which word in the TextView was clicked?

Is there a way to get the text from a checked radiobutton from a radiogroup?

I have a static radiogroup in my XML layout, but I have created all of the radiobuttons dynamically as an effect of user input. Since none of these have ID's, is there a way I can call a method on a whole radiogroup to just return the text (or label) of the checked radio button? All the labels are URL's so if I can get the URL of the checked radio button then I can pass it to a method i created to open the web browser.
I can use ID's but then I would have to create a bunch of extra text for the radio button labels anyways, then a bunch of if statements with the labels written a second time.. [ if this ID is selected go to this URL, if this ID is open go to this URL...] Where as I just want to take the RadioButton's label and just put it right into the browser method.
I found this post: Android: How to get text of dynamically created radio button selected by the user?
but it wasn't answered correctly.
Thanks in advance.
If IDs are no good for you, maybe you should use the tag property that all Views have, i.e. call radioButton.setTag(url) when you create the button. Then call View.getTag() in your onClickListener and cast to a String to get your URL back.

Categories

Resources