My app(API 11) creates EditTexts based on user input and gives each an Id. When the user fills each edit text I need to perform a null check, get and send the data to the next activity. But now I'm stuck because I can not refer to them. what should I do?
Thanks
The 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: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="com.wima.civilengineeringcalculator.twodtruss"
tools:showIn="#layout/activity_twodtruss"
android:orientation="vertical"
android:background="#4A148C"
android:layoutDirection="ltr">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fillViewport="true"
android:layout_alignParentTop="true"
android:id="#+id/scr1">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="تحلیل خرپاهای دو بعدی:"
android:textColor="#FFFFFF"
android:id="#+id/textView1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<View
android:layout_height="2dip"
android:layout_width="wrap_content"
android:layout_below="#+id/textView1"
android:background="#4A148C"
android:id="#+id/V3"/>
<View
android:layout_height="2dip"
android:layout_width="wrap_content"
android:layout_below="#+id/V3"
android:background="#4A148C"
android:id="#+id/V5"/>
<View
android:layout_height="2dip"
android:layout_width="wrap_content"
android:layout_below="#+id/V5"
android:background="#FF909090"
android:id="#+id/V4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="تعداد اعضا:"
android:textColor="#FFFFFF"
android:id="#+id/textView4"
android:layout_below="#+id/V4"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/textView4"
android:layout_toStartOf="#+id/textView4"
android:textColor="#FFFFFF"
android:ems="10"
android:id="#+id/editT"
android:layout_below="#+id/V4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:inputType="number" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="تعداد گره های سازه:"
android:id="#+id/textView5"
android:textColor="#FFFFFF"
android:layout_below="#+id/editT"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:textColor="#FFFFFF"
android:id="#+id/editT2"
android:layout_below="#+id/editT"
android:layout_toLeftOf="#+id/textView5"
android:layout_toStartOf="#+id/textView5"
android:layout_alignLeft="#+id/editT"
android:layout_alignStart="#+id/editT"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="تعداد مقاطع مورد نیاز:"
android:textColor="#FFFFFF"
android:id="#+id/textView6"
android:layout_below="#+id/editT2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/editT3"
android:textColor="#FFFFFF"
android:layout_below="#+id/editT2"
android:layout_alignLeft="#+id/editT"
android:layout_toLeftOf="#+id/textView6"
android:layout_alignStart="#+id/editT"
android:layout_toStartOf="#+id/textView6"
android:inputType="number" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="مرحله بعد"
android:textColor="#000000"
android:background="#drawable/btn_selector"
android:id="#+id/button5"
android:layout_below="#+id/editT3"
android:layout_alignLeft="#+id/editT3"
android:layout_alignRight="#+id/editT3"
android:layout_alignStart="#+id/editT3"
android:layout_alignEnd="#+id/editT3"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="راهنما"
android:textColor="#000000"
android:background="#drawable/btn_selector"
android:id="#+id/button6"
android:layout_below="#+id/editT3"
android:layout_alignLeft="#+id/textView5"
android:layout_alignRight="#+id/textView4"
android:layout_alignStart="#+id/textView5"
android:layout_alignEnd="#+id/textView4"/>
<View
android:layout_height="2dip"
android:layout_width="wrap_content"
android:layout_below="#+id/button5"
android:background="#4A148C"
android:id="#+id/V1"/>
<View
android:layout_height="2dip"
android:layout_width="wrap_content"
android:layout_below="#+id/V1"
android:background="#FF909090"
android:id="#+id/V2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="مشخصات مقاطع را وارد نمایید:"
android:textColor="#FFFFFF"
android:id="#+id/textView7"
android:layout_below="#+id/V1"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"/>
<TableLayout 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/sectable"
android:layout_below="#+id/textView7">
</TableLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="مرحله بعد"
android:textColor="#000000"
android:background="#drawable/btn_selector"
android:id="#+id/button1"
android:layout_below="#+id/sectable"
android:layout_alignLeft="#+id/button6"
android:layout_alignRight="#+id/button6"
android:layout_alignStart="#+id/button6"
android:layout_alignEnd="#+id/button6"
android:onClick="next1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="شروع مجدد"
android:textColor="#000000"
android:background="#drawable/btn_selector"
android:id="#+id/button2"
android:layout_below="#+id/sectable"
android:layout_alignLeft="#+id/editT3"
android:layout_alignRight="#+id/editT3"
android:layout_alignStart="#+id/editT3"
android:layout_alignEnd="#+id/editT3"/>
</RelativeLayout>
</ScrollView>
The Java code :
public class MatrixAnalysis extends AppCompatActivity {
private TableLayout mLayout;
private EditText noofsecs;
private EditText Noele;
private EditText Nonode;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_twodtruss);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Noele = (EditText) findViewById(R.id.editT);
Nonode = (EditText) findViewById(R.id.editT2);
noofsecs = (EditText) findViewById(R.id.editT3);
title = (TextView) findViewById(R.id.textView1);
Button bt_calculate1 = (Button) findViewById(R.id.button5);
Button bt_help = (Button) findViewById(R.id.button6);
Button bt_N1 = (Button) findViewById(R.id.button1);
Button bt_reset = (Button) findViewById(R.id.button2);
TextView inter = (TextView) findViewById(R.id.textView7);
bt_help.setVisibility(View.INVISIBLE);
bt_N1.setVisibility(View.INVISIBLE);
bt_reset.setVisibility(View.INVISIBLE);
inter.setVisibility(View.INVISIBLE);
bt_calculate1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if ( Noele.getText().toString().trim().equals("")
|| Nonode.getText().toString().trim().equals("")
|| noofsecs.getText().toString().trim().equals("")) {
Snackbar.make(findViewById(android.R.id.content), "لطفا مقادیر تمامی متغیرها را وارد کنید ", Snackbar.LENGTH_LONG)
.show();
}
else {
double noofsecs1 = Double.valueOf(noofsecs.getText().toString());
for (nsecctr = 1; nsecctr < noofsecs1 + 1; nsecctr++) {
mLayout = (TableLayout) findViewById(R.id.sectable);
prepuserinput();
}
}
}
});
Double Dnoofsecs = 5.0;
int aRows = Dnoofsecs.intValue();
double [] Asec = new double[aRows];
double [] Esec = new double[aRows];
double [] Gsec = new double[aRows];
double [] Iysec = new double[aRows];
double [] Izsec = new double[aRows];
double [] Jsec = new double[aRows];
public void prepuserinput() {
Button bt_calculate1 = (Button) findViewById(R.id.button5);
Button bt_help = (Button) findViewById(R.id.button6);
Button bt_N1 = (Button) findViewById(R.id.button1);
Button bt_reset = (Button) findViewById(R.id.button2);
TextView inter = (TextView) findViewById(R.id.textView7);
Noele = (EditText) findViewById(R.id.editT);
Nonode = (EditText) findViewById(R.id.editT2);
noofsecs = (EditText) findViewById(R.id.editT3);
bt_calculate1.setVisibility(View.INVISIBLE);
bt_help.setVisibility(View.VISIBLE);
bt_N1.setVisibility(View.VISIBLE);
bt_reset.setVisibility(View.VISIBLE);
inter.setVisibility(View.VISIBLE);
Noele.setEnabled(false);
Nonode.setEnabled(false);
noofsecs.setEnabled(false);
Intent i = getIntent();
String pro = i.getStringExtra("Problem");
double pro1 = Double.parseDouble(pro.replace(",", "."));
int width = getApplicationContext().getResources().getDisplayMetrics().widthPixels;
int width1 = width * 13 / 100;
int width2 = width * 12 / 100;
TableRow tr = new TableRow(this);
tr.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PAREN));
TextView textview = new TextView(this);
textview.setId(nsecctr + 0);
textview.setText(":" + nsecctr + "مقطع");
textview.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.MATCH_PARENT,
TableRow.LayoutParams.MATCH_PARENT));
textview.setTextColor(Color.parseColor("#FFFFFF"));
tr.addView(textview);
EditText A = new EditText(this);
A.setId(nsecctr + 0);
A.setHint("A");
A.setLayoutParams(new TableRow.LayoutParams(width2, 50));
A.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
A.setTextColor(Color.parseColor("#FFFFFF"));
A.setHintTextColor(Color.parseColor("#bdbdbd"));
//width, height
tr.addView(A);
EditText E = new EditText(this);
E.setId(nsecctr + 0);
E.setHint("E");
E.setLayoutParams(new TableRow.LayoutParams(width2, 50));
E.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
E.setTextColor(Color.parseColor("#FFFFFF"));
E.setHintTextColor(Color.parseColor("#bdbdbd"));
tr.addView(E);
EditText G = new EditText(this);
G.setId(nsecctr + 0);
G.setHint("G");
G.setLayoutParams(new TableRow.LayoutParams(width2, 50));
G.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
G.setTextColor(Color.parseColor("#FFFFFF"));
G.setHintTextColor(Color.parseColor("#bdbdbd"));
tr.addView(G);
EditText Iy = new EditText(this);
Iy.setId(nsecctr + 0);
Iy.setHint("Iy");
Iy.setLayoutParams(new TableRow.LayoutParams(width1, 50));
Iy.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Iy.setTextColor(Color.parseColor("#FFFFFF"));
Iy.setHintTextColor(Color.parseColor("#bdbdbd"));
tr.addView(Iy);
EditText Iz = new EditText(this);
Iz.setId(nsecctr + 0);
Iz.setHint("Iz");
Iz.setLayoutParams(new TableRow.LayoutParams(width1, 50));
Iz.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
Iz.setTextColor(Color.parseColor("#FFFFFF"));
Iz.setHintTextColor(Color.parseColor("#bdbdbd"));
tr.addView(Iz);
EditText J = new EditText(this);
J.setId(nsecctr + 0);
J.setHint("J");
J.setLayoutParams(new TableRow.LayoutParams(width2, 50));
J.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
J.setTextColor(Color.parseColor("#FFFFFF"));
J.setHintTextColor(Color.parseColor("#bdbdbd"));
tr.addView(J);
mLayout.addView(tr, new TableLayout.LayoutParams(TableLayout.LayoutParams.MATCH_PARENT,
TableLayout.LayoutParams.MATCH_PARENT));
}
public void next1 (View view) {
double noofsecs1 = Double.valueOf(noofsecs.getText().toString());
Jnew = (EditText) findViewById(R.id.editT3);
for (nsecctr = 1; nsecctr < noofsecs1 + 1; nsecctr++) {
mLayout = (TableLayout) findViewById(R.id.sectable);
Asec[nsecctr] = A.getId();
Esec[nsecctr] = E.getId();
Gsec[nsecctr] = G.getId();
Iysec[nsecctr] = Iy.getId();
Izsec[nsecctr] = Iz.getId();
Jsec[nsecctr] = J.getId();
}
}
You should create a HashMap for stores all your EditText
HashMap <String, EditText > mapEditText = new HashMap<String, EditText >();
Whenever you create a EditText programmatically, remember to put it to HashMap
EditText A = new EditText(this);
...
mapEditText.put("A",A);
EditText E = new EditText(this);
...
mapEditText.put("E",E);
....
And in next method, you can access to your EditText via
public void next1 (View view) {
...
Asec[nsecctr] = mapEditText.get("A").getId();
Esec[nsecctr] = mapEditText.get("E").getId();
}
Try changing your next1 as below
public void next1 (View view) {
double noofsecs1 = Double.valueOf(noofsecs.getText().toString());
Jnew = (EditText) findViewById(R.id.editT3);
for (nsecctr = 1; nsecctr < noofsecs1 + 1; nsecctr++) {
mLayout = (TableLayout) findViewById(R.id.sectable);
Asec[nsecctr] = view.A.getId();
Esec[nsecctr] = view.E.getId();
Gsec[nsecctr] = view.G.getId();
Iysec[nsecctr] = view.Iy.getId();
Izsec[nsecctr] = view.Iz.getId();
Jsec[nsecctr] = view.J.getId();
}
}
Related
I have some EditTexts and I'm trying to save their values to an ArrayList, but my EditTexts are being added by using an add button which adds a LinearLayout, and the EditTexts are within the LinearLayouts being created.
Normally having a fixed amount of EditTexts would be easy to save, but how would I save a continuous amount of EditTexts. I'm adding the LinearLayouts in the method createNewLinearLayout() below, and I want to save the input from the EditTexts from those LinearLayouts.
How can I do that?
My layout
<?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">
<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/relativeOverall"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingTop="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
tools:context=".MainActivity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:id="#+id/relativeLayout">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/addCounter"
android:text="Add"
android:layout_marginLeft="0dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/saveBtn"
android:layout_toRightOf="#+id/addCounter"
android:text="Save Data"
android:layout_marginLeft="30dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnView"
android:text="View Data"
android:layout_marginLeft="30dp"
android:layout_toRightOf="#+id/saveBtn" />
</RelativeLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:id="#+id/horizontalLine"
android:background="#B3B3B3"
android:layout_below="#+id/relativeLayout"
android:layout_marginTop="10dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/nameEditText"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:hint="Name of Patient"
android:layout_below="#+id/horizontalLine"/>
<View
android:layout_width="match_parent"
android:layout_height="1sp"
android:id="#+id/horizontalLine2"
android:background="#B3B3B3"
android:layout_below="#+id/nameEditText"
android:layout_marginTop="0dp"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/idEditText"
android:textAlignment="center"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:hint="ID of Patient"
android:layout_below="#+id/horizontalLine2"/>
<View
android:layout_width="match_parent"
android:layout_height="1sp"
android:id="#+id/horizontalLine3"
android:background="#B3B3B3"
android:layout_below="#+id/idEditText"
android:layout_marginTop="0dp"/>
<EditText
android:layout_width="245dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/actionEditText"
android:textAlignment="center"
android:hint="Action To Record"
android:layout_below="#+id/horizontalLine3"/>
<EditText
android:layout_width="245dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/durationEditText"
android:textAlignment="center"
android:hint="Duration of Action"
android:layout_below="#+id/actionEditText"/>
<LinearLayout
android:id="#+id/linearLayout"
android:layout_below="#+id/durationEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="horizontal"
android:layout_marginTop="0dp">
<Button
android:id="#+id/minusBtn"
android:layout_width="55sp"
android:layout_height="55sp"
android:text="-"
android:textSize="25dp"/>
<TextView
android:id="#+id/counterTxt"
android:layout_width="wrap_content"
android:layout_height="55dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:textSize="45sp"
android:text="25"/>
<Button
android:id="#+id/plusBtn"
android:layout_width="55sp"
android:layout_height="55sp"
android:text="+"
android:textSize="25sp"/>
</LinearLayout>
<Button
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/delete"
android:layout_below="#+id/linearLayout"
android:text="Delete"
android:textAlignment="center"
android:layout_centerHorizontal="true"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:orientation="vertical"
android:id="#+id/linearLayoutText"
android:layout_below="#+id/horizontalLine3">
</LinearLayout>
</RelativeLayout>
</ScrollView>
My method
private LinearLayout createNewLinearLayout(){
final LinearLayout.LayoutParams lparams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lparams.gravity = Gravity.CENTER_HORIZONTAL;
final ViewGroup.LayoutParams btnParam = plusBtn.getLayoutParams();
final ViewGroup.LayoutParams textParam = counterTxt.getLayoutParams();
final LinearLayout.LayoutParams deleteParam = new LinearLayout.LayoutParams(delete.getLayoutParams());
deleteParam.gravity = Gravity.CENTER_HORIZONTAL;
final LinearLayout.LayoutParams editParams = new LinearLayout.LayoutParams(actionEditText.getLayoutParams());
editParams.gravity = Gravity.CENTER_HORIZONTAL;
final LinearLayout.LayoutParams durationParams = new LinearLayout.LayoutParams(durationEditText.getLayoutParams());
durationParams.gravity = Gravity.CENTER_HORIZONTAL;
LinearLayout.LayoutParams line = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,TypedValue.COMPLEX_UNIT_SP, 1);
line.setMargins(0,15,0,0);
final LinearLayout newLinear = new LinearLayout(this);
newLinear.setLayoutParams(lparams);
final LinearLayout combined = new LinearLayout(this);
combined.setLayoutParams(lparams);
final Button plus = new Button(this);
plus.setLayoutParams(btnParam);
final Button minus = new Button(this);
minus.setLayoutParams(btnParam);
final TextView count = new TextView(this);
count.setLayoutParams(textParam);
final EditText editText = new EditText(this);
editText.setLayoutParams(editParams);
editText.setHint("Action To Record");
editText.setGravity(Gravity.CENTER_HORIZONTAL);
final View v = new View(this);
v.setLayoutParams(line);
v.setBackgroundColor(Color.parseColor("#B3B3B3"));
final EditText duration = new EditText(this);
duration.setLayoutParams(durationParams);
duration.setHint("Duration of Action");
duration.setGravity(Gravity.CENTER_HORIZONTAL);
final Button delete = new Button(this);
delete.setLayoutParams(deleteParam);
delete.setText("delete");
delete.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
linearLayoutText.removeView(combined);
}
});
plus.setText("+");
plus.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
plus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
counting[0]++;
count.setText(counting[0] + "");
}
});
minus.setText("-");
minus.setTextSize(TypedValue.COMPLEX_UNIT_SP, 25);
minus.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
if (counting[0] > 0)
counting[0]--;
count.setText(counting[0] + "");
}
});
count.setTextSize(TypedValue.COMPLEX_UNIT_SP, 45);
newLinear.setOrientation(LinearLayout.HORIZONTAL);
newLinear.setGravity(Gravity.CENTER_HORIZONTAL);
combined.setOrientation(LinearLayout.VERTICAL);
combined.setGravity(Gravity.CENTER_HORIZONTAL);
newLinear.addView(minus);
newLinear.addView(count);
newLinear.addView(plus);
combined.addView(editText);
combined.addView(duration);
combined.addView(newLinear);
combined.addView(delete);
combined.addView(v);
counting[0] = 0;
count.setText(counting[0] + "");
return combined;
}
}
I suggest :
for (int i =0;i<LinearLayout.getChildCount();i++)
{
EditText edt= new EditText (this);
edt = LinearLayout.getChildAt(i);
YourAarraylist[i]=edt.getText().toString();
}
If you have multiple views inside your LinearLayout you can repeat the for like this :
for (int i =0;i<LinearLayout.getChildCount();i++)
{
LinearLayout secondLayout= (LinearLayout) LinearLayout.getChildAt(i);
// and then:
EditText edt= new EditText (this);
edt = secondLayout.getChildAt(indexofyoutEdittext);//You should know this because it's the order of the views you add in your layout
YourAarraylist[i]=edt.getText().toString();
}
I hope it helps !
**activity_main 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_main"
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.hynes.equations.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please select what type of equation to solve"
android:id="#+id/textView2" />
<Button
android:text="Check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView3"
android:layout_alignEnd="#+id/textView3"
android:layout_marginRight="230dp"
android:layout_marginEnd="230dp"
android:id="#+id/button2" />
<TextView
android:layout_centerVertical="true"
android:id="#+id/textView5"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_height="50dp"
android:layout_width="250dp"
android:layout_alignRight="#+id/textView2"
android:layout_alignEnd="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:layout_above="#+id/button2"
android:layout_alignRight="#+id/textView5"
android:layout_alignEnd="#+id/textView5"
android:layout_marginRight="170dp"
android:layout_marginEnd="170dp"
android:layout_marginBottom="36dp"
android:id="#+id/editText2" />
<RadioGroup android:id="#+id/radio_group"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<RadioButton
android:text="Addition"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="26dp"
android:id="#+id/addition"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:text="Subtraction"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="14dp"
android:id="#+id/subtraction"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:text="Division"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="16dp"
android:id="#+id/division"
android:onClick="onRadioButtonClicked"/>
<RadioButton
android:text="Multiplication"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/radioButton3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:id="#+id/multiplication"
android:onClick="onRadioButtonClicked"/>
</RadioGroup>
`
MAIN ACTIVITY
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;`
import android.widget.RadioGroup;
import android.widget.TextView;
import java.util.Random;
public class MainActivity extends AppCompatActivity {
private int x;
private int y;
public int a;
public int b;
Random random = new Random();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void onRadioButtonClicked(View view){
TextView display = (TextView) findViewById(R.id.textView5);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
int btnID = radioGroup.getCheckedRadioButtonId();
switch(btnID){
case R.id.addition:
x = random.nextInt(100);
y = random.nextInt(100);
display.setText(x + "+" + y + "=");
break;
case R.id.subtraction:
x = random.nextInt(100);
y = random.nextInt(100);
display.setText(x + "-" + y + "=");
break;
case R.id.division:
y = random.nextInt(5);
x = y*2;
display.setText(x + "/" + y + "=");
break;
case R.id.multiplication:
x = random.nextInt(10);
y = random.nextInt(10);
display.setText(x + "*" + y + "=");
break;
}
public void onClickCheck(View view){
TextView result = (TextView) findViewById(R.id.textView5);
EditText ans = (EditText) findViewById(R.id.editText2);
RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radio_group);
int btnID = radioGroup.getCheckedRadioButtonId();
if(boolean(RadioButton) findViewById(R.id.addition)) ){
int key = x+y;
int try = 0;
while(!ans.equals(key)){
try = try + 1;
if(try ==0){
result.setText("Incorrect, try again!");
try++
}else if (try>2){
result.setText("Incorrect, the answer is : " + key);
}
if(ans.equals(key)){
result.setText("You are correct !");
}
}
}
}
}
}
}
This is a android studio app that's suppose to randomly generate a math equation upon creation which I've done. There's addition, subtraction, division, and multiplication. My random equation generator works perfectly. But I'm stuck at my onClickCheck method which i want to get the user's answer/input and check to see if it's correct or incorrect and display a message accordingly. My onClick method is wrong but it's a start. I don't know what methods i need to get my code to accept inputs properly. Any help would be appreciated.
It looks like you aren't assigning an OnClickListener to the button.
You need to put:(in onCreate)
Button button = (Button)findViewById(R.id.button2);
button.setOnClickListener(onClickCheck);
or
Button button = (Button)findViewById(R.id.button2);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClickCheck(view);
}
});
Same for the radio button.
I wanted to create a simple quadratic equation solver but when I run it, it gets caught at the first if statement of each button. Any ideas?
public class MainActivity extends AppCompatActivity {
Button button1, button2;
String a_temp;
EditText b_temp;
EditText c_temp;
TextView tv1, tv2;
Double a,b,c;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG).setAction("Action", null).show();
}
});
a_temp = ((EditText)findViewById(R.id.editText)).getText().toString();
if (a_temp.equals("")) {
a = Double.valueOf(0);
} else {
a = Double.valueOf(a_temp);
}
b_temp = (EditText)findViewById(R.id.editText2);
if (!b_temp.getText().toString().equals("")) {
b = Double.parseDouble(b_temp.getText().toString());
}else {
b = Double.valueOf(0);
}
c_temp = (EditText)findViewById(R.id.editText3);
if (!c_temp.getText().toString().equals("")) {
c = Double.parseDouble(c_temp.getText().toString());
}else {
c = Double.valueOf(0);
}
tv1 = (TextView) findViewById(R.id.textView2);
tv2 = (TextView) findViewById(R.id.textView3);
button1 = (Button) findViewById(R.id.button);
button2 = (Button) findViewById(R.id.button2);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (a == 0) {
tv1.setText("That's not a quadratic equation");
}else {
if (b*b - 4*a*c < 0) {
tv1.setText("This quadratic equation has no real roots");
} else {
double root1 = (-b + Math.sqrt(Math.pow(b,2) - 4 * a * c))/(2*a);
double root2 = (-b - Math.sqrt(Math.pow(b,2) - 4 * a * c))/(2*a);
double result = Math.max(root1,root2);
tv1.setText(String.valueOf(result));
}
}
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (a == 0) {
tv2.setText("That's not a quadratic equation");
}else {
if (b * b - 4 * a * c < 0) {
tv2.setText("This quadratic equation has no real roots");
} else {
double root1 = (-b + Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
double root2 = (-b - Math.sqrt(Math.pow(b, 2) - 4 * a * c)) / (2 * a);
double result = Math.min(root1, root2);
tv2.setText(" " + result);
}
}
};
});
}
XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_main" tools:context=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Type the operands of the quadratic equation you want to solve"
android:id="#+id/textView"
android:layout_marginTop="39dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Root #1"
android:id="#+id/button"
android:layout_marginTop="60dp"
android:layout_below="#+id/editText3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Root #2"
android:id="#+id/button2"
android:layout_marginTop="69dp"
android:layout_alignTop="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textView2"
android:layout_alignBottom="#+id/button"
android:layout_alignTop="#+id/button"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView"
android:layout_toRightOf="#+id/button"
android:layout_toEndOf="#+id/button"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="false" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/textView3"
android:layout_alignBottom="#+id/button2"
android:layout_toRightOf="#+id/button2"
android:layout_alignTop="#+id/button2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number|numberSigned"
android:ems="10"
android:id="#+id/editText"
android:layout_marginTop="32dp"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number|numberSigned"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number|numberSigned"
android:ems="10"
android:id="#+id/editText3"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/textView2"
android:layout_toStartOf="#+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="x^2"
android:id="#+id/textView4"
android:layout_alignTop="#+id/editText"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="x"
android:id="#+id/textView5"
android:layout_below="#+id/editText"
android:layout_alignLeft="#+id/textView4"
android:layout_alignStart="#+id/textView4" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="=0"
android:id="#+id/textView6"
android:layout_alignBottom="#+id/editText3"
android:layout_alignLeft="#+id/textView5"
android:layout_alignStart="#+id/textView5" />
</RelativeLayout>
You are comparing double values with ==.
The problem is that it will likely not be equal 0, because of the minor error it has when storing double values. For example, it will be something like 0.000000001 or -0.00000001. Instead, compare like this:
if (Math.abs(a) < 0.0000001) { ... }
Also you forgot to divide your roots by 2 * a.
So the main problem I see is, that you are assigning your values for a, b and c in the onCreate method of your activity class.
This means, as soon as your activity is created, i.e. before the user had any time to input any data into your EditText fields, you are trying to read said non-existing input - i.e. 0.
This is why in each onClick method, the first if() clause which checks for zero-values, returns true. Because the values are zero.
What you should do is move this part of each variable:
if (!c_temp.getText().toString().equals("")) {
c = Double.parseDouble(c_temp.getText().toString());
}else {
c = Double.valueOf(0);
}
into your OnClickListener classes, i.e. into the onClick methods, to "refresh" the values you are using in your calculations based on the current user input.
I am creating a simple calculator and I use radio buttons for choosing operators, but I faced the problem with radio button check. I hope somebody will help me. Thanks in advance.
Here are the following codes:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/tekstas"
android:id="#+id/textas"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="#+id/nr1"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="#+id/nr2"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="#+id/nr3"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="number"
android:id="#+id/nr4"
android:layout_marginTop="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_gravity="center_horizontal" />
<RadioGroup xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/veiksmai">
<RadioButton android:id="#+id/plius"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/pliusas"
android:padding="30dp"
android:onClick="onRadioButtonClicked"
/>
<RadioButton android:id="#+id/minus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/minusas"
android:padding="30dp"
android:onClick="onRadioButtonClicked"
/>
<RadioButton android:id="#+id/daugyba"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/daugyba"
android:padding="30dp"
android:onClick="onRadioButtonClicked"
/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/rezultatas"
android:id="#+id/btn"
android:layout_gravity="center_horizontal"
android:onClick="onClick"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_gravity="center_horizontal" />
</LinearLayout>
java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View view) {
RadioGroup rg =(RadioGroup) findViewById(R.id.veiksmai);
EditText n1 = (EditText) findViewById(R.id.nr1);
EditText n2 = (EditText) findViewById(R.id.nr2);
EditText n3 = (EditText) findViewById(R.id.nr3);
EditText n4 = (EditText) findViewById(R.id.nr4);
EditText edtResult = (EditText) findViewById(R.id.editText);
int selected = rg.getCheckedRadioButtonId();
RadioButton plus2 = (RadioButton) findViewById(selected);
RadioButton minus2 = (RadioButton) findViewById(selected);
RadioButton multi = (RadioButton) findViewById(selected);
if("+".equals(plus2.getText())) {
float result = Float.parseFloat(n1.getText().toString()) + Float.parseFloat(n2.getText().toString())+ Float.parseFloat(n3.getText().toString()) + Float.parseFloat(n4.getText().toString());
edtResult.setText(Float.toString(result));
}else if("-".equals(minus2.getText())){
float result = Float.parseFloat(n1.getText().toString()) - Float.parseFloat(n2.getText().toString())- Float.parseFloat(n3.getText().toString())- Float.parseFloat(n4.getText().toString());
edtResult.setText(Float.toString(result));
}else if("*".equals(multi.getText())){
float result = Float.parseFloat(n1.getText().toString()) * Float.parseFloat(n2.getText().toString())* Float.parseFloat(n3.getText().toString())* Float.parseFloat(n4.getText().toString());
edtResult.setText(Float.toString(result));
}
}
};
// Getting reference of the button btnResult
Button btn = (Button) findViewById(R.id. btn);
// Setting onclick listener
btn.setOnClickListener(listener);
}
}
You need to id your elements in onCreate:
RadioGroup rg =(RadioGroup) findViewById(R.id.veiksmai);
EditText n1 = (EditText) findViewById(R.id.nr1);
EditText n2 = (EditText) findViewById(R.id.nr2);
EditText n3 = (EditText) findViewById(R.id.nr3);
EditText n4 = (EditText) findViewById(R.id.nr4);
EditText edtResult = (EditText) findViewById(R.id.editText);
RadioButton plus2 = (RadioButton) findViewById(R.id.plius);
RadioButton minus2 = (RadioButton) findViewById(R.id.minus);
RadioButton multi = (RadioButton) findViewById(R.id.daugyba);
And the attach an onCheckedChangedListener to it, not an onClick listener:
rg.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup arg0, int arg1) {
if (arg0.getChildAt(0).isPressed()
|| arg0.getChildAt(1).isPressed()
|| arg0.getChildAt(2).isPressed()) {
if (arg0.getChildAt(0).isPressed()) {
//do your stuff here
} else if (arg0.getChildAt(1).isPressed()) {
//do your stuff here
} else if (arg0.getChildAt(2).isPressed()) {
//do your stuff here
}
}
}
});
In your xml you have
android:inputType="number"
this does not allow decimals or negatives. if you want decimals use
android:inputType="numberDecimal"
if you want to allow decimals and negative numbers use
android:inputType="numberDecimal|numberSigned"
When you use:
android:onClick="onRadioButtonClicked"
All you need to do is make a method passing in a view and do whatever it is you want on button (or radioButton) click. I made each radio button have their own method named onRadioButtonClicked for plus, onRadioButtonClicked1 for minus , and onRadioButtonClicked2 for multiplication. Then used the changed sign to get results in an onClickListener for a button.
private char sign;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RadioGroup rg = (RadioGroup) findViewById(R.id.veiksmai);
final EditText n1 = (EditText) findViewById(R.id.nr1);
final EditText n2 = (EditText) findViewById(R.id.nr2);
final EditText n3 = (EditText) findViewById(R.id.nr3);
final EditText n4 = (EditText) findViewById(R.id.nr4);
final EditText edtResult = (EditText) findViewById(R.id.editText);
RadioButton plus2 = (RadioButton) findViewById(R.id.plius);
RadioButton minus2 = (RadioButton) findViewById(R.id.minus);
RadioButton multi = (RadioButton) findViewById(R.id.daugyba);
Button btn = (Button) findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (sign == '+') {
float result = Float.parseFloat(n1.getText().toString()) + Float.parseFloat(n2.getText().toString()) + Float.parseFloat(n3.getText().toString()) + Float.parseFloat(n4.getText().toString());
edtResult.setText(Float.toString(result));
} else if (sign == '-') {
float result = Float.parseFloat(n1.getText().toString()) - Float.parseFloat(n2.getText().toString()) - Float.parseFloat(n3.getText().toString()) - Float.parseFloat(n4.getText().toString());
edtResult.setText(Float.toString(result));
} else if (sign == '*') {
float result = Float.parseFloat(n1.getText().toString()) * Float.parseFloat(n2.getText().toString()) * Float.parseFloat(n3.getText().toString()) * Float.parseFloat(n4.getText().toString());
edtResult.setText(Float.toString(result));
}
}
});
}
public void onRadioButtonClicked(View view) {
sign = '+';
}
public void onRadioButtonClicked1(View view) {
sign = '-';
}
public void onRadioButtonClicked2(View view) {
sign = '*';
}
I want to add something like scrolling from top to bottom and vice versa but i don't know how to add this function, i was trying with ScrollView but that gave nothing.
MainActivity.java :
package com.example.marek.przelicznikpaliw;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private EditText a1;
private EditText b1;
private EditText c1;
private EditText d1;
private TextView tv;
private TextView tv2;
private TextView tv3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//pola
a1 = (EditText) findViewById(R.id.cel);
b1 = (EditText) findViewById(R.id.pali);
c1 = (EditText) findViewById(R.id.litr);
d1 = (EditText) findViewById(R.id.koszt);
// wynik
tv = (TextView) findViewById(R.id.wynikKoszt);
tv2 = (TextView) findViewById(R.id.wynikKilometry);
tv3 = (TextView) findViewById(R.id.wynikKosztx2);
findViewById(R.id.button).setOnClickListener(onClickListener);
findViewById(R.id.button2).setOnClickListener(onClickListener);
}
private View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
float a = 0;
float b = 0;
float c = 0;
float d = 0;
float wynik = 0;
float wynik2 = 0;
float wynik3 = 0;
try {
a = Float.parseFloat(a1.getText().toString());
b = Float.parseFloat(b1.getText().toString());
c = Float.parseFloat(c1.getText().toString());
d = Float.parseFloat(d1.getText().toString());
} catch (Exception e) {
e.printStackTrace();
}
if (view.getId() == R.id.button) {
wynik = ((b / 100) * a) * c;
tv.setText(String.valueOf(wynik));
wynik2 = (((b / 100) * a) * c)*2;
tv3.setText(String.valueOf(wynik2));
} else if (view.getId() == R.id.button2) {
wynik3 = d / ((b * c) / 100);
tv2.setText(String.valueOf(wynik3));
}
}
};
}
activity_main.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: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"
android:orientation="vertical"
android:onClick="onButtonClick"
android:weightSum="1">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/cel"
android:hint="Ilosc km do celu to:"
android:layout_centerHorizontal="true"
android:numeric="integer|decimal" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pali"
android:hint="Ile litrow pali na 100km"
android:layout_centerHorizontal="true"
android:layout_below="#+id/cel"
android:numeric="integer|decimal" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/litr"
android:hint="Cena za litr"
android:layout_below="#+id/pali"
android:layout_centerHorizontal="true"
android:numeric="integer|decimal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_below="#+id/litr"
android:layout_centerHorizontal="true"
android:hint="Licz zlotowki"
android:text="Licz koszty"
android:clickable="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/koszt"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:hint="Za ile zl zostalo zatankowane gaz/paliwo?"
android:numeric="integer|decimal" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Licz kilometry"
android:id="#+id/button2"
android:layout_below="#+id/koszt"
android:layout_centerHorizontal="true"
android:clickable="true" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Dojedziesz w wybrany cel za(zł):"
android:id="#+id/textView" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/emptystring"
android:id="#+id/wynikKoszt"
android:textIsSelectable="false" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Oraz spowrotem za(zł):"
android:id="#+id/textView2" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/emptystring"
android:id="#+id/wynikKosztx2"
android:layout_weight="0.06" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Za tę kwotę przejedziesz(km):"
android:id="#+id/textView3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/emptystring"
android:id="#+id/wynikKilometry" />