How to programmatically add different buttons to a table, android studio - android

I'm new to android application development, I need to add buttons with different names, that is, the name of the button is taken from EditText and a button is created. The nuance of all this is that after I try to create another button and enter another name, this very name is transferred to other buttons. That is, if I create a button for the first time, then it is given the text Sometning1, but after I create the second button with the text Sometning2, then the first button gets the name of the second one, and so on, all previous buttons get the name of the new one, how to fix this? By the way, each button should be completely different, with different properties, but for now let's focus on the text in it.(sorry, hard to explain)
Xml code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity6">
<TableLayout
android:id="#+id/Tb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<TableRow
android:id="#+id/r1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TableRow
android:id="#+id/r2"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r4"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r5"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r6"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r7"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r8"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
</TableLayout>
<EditText
android:id="#+id/adress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="70dp"
android:inputType="text"
android:text="Something"
android:textColor="#424242"
app:layout_constraintBottom_toBottomOf="#+id/btn1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="LabelFor" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
package com.example.serverconnectionukaa;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
public class MainActivity6 extends AppCompatActivity {
EditText adress;
Button btn1;
private TableLayout buttonTableLayout;
int clickcount=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
adress = findViewById(R.id.adress);
btn1 = (Button) findViewById(R.id.btn1);
buttonTableLayout = (TableLayout) findViewById(R.id.Tb1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickcount=clickcount+1;
if(clickcount==0)
{
}
else
{
//check how many times clicked and so on
Toast.makeText(getApplicationContext(),"Button clicked count is"+clickcount, Toast.LENGTH_LONG).show();
}
for (int row = 0; row < buttonTableLayout.getChildCount(); ++row)
((TableRow) buttonTableLayout.getChildAt(row)).removeAllViews();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int x = 0;
for (int row = 0; row < 5; row++) {
TableRow currentTableRow = getTableRow(row);
for (int column = 0; column < 2; column++) {
Button newGuessButton = new Button(MainActivity6.this);
String myName = adress.getText().toString();
newGuessButton.setText(myName);
currentTableRow.addView(newGuessButton);
x++;
if (x==clickcount) {
break;
}
}
if (x==clickcount) {
break;
}
}
}
});
}
private TableRow getTableRow(int row) {
return (TableRow) buttonTableLayout.getChildAt(row);
}
}
Thanks to everyone

Related

My app crashed for unknown reason when I tried to setOnClickListener for a button in ListView

I'm building a Contact app using ListView. My ListView has 2 button. In this app, to test the respond ability of the buttons, I intended to set the button "Edit" so that when I click on it, it will change to "Clicked", and the button "Delete" will change to "Clicked", too. When I ran the Debug, the app stopped working and I couldn't get it work again (before I added the onClickListener, this app had worked property).
I don't know what is the error, and have tried many ways to fix.
Here is my row_listview.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent" android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="5">
<ImageView
android:id="#+id/imgAvatar"
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="1"
android:src="#drawable/if_male3_403019" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2.7"
android:orientation="vertical"
android:weightSum="2">
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingLeft="15dp"
android:text="Akai Shuichi"
android:textSize="16dp"
android:textColor="#000"
android:gravity="center_vertical"
/>
<TextView
android:id="#+id/tvNumber"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:paddingLeft="15dp"
android:text="0982xxxxxx"
android:textSize="16dp"
android:textColor="#000"
android:gravity="center_vertical"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.3"
android:weightSum="2"
android:orientation="vertical">
<Button
android:id="#+id/btnEdit"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:backgroundTint="#3FE0FF"
android:onClick="myClickHandler"
android:text="Edit"
android:textColor="#fff"
android:textStyle="bold" />
<Button
android:id="#+id/btnDelete"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:backgroundTint="#F73131"
android:onClick="myClickHandler"
android:text="Delete"
android:textColor="#fff"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
And here is my MainActivity.java:
package com.huy9515gmail.mycontact;
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.LinearLayout;
import android.widget.ListView;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import java.util.ArrayList;
import butterknife.BindView;
import butterknife.ButterKnife;
public class MainActivity extends AppCompatActivity {
#BindView(R.id.edt_inputName) EditText edtName;
#BindView(R.id.btnAdd) Button btnAdd;
#BindView(R.id.btnEdit) Button btnEdit;
#BindView(R.id.btnDelete) Button btnDelete;
#BindView(R.id.edt_inputNumber) EditText edtNumber;
#BindView(R.id.rdbtn_male) RadioButton rdbtn_male;
#BindView(R.id.rdbtn_female) RadioButton rdbtn_female;
#BindView(R.id.rdbtn_others) RadioButton rdbtn_others;
#BindView(R.id.gender) RadioGroup genderSelection;
private ListView lvContact;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
lvContact = (ListView) findViewById(R.id.lv_contact);
final ArrayList<Contact> arrContact = new ArrayList<>();
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//validating contact info
if (((edtName.getText().toString().trim()) == "") || (edtNumber.getText().toString().trim() == "") || ((rdbtn_male.isChecked()==false) && (rdbtn_female.isChecked()==false) &&(rdbtn_others.isChecked()==false))) {
Toast.makeText(MainActivity.this, "Invalid contact info! Please try again!", Toast.LENGTH_SHORT).show();
}
//adding contact info
else {
Contact contact = new Contact(Gender.male, "", "");
//adding info
contact.setName(edtName.getText().toString());
contact.setNumber(edtNumber.getText().toString());
arrContact.add(contact);
}
CustomAdapter customAdapter = new CustomAdapter(MainActivity.this, R.layout.row_listview, arrContact);
lvContact.setAdapter(customAdapter);
}
});
}
public void myClickHandler(View v) {
LinearLayout vwParentRow = (LinearLayout) v.getParent();
Button btnEdit = (Button) vwParentRow.getChildAt(0);
Button btnDelete = (Button) vwParentRow.getChildAt(1);
btnEdit.setText("Clicked");
btnDelete.setText("Clicked");
}
}
And here is the row_listview.xml layout:
Instead of setting the android:onClick in XML, do the same as you did for btnAdd.
Find the view by id or use butterknife, then put the following in onCreate()
btnEdit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code here
}
});
btnDelete.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Your code here
}
});
Make sure you have an element in your XML file with the id like this:
android:id="#+id/btnAdd"

how to make number picker work in fragment with on clicklistener

I have a tab layout, number picker in the fragment doesn't seem to be working, the values remain 0 always.
Basically I am making random number app.
there are three number pickers in my fragment, one gives minimum number, one gives maximum number and one gives quantity of random numbers.
after setting these values in number picker user hits a button to get random numbers.
for me button is not doing anything when the values are set.
Please suggest me how to do this.
My fragment activity
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.NumberPicker;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.Collections;
public class TabFragment2 extends Fragment {
//declare Spinner object
Spinner generatorType;
Button getRandomNumberBtn;
int minimumNumber;
int maximumNumber;
int quantity1;
TextView randNumbText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_fragment_2, container, false);
getRandomNumberBtn = (Button) view.findViewById(R.id.get_number);
NumberPicker miniText;
final TextView minimumTextView = (TextView) view.findViewById(R.id.textView3);
miniText = (NumberPicker) view.findViewById(R.id.start_number);
miniText.setMinValue(0);
miniText.setMaxValue(10000);
miniText.setWrapSelectorWheel(true);
miniText.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal1, int newVal1) {
//Display the newly selected number from picker
minimumTextView.setText("Min: " + newVal1);
}
});
minimumNumber = miniText.getValue();
NumberPicker maxiText;
final TextView maximumTextView = (TextView) view.findViewById(R.id.textView2);
maxiText = (NumberPicker) view.findViewById(R.id.end_number);
maxiText.setMinValue(0);
maxiText.setMaxValue(10000);
maxiText.setWrapSelectorWheel(true);
maxiText.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal2, int newVal2) {
//Display the newly selected number from picker
maximumTextView.setText("Max: " + newVal2);
}
});
maximumNumber = maxiText.getValue();
NumberPicker quantity;
final TextView quantityTextView = (TextView) view.findViewById(R.id.quantity_text);
quantity = (NumberPicker) view.findViewById(R.id.quantity_picker);
quantity.setMinValue(0);
quantity.setMaxValue(100);
quantity.setWrapSelectorWheel(true);
quantity.setOnValueChangedListener(new NumberPicker.OnValueChangeListener() {
#Override
public void onValueChange(NumberPicker picker, int oldVal3, int newVal3) {
//Display the newly selected number from picker
quantityTextView.setText("Quantity: " + newVal3);
}
});
quantity1 = quantity.getValue();
randNumbText = (TextView) view.findViewById(R.id.random_number_display);
getRandomNumberBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int b = maximumNumber + 1;
int a = minimumNumber;
int c = quantity1;
String multipleRandoms = "";
ArrayList<Integer> list = new ArrayList<Integer>();
ArrayList<Integer> list1 = new ArrayList<Integer>();
for (int i = a; i < b; i++) {
list.add(new Integer(i));
}
int k = list.size();
Collections.shuffle(list);
if (k>c) {
for (int i = 1; i <= c; i++) {
list1.add(new Integer(list.get(i)));
multipleRandoms = list1.toString();
}
}else if (k==c) {
multipleRandoms = list.toString();
} else {
Toast toast = Toast.makeText(getActivity(),"Quantity can not be greater than numbers in range", Toast.LENGTH_LONG);
toast.show();
}
randNumbText.setText(multipleRandoms);
}
});
return view;
}
My fragment layout
<?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/tab_fragment_2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
tools:context="com.example.tabstrial2.TabFragment2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="#dimen/activity_vertical_margin">
<LinearLayout
android:id="#+id/linear_layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/generator_type_spinner"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/linear_layout2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10px"
android:text="Min" />
<NumberPicker
android:id="#+id/start_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20px"
android:layout_marginTop="20px" />
</LinearLayout>
<LinearLayout
android:layout_width="2px"
android:layout_height="match_parent"
android:background="#android:color/black">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/quantity_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10px"
android:text="Quantity" />
<NumberPicker
android:id="#+id/quantity_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20px"
android:layout_marginTop="20px" />
</LinearLayout>
<LinearLayout
android:layout_width="2px"
android:layout_height="match_parent"
android:background="#android:color/black">
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10px"
android:text="Max" />
<NumberPicker
android:id="#+id/end_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView2"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="20px"
android:layout_marginTop="20px" />
</LinearLayout>
</LinearLayout>
<TextView
android:id="#+id/random_number_display"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linear_layout1"
android:layout_centerHorizontal="true"
android:layout_margin="10px"
android:textColor="#android:color/black"
android:textSize="40dp" />
<Button
android:id="#+id/get_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/random_number_display"
android:layout_centerHorizontal="true"
android:background="#android:color/holo_orange_light"
android:focusable="true"
android:focusableInTouchMode="true"
android:onClick="getRandomNumber"
android:paddingLeft="20px"
android:paddingRight="20px"
android:text="Get Random Number" />
</RelativeLayout>
</ScrollView>

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.

How to show same image multiple time in row?

I am developing an application for children. Basic maths for children to learn with fun.
In this app I am using apple image for displaying on the screen. for addition when they give 1st input and second input it should show apple images as specified in 1st and 2nd input.
Is any one know solution for this or any one having source code thn help me
this is the way you can do it ,quite simple though :)
your xml should look like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/input1" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/input2" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="Result"/>
<HorizontalScrollView
android:id="#+id/scroll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/linear1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:id="#+id/scroll2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear2"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:id="#+id/scroll3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear3"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
and you Activity looks like this
package com.example.stackanswer;
import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class AppleActivity extends Activity {
private LinearLayout linear1;
private LinearLayout linear2;
private LinearLayout linear3;
private HorizontalScrollView scrollbar1;
private HorizontalScrollView scrollbar2;
private HorizontalScrollView scrollbar3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apple);
final EditText input1 = (EditText)findViewById(R.id.input1);
final EditText input2 = (EditText)findViewById(R.id.input2);
scrollbar1 = (HorizontalScrollView)findViewById(R.id.scroll1);
scrollbar2 = (HorizontalScrollView)findViewById(R.id.scroll2);
scrollbar3 = (HorizontalScrollView)findViewById(R.id.scroll3);
linear1 = (LinearLayout)findViewById(R.id.linear1);
linear2 = (LinearLayout)findViewById(R.id.linear2);
linear3 = (LinearLayout)findViewById(R.id.linear3);
Button output = (Button)findViewById(R.id.button);
output.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String input1A = input1.getText().toString();
String input1B = input2.getText().toString();
int value1 = Integer.parseInt(input1A);
int value2 = Integer.parseInt(input1B);
Log.i("1",""+value1);
Log.i("2",""+value2);
linear1.removeAllViews();
linear2.removeAllViews();
linear3.removeAllViews();
linear1.setScrollContainer(true);
ImageView image ;
for(int i=0;i<value1;i++){
image = new ImageView(getApplicationContext());
image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) );
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear1.addView(image);
scrollbar1.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
for(int i=0;i<value2;i++){
image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear2.addView(image);
scrollbar2.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
int sum = value1+value2;
for(int i=0;i<sum;i++){
image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear3.addView(image);
scrollbar3.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}
});
}
}
Complete source code here

Button click event issure, layout,and parsing issue?

There are three problems I am facing:
When I click(+)button, the edit boxes are going underneath the button, whereas i want them to be displayed above.
currently displaying:
Want like this:
<edit text1> <edit text2>
<edit text3> <edit text4>
<edit text5> <edit text6>
<edit text7> <edit text8>
<button> <button2>
As you can see, I tried parsing the edit text value which i got from the xml, into the docalc() function, and displaying the value in textbox. but its not working out. showing me nothing.
can I parse values in dostuff, if yes how will i inter-relate those in docalc?
Thanks in advance...
Java Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PlusbuttonActivity extends Activity
implements OnClickListener {
TextView tt;
TextView j;
EditText amount1;
EditText amount2;
double x=0;
double y=0;
double a=0;
double z=0;
double b=0;
Button btnButton;
/** Called when the activity is first created. */
private LinearLayout root;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// modified
amount1=(EditText)findViewById(R.id.edittext1);
amount2=(EditText)findViewById(R.id.edittext2);
// modified
View btnButton =(Button) findViewById(R.id.button_next);
Button mButton = (Button) findViewById(R.id.button);
mButton.setGravity(Gravity.CENTER);
tt=(TextView)findViewById(R.id.tt);
j=(TextView)findViewById(R.id.j);
root = (LinearLayout) findViewById(R.id.linearLayout);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
View view = doStuff();
addViewToRoot(view);
break;
case R.id.button_next:
View view1 = doCalc();
addViewToRoot(view1);
break;
}
}
private View doCalc() {
// TODO Auto-generated method stub
x=Double.parseDouble(amount1.getText().toString());
y=Double.parseDouble(amount2.getText().toString());
z=(x*703);
tt.setText(Double.toString(z));
return tt;
}
private View doStuff() {
EditText t = new EditText(PlusbuttonActivity.this);
t.setGravity(Gravity.LEFT);
t.setWidth(250);
EditText a = new EditText(PlusbuttonActivity.this);
a.setGravity(Gravity.RIGHT);
a.setWidth(250);
LinearLayout l = new LinearLayout(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// t.setBackgroundColor(0xffCECECE);
a.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.addView(t);
l.addView(a);
return l;
}
private void addViewToRoot(View v){
root.addView(v);
}
}
xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
android:text="Units"
android:id="#+id/Units"
/>
<TextView android:layout_height="wrap_content"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="Grades"
android:id="#+id/j"></TextView>
</LinearLayout>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="250px"
android:layout_height="wrap_content"
android:id="#+id/edittext1">
</EditText>
<EditText
android:layout_height="wrap_content"
android:id="#+id/edittext2" android:layout_width="150dp">
</EditText>
</LinearLayout>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="250px"
android:layout_height="wrap_content"
android:id="#+id/edittext3">
</EditText>
<EditText
android:layout_height="wrap_content"
android:id="#+id/edittext4" android:layout_width="150dp">
</EditText>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button android:layout_height="wrap_content"
android:gravity="center" android:id="#+id/button" android:text="+" android:layout_width="wrap_content"></Button>
<Button
android:id="#+id/button_next"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:text="CALCULATE"
>
</Button>
</RelativeLayout>
<TextView android:text="TextView"
android:id="#+id/tt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
First, set your LinearLayout's id to something other than main. Like root. Main is the name of the xml file containing your layout, not the id of the LinearLayout item.
Second, Instead of using this in the constructor for the EditText, use PlusbuttonActivity.this. So change the line you use to create the EditText to this:
EditText t = new EditText(PlusbuttonActivity.this);
You have to do this because when you're in the onClick method your technically in the OnClickListener class, and this refers to the OnClickListener object. By using PlusbuttonActivity.this you're clarifying that you mean the PlusbuttonActivity object that you're currently in, not the OnClickListener.
Third, to address the other issue, you can't reference root from you anonymous OnClickListener class. Instead of calling root.addView(t), you can extract that out to a method. Putting it all together we get this:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class PlusbuttonActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout root;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButton = (Button) findViewById(R.id.button1);
root = (LinearLayout) findViewById(R.id.root);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText t = new EditText(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addViewToRoot(t);
}
});
}
private void addViewToRoot(View v){
root.addView(v);
}
}
Change the id of LinearLayout in main.xml, use different name.
Save & Clean project.
Fix like #Kurtis Nusbaum mentioned above!

Categories

Resources