Retrieving and sending a Textview value from/to a Server - android

I have an app with a button and also a textview (Numeric ex . 100)
i am click button then increment Textview (value) +5.
I am trying to send the value to Php Table Row and Also Retrieve Table Row Data in Same Text View.
in app was close and Open then fetch PHP Data(server) as per Texview
my_table
id
user_Name
amount
MyActivity.java
public class MainActivity extends AppCompatActivity {
int minteger = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void increaseInteger(View view) {
minteger = minteger + 5 ;
display(minteger);
}
private void display(int number) {
TextView displayInteger = (TextView) findViewById(
R.id.integer_number);
displayInteger.setText("Integer: " + number);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="16dp"
android:orientation="vertical"
android:gravity="center_horizontal"
tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click the plus button to increase integer number" />
<TextView
android:id="#+id/integer_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:textSize="30sp"
android:text="Integer: 0" />
<Button
android:id="#+id/increase"
android:onClick="increaseInteger"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="INCREASE" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom"
android:textSize="20sp"
android:text="viralandroid.com"/>
</LinearLayout>

Initialize the TextView & your Button inside onCreate first and remove the initialization from display method.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
displayInteger = (TextView) findViewById(
R.id.integer_number);
Button myButton = (Button) findViewById(
R.id.yourButtonId);
// Here you'll need onClickListener for the button to handle if button clicked, increase the number and etc or calling methods.
}
Follow Android Button Onclick for handle Android Button click.
But, the point is, if you add onClickListener to the Button, you may call display or just the setText inside onClickListener and no need for another method.(Simplifying)
Also add:
TextView displayInteger;
In above of onCreate to have access all over the Activity and the current class.
To send string to server side, follow this link : How send data to website by using android app
You'll need to first get the text by getText().toString() then sending as string to server: How to get EditText value and display it on screen through TextView?

Related

How to Move cursor from one EditText to another one and back to the first edit text if a custom button is clicked?

i am basically developing a small mathematics app, in a activity their will be problems like additions subtractions etc. the user has to fill the answers in edittext from the custom buttons from 0-9, a dot, a slash button and a backspace button which i created on the same activity. now i like to add up and down button, so that when the up button is pressed the cursor has to move towards upside edit text and vice versa.
here is a sample code which i used
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#android:id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<EditText
android:id="#android:id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Some string of text"
/>
<Button
android:id="#android:id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button"
/>
</LinearLayout>
Class:
public class Example extends Activity {
TextView et1;
TextView et2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
et1 = (EditText) findViewById(android.R.id.et1);
et2 = (EditText) findViewById(android.R.id.et2);
Button button = (Button) findViewById(android.R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Selection.setSelection((Editable) et2.getText(), et1.getSelectionStart());
et2.requestFocus();
}
});
}
}
You could create an array of of your EditTexts and a variable containing your current position, defaulted to 0, meaning the first text box. Then if the user presses the up button, if the variable is greater than 0, set the position -1 and then get the textbox object from the array and call focus(). Below is an example piece of code, its not accurate but should get you started
List<EditText> textfields = null;
int currentTextFieldPosition = 0;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
textfields = new ArrayList<EditText>();
textfield1 = (EditText)findViewById(R.id.textfield1);
.....
textfields.add(textfield1);
......
}
protected OnClickListener mBtnUpClickListener = new OnClickListener()
{
public boolean onClick()
{
if (currentTextfieldPosition > 0)
{
currentTextfieldPosition--;
textfields.get(currentTextFieldPosition).focus()
}
}
}

Programmatically adding EditText box

I have the following text with me.
"I drink tea and coffee". Now the requirement is to change this text to "I drink EditText AND EditText"....Here the EditText is a edit box where in the user can enter answers once it is clicked. I need to make this change pro grammatically.
Any suggestions on this, as to how this can be achieved????
You could use the following code in button's click event handler:
String str = "I drink tea and coffee";
String editTextString = editText.getText().ToString();
str.replace("coffee", editTextString);
str.replace("tea", editTextString);
You can ctreate activity structure in your layout xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="I drink " />
<Button
android:id="#+id/firstAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" and " />
<Button
android:id="#+id/secondAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="EditText" />
</LinearLayout>
And set listners to your buttons
private String[] answers = { "tea", "coffee", "juice", "compote" };
...
Button firstAnswer = (Button) findViewById(R.id.firstAnswer);
firstAnswer.setOnClickListener(new OnClickListener() {
private int position = 0;
#Override
public void onClick(View view) {
((Button) view).setText(answers[position]);
position++;
if (position == answers.length)
position = 0;
}
});
By using getText()
Example
Button mButton;
EditText mEdit;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mButton = (Button)findViewById(R.id.button);
mEdit = (EditText)findViewById(R.id.edittext);
mButton.setOnClickListener(
new View.OnClickListener()
{
public void onClick(View view)
{
Log.v("EditText", mEdit.getText().toString());
}
});
}
after that
mEdit.setText("Tea");
EditText et=(EditText)findViewByID(R.id.yourId);
Button bt=(Button)findViewById(R.id.yourBtId);
bt.setOnClickListener(
new View.setOnClickListener()
{
public void onClick(View v)
{
et.setText("You Drink EditText");
}
});
Put These code in onCreate() method
Use ViewSwitcher. This widget displays one of two views it contains:
<ViewSwitcher
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</ViewSwitcher>
And then, when button is pressed, switch it and load text from EditText to TextView or vice versa:
editText.setText(textView.getText());
viewswitcher.showNext();
or
textView.setText(editText.getText());
viewswitcher.showPrevious();
Refer to this for details.
You can use 4 textboxes with texts "I drink" ,"tea" , " and ", "coffee" respectively.
On the ontouch event of the 2nd and 4th textbox, you can display a textbox or edittext and edit the text. On a button click you can get the texts from the textboxes and display it again.
Just Use this : yourTextField.setText("..."+yourEditText.getText().toString());

how to click on textview in android

Click on text view is not working
My xml is
<TextView
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="5dp"
android:layout_weight="3"
android:gravity="center"
android:textColor="#color/white"
android:text="click to download sevenstepstocollege"
android:textSize="15dp"
android:onClick="downloadLink"
android:clickable="true">
</TextView>
and My Activity code is
public void downloadLink(View v)
{
//String requestId = PurchasingManager.initiatePurchaseRequest(skuKye);
//String requestId=PurchasingManager.initiateItemDataRequest("DeveloperSKU-1234");
skuSet.add(skuKye);
final String requestId = PurchasingManager.initiateItemDataRequest(skuSet);
}
But it is not working.I am not able to click that link.please guide me
Well, I use the following code to make a TextView clickable.
First, add this at your activity.xml, to make TextView clikable:
<TextView
android:id=android:id="#+id/button2" <!-- id to get this TextView -->
(...)
android:onClick="onClick" <!-- Add the function onClick() -->
android:clickable="true" <!-- Set the boolean clickable to true -->
/>
Then, at your MainActivity.java, you add:
private TextView textview;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Get the TextView by id
textview = (TextView) findViewById(R.id.button2);
textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO when clicked on your link(TextView)
}
});
}
As I see now that you are trying to make a link from the TextView clickable, not just be able to click on the TextView I will let a link below to a similar question solved in Stack Overflow that you might find helpful.
The android developer reference to TextView:
https://developer.android.com/reference/android/widget/TextView.html
The similar question in Stack Overflow about how to make a links in a clickable that might be helpful:
How do I make links in a TextView clickable?
You may use like this
<TextView
android:id="#+id/topPaid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoLink="all"
android:clickable="true"
android:text="#string/topPaid"
android:textColor="#000000"
android:textColorLink="#33CC33" />
and At activity
TextView topPaid = (TextView) findViewById(R.id.topPaid);
Linkify.addLinks(topPaid, Linkify.ALL);
topPaid.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//write ur logic
}
}
TextView textview = (TextView) findViewById(R.id.text);
textview.setText(Html.fromHtml("<b>Text:</b> Text with a " + "link " + "Created in the Java source code using HTML."));
XML Code
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
JAVA Code
TextView textView = (TextView) findViewById(R.id.textView1);
textView.setText(Html.fromHtml("google "));
textView.setMovementMethod(LinkMovementMethod.getInstance());
In your XML as the following:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:onClick="onTextViewPressed"
/>
In your attached activity
public void onTextViewPressed(View view) {
...
}

use checkbox to send textview value to another activity using image button onclick

i am making a program, in which i am using 3 checkboxes for the item variations along with 3 textviews for their prices, now i want to know, whenever user will click on checkbox1 then price appears in textview1 send to next activity, like in second activity you have selected : checkbox1 $2.00,i want to do this using image button for add to order, please write some brief code how is it possible for me. Here i am placing main.xml code:-
<TextView
android:id="#+id/regular"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/var1"
android:layout_marginLeft="40dp"
android:text="$2.00"
android:textColor="#343434"
android:textSize="15dip" />
<CheckBox
android:id="#+id/var2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/cost"
android:layout_toRightOf="#+id/var1"
android:text="Small" />
<TextView
android:id="#+id/small"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/var2"
android:layout_toRightOf="#+id/var1"
android:layout_marginLeft="40dp"
android:text="$1.00"
android:textColor="#343434"
android:textSize="15dip" />
<CheckBox
android:id="#+id/var3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/cost"
android:layout_toRightOf="#+id/var2"
android:text="Large" />
<TextView
android:id="#+id/large"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/var3"
android:layout_toRightOf="#+id/var2"
android:layout_marginLeft="40dp"
android:text="$3.00"
android:textColor="#343434"
android:textSize="15dip" />
<ImageButton
android:id="#+id/add_order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/add_order" />
You need to add an onClickListener to your ImageButton. Now if the onClick method is called retrieve the state of the checkboxes and determine the price of the item.
This price can now be added to the Intent that you use to start the next Activity.
Use the setExtra and getExtra Methods of the Intent class as explained in this Tutorial
Try this code
public class MainActivity extends Activity {
private ImageButton btn;
private TextView txtSmall, txtMed,txtLarge;
private CheckBox chkSmall, chkLarge;
private String strSmall, strLarge;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Checkbox Declaration
chkSmall = (CheckBox)findViewById(R.id.var2);
chkLarge = (CheckBox)findViewById(R.id.var3);
//TextView Declaration
txtSmall = (TextView)findViewById(R.id.small);
txtLarge = (TextView)findViewById(R.id.large);
//ImageButton
btn = (ImageButton)findViewById(R.id.add_order);
//RESET VALUES
strSmall = txtSmall.getText().toString();
strLarge = txtLarge.getText().toString();
btn.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if(chkSmall.isChecked()){
Toast.makeText(MainActivity.this, "SMALL CHECKBOX SELECTED", Toast.LENGTH_SHORT).show();
txtLarge.setText(txtSmall.getText().toString());
}
else if(chkLarge.isChecked()){
Toast.makeText(MainActivity.this, "LARGE CHECKBOX SELECTED", Toast.LENGTH_SHORT).show();
txtSmall.setText(txtLarge.getText().toString());
}
else{
Toast.makeText(MainActivity.this, "RESET Called", Toast.LENGTH_SHORT).show();
txtSmall.setText(strSmall);
txtLarge.setText(strLarge);
}
}
});
}
Here Your XML code set to layout.

Can't get text from edittext?

I have updated to ADT version 20. After that I can't use EditText. For API level 16 it shows some error. For lower API levels there is no error but I can't get the input of EditText. It shows an empty result.
Sample code I used:
java code
public class sample extends Activity {
EditText edt;
Button btn;
TextView txt;
String str;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edt = (EditText) findViewById(R.id.editText1);
btn = (Button) findViewById(R.id.button1);
txt = (TextView) findViewById(R.id.textView2);
str = edt.getText().toString();
btn.setClickable(true);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.d("WordWeb","entered - "+str);
txt.setText("you entered"+str);
}
});
}
}
XML CODING
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Search"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="10pt"
android:text="Button" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
Actually, you are trying to get text from EditText in onCreate() of Activity, which has no any text contains yet.
So Just put the line, str = edt.getText().toString(); in button's click() then you will get the result,
Like,,
btn.setClickable(true);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
str = edt.getText().toString();
Log.d("WordWeb","entered - "+str);
txt.setText("you entered"+str);
}
});
Your statement str = edt.getText().toString(); should be inside the onClick handler.
The code cannot access the value because after onCreate ends the value found in the EditText abd str is beyond the scope. So put the str = edt.getText().toString(); statement inside the onClick so that when user clicks the button it can access the value.
I think you are trying to get EditText (i.e. edt) value in onCreate() and I think you are entering value after the completion of onCreate() method.So that means you are not getting value for your str.
That's why you are not able to set the value to TextView.
just put text inside your button otherwise it will not work becz whatever u are getting in strings is also working like u are just cre

Categories

Resources