Assuming Imports and Stuff
Im trying to build a simple Calorie Counter, but having a trouble displaying
the number of calories. I also want it to add to the previous input..
for instance if the user enters 500, 400, 300 the total in the TextField
should be 1200.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/CalorieAdd"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="rav_singh.caloriecounter.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:text="Current Calories "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/caloriesNeeded"
android:textSize="30dp"
android:layout_marginTop="41dp"
android:layout_marginEnd="69dp"
android:layout_below="#+id/calCount"
android:layout_alignParentEnd="true" />
<TextView
android:text="0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calCount"
android:textAlignment="center"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="40dp" />
<Button
android:text="Add "
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/calorieAdd"
android:layout_marginTop="88dp"
android:layout_below="#+id/caloriesNeeded"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/calorieinput"
android:hint="Add Calories "
android:layout_marginTop="12dp"
android:layout_below="#+id/caloriesNeeded"
android:layout_alignStart="#+id/caloriesNeeded"
android:textAlignment="center" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity
{
EditText input;
TextView calTotal;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
calTotal = (TextView) findViewById(R.id.calCount);
input = (EditText) findViewById(R.id.calorieinput);
Button calorieAdd = (Button) findViewById(R.id.calorieAdd);
calTotal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Float n1 = Float.valueOf(input.getText().toString());
calTotal.setText(Float.toString(n1));
}
});
}
}
previous input.. for instance if the user enters 500, 400, 300 the
total in the TextField should be 1200
Add both TextView and EditText data before updating counter in TextView like:
#Override
public void onClick(View view) {
float n0 = Float.parseFloat(calTotal.getText().toString());
float n1 = Float.parseFloat(input.getText().toString());
calTotal.setText(Float.toString(n0+n1));
}
Also added empty check for EditText before parsing value to Float to avoid NumberFormatException Exception.
try
if(input.getText().toString().trim().length() > 0)
float n1 = Float.parseFloat(input.getText().toString());
instead of:
Float n1 = Float.valueOf(input.getText().toString());
Related
I have seen/tried the following questions, but mine is not the same:
Android keyboard next button issue on EditText
Move to another EditText when Soft Keyboard Next is clicked on Android
First, I have a CardView wherein I have a EditText, like below:
<RelativeLayout
android:id="#+id/topCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myCard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:foreground="?android:attr/selectableItemBackground"
android:orientation="horizontal"
app:cardCornerRadius="5dp"
app:cardElevation="3dp"
app:cardPreventCornerOverlap="false"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="5dp"
android:layout_height="match_parent"
android:background="#26c2ef">
</RelativeLayout>
<RelativeLayout
android:id="#+id/cardTopTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="5dp">
<TextView
android:id="#+id/lessonTitleHeading"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="10dp"
android:text="Please provide a name:"
android:textColor="#000000"
android:textSize="18sp" />
<ImageView
android:id="#+id/horiLine"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#+id/lessonTitleHeading"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:background="#60000000" />
<EditText
android:id="#+id/noteEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/horiLine"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:hint="Enter name here..."
android:imeOptions="actionNext" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
I then inflate this layout in another by using include like this:
<include
android:id="#+id/lessonTitle"
layout="#layout/activity_notes_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
and in my Activity class I do the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_leason);
View tvOne = findViewById(R.id.tvOne);
View tvTwo = findViewById(R.id.tvTwo);
View tvThree = findViewById(R.id.tvThree);
View tvFour = findViewById(R.id.tvFour);
final TextView tvOne = (TextView)vLessonTitle.findViewById(R.id.lessonTitleHeading);
final EditText etOne = (EditText) vLessonTitle.findViewById(R.id.noteEditText);
TextView tvTwo = (TextView)vlessonProbStu.findViewById(R.id.lessonTitleHeading);
final EditText etTwo = (EditText) vlessonProbStu.findViewById(R.id.noteEditText);
TextView tvThree = (TextView)vLessonWorkedOn.findViewById(R.id.lessonTitleHeading);
final EditText etThree = (EditText) vLessonWorkedOn.findViewById(R.id.noteEditText);
TextView tvFour = (TextView)vlessonWhatStudShouldWorkOn.findViewById(R.id.lessonTitleHeading);
final EditText etFour = (EditText)vlessonWhatStudShouldWorkOn.findViewById(R.id.noteEditText);
tvOne.setText("This is the first CardView:");
etOne.setHint("Enter Title Here...");
tvTwo.setText("This is the second CardView:");
etTwo.setHint("Enter Text Here...");
tvThree.setText("This is the third CardView:");
etThree.setHint("Enter Text Here...");
tvFour.setText("This is the fourth CardView:");
etFour.setHint("Enter Text Here...");
//I tried this, but it didn't work...
etOne.setOnKeyListener(new View.OnKeyListener() {
public boolean onKey(View v, int keyCode, KeyEvent event) {
// If the event is a key-down event on the "enter" button
if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
(keyCode == KeyEvent.KEYCODE_ENTER))
{
// Perform action on Enter key press
etLessonTitle.clearFocus();
etLessonProbStu.requestFocus();
return true;
}
return false;
}
});
}
The Layout now looks like this:
At this point I have a bunch of CardViews with EditTexts underneath each other like above. I would like to use the next button in the keyboard to go to the next CardsViews EditText.
If you have a look at my Activity class above, I tried using setOnKeyListener and clearFocus from the current EditText then requestFocus on the next EditText, but that didn't work.
Any ideas how I can achieve this?
Try this you need to set android:inputType="" to your Editext with android:imeOptions="actionNext"
<EditText
android:id="#+id/noteEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/horiLine"
android:layout_marginEnd="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginStart="10dp"
android:layout_marginTop="5dp"
android:background="#android:color/transparent"
android:hint="Enter name here..."
android:inputType="text" // specifies your input type here
android:imeOptions="actionNext" />
Always getting this warning TextInputLayout: EditText added is not a TextInputEditText. Please switch to using that class instead. and not finishing Activity for First time. On Second time not getting an warning and activity finishing perfectly.
activity_login.xml
<LinearLayout
android:id="#+id/ll_login_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center_horizontal"
android:orientation="vertical"
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:visibility="visible">
<android.support.design.widget.TextInputLayout
android:id="#+id/email_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:hint="#string/email_phone"
android:paddingTop="48dp">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="textEmailAddress"
android:paddingEnd="0dp"
android:paddingStart="8dp" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:id="#+id/password_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/password"
android:paddingBottom="16dp">
<android.support.v7.widget.AppCompatEditText
android:id="#+id/et_password"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="textPassword"
android:paddingEnd="0dp"
android:paddingStart="8dp" />
</android.support.design.widget.TextInputLayout>
<Button
android:id="#+id/btn_login"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="#dimen/button_login_social_margin"
android:layout_marginStart="#dimen/button_login_social_margin"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:text="#string/login"
android:textColor="#android:color/black" />
</LinearLayout>
LoginActivity.java
public class LoginActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
final AppCompatEditText etEmailOrPh = (AppCompatEditText) findViewById(R.id.et_email);
final AppCompatEditText etPassword = (AppCompatEditText) findViewById(R.id.et_password);
final Button btnLogin = (Button) findViewById(R.id.btn_login);
assert etEmailOrPh != null;
assert etPassword != null;
assert btnLogin != null;
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String emailOrPhone = etEmailOrPh.getText().toString().trim();
String password = etPassword.getText().toString().trim();
if(emailOrPhone.isEmpty()){
etEmailOrPh.setError(getResources().getString(R.string.email_phone_mandatory));
etEmailOrPh.requestFocus();
} else if(emailOrPhone.contains("#") && CommonUtil.isValidEmail(emailOrPhone)) {
etEmailOrPh.setError(getResources().getString(R.string.email_error));
etEmailOrPh.requestFocus();
} else if(password.isEmpty()) {
etPassword.setError(getResources().getString(R.string.password_mandatory));
etPassword.requestFocus();
} else {
SharedPreferences cache = LoginActivity.this.getSharedPreferences(Constants.SHARED_PREF_NAME, Context.MODE_PRIVATE);
final SharedPreferences.Editor preferenceEditor = cache.edit();
preferenceEditor.putInt(Constants.SHARED_PREF_ITEM_USER_ID, 1);
preferenceEditor.apply();
setResult(RESULT_OK);
finish();
}
}
});
}
}
Change this EditText
android.support.v7.widget.AppCompatEditText
to this
android.support.design.widget.TextInputEditText
Full Code :
<android.support.design.widget.TextInputLayout
android:id="#+id/email_field"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="16dp"
android:hint="#string/email_phone"
android:paddingTop="48dp">
<android.support.design.widget.TextInputEditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="textEmailAddress"
android:paddingEnd="0dp"
android:paddingStart="8dp"
/>
</android.support.design.widget.TextInputLayout>
Try not using
<android.support.v7.widget.AppCompatEditText
instead use
<EditText
So you will get:
<EditText
android:id="#+id/et_email"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:inputType="textEmailAddress"
android:paddingEnd="0dp"
android:paddingStart="8dp" />
After doing a bit of research on the issue i found this post:
EditText added is not a TextInputEditText. Please switch to using that class instead
let me know if it helps you.
Although I've checked the solutions for the related questions in this website, I couldn't solve my problem. I'm trying to build a quiz app that takes the correct answer, adds 1 to the score and updates the score on the score TextView. I tried calling the score method through android:onClick and also tried the setOnClickListener methods but none of them seem to work.
This is my XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome to the Canada Quiz"
android:textSize="16sp"
android:textColor="#000000"
android:textStyle="bold"
android:layout_marginBottom="16dp"
android:layout_gravity="center"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter name"
android:layout_marginBottom="8dp"
/>
<TextView
android:id="#+id/scoreText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginBottom="16dp"
android:textColor="#000000"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1. What is the capital of Canada?"
android:textSize="20dp"
android:textColor="#000000"
android:textStyle="bold"
/>
<RadioGroup
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RadioButton
android:id="#+id/tokyo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tokyo"
android:layout_marginLeft="24dp"
android:textStyle="bold"
android:textSize="16dp"/>
<RadioButton
android:id="#+id/newYork"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New York"
android:layout_marginLeft="24dp"
android:textStyle="bold"
android:textSize="16dp"/>
<RadioButton
android:id="#+id/ottawa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ottawa"
android:layout_marginLeft="24dp"
android:textStyle="bold"
android:textSize="16dp"/>
<RadioButton
android:id="#+id/hongKong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hong Kong"
android:layout_marginLeft="24dp"
android:textStyle="bold"
android:onClick="calculateScore"
android:textSize="16dp"/>
</RadioGroup>
<Button
android:id="#+id/scoreButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Calculate score"
android:layout_marginTop="16dp"
/>
</LinearLayout>
</ScrollView>
And this is my Java:
public class MainActivity extends AppCompatActivity {
int score = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioButton rb = (RadioButton)findViewById(R.id.ottawa);
Button calculate = (Button) findViewById(R.id.scoreButton);
final TextView scoreShow = (TextView) findViewById(R.id.scoreText);
final boolean rbChecked = rb.isChecked();
calculate.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
if(rbChecked){
score += 1;
scoreShow.setText("Your score is: " + score + "/10");
}
}
});
}
// public void calculateScore(){
// RadioButton rb = (RadioButton)findViewById(R.id.ottawa);
// //Button calculate = (Button) findViewById(R.id.scoreButton);
// TextView scoreShow = (TextView) findViewById(R.id.scoreText);
//
// boolean rbChecked = rb.isChecked();
// if(rbChecked){
// score += 1;
// scoreShow.setText("Your score is: " + score + "/10");
// }
// }
}
Honestly, it really looks like it would work but it doesn't.
You are only storing the value of the checked state when the view is loaded and it never is updated.
Always check rb.isChecked() inside the click listener instead of storing the boolean value outside of it.
It should be:
if(rbChecked.isChecked()){
score += 1;
scoreShow.setText("Your score is: " + score + "/10");
}
you need to check for the state of radiobutton when user click on calculate button....your code should be like this...
calculate.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
rbChecked = rb.isChecked();
if(rbChecked){
score += 1;
scoreShow.setText("Your score is: " + score + "/10");
}
}
});
I'm trying to convert a string to a float.
Basically, I have an edittext that's numerical only, and I'm trying to get the input of it and do some math with it before outputting the result onto another edittext.
Here is my main activity.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
//Declare views
EditText ptv = (EditText) findViewById(R.id.priceet);
EditText ttv = (EditText) findViewById(R.id.taxet);
EditText totaltv = (EditText) findViewById(R.id.totalet);
//Declare variables
float price = 0;
price = Float.valueOf(ptv.getText().toString());
float tax = 0.0F;
tax = Float.valueOf(ttv.getText().toString()) / 100;
float total = price*tax + price;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
Here is my activity_main.xml
<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="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:listSeparatorTextViewStyle"
android:text="Price"
android:id="#+id/cost"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/priceet" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/priceet"
android:layout_below="#+id/cost"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:listSeparatorTextViewStyle"
android:text="Tax %"
android:id="#+id/taxtv"
android:layout_below="#+id/priceet"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/taxet"
android:layout_below="#+id/taxtv"
android:layout_alignParentStart="true"
android:layout_alignEnd="#+id/totalet" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:listSeparatorTextViewStyle"
android:text="Total"
android:id="#+id/totaltv"
android:layout_below="#+id/taxet"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:id="#+id/totalet"
android:layout_below="#+id/totaltv"
android:layout_alignParentStart="true"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_alignEnd="#+id/cost" />
and here is the error I'm getting
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.jimlarck.taxcalculator/com.jimlarck.taxcalculator.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
The error points to this line of code:
price = Float.valueOf(ptv.getText().toString());
How would I fix this? I read somewhere around here that this error is thrown when something isn't initialized properly but everything looks fine on my end. Any help is appreciated. Thank you :)
You need to instantiate your EditTexts after you call super.onCreate() and setContentView():
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Declare views
EditText ptv = (EditText) findViewById(R.id.priceet);
EditText ttv = (EditText) findViewById(R.id.taxet);
EditText totaltv = (EditText) findViewById(R.id.totalet);
//Declare variables
float price = 0;
price = Float.valueOf(ptv.getText().toString());
float tax = 0.0F;
tax = Float.valueOf(ttv.getText().toString()) / 100;
float total = price*tax + price;
}
So I have an application that dynamically adds a table row every time the "Add Row" button is pressed. The code attached is the code that runs when "Add Row" is pressed. What I am trying to do is have 3 EditTexts next to each other on the same row. In order to do that, I need to change the Layout_Width of each EditText so the first EditText doesn't cut the other two off the screen. I can't seem to figure out a way to properly do this and was wondering if anyone would help me out. After I figure out how to do that, the next step is to adjust the layout_width according to the screen size but thats later down the road. It needs to be done programmatically because a user can theoretically have as many rows as they want to.
private OnClickListener btnListener = new OnClickListener()
{
public void onClick(View v)
{
tablerow = new TableRow(getActivity());
ed1 = new EditText(getActivity());
ed1.setInputType(InputType.TYPE_CLASS_TEXT);
ed2 = new EditText(getActivity());
ed2.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
ed3 = new EditText(getActivity());
ed3.setInputType(InputType.TYPE_NUMBER_FLAG_DECIMAL);
tablerow.addView(ed1);
tablerow.addView(ed2);
tablerow.addView(ed3);
table1.addView(tablerow);
}
};
I believe what you are looking for is LayoutParams and its "weight" value.
Try:
TableRow.LayoutParams params = new TableRow.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT,1.0f);
ed1 = new EditText(getActivity());
ed1.setInputType(InputType.TYPE_CLASS_TEXT);
ed1.setLayoutParams(params);
The third value (1.0f) in the LayoutParams constructor is a weight...all EditTexts in that TableRow should end up the same width.
You can try next approach, it is based on xml. You will be able to play around with layout at table_row.xml.
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.a_mn_button_add_row:
onClickButtonAddRow();
break;
}
}
private void onClickButtonAddRow() {
TableLayout tableLayout = (TableLayout) findViewById(R.id.a_mn_table);
tableLayout.addView(View.inflate(this, R.layout.table_row, null));
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="#+id/a_mn_button_add_row"
android:onClick="onClick"
android:text="Add row"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout
android:id="#+id/a_mn_table"
android:stretchColumns="0,1,2"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</ScrollView>
</LinearLayout>
table_row.xml
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text" />
<EditText
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<EditText
android:inputType="text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</TableRow>
table_row.xml for use case with 3-1-2 proportions.
<?xml version="1.0" encoding="utf-8"?>
<TableRow xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:layout_weight="3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:inputType="text" />
<EditText
android:layout_weight="1"
android:inputType="text"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<EditText
android:layout_weight="2"
android:inputType="text"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</TableRow>