i build complicated application on android studio and i have this code which i have to put it in another function upon click it:
EditText e1 = (EditText) findViewById(R.id.Name);
EditText e2 = (EditText) findViewById(R.id.Room);
EditText e3 = (EditText) findViewById(R.id.Date);
EditText e4 = (EditText) findViewById(R.id.Age);
EditText e5 = (EditText) findViewById(R.id.Height);
EditText e6 = (EditText) findViewById(R.id.Weight);
EditText e7 = (EditText) findViewById(R.id.SerumC);
EditText e8 = (EditText) findViewById(R.id.SUN);
EditText e9 = (EditText) findViewById(R.id.ALB);
SResult.setVisibility(View.INVISIBLE);
r11b.setVisibility(View.INVISIBLE);
iup.setVisibility(View.INVISIBLE);
alert.setVisibility(View.INVISIBLE);
rup.setVisibility(View.INVISIBLE);
r11.setVisibility(View.INVISIBLE);
e1.setVisibility(VISIBLE);
e2.setVisibility(VISIBLE);
e3.setVisibility(VISIBLE);
e4.setVisibility(VISIBLE);
e5.setVisibility(VISIBLE);
e6.setVisibility(VISIBLE);
e7.setVisibility(VISIBLE);
e8.setVisibility(VISIBLE);
e9.setVisibility(VISIBLE);
Male.setVisibility(VISIBLE);
Female.setVisibility(VISIBLE);
Black.setVisibility(VISIBLE);
NonBlack.setVisibility(VISIBLE);
Stable.setVisibility(VISIBLE);
NonStable.setVisibility(VISIBLE);
Scrlabel.setVisibility(VISIBLE);
clearH.setVisibility(VISIBLE);
clearHN.setVisibility(VISIBLE);
calcH.setVisibility(VISIBLE);
calcHN.setVisibility(VISIBLE);
menuH.setVisibility(VISIBLE);
menuHN.setVisibility(VISIBLE);
equatlab.setVisibility(VISIBLE);
r12equ.setVisibility(VISIBLE);
printC.setVisibility(View.INVISIBLE);
printCH.setVisibility(View.INVISIBLE);
homeC.setVisibility(View.INVISIBLE);
homeCH.setVisibility(View.INVISIBLE);
returnC.setVisibility(View.INVISIBLE);
returnCH.setVisibility(View.INVISIBLE);
par.setVisibility(VISIBLE);
but as you see it's long one and with time i will face a hard problem to make any change in my app, so i need to store this in a custom function and upon click the button the action of this function start like this:
public class visistore {
EditText e1 = (EditText) findViewById(R.id.Name);
EditText e2 = (EditText) findViewById(R.id.Room);
EditText e3 = (EditText) findViewById(R.id.Date);
EditText e4 = (EditText) findViewById(R.id.Age);
EditText e5 = (EditText) findViewById(R.id.Height);
EditText e6 = (EditText) findViewById(R.id.Weight);
EditText e7 = (EditText) findViewById(R.id.SerumC);
EditText e8 = (EditText) findViewById(R.id.SUN);
EditText e9 = (EditText) findViewById(R.id.ALB);
SResult.setVisibility(View.INVISIBLE);
r11b.setVisibility(View.INVISIBLE);
iup.setVisibility(View.INVISIBLE);
alert.setVisibility(View.INVISIBLE);
rup.setVisibility(View.INVISIBLE);
r11.setVisibility(View.INVISIBLE);
e1.setVisibility(VISIBLE);
e2.setVisibility(VISIBLE);
e3.setVisibility(VISIBLE);
e4.setVisibility(VISIBLE);
e5.setVisibility(VISIBLE);
e6.setVisibility(VISIBLE);
e7.setVisibility(VISIBLE);
e8.setVisibility(VISIBLE);
e9.setVisibility(VISIBLE);
Male.setVisibility(VISIBLE);
Female.setVisibility(VISIBLE);
Black.setVisibility(VISIBLE);
NonBlack.setVisibility(VISIBLE);
Stable.setVisibility(VISIBLE);
NonStable.setVisibility(VISIBLE);
Scrlabel.setVisibility(VISIBLE);
clearH.setVisibility(VISIBLE);
clearHN.setVisibility(VISIBLE);
calcH.setVisibility(VISIBLE);
calcHN.setVisibility(VISIBLE);
menuH.setVisibility(VISIBLE);
menuHN.setVisibility(VISIBLE);
equatlab.setVisibility(VISIBLE);
r12equ.setVisibility(VISIBLE);
printC.setVisibility(View.INVISIBLE);
printCH.setVisibility(View.INVISIBLE);
homeC.setVisibility(View.INVISIBLE);
homeCH.setVisibility(View.INVISIBLE);
returnC.setVisibility(View.INVISIBLE);
returnCH.setVisibility(View.INVISIBLE);
par.setVisibility(VISIBLE);
}
then, in my main activity :
button.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_UP) {
return true;
}
if (event.getAction()== MotionEvent.ACTION_DOWN)
{
visistore
}
return true;
}
});
Is this right?! or if it's wrong so i need to correct me
If i understood your problem correctly then , it is simple as Just define a boolean isButtonClicked
make this isButtonClicked = true onClick event
then put if(isButtonClicked) condition in your touchClick event.
Hope you got it
You don't want to set an OnTouchListener but an OnClickListener. The in the onClick() method of the OnClickListener call the appropriate function.
Related
This is what I want and can't do. After I choose a date, I need to open a dialog with information about the date.
Can anyone help me with the code for this?
This is the code to open the method:
private void abrirPopUpAlSeleccionarFecha(){
//defino los objetos de mi popup
EditText txtNombrePersona;
EditText txtEdadPersona;
EditText txtFechaSeleccionada;
EditText txtIdeasRegalo;
Button btnCerrarPopUp;
txtNombrePersona = (EditText) findViewById(R.id.txtNombrePersona);
txtEdadPersona = (EditText) findViewById(R.id.txtEdadPersona);
txtFechaSeleccionada = (EditText) findViewById(R.id.txtFechaEscogida);
txtIdeasRegalo = (EditText) findViewById(R.id.txtIdeasRegalo);
btnCerrarPopUp = (Button) findViewById(R.id.buttonClosePopUp);
dialogoPopUpFechaSeleccionada.setContentView(R.layout.fecha_seleccionada_popup);
dialogoPopUpFechaSeleccionada.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
}
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View view = LayoutInflater.from(context).inflate(R.layout.layout_dialog, null);
EditText txtNombrePersona;
EditText txtEdadPersona;
EditText txtFechaSeleccionada;
EditText txtIdeasRegalo;
Button btnCerrarPopUp;
txtNombrePersona = (EditText)view.findViewById(R.id.txtNombrePersona);
txtEdadPersona = (EditText) view.findViewById(R.id.txtEdadPersona);
txtFechaSeleccionada = (EditText) view.findViewById(R.id.txtFechaEscogida);
txtIdeasRegalo = (EditText) view.findViewById(R.id.txtIdeasRegalo);
btnCerrarPopUp = (Button) view.findViewById(R.id.buttonClosePopUp);
builder.setView(view);
builder.setCancelable(true);
final Dialog dialog = builder.create();
dialog.show();
I am making a calculator and below is my code. I was wondering is there any way to shorten the code, I have 18 buttons and I have to write 50 lines of code just to take reference from XML and add click listener to it
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String SelectedOpertator;
int num1, num2, result;
EditText input;
Button b1, b2, b3, b4, b5, b6 ,b7, b8, b9, b0, bdot;
Button bc, bs, bd, bp, bmi, bm, be;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
b1 = (Button) findViewById(R.id.b1);
b2 = (Button) findViewById(R.id.b2);
b3 = (Button) findViewById(R.id.b3);
b4 = (Button) findViewById(R.id.b4);
b5 = (Button) findViewById(R.id.b5);
b6 = (Button) findViewById(R.id.b6);
b7 = (Button) findViewById(R.id.b7);
b8 = (Button) findViewById(R.id.b8);
b9 = (Button) findViewById(R.id.b9);
b0 = (Button) findViewById(R.id.b0);
bdot = (Button) findViewById(R.id.bdot);
bc = (Button) findViewById(R.id.bc);
bs = (Button) findViewById(R.id.bs);
bd = (Button) findViewById(R.id.bd);
bp = (Button) findViewById(R.id.bp);
bmi = (Button) findViewById(R.id.bmi);
bm = (Button) findViewById(R.id.bm);
be = (Button) findViewById(R.id.be);
b1.setOnClickListener(this);
b2.setOnClickListener(this);
b3.setOnClickListener(this);
b4.setOnClickListener(this);
b5.setOnClickListener(this);
b6.setOnClickListener(this);
b7.setOnClickListener(this);
b8.setOnClickListener(this);
b9.setOnClickListener(this);
b0.setOnClickListener(this);
bdot.setOnClickListener(this);
bc.setOnClickListener(this);
bs.setOnClickListener(this);
bd.setOnClickListener(this);
bp.setOnClickListener(this);
bmi.setOnClickListener(this);
bm.setOnClickListener(this);
be.setOnClickListener(this);
}
Is there any other way to write this code in shorter way?
If you add a method to do the findViewById and setOnClickListener, you can reduce the lines for each button from two to one:
private Button findAndSetClickListener(int id) {
Button button = (Button) findViewById(id);
button.setOnClickListener(this);
return button;
}
Then:
b1 = findAndSetClickListener(R.id.b1);
// etc.
int ids[] = new int[] {R.id.b1, R.id.b2, R.id.b3, R.id.b4, R.id.b5, R.id.b6, R.id.b7, R.id.b8, R.id.b9, R.id.b0}
for(int i = 0; i < ids.length, i += 1){
findViewById(ids[i]).setOnClickListener(this);
}
There's no need for storing reference to each button as class members. In the onClick listener we can determine which button was clicked.
public onClick(View v){
int number = Arrays.asList(ids).indexOf(v.getId()) + 1;
// Button 'number' was clicked
}
try this type code.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
String SelectedOpertator;
int num1, num2, result;
EditText input;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText) findViewById(R.id.input);
((Button) findViewById(R.id.b1)).setOnClickListener(this);
((Button) findViewById(R.id.b2)).setOnClickListener(this);
((Button) findViewById(R.id.b3)).setOnClickListener(this);
((Button) findViewById(R.id.b4)).setOnClickListener(this);
((Button) findViewById(R.id.b5)).setOnClickListener(this);
((Button) findViewById(R.id.b6)).setOnClickListener(this);
((Button) findViewById(R.id.b7)).setOnClickListener(this);
((Button) findViewById(R.id.b8)).setOnClickListener(this);
((Button) findViewById(R.id.b9)).setOnClickListener(this);
((Button) findViewById(R.id.b0)).setOnClickListener(this);
((Button) findViewById(R.id.bdot)).setOnClickListener(this);
((Button) findViewById(R.id.bc)).setOnClickListener(this);
((Button) findViewById(R.id.bs)).setOnClickListener(this);
((Button) findViewById(R.id.bp)).setOnClickListener(this);
((Button) findViewById(R.id.bmi)).setOnClickListener(this);
((Button) findViewById(R.id.bm)).setOnClickListener(this);
((Button) findViewById(R.id.be)).setOnClickListener(this);
}
You could write an array where you store the views.
So that you can do something like
for (View v : array.getView()) {
v.setOnClickListener(this)
}
I think the actual findViewById would be a little more complicated to simplify
You could shorten it at about 50% (and make it a lot more readable) with a private method:
private Button getButtonWithListener(int id) {
Button btn = (Button) findViewById(id);
btn.setOnClickListener(this);
return btn;
}
and call this at every button: (saves you the setOnClickListener)
b1 = getButtonWithListener(R.id.b1);
b2 = getButtonWithListener(R.id.b2);
b3 = getButtonWithListener(R.id.b3);
b4 = getButtonWithListener(R.id.b4);
b5 = getButtonWithListener(R.id.b5);
b6 = getButtonWithListener(R.id.b6);
b7 = getButtonWithListener(R.id.b7);
b8 = getButtonWithListener(R.id.b8);
b9 = getButtonWithListener(R.id.b9);
b0 = getButtonWithListener(R.id.b0);
bdot = getButtonWithListener(R.id.bdot);
bc = getButtonWithListener(R.id.bc);
bs = getButtonWithListener(R.id.bs);
bd = getButtonWithListener(R.id.bd);
bp = getButtonWithListener(R.id.bp);
bmi = getButtonWithListener(R.id.bmi);
bm = getButtonWithListener(R.id.bm);
be = getButtonWithListener(R.id.be);
This way you're not losing the reference to the button. If you need those references to the buttons it won't become much shorter. If you don't need them, then there are some excellent possibilities in the other answers here!
If you need to shorten the button code, Use View's in OnClick() with help of switch statement.
In your XML Layout:
Create the number of buttons that you want with different button id. But use same method name for all button's onClick attribute. Eg: android:onClick="submitBTN" used for all buttons.
In your MainActivity:
Implement that method name to perform different operations using switch statement
public void submitBTN(View view) {
switch (view.getId()) {
case R.id.btnClick1: // Code of button 1 Click
break;
case R.id.btnClick2: // Code of button 2 Click
break;
case R.id.btnClick3: // Code of button 3 Click
break;
case R.id.btnClickn: // Code of Button n Click
}
}
Try using butter knife library. You can use syntax like:
#Onclick({R.id.b1, R.id.b2...})
public void handleClick(Button btn){
// handle click event here
}
I have an activity with 3 edittexts and a button.
I have a second activity with 2 textviews.
When button is clicked i want two random edittexts values from activity1 to replace the text on textviews on activity2.
I managed to do that, but not randomly. How can I make it random?
Here is the first activity.
final EditText et = (EditText) findViewById(R.id.editText);
final EditText et1 = (EditText) findViewById(R.id.editText2);
final EditText et2 = (EditText) findViewById(R.id.editText3);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick (View v){
Intent i = new Intent(MainActivity.this, postavke.class);
i.putExtra("prvi", et.getText().toString());
i.putExtra("drugi", et1.getText().toString());
i.putExtra("treci", et2.getText().toString());
startActivity(i);
}
});
}
Here is the second activity.
TextView tv = (TextView) findViewById(R.id.asdf);
tv.setText(getIntent().getExtras().getString("prvi"));
TextView dr = (TextView) findViewById(R.id.asdg);
dr.setText(getIntent().getExtras().getString("drugi"));
In your second activity:
String[] texts = new String[]{
getIntent().getExtras().getString("prvi"),
getIntent().getExtras().getString("drugi"),
getIntent().getExtras().getString("treci"),
};
TextView tv = (TextView) findViewById(R.id.asdf);
TextView dr = (TextView) findViewById(R.id.asdg);
Random random = new Random();
tv.setText(texts[random.nextInt(3)]);
dr.setText(texts[random.nextInt(3)]);
Or, for unique values:
First activity:
final EditText et = (EditText) findViewById(R.id.editText);
final EditText et1 = (EditText) findViewById(R.id.editText2);
final EditText et2 = (EditText) findViewById(R.id.editText3);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick (View v){
ArrayList<String> texts = new ArrayList<>();
texts.add(et.getText().toString());
texts.add(et1.getText().toString());
texts.add(et2.getText().toString());
Intent i = new Intent(MainActivity.this, postavke.class);
i.putExtra("texts", texts);
startActivity(i);
}
});
}
Second activity:
ArrayList<String> texts = getIntent().getExtras().getStringArrayList("texts");
TextView tv = (TextView) findViewById(R.id.asdf);
TextView dr = (TextView) findViewById(R.id.asdg);
Collections.shuffle(texts);
tv.setText(texts.get(0));
dr.setText(texts.get(1));
Add your Strings(Eg : getIntent().getExtras().getString("prvi")) from previous Activity to a ArrayList.
Then do simply Collection.shuffle(your_list);
Get items from ArrayList & set it to your TextViews.
Declare an array that will contain the values:
public String[] randomAnswers = new String[3]; //3 because you said you have 3 random strings
In your onCreate() add the values to the array:
randomAnswers[0] = getIntent().getExtras().getString("prvi");
randomAnswers[1] = getIntent().getExtras().getString("drugi");
randomAnswers[2] = getIntent().getExtras().getString("treci");
Select two random values:
private static final Random RANDOM = new Random(); //in java.util package
Inside onCreate:
final int randomInt1 = RANDOM.nextInt(3); //nextInt(int) goes from 0 to int-1, considering arrays start at index=0 we want to get a random number from: 0, 1, 2
int randomInt2;
do {
randomInt2 = RANDOM.nextInt(3);
} while (randomInt1 == randomInt2);
final String firstRandomString = values[randomInt1];
final String secondRandomString = values[randomInt2];`
you can use some thing like this the edited form of #Joe Frostick answere:
String[] texts = new String[]{
getIntent().getExtras().getString("prvi"),
getIntent().getExtras().getString("drugi"),
getIntent().getExtras().getString("treci"),
};
TextView tv = (TextView) findViewById(R.id.asdf);
TextView dr = (TextView) findViewById(R.id.asdg);
Random random = new Random();
texts.remove(random.nextInt(3));
tv.setText(texts[0]);
dr.setText(texts[1]);
Thanks for the help everyone, I really didn't expect so many answers in not much time. You're the best!
I created an AlertDialog such that when you press a Button, the Dialog pops up and a layout appears with EditTexts. However, I created the layout in the actual code rather than in the xmlfile. For some reason, when the AlertDialog pops up, it's not able to find the EditText field and gives me a NullPointerException.
//private Lecture lecture;
private LectureManager lectureManager;
public void addWork(View view) {
LinearLayout layout = new LinearLayout(this);
EditText weight = new EditText(this);
EditText mark = new EditText(this);
mark.setInputType(InputType.TYPE_CLASS_NUMBER);
weight.setInputType(InputType.TYPE_CLASS_NUMBER);
weight.setId(99);
mark.setId(100);
layout.addView(mark);
layout.addView(weight);
AlertDialog.Builder addwork = new AlertDialog.Builder(this);
addwork.setView(layout);
addwork.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
EditText eweight = (EditText) findViewById(99);
EditText emark = (EditText) findViewById(100);
String coursename = ecoursename.getText().toString();
And for some reason, I'm getting a NullPointerException at the "EditText weight" line. I believe that maybe it's not finding anything with ID 99 and that the EditText might be out of scope? Thanks in advance!
Actually, when you make these calls:
EditText eweight = (EditText) findViewById(99);
EditText emark = (EditText) findViewById(100);
you're calling the findViewById() method of your Activity, not the AlertDialog. In order to retrieve the views from the Dialog, you can use something like this, inside onClick():
EditText eweight = (EditText) ((AlertDialog)dialog).findViewById(99);
EditText emark = (EditText) ((AlertDialog)dialog).findViewById(100);
Hope this helps.
Try this..
Use final for both EditText while initilization like below
final EditText weight = new EditText(this);
final EditText mark = new EditText(this);
Then you can get the text from EditText in PositiveButton
String weight_txt = weight.getText().toString().trim();
String mark_txt = mark.getText().toString().trim();
No need to set id for EditText also no need findViewById.
i think you need to do this
EditText eweight = (EditText) view.findViewById(99);
and view is , what you have passed in your addWork() method parameter.
I'm trying to create a calculator for Android using Eclipse. This is my code for the function 'cal', 'cal' is executed when a button is clicked, But clicking on the button closes the application, I tried an other button with different function and it works fine. Can anyone point out the mistake that I've done?
public void cal( View view ){
EditText op = (EditText) findViewById(R.id.editText2);
EditText n1 = (EditText) findViewById(R.id.editText1);
EditText n2 = (EditText) findViewById(R.id.editText3);
EditText res = (EditText) findViewById(R.id.textView1);
String sn1 = n1.getText().toString();
String sn2 = n2.getText().toString();
String sres;
String sop;
int in1 = Integer.parseInt(sn1);
int in2 = Integer.parseInt(sn2);
int ires;
sop = op.getText().toString();
if(sop == "+"){
ires = in1 + in2;
sres = Integer.toString(ires);
res.setText(sres);
}
}
There is no exception in this.
I think you forgot to set the activity layout which causes your application force close.
or
Problem here
EditText res = (EditText) findViewById(R.id.textView1);
Are you sure your editText name is textView1 in your layout.
If it textView then do like this.
TextView res = (TextView) findViewById(R.id.textView1);