How to add a wait function till a button is pressed? - java

I want to add a wait till, Button (Enter) is pressed function to my code But i am still new to this.
I know there are some errors in my code I was playing around with it, But what I want to do is when I press the line Button I want it to display Input X,Y,Z then wait till enter is pressed to execute the rest of my code I want to add in. How would I implement something like this in my code?
Here is my MainActivity Class:
public class MainActivity extends Activity implements OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button enter = (Button) findViewById(R.id.enter);
Button line = (Button) findViewById(R.id.line);
Button arc = (Button) findViewById(R.id.arc);
line.setOnClickListener(this);
enter.setOnClickListener(this);
arc.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
TextView vector = (TextView) findViewById(R.id.point);
TextView index = (TextView) findViewById(R.id.index);
TextView info = (TextView) findViewById(R.id.info);
EditText cl = (EditText) findViewById(R.id.editText1);
DrawingUtils call = new DrawingUtils();
switch (v.getId()) {
case R.id.line:
info.setText("Input X,Y,Z");
// This Is Where the Wait Function Will GO
vector.setText(call.addVertice());
index.setText("1");
break;
case R.id.enter:
String In = cl.getText().toString();
call.setInputCoords(In);
break;
case R.id.arc:
info.setText("Enter Vertice1 ");
// Code for entering Vertice1(Also has wait function)
info.setText("Enter Vertice2");
// Code for entering Vertice2(Also has wait function)
info.setText("Enter Height");
//Code for entering Height(Also has wait function)
}
}
}
Here is my DrawingUtils Class:
public class DrawingUtils {
String inputCoords;
String[] vertice;
public String getInputCoords() {
return inputCoords;
}
public void setInputCoords(String inputCoords) {
this.inputCoords = inputCoords;
}
public String addVertice() {
int i = 0;
vertice = inputCoords.split(",");
return vertice[i];
}
}

I think this is what you are after. Apologies if not!
Use a boolean flag to handle state within your app. This way you can execute different code if something has happened.
boolean enterPressed = false;
#Override
public void onClick(View v) {
TextView vector = (TextView) findViewById(R.id.point);
TextView index = (TextView) findViewById(R.id.index);
TextView info = (TextView) findViewById(R.id.info);
EditText cl = (EditText) findViewById(R.id.editText1);
DrawingUtils call = new DrawingUtils();
switch (v.getId()) {
case R.id.line:
if (enterPressed) {
vector.setText(call.addVertice());
index.setText("1");
}
else {
info.setText("Input X,Y,Z");
}
break;
case R.id.enter:
String In = cl.getText().toString();
call.setInputCoords(In);
enterPressed = true;
break;
case R.id.arc:
info.setText("Enter Vertice1 ");
// Code for entering Vertice1
info.setText("Enter Vertice2");
// Code for entering Vertice2
}
}

Related

Detect which button is clicked

i have 3 button in my page :
red, blue and green
how i can detect which button is clicked
i want use the background color for next page(red,blue or green)
this is my code in main page:
public void color(View view) {
Intent color= new Intent(this, colorPage.class);
Click(view);
String What_color = view.toString();
color .putExtra("What_color",What_color);
startActivity(colorPage);
}
private void Click(View view1) {
int id = view1.getId();
}
}
and this is my code in colorPage:
Bundle What_color = getIntent().getExtras();
if (What_color != null) {
String mycol = What_color.getString("What_color");
TextView result = (TextView) findViewById(R.id.tResult);
result.setText(mycol);
}
i see in textView this code:
android.support.v7.widget.AppCompatButton{366bb1b VFED..C. ...P.... 0,0-728,114 #7f070023 app:id/Red
or this code:
ndroid.support.v7.widget.AppCompatButton{366bb1b VFED..C. ...P.... 0,0-728,114 #7f070023 app:id/Blue
please help me
Add a listener to each button with their id.
Button buttonOne = (Button) findViewById(R.id.YOURBUTTONID);
buttonOne.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
//Do change the color.
}
});
//This code changing background on your activity but You have to give color as a hexadecimal
public void setActivityBackgroundColor(int color) {
View view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
you can create a global string like
String detectBtnStr = "";
private void Click(View view1) {
int id = view1.getId();
switch(id){
case: R.id.red_btn:
detectBtnStr = "red";
break;
case: R.id.blue_btn:
detectBtnStr = "blue";
break;
case: R.id.green_btn:
detectBtnStr = "green";
break;
}
}
and when come back then check with detectBtnStr like
if (detectBtnStr.equals("red")) {
// do your code}
else if (detectBtnStr.equals("blue")) {
// do your code
}
else if (detectBtnStr.equals("green")){
// do your code
}
hope this will help you.

How do i use getText() on an editText field while the editText is inside another onclick?

Currently the issue with my application is that when switching views of my application via a floating action buttons onClickListener, i cannot access what is being put inside my editText because it is being created in a separate for loop.
start_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
for(int j=0;j<amount;j++){
...
final EditText team = new EditText(MainActivity.this);
team.setHint("TeamName");
team.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
ll.addView(team);
...
});
lm.addView(ll);
}
fab2.setVisibility(View.VISIBLE);
fab2.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(lm.getVisibility()==View.GONE|lm2.getVisibility()==View.VISIBLE) {
lm2.setVisibility(View.GONE);
lm2.removeAllViews();
lm.setVisibility(View.VISIBLE);
}else{
if(lm.getVisibility()==View.VISIBLE|lm2.getVisibility()==View.GONE){
lm.setVisibility(View.GONE);
for(int x=0;x<amount;x++){
if(myStringArray[x]==null){
myStringArray[x]="Empty Team Name";
x--;
}else {
myStringArray[x] = (team.getText().toString());
TextView score_name = new TextView(MainActivity.this);
TextView final_score = new TextView(MainActivity.this);
score_name.setTextSize(15);
score_name.setTypeface(null, Typeface.BOLD);
final_score.setTextSize(25);
score_name.setLayoutParams(params);
final_score.setLayoutParams(params);
lm2.addView(score_name);
lm2.addView(final_score);
String a = Arrays.toString(myIntArray);
String ar[] = a.substring(1, a.length() - 1).split(", ");
score_name.append(myStringArray[x]);
final_score.append(ar[x]);
score_name.append("\n");
}
}
lm2.setVisibility(View.VISIBLE);
}
}
});
currently it is not recognizing the variable (team) when using
myStringArray[x] = (team.getText().toString());
I have tried moving around my initializer, {{final EditText team = new EditText(MainActivity.this);}}, but if i move it outside the for loop the application crashes upon the onclick of start_button
Define your EditText team out site of start_button.setOnClickListener(new OnClickListener() then initialise it in onClickListener method. Check the code below.
final EditText team;
start_button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
for(int j=0;j<amount;j++){
team = new EditText(MainActivity.this);
team.setHint("TeamName");
team.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS);
ll.addView(team);
...
});
lm.addView(ll);
}
fab2.setVisibility(View.VISIBLE);
fab2.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
if(lm.getVisibility()==View.GONE|lm2.getVisibility()==View.VISIBLE) {
lm2.setVisibility(View.GONE);
lm2.removeAllViews();
lm.setVisibility(View.VISIBLE);
}else{
if(lm.getVisibility()==View.VISIBLE|lm2.getVisibility()==View.GONE){
lm.setVisibility(View.GONE);
for(int x=0;x<amount;x++){
if(myStringArray[x]==null){
myStringArray[x]="Empty Team Name";
x--;
}else {
myStringArray[x] = (team.getText().toString());
TextView score_name = new TextView(MainActivity.this);
TextView final_score = new TextView(MainActivity.this);
score_name.setTextSize(15);
score_name.setTypeface(null, Typeface.BOLD);
final_score.setTextSize(25);
score_name.setLayoutParams(params);
final_score.setLayoutParams(params);
lm2.addView(score_name);
lm2.addView(final_score);
String a = Arrays.toString(myIntArray);
String ar[] = a.substring(1, a.length() - 1).split(", ");
score_name.append(myStringArray[x]);
final_score.append(ar[x]);
score_name.append("\n");
}
}
lm2.setVisibility(View.VISIBLE);
}
}
});

Exception Handling in If statement?

I'm struggling to create a very simple app. There are Product Names with matching Product Numbers. User enters a Number and the corresponding Name appears.
editTextField1 = the number user types
button1 = the search button
editTextField2 = the product name appears
Now my code is working but there is only one problem, when the user doesn't enter anything and clicks on button1 the app crashes. So I need some sort of exception handler maybe? Im struggling to get my head around it. I know how I'm coding, by declaring loads of variables, is long-winded lol.
public class MainActivity extends AppCompatActivity {
private Button button1;
private EditText editText1;
private EditText editText2;
private int a = 123;
private String b = "TV";
private int c = 333;
private String d = "Radio";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1 = (Button) findViewById(R.id.button1);
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
View.OnClickListener myOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
double pNumber = Double.parseDouble(editText1.getText().toString());
if(pNumber == a){
editText2.setText(b);
}
else if (pNumber == c){
editText2.setText(d);
}
else
editText2.setText("Invalid Product Number");
}
};
button1.setOnClickListener(myOnClickListener);
In Addition to Spartas Answer, it would also be a good idea to check for a valid Double input:
try {
double pNumber = Double.parseDouble(text);
catch(NumberFormatException e){
//Do ExceptionHandling
}
Use this:
View.OnClickListener myOnClickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
String text = editText1.getText().toString();
if(!TextUtils.isEmpty(text)) {
// EditText1 is not empty
try {
double pNumber = Double.parseDouble(text);
if(pNumber == a)
editText2.setText(b);
else if (pNumber == c){
editText2.setText(d);
else
editText2.setText("Invalid Product Number");
}
catch(NumberFormatException e) {
// Invalid input, not a double
editText2.setText("Invalid double input");
}
}
else {
// EditText1 is empty
editText2.setText("EditText1 is empty");
}
}
};
There are basically 2 ways how you can handle this
1) Use try - catch
try{
double pNumber = Double.parseDouble(editText1.getText().toString());
}catch(NumberFormatException e){
//show a message to user
}
2) Use input sanitation
String number = editText1.getText().toString();
boolean isNumber = number.matches("^-?\\d+$"));//haven't tried this though
if(!isNumber) //if not number
//show a message to user

how to connect the spinner with my converter

I need to connect the spinner with editText to get the result for converter
There is no connection between spinner and my converter.
public class A01Acre extends Activity {
Spinner spinnerarea;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.a01acre);
final EditText editAcre = (EditText) findViewById(R.id.editAcre);
final EditText editHectar = (EditText) findViewById(R.id.editHectar);
final EditText editSquareinch = (EditText) findViewById(R.id.editSquareinch);
final EditText editSquarekm = (EditText) findViewById(R.id.editSquarekm);
final EditText editSquaremeter = (EditText) findViewById(R.id.editSquaremeter);
final EditText editSquaremile = (EditText) findViewById(R.id.editSquaremile);
final EditText editSquareyard = (EditText) findViewById(R.id.editSquareyard);
Button buttonConvert = (Button) findViewById (R.id.buttonConvertAcre);
buttonConvert.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//try
try {
//
double acre = Double.valueOf (editAcre.getText().toString());
double hectar = acre * 0.404686;
double squareinch = acre * 6.273e+6;
double squarekm = acre * 0.00404686;
double squaremeter = acre * 4046.86;
double squaremile = acre * 0.0015625;
double squareyard = acre * 4840;
editHectar.setText (String.valueOf(hectar));
editSquareinch.setText (String.valueOf(squareinch));
editSquarekm.setText (String.valueOf(squarekm));
editSquaremeter.setText (String.valueOf(squaremeter));
editSquaremile.setText (String.valueOf(squaremile));
editSquareyard.setText (String.valueOf(squareyard));
//catch
} catch (NumberFormatException ex) {
// write a message to users
editHectar.setText ("");
}
}
});
spinnerarea = (Spinner) findViewById(R.id.spinnerarea);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.spinnerTestArray, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinnerarea.setAdapter(adapter);
spinnerarea.setOnItemSelectedListener(new MyOnItemSelectdListerne());
}
#Override
public void onDestroy(){
super.onDestroy();
}
public class MyOnItemSelectdListerne implements OnItemSelectedListener {
#Override
public void onItemSelected(AdapterView<?> patent,
View view, int pos, long id) {
String str = patent.getItemAtPosition(pos).toString().toUpperCase();
}
#Override
public void onNothingSelected(AdapterView parent) {
}
}
}
Add a switch statement to your onItemSelected() method - switching on pos.
For each unit (each case), you can recalculate and then output the value to the user.
Something like,
switch(pos) {
case 0:
//acre - redo calculation
break;
case 1:
//hectare - redo calculation
break;
case 2:
//squareinch - redo calculation
break;
//...
default:
throw new UnexpectedException();
}
//display results to user

Android - Eclipse: Syntax error on token "(", Expression expected after this token

I'm still learning and I've tried to figure this out and I figured I'd ask the experts. Any ideas?
I get the error on this line:
final Button btn = (Button) findViewById ([btnIds]);
public class choiceTest extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.atcchoice);
}
final OnClickListener listener = new OnClickListener() {
public void onClick(View v){
switch(v.getId()){
case R.id.sec1:
break;
case R.id.sec2:
break;
case R.id.sec3:
break;
}
}
};
final int[] btnIds = new int[]{R.id.sec1, R.id.sec2, R.id.sec3};{
for(int i = 0; i < btnIds.length; i++) {
final Button btn = (Button) findViewById ([btnIds]);
btn.setOnClickListener(listener);
}
}
}
Also, this is for a downloader program and I am using Async to download my files. What could I add to the above code to change my downloader URL to a different one? Otherwise I will have a very long code... Here is how download URL code.
private void startDownload() {
DownloadFile downloadFile = new DownloadFile();
downloadFile.execute("http://www.website.com/document.pdf");
}
The async code to download it:
class DownloadFile extends AsyncTask<String, Integer, String> {
URL url = new URL(aurl[0]);
You access an array like this:
Button btn = (Button) findViewById( btnIds[i] );
your are passing whole array.
change it:
for(int i = 0; i < btnIds.length; i++) {
final Button btn = (Button) findViewById ([btnIds]);
btn.setOnClickListener(listener);
}
with
for(int i = 0; i < btnIds.length; i++) {
final Button btn = (Button) findViewById (btnIds[i]);
btn.setOnClickListener(listener);
}

Categories

Resources