Android Calculator - Editview cannot input decimal places - android

I am new to Android code development...I am developing a Android calculator apps and does not understand why the two EditTexts (first input and second input) cannot accept decimal places but can only input integers...Here attached as follows are the codes:
Thanks!
=============Main Activity===============================
package com.trial.jm4_calculator;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class MainActivity extends Activity {
private TextView output;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(btn1Listener);
output = (TextView) findViewById(R.id.lblOutput);
}
View.OnClickListener btn1Listener = new View.OnClickListener() {
public void onClick(View v) {
double opd1, opd2;
double result = 0.0;
EditText txtOpd1, txtOpd2;
RadioButton rdbAdd, rdbSubtract, rdbMultiply, rdbDivide;
CheckBox chkDivide;
txtOpd1 = (EditText) findViewById(R.id.txtOpd1);
txtOpd2 = (EditText) findViewById(R.id.txtOpd2);
opd1 = Double.parseDouble(txtOpd1.getText().toString());
opd2 = Double.parseDouble(txtOpd2.getText().toString());
rdbAdd = (RadioButton) findViewById(R.id.rdbAdd);
if (rdbAdd.isChecked()) {
result = opd1 + opd2;
}
rdbSubtract = (RadioButton) findViewById(R.id.rdbSubtract);
if (rdbSubtract.isChecked()) {
result = opd1 - opd2;
}
rdbMultiply = (RadioButton) findViewById(R.id.rdbMultiply);
if (rdbMultiply.isChecked()) {
result = opd1 * opd2;
}
rdbDivide = (RadioButton) findViewById(R.id.rdbDivide);
if (rdbDivide.isChecked()) {
result = opd1 / opd2;
}
output.setText("Answer = " + result);
}
};
}
====================Main.xml===================================
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="First Input: "/>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="number"
android:id="#+id/txtOpd1"/>
</LinearLayout>
<RadioGroup
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
android:id="#+id/rdgOp">
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="+ "
android:id="#+id/rdbAdd"/>
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="- "
android:id="#+id/rdbSubtract"/>
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="* "
android:id="#+id/rdbMultiply"/>
<RadioButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="/ "
android:id="#+id/rdbDivide"/>
</RadioGroup>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal">
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Second Input: "/>
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="number"
android:id="#+id/txtOpd2"/>
</LinearLayout>
<Button
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Compute"
android:id="#+id/button1"/>
<TextView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/lblOutput"/>
</LinearLayout>

If you want to use Decimal Number only on your EditText
use the xml attribute android:inputType="numberDecimal" in your EditText widget your EditText declaration will be like this:
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal" />
If you want to use Signed Decimal Number than combine the two Xml attributes android:inputType="numberDecimal" and android:inputType="numberSigned". Your EditText declaration will be like this:
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="numberDecimal|numberSigned" >
</EditText>

Change android:inputType from "number" to "numberDecimal". See the documentation for even more options for inputType.

inputType="number" doesnt allow floats. try changing:
android:inputType="number"
to:
android:numeric="integer|decimal"

You need to change the input type of your EditText in the XML code.
Change the inputType attribute of the EditText from
android:inputType="number"
to
android:inputType="numberDecimal"
<EditText
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="numberDecimal"
android:id="#+id/txtOpd1"/>

Related

Can't make calculations using a mix of TextView and EditText

I'm currently making a very simple math game. In this math game I want the player to solve easy equations. It looks like this: 9 * __ = 45, the player then fill in the correct number to solve the equation and then presses a Correct-button. If correct scores are added to the player.
The empty space is an EditText and the others are TextViews. Because I use a mix of TextView & EditText I need somehow to make a convertion for the program to be able to read and calculate. I've been reading like crazy and tried all kinds of different methods without success. How should I do to get around this?
This is how my data looks like:
activity_play.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_play"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.android.laboration2.PlayActivity">
<TextView
android:id="#+id/textPlayMultiply"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textPlayNumber1"
android:layout_centerHorizontal="true"
android:text="#string/multiply"
android:textAlignment="textStart"
android:layout_gravity = "start"
android:textColor="#android:color/black"
android:textSize="40sp" />
<TextView
android:id="#+id/textPlayNumber1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/textPlayScore"
android:layout_alignParentTop="true"
android:layout_alignRight="#+id/textPlayScore"
android:layout_marginTop="48dp"
android:text="#string/number_1"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="50sp" />
<TextView
android:id="#+id/textPlayEqual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="#string/equal"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="50sp"
android:layout_below="#+id/editTextPlayNumber2"
android:layout_alignLeft="#+id/textPlayMultiply"
android:layout_alignStart="#+id/textPlayMultiply" />
<TextView
android:id="#+id/textPlayResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="29dp"
android:text="#string/result_number3"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="50sp"
android:layout_below="#+id/textPlayEqual"
android:layout_alignLeft="#+id/textPlayEqual"
android:layout_alignStart="#+id/textPlayEqual" />
<TextView
android:id="#+id/textPlayScore"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="31dp"
android:layout_toStartOf="#+id/answerButton"
android:text="#string/score_0"
android:textAlignment="textStart"
android:layout_gravity="start"
android:textColor="#android:color/black"
android:textSize="24sp"
android:layout_toLeftOf="#+id/answerButton" />
<Button
android:id="#+id/answerButton"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginTop="36dp"
android:background="#android:color/holo_orange_dark"
android:text="#string/button_result"
android:textAllCaps="false"
android:textColor="#android:color/white"
android:textSize="18sp"
android:textStyle="bold"
android:layout_below="#+id/textPlayResult"
android:layout_centerHorizontal="true" />
<EditText
android:id="#+id/editTextPlayNumber2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textPlayMultiply"
android:layout_alignBottom="#+id/textPlayMultiply"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
android:layout_toEndOf="#+id/answerButton"
android:layout_toRightOf="#+id/answerButton"
android:hint=" "
android:inputType="number"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="50sp" />
<TextView
android:id="#+id/textPlayLevel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textPlayScore"
android:layout_alignBottom="#+id/textPlayScore"
android:layout_toEndOf="#+id/answerButton"
android:layout_toRightOf="#+id/answerButton"
android:text="#string/level_0"
android:textAlignment="center"
android:textColor="#android:color/black"
android:textSize="24sp" />
</RelativeLayout>
.
PlayActivity.java
package com.example.android.laboration2;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class PlayActivity extends AppCompatActivity implements View.OnClickListener {
TextView textPlayNumber1;
EditText editTextPlayNumber2;
TextView textPlayResult;
TextView textPlayScore;
TextView textPlayLevel;
Button answerButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play);
textPlayNumber1 = (TextView) findViewById(R.id.textPlayNumber1);
editTextPlayNumber2 = (EditText) findViewById(R.id.editTextPlayNumber2);
textPlayResult = (TextView) findViewById(R.id.textPlayResult);
textPlayScore = (TextView) findViewById(R.id.textPlayScore);
textPlayLevel = (TextView) findViewById(R.id.textPlayLevel);
answerButton = (Button) findViewById(R.id.answerButton);
answerButton.setOnClickListener(this);
}//onCreate ends here
#Override
public void onClick(View v) {
}//onClick ends here
}//PlayActivity ends here
You can do
int playNumberValue = Integer.getInteger(textPlayNumber1.getText().toString());
int userInputValue = Integer.getInteger(editTextPlayNumber2.getText().toString());
int result = Integer.getInteger(textPlayResult.getText().toString());
if(result == userInputValue+playNumberValue)
//win game
Your TextView and EditText all have String type values. You have to parse those value to Integer then calculate and show the result.
Here is the action onClick answer button-
int score = 0;
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.answerButton:
int playNum1 = Integer.parseInt(textPlayNumber1.getText().toString());
int playNum2 = Integer.parseInt(editTextPlayNumber2.getText().toString());
int playResult = Integer.parseInt(textPlayResult.getText().toString());
if(playNum1*playNum2 == playResult){
score++;
textPlayScore.setText(""+score);
}
break;
}
}
Hope this helps.

Getting the string of a selected RadioButton in Android

Recently I decided to develop an app in Android. I have used this forum and other tools online to teach myself how to develop a simple app that asks the user for their name and answer few questions. My aim to allow the user to send the answers to any email of their choice. So far I have been able to achieve this:
From: ********#gmail.com
To: ********#gmail.com
Subject: Survey result for John Doe
Name: John Doe
Question 1: true
End of Survey.
My aim is to include in the email not the state of question 1 (i.e. "True") but instead I want to show the answer of their question. For Example, Question 1: B. 26-35.
I have searched online for the past couple of days but I have been having issues to find some help as I'm still new to programming. The closest thing I came across was this SO Question but it is not what I'm looking for. Any help or guidance would be highly appreciated.
MainActivity.java
package com.example.android.sampleupdrs;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity
{
TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv=(TextView)findViewById(R.id.textView1);
}
public void emailResult (View view)
{
EditText nameField = (EditText) findViewById(R.id.name_field);
String name = nameField.getText().toString();
RadioGroup radioQ1 = (RadioGroup) findViewById(R.id.radio_Q1);
final RadioButton checkedRadioQ1 = (RadioButton) radioQ1.findViewById(radioQ1.getCheckedRadioButtonId());
boolean isCheckedRadioQ1 = checkedRadioQ1.isChecked();
radioQ1.setOnCheckedChangeListener (new RadioGroup.OnCheckedChangeListener()
{
public void onCheckedChanged (RadioGroup group, int checkedQ1Id)
{
RadioButton checkedRadioButton = (RadioButton)group.findViewById(checkedQ1Id);
boolean isCheckedRadioQ1 = checkedRadioQ1.isChecked();
if (isCheckedRadioQ1)
{
tv.setText("Checked: " +checkedRadioButton.getText());
}
}
});
String resultsEmail = createEmailSummary(name, isCheckedRadioQ1);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse("mailto:"));
intent.putExtra(Intent.EXTRA_SUBJECT, "Survery result for " + name);
intent.putExtra(Intent.EXTRA_TEXT, resultsEmail);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivity(intent);
}
}
private String createEmailSummary (String name, boolean isCheckedRadioQ1) {
String resultsEmail = "Name: " + name;
resultsEmail += "\nQuestion 1 " + isCheckedRadioQ1;
resultsEmail += "\nEnd of Survey";
return resultsEmail;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<EditText
android:id="#+id/name_field"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:hint="Name"
android:inputType="textCapWords"/>
<TextView
android:text="#string/q1string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/textView1"/>
<RadioGroup
android:id="#+id/radio_Q1"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/q1b0_url"
android:text="#string/q1b0string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp" />
<RadioButton
android:id="#+id/q1b1_url"
android:text="#string/q1b1string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1b0_url"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:gravity="top" />
<RadioButton
android:id="#+id/q1b2_url"
android:text="#string/q1b2string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1b1_url"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:gravity="top" />
<RadioButton
android:id="#+id/q1b3_url"
android:text="#string/q1b3string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1b2_url"
android:layout_alignParentStart="true"
android:layout_marginBottom="15dp"
android:gravity="top" />
<RadioButton
android:id="#+id/q1b4_url"
android:text="#string/q1b4string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/q1b3_url"
android:layout_marginBottom="30dp"
android:gravity="start" />
</RadioGroup>
<TextView
android:text="#string/q2string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/textView2"/>
<RadioGroup
<Button
android:text="Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/nextQ1"
android:onClick="emailResult"
android:layout_weight="1"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="right" />
</LinearLayout>
</ScrollView>
strings.xml
<resources>
<string name="app_name">Survey</string>
<string name="q1string">1. Select age group</string>
<string name="q1b0string">A. 18-25</string>
<string name="q1b1string">B. 26-35</string>
<string name="q1b2string">C. 36-45</string>
<string name="q1b3string">D. 46-59</string>
<string name="q1b4string">E. 60+</string>
</resources>
In MainActivity class,
you can get the text of the Radio button in this way:
boolean isCheckedRadioQ1 = checkedRadioQ1.isChecked();
text = "";
if (isCheckedRadioQ1) {
text = checkedRadioQ1.getText().toString();
tv.setText("Checked: " +checkedRadioButton.getText());
}
In method createEmailSummary do
resultsEmail += "\nQuestion 1 " + text;
instead of
resultsEmail += "\nQuestion 1 " + isCheckedRadioQ1
You'll have to send text as a parameter to createEmailSummary instead of isCheckedRadioQ1
If I understand correctly, what you need is checkedRadioButton.getText().toString() and not the boolean isCheckedRadioQ1.

Alignment Layout exchange between classes - Android Studio

I am implementing a program that uses database and interacts with different layouts of entries and editing users.
I'm working with RelativeLayout on all screens. In one of thelayouts, I insert a perfectly aligned button and give the command android:visibility="gone" for him to show upon request.
The problem is that when I need to use it at command editarBt.setVisibility(View.VISIBLE), the button appears out of alignment and overlaps the fields for entering information.
Is there any way to keep the position of command by button?
I will not put the entire code because it has 7 classes, so I'll just put the classes that interest.
EnterPatientActivity Class
package br.luizhmu.aulas_android_sqlite;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
/**
* Created by LuizHMU on 2/17/15.
*/
public class EnterPatientActivity extends Activity {
private Paciente paciente = new Paciente();
private EditText nomeEt;
private EditText emailEt;
private EditText senhaEt;
private Button salvarBt;
private Button editarBt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_inserir_paciente);
nomeEt = (EditText) findViewById(R.id.editTextNome);
emailEt = (EditText) findViewById(R.id.editTextEmail);
senhaEt = (EditText) findViewById(R.id.editTextSenha);
salvarBt = (Button) findViewById(R.id.buttonSalvar);
editarBt = (Button) findViewById(R.id.buttonEditar);
Intent intent = getIntent();
if(intent != null){
Bundle bundle = intent.getExtras();
if(bundle != null){
paciente.setId(bundle.getLong("id"));
paciente.setNome(bundle.getString("nome"));
paciente.setEmail(bundle.getString("email"));
nomeEt.setText(paciente.getNome());
emailEt.setText(paciente.getEmail());
senhaEt.setVisibility(View.GONE);
salvarBt.setVisibility(View.GONE);
editarBt.setVisibility(View.VISIBLE);
}
}
}
public void salvar(View view){
paciente.setNome(nomeEt.getText().toString());
paciente.setEmail(emailEt.getText().toString());
paciente.setSenha(senhaEt.getText().toString());
DataBase bd = new DataBase(this);
bd.inserir(paciente);
Toast.makeText(this, "Paciente inserido com sucesso!", Toast.LENGTH_SHORT).show();
}
public void editar(View view){
paciente.setNome(nomeEt.getText().toString());
paciente.setEmail(emailEt.getText().toString());
DataBase bd = new DataBase(this);
bd.atualizar(paciente);
Toast.makeText(this, "Paciente \""+paciente.getNome()+"\" atualizado com sucesso.", Toast.LENGTH_SHORT).show();
}
}
activity_inserir_paciente.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingLeft="15dp"
android:paddingRight="15dp"
android:background="#ffffea0a"
tools:context=".EnterPatientActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Novo paciente"
android:id="#+id/textView3"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textColor="#ff1727ff"
android:textSize="20dp"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="*Nome"
android:ems="10"
android:id="#+id/editTextNome"
android:layout_below="#+id/textView3"
android:layout_alignRight="#+id/buttonSalvar"
android:layout_alignEnd="#+id/buttonSalvar" />
<EditText
android:hint="Telefone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="phone"
android:ems="10"
android:id="#+id/editTextTelefone"
android:layout_below="#+id/editTextNome"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:hint="*E-mail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:ems="10"
android:id="#+id/editTextEmail"
android:layout_below="#+id/editTextTelefone"
android:layout_alignLeft="#+id/editTextTelefone"
android:layout_alignStart="#+id/editTextTelefone" />
<EditText
android:hint="*Senha"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editTextSenha"
android:layout_below="#+id/editTextEmail"
android:layout_alignLeft="#+id/editTextEmail"
android:layout_alignStart="#+id/editTextEmail" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Salvar"
android:id="#+id/buttonSalvar"
android:onClick="salvar"
android:layout_below="#+id/textView4"
android:layout_alignRight="#+id/editTextSenha"
android:layout_alignEnd="#+id/editTextSenha" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Editar"
android:id="#+id/buttonEditar"
android:layout_alignTop="#+id/buttonSalvar"
android:layout_toLeftOf="#+id/buttonSalvar"
android:layout_toStartOf="#+id/buttonSalvar"
android:visibility="gone"
android:onClick="editar"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="* Campos de preenchimento obrigatório"
android:textColor="#000000"
android:id="#+id/textView4"
android:layout_below="#+id/editTextSenha"
android:layout_alignLeft="#+id/editTextSenha"
android:layout_alignStart="#+id/editTextSenha" />
</RelativeLayout>
In a RelativeLayout, you can prevent views from overlapping by using the layout_toLeftOf, layout_toRightOf, layout_above and layout_below attributes, which expect a view id as value.
Also, you might want to use View.INVISIBLE instead of View.GONE: The former will consider the view during layouting, but hide it. The latter will pretend the view does not exist, therefore altering your layout result.

how to display value of textbox on same screen in android application just like whatsaap

This is my main file where i am calling the click event on button to pass the value.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setTitle("aakash");
SetContentView(R.id.lst);
Button b = (Button) findViewById(R.id.sendbtn);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(android.view.View v) {
startActivity(new Intent(View.this,View.class));
}
});
}
**xml**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:text="aakash"
android:background="#c0c0c0"
/>
<ScrollView
android:id="#+id/lst"
android:background="#fefefe"
android:layout_below="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_holder"
>
</ScrollView>
<EditText
android:id="#+id/chattxt"
android:layout_width="230dp"
android:layout_height="45dp"
android:hint="type here"
android:inputType="textMultiLine" />
<Button
android:id="#+id/sendbtn"
android:layout_width="75dp"
android:layout_height="45dp"
android:text="Send" />
</LinearLayout>
I want the value of textbox to display on the same screen(scrollview)box and also i want to pass a httppost request to pass a value..plz help me out
Thnks in advance
I think, you can use a ListView insted of scroll view. and add list items from the chat text value dynamically.
call notifydatasetchanged() every time when you add a new item to it.
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:text="aakash"
android:background="#c0c0c0"
/>
<ListView
android:id="#+id/listView_chats"
android:layout_width="match_parent"
android:layout_height="100dp"
>
</ListView>
<EditText
android:id="#+id/chattxt"
android:layout_width="230dp"
android:layout_height="45dp"
android:hint="type here"
android:inputType="textMultiLine" />
<Button
android:id="#+id/sendbtn"
android:layout_width="75dp"
android:layout_height="45dp"
android:text="Send" />
</LinearLayout>
Write a custom adapter for the listView and add the code to manage the list data.
i got the answer to my question
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ButtonActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
}
public void setText(){
Button myButton = (Button)findViewById(R.id.button1);
final TextView myText = (TextView)findViewById(R.id.text1);
final EditText myInput = (EditText)findViewById(R.id.edit);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
myText.setText(myInput.getText());
}
});
}
}

Getting value from dynamically created textview in android

MainActivity's code
How can I get values from the text boxes and the qty box in that user will input the value. price will come from server and than i want to calculate the bill and show it in next layout. plz give me some easy explanation as i am not expert in it. thanks in advance.
MainActivity .java
package com.example.myone;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText textIn;
Button buttonAdd;
LinearLayout container;
private Button done;
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textIn = (EditText)findViewById(R.id.textin);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
done = (Button) findViewById(R.id.cal);
tv= (TextView) findViewById(R.id.tv);
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
//(int1);
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);
}});
}
}
MainActivity.xml
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="#drawable/mainbg"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/textin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter your item here"
/>
<Button
android:id="#+id/cal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/textin"
android:text="Done!"
android:layout_alignParentLeft="true"
/>
<Button
android:id="#+id/add"
android:text="Add"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#+id/cal"
/>
</RelativeLayout>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
Row.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#drawable/borderbg"
android:orientation="horizontal"
android:id="#+id/rel"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="Item"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
/>
<EditText
android:id="#+id/qty"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignTop="#+id/price"
android:layout_toRightOf="#+id/price"
android:layout_marginLeft="50dp"
android:textSize="12sp"
android:hint="Qty" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textout"
android:layout_alignBottom="#+id/textout"
android:layout_marginLeft="42dp"
android:layout_toRightOf="#+id/textout"
android:hint="Price" />
<Button
android:id="#+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="remove" />
</RelativeLayout>
Simply I want to create a basic shopping app that have 20 products. the price and product name will come from server and quantity will input by user so how do I that?
You are looking for something like this
int count = container.getChildCount();
for (int i = 0; i < count; i++) {
final View row = container.getChildAt(i);
TextView textOut = (TextView)row.findViewById(R.id.textout);
String data = textOut.getText().toString();
}
Where container is your LinearLayout in which you have added your inflated rows.
Try this:-
To take user input from text box:-
int qty = Integer.parseInt(qtyTextBox.getText().toString());
Caluculate bill:-
double bill = price*qty;
Send bill value to next activity:-
Intent in = new Intent();
in.putExtra("Bill", bill);
startActivity(in);
Get bill value in next activity:-
Intent in = getIntent();
int mBill = in.getExtra("Bill");
string bill = String.valueOf(mBill);
Now you can set the value of 'bill' to the TextView where you want to display it.

Categories

Resources