How to Get Data from AutoComplete Text View on Button Press? - java

I want to use the if-else statement on Button press - if-else statement get the string from autoComplete text view... but it not works...
I want to make the calculation when selecting the first product - but it does not work.
public class MainActivity extends AppCompatActivity {
TextInputLayout textInputLayout,alkalinityInput,waterVoume;
TextInputEditText waterText,alkaText;
TextView result;
AutoCompleteTextView dropDownText;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
waterVoume=findViewById(R.id.waterVolumeInputText);
textInputLayout = findViewById(R.id.productTextDropdown);
dropDownText = findViewById(R.id.dropdown_text);
alkalinityInput = findViewById(R.id.alkalinityInputText);
waterText = findViewById(R.id.waterVtext);
alkaText = findViewById(R.id.alkaVtext);
result=findViewById(R.id.resultTextView);
String[] items = new String[] {
"AquaForest - KH Plus",
"Brightwell - Reef Code B",
};
ArrayAdapter<String> adapter = new ArrayAdapter<>(
MainActivity.this,
R.layout.dropdown_item,
items
);
dropDownText.setAdapter(adapter);
button=findViewById(R.id.calculateButton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(dropDownText.getText().equals("AquaForest - KH Plus"))
{makeCalculations();
}
}
private void makeCalculations() {
double vouumeValue = Double.valueOf(waterText.getText().toString());
double alkvalue = Double.valueOf(alkaText.getText().toString());
double resultView = 1*vouumeValue*alkvalue;
result.setText("The result is: " + resultView );
double resultView = 5*vouumeValue*alkvalue;
result.setText("The result is: " + resultView );}*/
});
}
}

Related

Moving from one activity to another based on the input in three spinners

I have three spinners in my activity. So I have to open specific activity based on the input of three spinners; if I have entered "1" in my first spinner "cse" in my second spinner and "b" in my third spinner I have to open the respected activity and so on for different combinations.
I have tried using if statements and the app is crashing every time since.
Please beware of the comments in the code those are the codes which I have tried and didn't get any result.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Button btn;
/*TextView selection1;
TextView selection2;
TextView selection3;*/
Spinner spin1;
Spinner spin2;
Spinner spin3;
String[] years = {"1","2","3","4"};
String[] branches = {"CSE","ECE","EEE","CIVIL","IT","MECH"};
String[] sections = {"A","B","C"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin1 = (Spinner) findViewById(R.id.spinner1);
spin1.setOnItemSelectedListener(this);
/*List<String> branches = new ArrayList<String>();
branches.add("CSE");
branches.add("ECE");
branches.add("IT");
branches.add("MECH");
branches.add("CIVIL");
branches.add("EEE");*/
ArrayAdapter<String> dataAdapter =new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,years);
//Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
//attaching dataAdapter to spinner
spin1.setAdapter(dataAdapter);
Spinner spin2 = (Spinner) findViewById(R.id.spinner2);
spin2.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter2 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,branches);
dataAdapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin2.setAdapter(dataAdapter2);
Spinner spin3 = (Spinner) findViewById(R.id.spinner3);
spin3.setOnItemSelectedListener(this);
ArrayAdapter<String> dataAdapter3 = new ArrayAdapter<String>(this,android.R.layout.simple_spinner_item,sections);
dataAdapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spin3.setAdapter(dataAdapter3);
Button btn= (Button) findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displaycheck();
}
});
}
public void displaycheck(){
/*Spinner s1 = (Spinner)findViewById(R.id.spinner1);
Spinner s2 = (Spinner)findViewById(R.id.spinner2);
Spinner s3 = (Spinner)findViewById(R.id.spinner3);
String sp1 = s1.getPrompt().toString();
String sp2 = s2.getPrompt().toString();
String sp3 = s3.getPrompt().toString();
if (sp1.equals("3")&&sp2.equals("CSE")&&sp3.equals("B")){*/
Toast.makeText(this, "Welcome", Toast.LENGTH_LONG).show();
Intent it = new Intent(this,M2.class);
startActivity(it);
//}
/*else{
Toast.makeText(this, "Invalid input", Toast.LENGTH_LONG).show();
}*/
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
//on selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
/*if (spin1.getSelectedItem().toString().equals("3")) {
if (spin2.getSelectedItem().toString().equals("CSE")) {
if (spin3.getSelectedItem().toString().equals("B")) {*/
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
/*Intent it = new Intent(this, M2.class);
startActivity(it);*/
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
/*Button btn =(Button)findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
displaycheck();
}
}
);
}
public void displaycheck(){
Spinner s1 =(Spinner) findViewById(R.id.spinner1);
Spinner s2 = (Spinner) findViewById(R.id.spinner2);
Spinner s3 = (Spinner) findViewById(R.id.spinner3);
String ye = y.getText().toString();
String br = b.getText().toString();
String se = s.getText().toString();
if (ye.equals("3")&&br.equals("cse")&&se.equals("b")) {
Toast.makeText(this, "ra ra ", Toast.LENGTH_LONG).show();
Intent it = new Intent(this, M2.class);
startActivity(it);
}
else{
Toast.makeText(this, "po po", Toast.LENGTH_LONG).show();
}
}*/
}
You are not giving value to variable which are global - Activity Scope - spin1, spin2, spin3.
Instead your are giving value to onCreate or Method specific scope; when you're fetching value from it it's never initialized.
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
private Button btn;
Spinner spin1;
Spinner spin2;
Spinner spin3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// get spinner in variable
spin1 = (Spinner) findViewById(R.id.spinner1);
...
// Your adapter, ItemListener, DropDownViewResource
spin2 = (Spinner) findViewById(R.id.spinner2);
... // Your adapter, ItemListener, DropDownViewResource
spin3 = (Spinner) findViewById(R.id.spinner3);
... // Your adapter, ItemListener, DropDownViewResource
Button btn= (Button) findViewById(R.id.display);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
displaycheck();
}
});
}
public void displaycheck(){
String sp1 = spin1.getSelectedItem().toString();
String sp2 = spin2.getSelectedItem().toString();
String sp3 = spin3.getSelectedItem().toString();
// Your combinations check
if (sp1.equals("3")&&sp2.equals("CSE")&&sp3.equals("B")){
// based on combination - redirect user
}
}
}
To get selected item from spinner you use this
String value1 = s1.getSelectedItem().toString();
String value2 = s2.getSelectedItem().toString();
String value3 = s3.getSelectedItem().toString();
Now you can check the values with any logic for example:
if(value1.equals("a") && value2.equals("cse") && value3.equals("b"))
openActivityCodeHere();

Need to sum up the total amount and create order summary in another intent/activity

XML LAYOUT SCREENSHOT
Trying to calculate the final amount of selected items in an another activity. Need to create order summary in another activity.
Tried introducing multiplication function but also need to hard code the quantity values to auto calculate the final amount.
public class MainActivity extends AppCompatActivity {
EditText pantqty;
EditText pantrate;
TextView result;
Button SUBMIT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pantqty = (EditText) findViewById(R.id.et_pantqty);
pantrate = (EditText) findViewById(R.id.et_pantrate);
SUBMIT = (Button) findViewById(R.id.et_submit);
result = (TextView) findViewById(R.id.et_result);
SUBMIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
int q = Integer.valueOf(pantqty.getText().toString());
float r = Integer.valueOf(pantrate.getText().toString());
float amount = q * r;
result.setText(Float.toString(amount));
}
});
}
}
Need to create a method in OnCLickListener to calculate the amount from the items selected.can if else case be used on OnclickListerButton?
Thanks for the help.
You can do something like this:
public class MainActivity extends AppCompatActivity {
EditText pantqty;
EditText pantrate;
EditText shirtQty;
EditText shirtRate;
TextView result;
Button SUBMIT;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pantqty = (EditText) findViewById(R.id.et_pantqty);
pantrate = (EditText) findViewById(R.id.et_pantrate);
shirtQty = (EditText) findViewById(R.id.shirtqtyId);
shirtRate = (EditText) findViewById(R.id.shirtRateId);
SUBMIT = (Button) findViewById(R.id.et_submit);
result = (TextView) findViewById(R.id.et_result);
SUBMIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
calculateSum();
}
});
}
private void calculateSum(){
int q=0;
String pantQty=pantqty.getText().toString().trim();
if(!pantQty.isEmpty() && pantQty!="") {
q=Integer.valueOf(pantQty);
}
float r = Integer.valueOf(pantrate.getText().toString());
int a = Integer.valueOf(shirtQty.getText().toString());
float b = Integer.valueOf(shirtRate.getText().toString());
float amount = (q * r)+(a*b);
result.setText(Float.toString(amount));
Intent intent=new Intent(this,SecondActivity.class);
intent.putExtra("total",amount);
startActivity(intent);
}
}
On the secondActivity you get get the total as:
float total=getIntent().getFloatExtra("total",0.0);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pantqty = (EditText) findViewById(R.id.lo_pantqty);
shirtqty = (EditText) findViewById(R.id.lo_shirtqty);
shareesqty = (EditText) findViewById(R.id.lo_shareesqty);
suitqty = (EditText) findViewById(R.id.lo_suitqty);
result = (TextView) findViewById(R.id.et_result);
SUBMIT = (Button) findViewById(R.id.io_enter);
SUBMIT.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
calculationSum();
}
});
}
private void calculationSum() {
int q = 0;
String pantQTY = pantqty.getText().toString().trim();
if (!pantQTY.isEmpty()&&pantQTY!=""){
q = Integer.valueOf(pantQTY);
}
int r = 0;
String shirtQTY = shirtqty.getText().toString().trim();
if (!shirtQTY.isEmpty()&&shirtQTY!=""){
r = Integer.valueOf(shirtQTY);
}
int a = 0;
String suitQTY = suitqty.getText().toString().trim();
if (!suitQTY.isEmpty()&&suitQTY!=""){
a = Integer.valueOf(shirtQTY);
}
int b = 0;
String shareesQTY = shareesqty.getText().toString().trim();
if (!shareesQTY.isEmpty()&&shareesQTY!=""){
b = Integer.valueOf(shirtQTY);
}
int amount = (q*30) + (r*35) + (a*220) + (b*60);
result.setText(Integer.toString(amount));
}
}

Create different textviews for list of strings

I have a class which I use a camera to point at an object and get all the strings it can see on the object. I then pass the list into this intent so the user can choose a specific string which I will then use to send to another activity.
However, I want to create a new textview for each string and then put an OnClickListener on each of the textviews, but I am unsure on how I can put an OnClickListener on a textview that I won't know exist as it will be created depending on the list.
public class SelectProductName extends AppCompatActivity {
ArrayList<String> getString = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_product_name);
getString = getIntent().getStringArrayListExtra("stringList");
LinearLayout ll = findViewById(R.id.layout_select_product_name);
for (int i = 0; i < getString.size();i++){
TextView text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
text.setText(getString.get(i));
System.out.println(getString.get(i));
ll.addView(text);
}
}
}
So for example: getString is {"Chicken","Pie"}
I want a textview to be created for each string.
I'm unsure on how I would get the name of the textview to add the OnClickListener to it.
You can add the listener just like you would do with any view:
LinearLayout ll = findViewById(R.id.layout);
for (int i = 0; i < getString.size();i++){
TextView text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
text.setText(getString.get(i));
System.out.println(getString.get(i));
ll.addView(text);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// your code goes here
}
});
}
public class SelectProductName extends AppCompatActivity implements OnClickListener{
ArrayList<String> getString = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_select_product_name);
getString = getIntent().getStringArrayListExtra("stringList");
LinearLayout ll = findViewById(R.id.layout_select_product_name);
for (int i = 0; i < getString.size();i++){
TextView text = new TextView(this);
text.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
text.setText(getString.get(i));
System.out.println(getString.get(i));
ll.addView(text);
text.setOnClickListener(this);
}
}
public void onClick(View view) {
TextView tv = (TextView)view;
Log.i("clicked", tv.getText().toString());
}
}

How to delete individual elements from textview on Android studio

I am doing this project, can anyone help me to delete individual elements from the text view?enter image description here
here's i implemented the 'button' to insert and 'button2' to delete, and here's the code down bellow
public class MainActivity extends Activity {
private EditText editText;
private Button button;
private TextView textView;
private static final String TAG = "MainActivity";
// To hold all data in different mode : Portrait and landscape
private final String text_context = "TX";
#Override
protected void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate: in");
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editText = (EditText)findViewById(R.id.editText);
button = (Button)findViewById(R.id.button);
textView = (TextView)findViewById((R.id.textView));
textView.setText(""); // make it no text at runtime, but text at view
textView.setMovementMethod(new ScrollingMovementMethod()); // make it scrolling
editText.setText("");
final View.OnClickListener onClickListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
String result = editText.getText().toString();
result += "\n";
textView.append(result);
editText.setText(""); // text on EDITTEXT will disappear as soon as we click the button
}
};
final View.OnClickListener onClickListener1 = new View.OnClickListener() {
#Override
public void onClick(View view) {
}
};
if(editText != null)
button.setOnClickListener(onClickListener);
Log.d(TAG, "onCreate:out");
}
}
Please help me TO IMPLEMENT DELETE ACTION VIA BUTTON2....
Right now your onclick listeners are generically listening for clicks on any view.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle button events
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Handle button2 events
}
});
Or better yet implement View.OnClickListener() on your activity
Use regex
String input = editText.getText().toString();
String regex = "\\b" + input + "\\b";
String tvText = textView.getText().toString();
tvText = tvText.replaceAll(regex, "");
textView.setText(tvText);
Put this code in
final View.OnClickListener onClickListener1 = new View.OnClickListener() {
#Override
public void onClick(View view) {
//code goes here
}
};
if(editText != null)
// add this onclick to your button2
button2.setOnClickListener(onClickListener1);
Log.d(TAG, "onCreate:out");
}

Get Position value of Dynamically added view in layout android

I m trying this tutorial : http://android-er.blogspot.in/2014/01/get-text-from-dynamically-added-view.html \
where i m trying to edit the text when user click the insert button, it works fine only if i put position as static value.
but when i try to get position this app unfortunat stop.
public class MainActivity extends Activity {
EditText textIn;
Button buttonAdd;
LinearLayout container;
Button buttonShowAll;
#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);
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);
final TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
Button buttonInsert = (Button)addView.findViewById(R.id.insert);
buttonInsert.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
//int position = (Integer)v.getTag(); // I m trying to get position of button here but stops the app
int position = 2; // Static value for changing the textview works fine
String showallPrompt = "";
View childView = container.getChildAt(position);
TextView childTextView = (TextView) (childView.findViewById(R.id.textout));
String childTextViewText = (String) (childTextView.getText());
showallPrompt += position + ": " + childTextViewText + "\n";
Toast.makeText(MainActivity.this,
showallPrompt,
Toast.LENGTH_LONG).show();
((TextView) (childTextView.findViewById(R.id.textout))).setText("HELLO ");
}});
container.addView(addView, 0);
}});
LayoutTransition transition = new LayoutTransition();
container.setLayoutTransition(transition);
}
}
So here, i used to get position by
//int position = (Integer)v.getTag(); // I m trying to get position of button here but stops the app
int position = 2; // Static value for changing the textview works fine
but not working . How to solve this problem.Actually i m tring to do listview with editable text and show as textview.

Categories