The following are my textfields, radio buttons etc.:
et1 = Edit Text Field
radioButton1 = Radio Button (part of group1)
radioButton2 = Radio Button (part of group1)
textView2 = Text View
What I am trying to is to constantly have textView2 update depending on what is inside et1 and which Radio Button is selected.
I was thinking the problem could be to do that the method convertUp / convertDown. It only runs when a Radio Button is actually clicked, and not selected.
If this is the problem can someone tell me how to do this. And if it is not, can someone explain to me where I am going wrong. I would be very grateful. Thanks in advance.
Here is my code I use:
("mark" is the variable that will be updating in textView2 and "value" is the variable that "mark" depends on - as seen in the methods)
public class MainActivity extends ActionBarActivity {
double mark = 0;
double value = 10;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EditText text = (EditText)findViewById(R.id.et1);
try{
value = Double.parseDouble(text.getText().toString());
} catch (final NumberFormatException e) {
value = 1.0;
}
OnClickListener listener1 = new OnClickListener(){
public void onClick(View v) {
mark = convertUp(value);
}
};
OnClickListener listener2 = new OnClickListener(){
public void onClick(View v) {
mark = convertDown(value);
}
};
RadioButton rb1 = (RadioButton) findViewById(R.id.radioButton1);
rb1.setOnClickListener(listener1);
RadioButton rb2 = (RadioButton) findViewById(R.id.radioButton2);
rb2.setOnClickListener(listener2);
TextView tv1 = (TextView)findViewById(R.id.textView2);
tv1.setText(""+mark);
}
You would need to do several things to have it constantly updating.
First, you need a TextWatcher for the EditText which would modify the textView every time you input a new letter.
et1.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
try{
value = Double.parseDouble(text.getText().toString());
} catch (final NumberFormatException e) {
value = 1.0;
}
if(rb1.isChecked()){ mark = convertUp(value); }
else{ mark = convertDown(value); }
tv1.setText(""+mark); //will update ur tV every time you input a lleter
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) { }
#Override
public void afterTextChanged(Editable s) { }
});
You also need to do this to modify your textView in case you change the radioButton
OnClickListener listener1 = new OnClickListener(){
public void onClick(View v) {
try{
value = Double.parseDouble(text.getText().toString());
} catch (final NumberFormatException e) {
value = 1.0;
}
mark = convertUp(value);
tv1.setText(""+mark);
}
};
OnClickListener listener2 = new OnClickListener(){
public void onClick(View v) {
try{
value = Double.parseDouble(text.getText().toString());
} catch (final NumberFormatException e) {
value = 1.0;
}
mark = convertDown(value);
tv1.setText(""+mark);
}
};
Hope it helps! Good Luck!
Or try tv1.setText(""+mark); in OnClickListener listener1 and OnClickListener listener2
Related
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");
}
i coded this lines to change textview1 Continuous when user clicked button but it sucks at value 1 in output why :\
public class dobeyti extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int num = 0;
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Becuase you defined the num value inside the onclick method so it will be reassigned to 0 every time.
Just put int num=0 outside the method like below:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dobeyti);
final TextView tView;
Button mButton;
tView = (TextView)findViewById(R.id.textView1);
mButton = (Button)findViewById(R.id.button);
assert mButton != null;
mButton.setOnClickListener(new View.OnClickListener() {
int num = 0;
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Move int num = 0; outside OnClickListener. Every time you click button value on variable num is set to 0 -> increased to 1 and set as text, that is why it stuck at 1.
int num = 0;
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num++;
tView.setText(Integer.toString(num));
}
});
}
}
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 8 years ago.
Improve this question
Basically, there is a text field where the user can input the subtotal of the meal. Then, when they hit done, the done method is supposed to take the tip field value, and multiply it by the subtotal value, so that the total includes the two pieces of data. However, when I hit the done button, the result is actually always equal to the subtotal, and the tip is always ignored. Here is the code:
public class MainActivity extends Activity implements OnRatingBarChangeListener {
// Testing Stuff to show the rating value, will need to use later for maths
static RatingBar rb;
TextView tipsTV;
ImageView greyPlus, greyMinus, greyPlus2, greyMinus2;
TextView peopleDiningTV, peopleDiningTitle;
int peopleDining = 2;
TextView tipValue;
int tipValueInt = 10;
TextView subtotal, total;
TextView subtotalTitle, totalTitle;
TextView epp, eppTitle;
Button done;
// Elements for hiding and such
static RelativeLayout rl;
static Button settingsButton;
public static int rating = 3;
// The Image used as the DropDown button, Rotate code below
ImageView dropDownButton;
Boolean hasRotated = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dropDownButton = (ImageView) findViewById(R.id.dropDownButton);
rb = (RatingBar) findViewById(R.id.ratingBar1);
rb.setRating(rating);
tipsTV = (TextView) findViewById(R.id.textView2);
tipValue = (TextView) findViewById(R.id.tipText);
greyPlus = (ImageView) findViewById(R.id.greyPlus);
greyMinus = (ImageView) findViewById(R.id.greyMinus);
greyPlus2 = (ImageView) findViewById(R.id.greyPlus2);
greyMinus2 = (ImageView) findViewById(R.id.greyMinus2);
peopleDiningTV = (TextView) findViewById(R.id.textViewPeople);
peopleDiningTitle = (TextView) findViewById(R.id.TextView02);
subtotal =(TextView) findViewById(R.id.subtotalText);
subtotalTitle =(TextView) findViewById(R.id.subtotalTitle);
total =(TextView) findViewById(R.id.totalText);
totalTitle =(TextView) findViewById(R.id.totalTitle);
epp = (TextView) findViewById(R.id.eppText);
eppTitle = (TextView) findViewById(R.id.eppTitle);
done = (Button) findViewById(R.id.buttonDone);
greyPlus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tipValueInt++;
tipValue.setText(tipValueInt + "%");
}
});
greyMinus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (tipValueInt >= 1) {
tipValueInt--;
tipValue.setText(tipValueInt + "%");
}
if(tipValueInt == 0){
tipValue.setText("No Tip.");
}
}
});
greyPlus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
peopleDining++;
peopleDiningTV.setText(peopleDining + "");
}
});
greyMinus2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (peopleDining > 1) {
peopleDining--;
peopleDiningTV.setText(peopleDining + "");
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
FragmentManager fm = getFragmentManager();
QuizFragment qf = new QuizFragment();
public void dropDown(View view) {
if (hasRotated == false) {
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_in,
android.R.animator.fade_out);
dropDownButton.setRotation(90);
ft.add(R.id.quizFragment, qf);
ft.show(qf);
ft.commit();
hasRotated = true;
// Hiding Elements, so they don't show through the fragment
tipsTV.setVisibility(View.INVISIBLE);
greyPlus.setVisibility(View.INVISIBLE);
greyMinus.setVisibility(View.INVISIBLE);
tipValue.setVisibility(View.INVISIBLE);
greyPlus2.setVisibility(View.INVISIBLE);
greyMinus2.setVisibility(View.INVISIBLE);
peopleDiningTV.setVisibility(View.INVISIBLE);
peopleDiningTitle.setVisibility(View.INVISIBLE);
subtotal.setVisibility(View.INVISIBLE);
subtotalTitle.setVisibility(View.INVISIBLE);
total.setVisibility(View.INVISIBLE);
totalTitle.setVisibility(View.INVISIBLE);
epp.setVisibility(View.INVISIBLE);
eppTitle.setVisibility(View.INVISIBLE);
done.setVisibility(View.INVISIBLE);
} else if (hasRotated == true) {
FragmentTransaction ft = fm.beginTransaction();
ft.setCustomAnimations(android.R.animator.fade_out,
android.R.animator.fade_out);
dropDownButton.setRotation(0);
hasRotated = false;
ft.remove(qf);
ft.commit();
// Hiding Elements, so they don't show through the fragment
tipsTV.setVisibility(View.VISIBLE);
greyPlus.setVisibility(View.VISIBLE);
greyMinus.setVisibility(View.VISIBLE);
tipValue.setVisibility(View.VISIBLE);
greyPlus2.setVisibility(View.VISIBLE);
greyMinus2.setVisibility(View.VISIBLE);
peopleDiningTV.setVisibility(View.VISIBLE);
peopleDiningTitle.setVisibility(View.VISIBLE);
subtotal.setVisibility(View.VISIBLE);
subtotalTitle.setVisibility(View.VISIBLE);
total.setVisibility(View.VISIBLE);
totalTitle.setVisibility(View.VISIBLE);
epp.setVisibility(View.VISIBLE);
eppTitle.setVisibility(View.VISIBLE);
done.setVisibility(View.VISIBLE);
}
}
public void openSettings(View view) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
#Override
public void onRatingChanged(RatingBar ratingBar, float rating,
boolean fromTouch) {
}
public void done(View view){
int subtotalCost = Integer.parseInt(subtotal.getText().toString());
int tip = tipValueInt / 100;
int totalCost = (subtotalCost * tip) + subtotalCost;
total.setText(totalCost+"");
}
}
You never put an OnClickListener on the button variable done so your clicks are just ignored, that is why the value doesn't change. You should add the listener and then call your done() method in the onClick(). You should also remove the View view parameter of done() since you are not using it at all.
// ...
done = (Button) findViewById(R.id.buttonDone);
// add listener
done.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
done();
}
});
// ...
You should probably change the name of the method done() to displayResult() or something more meaningful and not similar to your button variable named done. Actually, you should probably change the names of most of your variables to names that more clearly describe what the variables are used for. For example, variable done that represents the button can be something like btnDone...
You've declared totalCost as int. So, if (subtotalCost * tip) is less than 1, then totalCost = subtotalCost. Try using floats instead of ints, if applicable.
I am currently fooling around with the android SDK (eclipse) and i am trying to make a simple button-based TicTacToe game.
I made an array where i save the Buttons and i am trying to make the ClickEvents dynamically:
Here is the Code (Which works so far):
public class MainActivity extends Activity
{
final Button[] buttons = new Button[9];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttons[0] = (Button) findViewById(R.id.button1);
buttons[1] = (Button) findViewById(R.id.button2);
buttons[2] = (Button) findViewById(R.id.button3);
buttons[3] = (Button) findViewById(R.id.button4);
buttons[4] = (Button) findViewById(R.id.button5);
buttons[5] = (Button) findViewById(R.id.button6);
buttons[6] = (Button) findViewById(R.id.button7);
buttons[7] = (Button) findViewById(R.id.button8);
buttons[8] = (Button) findViewById(R.id.button9);
for(int i = 0;i < 9;i++)
{
buttons[i].setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
//
}
});
}
}
}
The problem comes when i try to reference to a button in the onClick-Method using 'i'.
For example:
#Override
public void onClick(View arg0)
{
//buttons[0].setText("X"); works by the way
buttons[i].setText("X");
}
Eclipse tells me: "Change modifier of 'i' to final"
Well but if 'i' would be final, i wouldn't be able to increase 'i' in the loop.
So i tried this:
#Override
public void onClick(View arg0)
{
final int position = i;
buttons[position].setText("X");
}
Which ends up resulting with the same Error "Change modifier of 'i' to final"
So my Question is: How can i make this work?
I have tried google but i wasn't able to find anything and this really is the first time i ever encountered this problem .
Hope you guys can help me out!
EDIT:
Well the solution was simple, here is the now working code:
for(int i = 0;i < 9;i++)
{
final int position = i;
buttons[i].setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
buttons[position].setText("X");
}
});
}
You can do something like this:
for(int i = 0;i < 9;i++)
{
final int position = i;
buttons[i].setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
buttons[position].setText("X");
}
});
}
Create another final int var in your loop set it the same value as i and reference this var from you listeners.
for(int i = 0;i < 9;i++)
{
final int index = i;
buttons[i].setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0)
{
buttons[index].setText("X");
}
});
}
Have a separate class which implements View.OnClickListener and have a constructor to accept the index value
First of all, I'm using Andorid and Java only for a week and I'm a total newbie.
I need to know which button user clicked and then compare it with good or bad answer.
Here is the code
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.questions);
Integer[] flags = {
R.drawable.flag_albania,
R.drawable.flag_andorra,
R.drawable.flag_angola,
R.drawable.flag_avganistan,
};
String[] questions = {
"What is the flag of Albania",
"What is the flag of Andorra",
"What is the flag of Angola",
"What is the flag of Avganistan",
};
TextView tv = (TextView) findViewById(R.id.textView1);
int arraySize = flags.length;
Random rand = new Random();
ImageButton button1 = (ImageButton) findViewById(R.id.imageButton1);
int index = rand.nextInt(arraySize);
int questionIndex1 = index;
button1.setImageResource(flags[index]);
flags[index] = flags[--arraySize];
ImageButton button2 = (ImageButton) findViewById(R.id.imageButton2);
index = rand.nextInt(arraySize);
int questionIndex2 = index;
button2.setImageResource(flags[index]);
flags[index] = flags[--arraySize];
ImageButton button3 = (ImageButton) findViewById(R.id.imageButton3);
index = rand.nextInt(arraySize);
int questionIndex3 = index;
button3.setImageResource(flags[index]);
flags[index] = flags[--arraySize];
ImageButton button4 = (ImageButton) findViewById(R.id.imageButton4);
index = rand.nextInt(arraySize);
int questionIndex4 = index;
button4.setImageResource(flags[index]);
flags[index] = flags[--arraySize];
Integer[] question = {
questionIndex1,
questionIndex2,
questionIndex3,
questionIndex4
};
int questionArraySize = question.length;
int questionArray = rand.nextInt(questionArraySize);
tv.setText(questions[question[questionArray]]);
My idea is to compare questionIndex that is randomly selected to a button id but I really don't know how to implement it. Every help is appreciated.
Use this reduced code
ImageButton button1,button2,button3,button4;
ImageButton imagebuttons[]={ button1,button2,button3,button4};
int ids[]={R.id.imageButton1,R.id.imageButton2,R.id.imageButton3,R.id.imageButton4};
for(final int i=0;i<imagebuttons.length;i++)
{
imagebuttons[i]=(ImageButton) findViewById(ids[i]);
int index=rand.nextInt(arraySize);
imagebuttons[i].setImageResource(flags[index]);
flags[index] = flags[--arraySize];
indexes.put(i,index);
imagebuttons[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if( questionArray==indexes.get(i))
{
}
}
});
I hope this will help you
Try out this way:
button1.setOnClickListener(onClickListener);
button2.setOnClickListener(onClickListener);
/**
* Common click listener
*/
OnClickListener onClickListener = new OnClickListener()
{
#Override
public void onClick(View p_v)
{
switch (p_v.getId())
{
case R.id.imageButton1:
//Your logic here.
break;
case R.id.imageButton2:
//Your logic here.
break;
}
}
}
Write below code
public class MainActivity extends Activity implements OnClickListener{
protected void onCreate(Bundle savedInstanceState) {
/// Your Above Code ///
tv.setText(questions[question[questionArray]]);
Imagebutton1.setOnClickListener(this);
Imagebutton2.setOnClickListener(this);
Imagebutton3.setOnClickListener(this);
Imagebutton4.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v == Imagebutton1){
// Write Your Code Here
} else if(v == Imagebutton2){
// Write Your Code Here
} else if(v == Imagebutton3){
// Write Your Code Here
} else if(v == Imagebutton4){
// Write Your Code Here
}
}
}
You can do it this way:
First, make your activity implements onClick Listener:
public class MyActivity extends Activity implements View.OnClickListener{
Then set the action listeners to your buttons:
button1.setOnClickListener(this);
button2.setOnClickListener(this);
.....
then in the listener, check the id:
public void onClick(View v) {
switch(v.getId()){
case R.id.imageButton1:
break;
case R.id.imageButton2:
break;
....
}
}
You can do the it this way also. You have to use some int variables to keep the tag value of the button. And also you have to create a onClickListner for the button actions. Coding is as follow
public class MyActivity extends Activity{
//Define the Tag values for image buttons.
private static final int TAG_IMAGE_BTN_1 = 0;
private static final int TAG_IMAGE_BTN_2 = 1;
private static final int TAG_IMAGE_BTN_3 = 2;
private static final int TAG_IMAGE_BTN_4 = 3;
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
initActivity();
}
private void initActivity(){
//Initialize the image buttons via xml or from the code itself.
//Add the onClickListner_BTN_CLICK and the corresponding tag to the image button
imageBtn1.setTag(TAG_IMAGE_BTN_1);
imageBtn1.setOnClickListener(onClickListner_BTN_CLICK);
imageBtn2.setTag(TAG_IMAGE_BTN_2);
imageBtn2.setOnClickListener(onClickListner_BTN_CLICK);
.......................................................
.......................................................
}
OnClickListener onClickListner_BTN_CLICK = new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int iTag = (Integer)v.getTag();
switch (iTag) {
case TAG_IMAGE_BTN_1:
break;
case TAG_IMAGE_BTN_2:
break;
case TAG_IMAGE_BTN_3:
break;
case TAG_IMAGE_BTN_4:
break;
}
}
};
}
The advantages of using the tag values for capture the action,
1)If we use R.id....... then we have to change the value in switch case if we change it in the xml file
2)We can use only one onClickListner for every action in an activity.