Reputation: 1
Im new in programing especially in java and Android studio.
I want to create activity where you can add or remove EditText with button. When all data is entered I want to calculate something by pressing calculate button and pass value to the next activity. I am attaching a picture of how it should look like. Any help is appreciated, thanks.
Upvotes: 0
Views: 607
Reputation: 740
To add a view use addView()
method and to remove a view use removeView()
. For example I'm using ConstraintLayout
here , you can try on others layout also.
activity_main.xml
<?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:id="@+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="20dp"
android:gravity="center_horizontal">
<Button
android:id="@+id/add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="4dp"
android:layout_marginLeft="4dp"
android:layout_marginBottom="18dp"
android:text="add"
app:layout_constraintBottom_toTopOf="@+id/remove"
app:layout_constraintStart_toStartOf="@+id/remove" />
<Button
android:id="@+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="144dp"
android:text="remove"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity-
public class MainActivity extends AppCompatActivity {
private Button add, remove;
private EditText editText;
private String editTextName;
private ConstraintLayout constraintLayout;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
add = (Button) findViewById(R.id.add);
remove = (Button) findViewById(R.id.remove);
AlertDialog.Builder alertdialog = new AlertDialog.Builder(this);
add.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.O)
@Override
public void onClick(View v) {
final EditText edittext = new EditText(getApplicationContext());
alertdialog.setMessage("Input a Name");
alertdialog.setTitle("Add New Item");
alertdialog.setView(edittext);
alertdialog.setPositiveButton("Add", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
editTextName = edittext.getText().toString();
constraintLayout = (ConstraintLayout) findViewById(R.id.main);
textView = new TextView(getApplicationContext());
textView.setText(editTextName);
constraintLayout.addView(textView);
editText = new EditText(getApplicationContext());
constraintLayout.addView(editText);
}
});
alertdialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// TODO
}
});
alertdialog.show();
}
});
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (editTextName != null) {
constraintLayout.removeView(editText);
constraintLayout.removeView(textView);
}
}
});
}
}
If you want run time layout design , please keep eye on layoutparams
.
Upvotes: 0
Reputation: 777
You can setVisibility
of the EditText
to hide and show.
Try the following code:
Button btn=findViewById(R.id.btn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
editText.setVisibility(View.GONE);
}
});
And if you want to sent data from one activity to another you can send it through Intent
.
Intent i=new Intent(this,SomeActivity.class);
i.putExtra("key","somevalue");
startActivity(i);
You can get value by getIntent()
in the next actcivity.
String key=getIntent().getStringExtra("key");
Upvotes: 2
Reputation: 3673
I suggest to take a look how to make a calculator first. You need to define operators (+,-,/,*
) which will allow you to calculate. I can provide you a very good article called Simple Calculator App in Android Studio for Beginners that will show you how it can be achieved.
Upvotes: 0