This is my code. In this code I am trying to change the content of four text on a button click on first time and on same button click I am trying to change the content of only three textview. But it's not working.
public class Home_page_Activity extends Activity {
Button next;
int textids[]={R.id.text1,R.id.text2,R.id.text3,R.id.text4};
String Question[]={"1","2","3","4"};
String three[]={"7","27","37"};
TextView t1,t2,t3,t4,t5,t6,t7;
int count=0;
int check=0;
//int button_id[]={R.id.next};
//Button btn[];
int test=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page_);
t1=(TextView)findViewById(R.id.text1);
t2=(TextView)findViewById(R.id.text2);
t3=(TextView)findViewById(R.id.text3);
t4=(TextView)findViewById(R.id.text4);
t5=(TextView)findViewById(R.id.text5);
t6=(TextView)findViewById(R.id.text6);
t7=(TextView)findViewById(R.id.text7);
next=(Button)findViewById(R.id.next);
next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if(test==1)
{
String get=(String) t1.getText();
//int inter=Integer.parseInt(get);
Log.i("t1", get);
t1.setText("");
t2.setText("");
t3.setText("");
t4.setText("");
threetext();
}
nextquestion();
}
});
}
public void nextquestion()
{
t1.setText(Question[count]);
t2.setText(Question[count+1]);
t3.setText(Question[count+2]);
t4.setText(Question[count+3]);
Log.i("count", "value" +count);
test++;
}
public void threetext()
{
Log.i("threetext", "working");
t1.setText(three[check]);
t2.setText(three[check+1]);
t3.setText(three[check+2]);
t4.setText("");
}
Could you increment a counter in the onClick and change the views based on the counter value?
Related
I want to calculate a value from the data in 2 text edits and show the result in a textview. How can I do it?
BMI calculator:
private TextView deinbmi;
private EditText weight;
private EditText height;
public void calculator ()
{
double ergebnis;
weight=(EditText)findViewById(R.id.text_calculator_weight);
height=(EditText)findViewById(R.id.text_calculator_height);
double a=Double.parseDouble(weight.getText().toString());
double b=Double.parseDouble(height.getText().toString());
ergebnis=a*b;
deinbmi.setText(""+ergebnis);
}
I don't have an idea; I just started with Java. I can program with C++ but just console.
If I search on the internet for this problem, everyone has another version of solving this, but none of these answers worked for me?
Try This Working code.
you Missing the Textview Inside oncreate, Then Add a Button for Getting the Answer
public class Mainactivity extends Activity implements OnClickListener
{
private TextView deinbmi;
private EditText weight;
private EditText height;
Button Answer;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.page1_landing);
weight=(EditText)findViewById(R.id.text_calculator_weight);
height=(EditText)findViewById(R.id.text_calculator_height);
deinbmi=(Textview)findViewById(R.id.text);
Answer=(Button)findViewById(R.id.answerbuttton);
Answer.SetOnclicklistener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.answerbuttton:
calculator ();
break;
}}
public void calculator ()
{
double ergebnis;
double a=Double.parseDouble(weight.getText().toString());
double b=Double.parseDouble(height.getText().toString());
ergebnis=a*b;
deinbmi.setText(""+ergebnis);
}
}
public class calculator extends ActionBarActivity {
private TextView deinbmi;
private EditText weight;
private EditText height;
public void calculator ()
{
double ergebnis;
weight=(EditText)findViewById(R.id.text_calculator_weight);
height=(EditText)findViewById(R.id.text_calculator_height);
double a=Double.parseDouble(weight.getText().toString());
double b=Double.parseDouble(height.getText().toString());
ergebnis=a*b;
deinbmi.setText(""+ergebnis);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_calculator);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_calculator, menu);
//rechnen ?!
Button credits_calculate = (Button)
findViewById(R.id.button_calculate);
credits_calculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
calculator();
}
});
//back
Button credits_back = (Button)
findViewById(R.id.button_calculator_back);
credits_back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
is my activity
i tryed to use your code but
Answer=(Button)findViewById(R.id.answerbuttton);
Answer.SetOnclicklistener(this);
he didnt understand this
I have the following code
public class Answer extends Activity implements KeyListener{
EditText ed1;
Button b;
LinearLayout l;
int flag=0;
int f=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.answer);
ed1=(EditText)findViewById(R.id.answer);
b=(Button)findViewById(R.id.ans);
l=(LinearLayout)findViewById(R.id.An);
ed1.setKeyListener(this);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
if(flag==1)
l.setBackgroundColor(Color.BLACK);
}
});
}
public boolean onKeyUp(View arg0, Editable arg1, int arg2, KeyEvent arg3) {
// TODO Auto-generated method stub
switch(arg2){
case KeyEvent.KEYCODE_A:
if(f==0){
ed1.setText("");
f++;
flag=1;
}
}
return false;
}
The problem with this code is that on pressing 'a' it is setting color but after that it doesn't show anything in the edittext - ie it is setting color but after that it is not showing anything in edittext.If we press 'a' followed by 'n' then it will hide both 'a' and 'n' but i want to hide 'a' only
If you are looking to remove all of the a's from your EditText every time that you type 'a', then ed1.setText(""); is the culprit (it's clearing your EditText value every time).
Try this edit in your switch case:
String currText = ed1.getText().toString();
ed1.setText(currText.replace("a", ""));
I want to increase the value of the text on the click of abutton. However, I want to increase the value by certain amount. My initial value is 250 and I want to increase the textvalue by 250 every time i click on button.
I have written logic for it but the value increases by one.
This is the relevant code:
public class SelectCartListViewAdapter extends BaseAdapter{
private Context mcontext;
private static int counter = 250;
private String stringVal;
public SelectCartListViewAdapter(Context c){
mcontext = c;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//... some other code
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Log.d("src", "Increasing value...");
counter++;
stringVal = Integer.toString(counter);
tv1.setText(stringVal);
}
});
//...some other code
return myView;
}
}
That's because you have used a incremental operator here,
counter++;
Incremental operator will increase value by one only.
It should be something like this,
counter= counter+actualValue;
Try this
int counter=0, staticCounter=250;
#Override
public void onClick(View v) {
counter= counter+staticCounter;
tv1.setText(String.valueOf(counter));
}
Replace part of your code with the below code fragment.
ImageButton button = (ImageButton)myView.findViewById(R.id.addbutton);
button.setOnClickListener(new OnClickListener() {
//private int _counter;
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//Toast.makeText(mcontext,"Button is clicked",Toast.LENGTH_SHORT).show();
Log.d("src", "Increasing value...");
counter+=250;
stringVal = Integer.toString(counter);
tv1.setText(stringVal);
// int value = (Integer.parseInt((String) tv1.getText()))+250;
// tv1.setText(value);
}
});
Hello I am very new in android
I am trying to get integer value from an EditText, But When I am parsing string to Integer I got NumberFormatException.
Please help me to come out of this error.
thanks in advance.
Program is:
int day,month,year;
EditText expense,ammount;
String[] exp=new String[10];
int[] amt=new int[10];
int count=0;
/** Called when the activity is first created. */
#Override
public void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Calendar cal=Calendar.getInstance();
day=cal.get(Calendar.DAY_OF_MONTH);
month=cal.get(Calendar.MONTH)+1;
year=cal.get(Calendar.YEAR);
final TextView txtdate=(TextView)findViewById(R.id.txtdate);
expense=(EditText)findViewById(R.id.exp);
ammount=(EditText)findViewById(R.id.amnt);
final Button add=(Button)findViewById(R.id.btnadd);
final Button cancel=(Button)findViewById(R.id.btncancel);
final Button done=(Button)findViewById(R.id.btndone);
txtdate.setText(day+"/"+month+"/"+year);
add.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
getval();
}
});
cancel.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
clean();
}
});
done.setOnClickListener(new OnClickListener() {
public void onClick(final View v) {
total();
getval();
clean();
final TextView tv=(TextView)findViewById(R.id.textView1);
tv.setText(Integer.toString(total()));
}
private int total() {
int total = 0;
// TODO Auto-generated method stub
for(int i=0;i<=count;i++)
{
total+=amt[i];
}
return total;
}
});
}
protected void clean() {
// TODO Auto-generated method stub
expense.setText(" ");
ammount.setText(" ");
}
protected void getval() {
// TODO Auto-generated method stub
final Editable e2=expense.getText();
final Editable e1=ammount.getText();
final int i=Integer.parseInt(e1.toString());
amt[count]=i;
exp[count]=e2.toString();
System.out.println(amt[count]);
System.out.println(exp[count]);
count++;
}
}
Exception is:
java.lang.NumberFormatException: unable to parse ' 600' as integer
Remove any leading or trailing spaces from the number first:
int inputNumber = Integer.parseInt(editText.getText().toString().trim());
Alternatively, you can remove all non-numeric characters from the string using regular expressions:
String cleanInput = editText.getText().toString().replaceAll("[^\\d]", "");
int inputNumber = Integer.parseInt(cleanInput);
Though if non-numeric input characters is a problem you'd probably want to restrict the EditText to numeric only. See this question. It says to add the following attribute to the EditText:
android:inputType="number"
You have a space in your integer.
Add the following attribute to your EditText in your xml to only allow entering integers:
android:inputType="number"
Try this..
final int i=Integer.parseInt(e1.toString().trim());
bacause there is a space before that number see ' 600' that's why that error..
Hope this will help..
use this code
final int f = -1; // or other invalid value
if (edittext.getText().toString().length() > 0)
f = Integer.parseInt(edittext.getText().toString());
I am writing a program that when the user enter a number text appears according to that number. My problem is that the button line has public void ... after this I am trying to use if statements and return methods, but because of the public void, the return method can not return anything. I tried to close the public void, but I am getting errors. Please help.
The code is as follows. I have included the different codes that I have tried like toast, etc..
ente#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button)findViewById(R.id.go);
button.setOnClickListener(mAddListener);
// tv = (TextView) findViewById(R.id.textView1);
}
private OnClickListener mAddListener = new OnClickListener()
{
public void onClick(View v) {
}}
;
//Toast.makeText(Num.this, "This Display", Toast.LENGTH_SHORT).show();
//toast.show();
//finish ();
// long id = 0;
// try
{
PleaseEnter=(EditText)findViewById(R.id.PleaseEnter);
{
}
if (PleaseEnter.equals("1"))
// tv.setText("This is the display 1.");
return "This display";
// Context context = getApplicationContext();
// CharSequence text ="this display";
// int duration =Toast.LENGTH_LONG;
// Toast toast =Toast.makeText(context, text, duration);
// toast.show();
else if (PleaseEnter.equals("2"))
return;
//tv.setText("Dispaly 2");
You can define your own method at Activity level, such as:
private void onTextEdited(String content) {
// deal with the String
}
In the onClick method of your OnClickListener, you can call it such as:
public void onClick(View v) {
EditText myEditText = (EditText) findViewById(R.id.PleaseEnter);
onTextEdited(myEditText.getText().toString());
}