Here's the thing:
I have two classes: Main and control.java
the Main class is an Activity class where I build my app and the control class I just use for variables controls that I must access from other classes.
The problem is: In class Main I got 2 methods, and I have an ImageView in each of them, I need to set the image view resource of the second method on a click listener of the first one. Like this:
public void first() {
final ImageView first = (ImageView) findViewById(R.id.myview);
first.setBackgroundResource(R.drawable.myimage);
}
public void second() {
final ImageView second = (ImageView) findViewById(R.id.myview2);
//And then, I want something like this: first.setBackgroundResource(first);
}
Thanks guys!
Why don't you just do something like:
public void second() {
final ImageView second = (ImageView) findViewById(R.id.myview2);
final ImageView first = (ImageView) findViewById(R.id.myview);
first.setBackgroundResource(R.drawable.myimage);
}
Otherwise, you'd have to use a private/public class variable and define it outside the method.
I admit to being a bit confused by your question but I think below is at least the start of what you're looking for
#Override
protected void onCreate(Bundle savedInstanceState) {
control.setImageView((ImageView) findViewById(R.id.myview));
second = (ImageView) findViewById(R.id.myview2);
}
public void first() {
control.getImageView().setBackgroundResource(R.drawable.myimage);
}
public void second() {
second.setBackgroundDrawable(control.getImageView().getDrawable());
}
ImageView second;
Related
I'm new at androidstudio and want to compare a imageView by the following:
I have 2 imageView, both are using a drawable i named "blank" at the start of the app, using if/else I want to chance those images to another drawable i have, i tried the following:
private ImageView equipament1;
private ImageView equipament2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
public void sentImg() {
if (equipament1.equals(R.drawable.blank)){
equipament1.setImageResource(R.drawable.reactor);
}
else if (equipament2.equals(R.drawable.blank)){
equipament2.setImageResource(R.drawable.reactor);
} else {finish();}
but it doesn't work, the app just replaces the first image and if i click on the button again, nothing happens (this if/else is inside a button).
I want to check if the first image is blank, if it is, the app should replace the blank image with the image "reactor" or, if is not blank, the app should move to the second blank image, and replace it and this go on for more 2 blank spaces.
I'm doing this because I'm building an program similar to LucidChart where you put your equipments in the app.
The problem is that the second time you have already changed the value of the comparator.
If the objective is just to change the images you don't need the if/else.
private ImageView equipament1;
private ImageView equipament2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
public void sentImg() {
equipament1.setImageResource(R.drawable.reactor);
equipament2.setImageResource(R.drawable.reactor);
}
When the user clicks your button, you want to do 2 things. You want to show some images, or you want to call finish().
I would suggest using a boolean as a flag the the state and compare that instead of comparing the ImageView itself. This'll be easier, and make your code easier to read.
I created a flag called firstClick that is set to true by default. When the user clicks your button (button1 in this example), we check against that and show the images. Then we set it to false,
so the next click will call finish().
private ImageView equipament1;
private ImageView equipament2;
// The current state of the Activity
private boolean firstClick = true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_analise)
equipament1 = findViewById(R.id.equipamento1);
equipament2 = findViewById(R.id.equipamento2);
// Setting your OnClickListener
Button button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if( firstClick ) {
firstClick = false;
sentImg();
} else {
finish();
}
}
});
}
public void sentImg() {
equipament1.setImageResource(R.drawable.reactor);
equipament2.setImageResource(R.drawable.reactor);
}
I wanted to make it with TransitionDrawable class, but it needs a separate file transition.xml. There I define which image I change to.
I need to define them in Java code because I don't know which images I will change too. I have many images and I accidentally get only two images which will change between each other. What can I do? Perhaps I need another class.
Code with transition.xml:
public class TransitionActivity extends Activity
implements OnClickListener {
private ImageView image;
private TransitionDrawable mTransition;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
image = (ImageView)findViewById(R.id.image);
image.setOnClickListener(this);
Resources res = this.getResources();
mTransition = (TransitionDrawable)res.getDrawable(R.drawable.transition);
}
#Override
public void onClick(View v) {
image.setImageDrawable(mTransition);
mTransition.startTransition(1000);
}
}
You can programmatically create a TransitionDrawable using the class' constructor. You don't need to acquire it from XML. This gives you the flexibility of dynamically assigning the Drawables it transitions between.
// drawable1 and drawable2 can be dynamically assigned in your Java code
Drawables[] drawables = new Drawables[] {
drawable1, drawable2
};
mTransition = new TransitionDrawable(drawables);
[SOLVED] Silly typo: This code solved my problem:
dateTimeEasyText.setText (""); changed to dateAndTimeEasyText.setText ("");
.
PROBLEM:
I have an ImageView which on click should reset two of my TextViews, one containing HighScore (numbers) and the other TextView containing Date & Time (String).
My coding:
public void resetHighcoreButtonEasy(View v) {
highscoreEasyText.setText("");
dateTimeEasyText.setText ("");
}//resetHighcoreButtonEasy ends here
.
Printscreen on the coding and the message:
.
JAVA-file:
public class HighScoreActivity extends AppCompatActivity implements View.OnClickListener {
TextView highscoreEasyText;
TextView dateAndTimeEasyText;
ImageView resetHighcoreButtonEasy;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_high_score);
resetHighcoreButtonEasy = (ImageView) findViewById(R.id.resetHighcoreButtonEasy);
SharedPreferences sharedPrefsEasyHighScore = getSharedPreferences("Prefs_EasyHighScore",MODE_PRIVATE);
int storedEasyHighScore = sharedPrefsEasyHighScore.getInt("easy_highScore",0);
highscoreEasyText = (TextView)findViewById(R.id.highscoreEasyText);
highscoreEasyText.setText("" + storedEasyHighScore + " p");
highscoreEasyText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.highscore_text));
SharedPreferences sharedPrefsEasyDateTime = getSharedPreferences("Prefs_EasyDateTime",MODE_PRIVATE);
String dateTime = sharedPrefsEasyDateTime.getString("easy_date_time", null);
dateAndTimeEasyText = (TextView)findViewById(dateTimeEasyText);
dateAndTimeEasyText.setText(dateTime);
dateAndTimeEasyText.setTextColor(ContextCompat.getColor(getApplicationContext(), R.color.highscore_text));
}//onCreate ends here
public void resetHighcoreButtonEasy(View v) {
highscoreEasyText.setText("");
dateTimeEasyText.setText ("");
}//resetHighcoreButtonEasy ends here
You are using wrong variable to access dateTimeEasyText.
As per your declaration it is suppose to be dateAndTimeEasyText.
Spell mistake.
Just a typo. You have:
TextView dateAndTimeEasyText;
And in your method you use it without And:
dateTimeEasyText.setText ("");
You should use:
dateAndTimeEasyText.setText("");
So dateTimeEasyText isn't a TextView, and it hasn't got a method setText(java.lang.String).
dateTimeEasyText should be `dateAndTimeEasyText`.
dateAndTimeEasyText.setText("");
In layout xml for ImageView resetHighcoreButtonEasy onClick should be there:
<ImageView
android:id="#+id/resetHighcoreButtonEasy"
...
...
android:onClick="resetHighcoreButtonEasy"
..... />
This line of code should be there to give click event from xml
android:onClick="resetHighcoreButtonEasy"
And your function should be public and have a param View in it, as you have done already
public void resetHighcoreButtonEasy(View v) {
I'm trying to crossfade two ImageViews by invoking a method when the first image is clicked, then we fade into the second image(alpha set to 0 initially), then I would like to fade back into the first image after clicking on the second image.
It works when crossfading from one image to the other using only one method, but if when I add the other method to crossfade back to the previous image, nothing happens when I click on the image.
public class MainActivity extends AppCompatActivity {
public void narutoFade(View view){
ImageView naruto =(ImageView) findViewById(R.id.naruto);
ImageView narutosage =(ImageView) findViewById(R.id.narutosage);
naruto.animate().alpha(0f).setDuration(2000);
narutosage.animate().alpha(1f).setDuration(2000);
}
public void narutoSageFade(View view) {
ImageView naruto2 = (ImageView) findViewById(R.id.naruto);
ImageView narutosage2 = (ImageView) findViewById(R.id.narutosage);
narutosage2.animate().alpha(0f).setDuration(2000);
naruto2.animate().alpha(1f).setDuration(2000);
}
}
Maybe you should think about declaring both as Fields, declare them only once in onCreate()/onResume(), and write one crossfade method that serves both :
public class MainActivity extends AppCompatActivity {
private ImageView naruto, narutosage;
#Override
protected void onCreate(Bundle savedInstanceState) {
...
naruto =(ImageView) findViewById(R.id.naruto);
narutosage =(ImageView) findViewById(R.id.narutosage);
}
public void crossfade(View fadeIn, View fadeOut) {
fadeIn.animate().alpha(1).setDuration(2000);
fadeOut.animate().alpha(0).setDuration(2000);
}
}
and in your Button onClicks you just call :
crossfade(naruto, narutosage);
or
crossfade(narutosage, naruto);
I have a serie of ImageViews on my Activity. And I wanna execute a method when one of these is touched. I have the next:
public void onClick(View v) {
switch(v.getId()){
case R.id.image1:
mymethod(1,1,movimientos,(ImageView)v);
break;
case R.id.image2:
ponerficha(1,2,movimientos,(ImageView)v);
break;
case R.id.image3:
...
But the method isn't executed, The problem not is the method, because any code in the cases not work. Any idea?
First thing what you need to check if you already register onClickListener for your Images
image.setOnClickListener(this);
(This you have to use if your class implements OnClickListener interface)
Then how you declare and initialize your ImageViews, whether have own ids.
image = (ImageView) findViewById(R.id.someId)
anotherImage = (ImageView) findViewById(R.id.anotherId)
...
You can work with onClickListeners like with anonyme classes like
image.setOnClickListener(
new View.OnClickListener() {
public void onClick(View v) {
// some actions
}
});
but more complex and better in the case you have many widgets to set implement OnClickListener.
public class ClassName extends Activity implements View.OnClickListener {}
First, your activity has to implement View.OnClickListener like so
public class myActivity implements View.OnClickListener {
Then you need to set your on click listener for your ImageViews. If all your ImageViews are in a linearlayout then the code would look like this
LinearLayout llImageViewHolder = (LinearLayout) findViewById(R.id.llImageViewHolder);
for (int i = 0; i < llImageViewHolder.getChildCount(); i++ {
ImageView iv = (ImageView) llImageViewHolder.getChildAt(i);
iv.setOnClickListener(this);
}
Cheers.
You need to add an onClick handler to each view you are interested in processing clicks for.
How you do it depends on how you want to process the click events. You can either use hawaii.five-0's approach and have one event handler for everything, or you can have one event handler per view item which you could add in the onCreate method of your activity:
ImageView imageView = (ImageView) findViewById(R.id.image_view);
imageView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// Do something
}
}