This question is unlikely to help any future visitors; it is only relevant to a small geographic area, a specific moment in time, or an extraordinarily narrow situation that is not generally applicable to the worldwide audience of the internet. For help making this question more broadly applicable, visit the help center.
Closed 9 years ago.
Can anyone help? I need to generate a random number, and then
public class MainActivity extends Activity implements OnClickListener {
TextView tv1;
Button bt1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bt1 = (Button) findViewById(R.id.button1);
bt1.setOnClickListener(this);
tv1 =(TextView) findViewById(R.id.textView1);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
#Override
public void onClick(View v) {
switch(v.getId()){
case R.id.button1:
Random r = new Random();
int i1=r.nextInt(80-65) + 65;
tv1 = (TextView) findViewById(R.id.textView1);
tv1.setText(i1);
break;};
}
}
And then it closes app after pushing button.
Change
tv1.setText(i1);
with
tv1.setText(String.valueOf(i1));
with tv1.setText(i1); you are looking for a String with id i1, inside string.xml. That id probably does not exists and will cause a crash for ResourcesNotFoundException
Edit: the tv1 = (TextView) findViewById(R.id.textView1); inside the onClick is useless since you are performing the same initialization inside the onCreate. Also you can move Random r = new Random(); inside the onCreate. Of course you have to keep r as class member
Related
This question already has answers here:
How do I compare strings in Java?
(23 answers)
Closed 6 years ago.
Click Event
public void buttonOnClick(View v) {
Button button=(Button) v;
((Button) v).setText("clicked");
Changes BackGround to an image
RelativeLayout rl = (RelativeLayout) findViewById(R.id.BackGround);
rl.setBackgroundResource(R.drawable.mapwork);
Makes Certain Items Disapear
button.setVisibility(View.INVISIBLE);
EditText mText2 = (EditText) findViewById(R.id.input);
mText2.setVisibility(View.INVISIBLE);
Changes what is in the output if they put in a certain number
EditText mText = (EditText) findViewById(R.id.output);
if("8" == mText.getText().toString()){
mText.setText("That does not work!");
}
}
When I test the application the If statement never happens even when the requirements are met. I have tried look for an answer and have not found one. Thank you for helping.
Now it will work
if(mText.getText().toString().equals("8")){
mText.setText("That does not work!");}
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 9 years ago.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Improve this question
I have a random number generator and I need to display different image,
depending on different Int values.
For example when randomNumber is let's say 1, I need to show specific text and image
and when number is 10 another specific text and image etc..
Can I even do that with ImageView, I do not know where to start?
package com.example.drinktivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class Main2Activity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button back = (Button) findViewById(R.id.buttonback); // Here the R.id.button1 is the button from you design
back.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
Intent i = new Intent(Main2Activity.this, MainActivity.class);
startActivity(i);
}
});
ImageView image = (ImageView) findViewById(R.id.imageView1);
Bundle bundle = getIntent().getExtras();
int random = bundle.getInt("Value");
TextView text = (TextView) findViewById(R.id.textView1);
if (random==1) {
text.setText("");
}
if (random==2) {
text.setText("");
}
if (random==3) {
text.setText("");
}
if (random==4) {
text.setText("");
}
if (random==5) {
text.setText("");
}
if (random==6) {
text.setText("");
}
if (random==7) {
text.setText("");
}
if (random==8) {
text.setText("");
}
if (random==9) {
text.setText("");
}
if (random==10) {
text.setText("");
}
}
#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;
}
}
Create a drawable array
public static final int[] im_smiley_drawable_smile_old = {
R.drawable.im_smiley_happy, R.drawable.im_smiley_sad,
R.drawable.im_smiley_winking,
R.drawable.im_smiley_tongue_sticking_out,
R.drawable.im_smiley_surprised, R.drawable.im_smiley_kissing,
R.drawable.im_smiley_yelling, R.drawable.im_smiley_cool}
send ramdom numbers to this array you will get random images
int[] i = {R.drawable.drawable1, R.drawable.drawable2, R.drawable.drawable2, R.drawable.drawable4, R.drawable.drawable5,};
Random rand = new Random();
int n = rand.nextInt(5);
imageView.setBackground(getResources().getDrawable(i[n]));
consider your int value as int random
and your random no consists of numbers 1 to 5.
then you can do this.
if(random==1){
imageview.setImageBitmap(yourbitmap);
//here imageview is your imageview. and then bitmap you want when the number generated is 1.
}
This is definitely possible, but there are a lot of ways of doing this. I think the main approach that you'd want to go for is generating a random number, and choosing an image from your resources based on that number. You can retrieve resources like this:
context.getResources().getIdentifier(i.getIcon(), "drawable", context.getPackageName())
Beginner here so sorry for the lack of skill.
I'm trying to make an app that dynamically add edittext's and multiply them(rows) and then add the totals in a grand total.
I managed to make it dynamically add edittexits,make it scroll,reset but i'm trying to figure out how to assign id's to every textedit and then multiply the values(from the two edit texts on the rows) and then sum all the results in a grand total.I think i know how to set id's for every edittext( with setid(i) with a for) but i can't find a way to
I think i found a way but it's a bit hard to try just to possibly fail so i'm asking you guys for help.
There has to be an easier way that i don't know,since i'm an absolute beginner.
Please give me some alternatives if available and examples of how i can apply them.
Thanks in advance !
Here's my code so far :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void produsnou(View v) {
LinearLayout l1 = (LinearLayout) findViewById(R.id.layout1);
EditText et = new EditText(this);
et.setHint("Produs");
l1.addView(et);
int count = 4;
int i = 4;
for (i = 4; i < count; i++)
;
LinearLayout l2 = (LinearLayout) findViewById(R.id.layout2);
EditText et2 = new EditText(this);
et2.setHint("Cantitate");
et2.setInputType(InputType.TYPE_CLASS_NUMBER
| InputType.TYPE_NUMBER_FLAG_DECIMAL);
l2.addView(et2);
et2.setId(i);
LinearLayout l3 = (LinearLayout) findViewById(R.id.layout3);
EditText et3 = new EditText(this);
et3.setHint("Pret");
et3.setInputType(InputType.TYPE_CLASS_NUMBER
| InputType.TYPE_NUMBER_FLAG_DECIMAL);
l3.addView(et3);
et3.setId(i + 1);
count = count++;
RelativeLayout l4 = (RelativeLayout) findViewById(R.id.layout4);
}
public void reload(View v) {
Intent intent = getIntent();
finish();
startActivity(intent);
}
#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;
}
}
Create a List and push each EditText you have created into the List.
Once the list is completed, just go all over it and make the calculations.
Here's a sample code:
List<EditText> someList = new ArrayList<EditText>();
//Let's say we'd like to add 10 EditTexts
for(int i = 0; i < 10; i++){
EditText t1 = new EditText(); //The EditText you'd like to add to the list
t1.setText("lol"); //Give the EditText the value 'lol'
someList.add(t1); //Add the EditText to the list
}
//Go over the list and get the values
for(EditText t : someList){
String val = t.getText().toString(); //Get the value for the temp EditText variable t
}
In theory it should work, yet I haven't checked it.
I have seen lots of example to which one use a if condition or a case statement to programmatically change the conditions of elements...yadda yadda. I need to change the value of a button based on what the user clicks. Below is the code that I currently have.
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){CONTINUES TO OTHER FUNCTIONS }
I basically need to know what button was pressed. Then dynamically populate it into findViewById() function. i.e.
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
This way by the time it gets to the final Button part of the code it will have the value to which the user clicked on. Code works if I only want to have one button or a page mile deep in different configuration (not ideal).
All the examples I have seen so far are after the user clicks the button (which is what I want) but they name the buttons name statically like above code shows but very much static.
Hope that all makes sense.
UPDATE:
I think I may have confused the situation. Below is the remaining code. Hopefully this will provide context. The btnOpenPopup needs to remain the same as it's used in the call to execute the command for a new window to actually popup. Hopefully this will provide a bit more context for what I'm trying to achieve.
final Button finalBtnOpenPopup = btnOpenPopup;
btnOpenPopup.setOnClickListener(new Button.OnClickListener(){
#Override
public void onClick(View arg0) {LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.meditationpopup, null);
//set the title of the popup
TextView titletext = (TextView) popupView.findViewById(R.id.chakratitle);
titletext.setText(activityName);
if (activityName.equals("Root"))
{
switch (arg0.getId())
{
case R.id.color:
//Rename the string so to get the value from the string xml
String stringName = activityName.toLowerCase().replaceAll(" ","")+"color";
TextView desctext = (TextView) popupView.findViewById(R.id.popupDesc);
desctext.setText(getString(getStringResource(getApplicationContext(),stringName)));
break;
case R.id.polarity:
//Rename the string so to get the value from the string xml
String polarityString = activityName.toLowerCase().replaceAll(" ","")+"polarity";
TextView polarityDesc = (TextView) popupView.findViewById(R.id.popupDesc);
//polarityDesc.setText(activityName);
polarityDesc.setText(getString(getStringResource(getApplicationContext(),polarityString)));
break;
}
}
I think
Button btnOpenPopup = (Button)findViewById(R.id.polarity);
btnOpenPopup = (Button) findViewById(R.id.color);
should be
Button btnOpenPopupFirst = (Button)findViewById(R.id.polarity);
Button btnOpenPopupSecond = (Button) findViewById(R.id.color);
you should declare different different button for diffrerent findviewbyid
also in my eclipse it is not accepting
btnOpenPopup.setOnClickListener(new Button.OnClickListener()
instead it works with
btnOpenPopup.setOnClickListener(new View.OnClickListener() {}
and you need to provide more clear view of what you want to perform
new thoughts,try doing this:
btnOpenPopupFirst.setOnClickListener(this);
btnOpenPopupSecond.setOnClickListener(this);
then option will come on both the above code lines
The method setOnClickListener(View.OnClickListener) in the type View is not applicable for the arguments (MainActivity)
choose this
let MainActivity implement OnClickListener
then this option will come
The type MainActivity must implement the inherited abstract method View.OnClickListener.onClick(View)
choose
add unimplemented method
now
#Override
public void onClick(View v)
will be created
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.polarity:
Toast.makeText(getApplicationContext(), "btnOpenPopupFirst(polarity) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
break;
case R.id.color:
Toast.makeText(getApplicationContext(), "btnOpenPopupSecond(color) is pressed", Toast.LENGTH_SHORT).show();
//other code to be performed regarding this button
default:
break;
}
}
And post your views after implementing this way.
int[] id={R.id.button1,R.id.button2};
Button b=(Button)findViewById(id[i]);
The onClick method in Button.OnClickListener has a View parameter... you can call getId() on that view to get the id of that button that was clicked on.
It doesn't make too much sense to me. If what you really want is this:
btnOpenPopup = (Button) findViewById(R.id.DYNAMTICVALUEOFBUTTONUSERHASPRESSED);
All you need to do is set your value in the onClick(View view) method of your OnClickListener
public void onClick(View view) {
btnOpenPopup = (Button)view;
}
I'm kind of new when it comes to Android Application development and I'm developing an app at the moment. I'm trying to get my TextView change every time the user clicks the Button(NEXT) and when another Button (PREVIOUS) gets clicked on I want it to change back to the original TextView. So basically I'd like to set up a certain amount of TextViews and be able to browse through them with the two Buttons I mentioned.
So far I only know how to make the the TextView change one time on a Button(NEXT) click. I'm using this piece of code for that:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton Next = (ImageButton) findViewById(R.id.Next);
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
TextView Text1= (TextView) findViewById(R.id.Text1);
Text1.setText("New Text");
}
});
NOTE: The Button "PREVIOUS" isn't included yet because I didn't know what to do with it yet.
I'm getting the feeling this code is only used when you want the TextView to change one time and you need a whole different method to make it change multiple times.
I hope I provided you with enough information and you are willing to help me out here.
Thanks in advance!
public class MyActivity extends Activity implements View.OnClickListener {
int stringIdList[] = {R.string.text1, R.string.text2, R.string.text3, R.string.text4}
int stringListCounter = 0;
TextView text1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton next = (ImageButton) findViewById(R.id.Next);
ImageButton previous = (ImageButton) findViewById(R.id.Previous);
text1 = (TextView) findViewById(R.id.Text1);
Next.setOnClickListener(this);
previous.setOnClickListener(this);
}
#Override
public void onClick(View v) {
int id = v.getId();
if(id == R.id.Next && stringListCounter < stringIdList.length - 1) {
stringListCounter++;
} else if (id == R.id.Previous && counter > 0) {
stringListCounter--;
}
Text1.setText(stringIdList[stringListCounter]);
}
What this does is assigns your Activity to an OnClickListener to handle the click events. If Next was pressed and the counter is within the range of the array list, it will increase the counter. The same for previous. At the end of the click, it will set the text to whatever the ID is. This assumes your strings are in a strings.xml file which is recommended in the Android spec and is static.
I think you can store history as List and have all states of textview in each moment.
Only thing that you have to do is to take previous value from this history stack after pressing previous button.