Image loading from dreawable folder using array - android

public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1;
final ImageView image;
button1 = (Button) findViewById(R.id.button1);
image = (ImageView) findViewById(R.id.imageView1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
int j=0;
while(j<=4){
int res=getResources().getIdentifier("d002_p00"+j,"drawable",getPackageName());
image.setBackgroundResource(res);
j = j+1;
}
}
});
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
}
});
}
}
I have 3 questions:
What is wrong with this code this code only show 1st and last image? how I can fix it?
I don't want to rotate my apps. How I can do that?
How I can mentioned a user in Stack Vverflow? I tried #username but it didn't work.

Answer for question 1: What is wrong with this code this code only show 1st and last image? how I can fix it?
Put the int j outside the onClick and change the while to an if
int j=0;
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if(j >= 0 && j <= 4){
int res = getResources().getIdentifier("d002_p00"+j, "drawable", getPackageName());
image.setBackgroundResource(res);
j++;
}
}
});
Answer for question 2: I don't want to rotate my apps. How I can do that?
See this answer of another stackoverflow question.
Answer for question 3: How I can mentioned a user in Stack Vverflow? I tried #username but it didn't work.
I don't know for sure myself, but I think you can't use #user_name to reply a comment when there is only one commenter apart from yourself (at least I couldn't). If you want to credit someone however, I would suggest putting their profile-page in a link like so: #user3546792 (Click their name, copy the link, click the Add Hyperlink-button in SO and paste the copied link. Then change [enter link description here] to [#user_name]

For question 2:
I don't want to rotate my apps. How I can do that?
Your just add this in your android Manifest:
android:orientation="portrait"

Related

How to change an Gif ImageView multiple times using the same button Android Studio

I'm trying to make something that's probably easy to do but I couldn't find any ways to do it. Basically, when I click on a button I want it to change one Imageview with the second Imageview, but then again, when I click on the same button, I want that second Imageview to be changed to the third Imageview, is that possible? So far, I've only done the first part, I can only change one Imageview with another, but can't do it to the third one. Here's my code
changeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
GifImageView gifImageView = findViewById(R.id.greengif);
gifImageView.setImageResource(R.drawable.yellow);
}
});
You need to count the click event.
int count = 0;
changeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
GifImageView gifImageView = findViewById(R.id.greengif);
if (count == 0) {
// show the second image
gifImageView.setImageResource(R.drawable.yellow);
} else if (count == 1) {
// show the third image
gifImageView.setImageResource(R.drawable.yellow);
}
count++;
}
}

Android: set one Button to do several things

Can anyone help me? I'm stuck on this thing for about week. I want to set one button to do several things. For example: when I pressed button once - b1.setBackgroundResource(R.drawable.a), when I pressed twice - b1.setBackgroundResource(R.drawable.b), when pressed third - b1.setBackgroundResource(R.drawable.c) and so on. It is possible to do this? Very thanks!
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b1 = (Button) findViewById(R.id.button);
b1.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
}
}
);
}
Have an int counter defined. In the onClick increment it and do whatever you want based on it's value
Yes it is possible
use button property known as setTag and getTag. It work same as that of flag
Example:
b1.setTag("1");
b1.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
if b1.getTag().equals("1"){
b1.setTag("2")
}
else if(b1.getTag().equals("2")) {}
}
}
);
I found a recent tutorial, detailing and explaining how to do what you want but it is accomplished using a short and long button press.
Try this: https://www.youtube.com/watch?v=cUgL8b7M8ns
Hope it helps.
Create a global variable
private int index = 0;
Then in your code do this
b1.setOnClickListener(
new Button.OnClickListener() {
public void onClick(View v) {
switch (index) {
case 0:
firstFunction();
break;
case 1:
secondFunction();
break;
}
// Advance your counter and extract the %
index++;
index = index % numOfDifferentFunctions;
}
}
);

GIF Listener in android

i'm using this How to animate .gif images in an android? solution (first answer of Shobhit Puri) to implements in my app some Gif. now i want to add some Listener on them (click listner).
every single procedure i tried didn't worked.
the idea is:
the gif is made by 3 image, when you click the first image the gif will change in another one, and so on.
Thanks for your attention.
EDIT:
actualy i'm trying this way
findViewById(R.id.ivAnimation).setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.i(TAG, "onClickGif PrincipalActivity");
}
});
on onCreate() of the mainActivity. the Log is well generated but i can't go over this
I'm not sure about gifs, but if you have separate files, you could do something like this.
Warning: Untested :)
final int[] imgs = new int[] {R.drawable.img1, R.drawable.img2, R.drawable.img3};
int position = 0;
final ImageView gifView = (ImageView) findViewById(R.id.ivAnimation);
gifView.setImageResource(imgs[position]);
gifView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v){
Log.i(TAG, "onClickGif PrincipalActivity");
position = (position + 1) % imgs.length; // wrap around to "restart" gif
gifView.setImageResource(imgs[position]);
}
});

Button pressed Image background set from Dreawable folder Using Array

public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1;
final ImageView image;
button1 = (Button) findViewById(R.id.button1);
image = (ImageView) findViewById(R.id.imageView1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
for (int j = 1; j < 6; j++) {
Drawable dreaw = getResources().getDrawable(getResources().getIdentifier("d002_p00"+j, "dreaw",getPackageName()));
image.setBackgroundResource("R.drawable." +dreaw);
}
}
});
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
}
}
i am new in android i want to change my image when i pressed my button image
in this line image.setBackgroundResource("R.drawable." +dreaw); it shows error how i can fix it please help me.
You can't do that :
Drawable dreaw = getResources().getDrawable(getResources().getIdentifier("d002_p00"+j, "dreaw",getPackageName()));
image.setBackgroundResource("R.drawable." +dreaw);
Try :
Drawable dreaw = getResources().getDrawable(getResources().getIdentifier("d002_p00"+j, "dreaw",getPackageName()));
image.setBackground(dreaw);
R.drawable.xxx is not a string it is a auto genarated intiger id for each resources.
image.setBackgroundResource("R.drawable." +dreaw);is wrong.
you can do onething
int res=getResources().getIdentifier("d002_p00"+j, "drawable",getPackageName()));
image.setBackgroundResource(res);
Try this
image.setBackgroundDrawable(dreaw);
This method is deprecated from api level 16 but fro backword compatibility you should use above method.
Just do:
image.setBackgroundDrawable(drew);
As your comment quotes "it worked bt it showa my first and last image."
You can only see the first and last image.Because the for loop is in a hurry burry,that means you can't see the changing of image,because the for loop is executed within ms(Milli or Micro Seconds).So you want to put this
Thread.sleep(<put_how_much_ms_you_want_to_make_the_for_loop_sleep);
to sleep the for loop time to time.If you put this
Thread.sleep(1000);//Make the thread or program blocked for 1 second.
For implementing in your For loop you can change your For loop like this.
for (int j = 1; j < 6; j++)
{
int res=getResources().getIdentifier("d002_p00"+j, "drawable",getPackageName()));
image.setBackgroundResource(res);
Thread.sleep(1000);//Make the for loop wait for 1 second.
}
By this you can see the transition of images.
May i know it helps you or not.

Android Dynamically Created Button: setOnClickListener doesn't work

The onClick never fires! Why not? Please help me.
for(int i = 0; i < 12; i++) {
String title = "Button" + i;
Button sliderButton = new Button(this);
sliderButton.setText(title);
glideMenuTray.addView(sliderButton,100,40);
sliderButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("gm", "Tapped ");
}
});
}
I'm no expert at stuff like this, but it's probably something to do with garbage collection, and the OnClickListeners passing out of scope.
Though I don't think you can use the super-easy approach to onClickListeners that Dimitar mentions, you can probably use the middle approach that the section he links to discusses, even though it's not a new approach. To repeat the example code here, it's:
View.OnClickListener handler = View.OnClickListener() {
public void onClick(View v) {
switch (v.getId()) {
case R.id.myButton: // doStuff
break;
case R.id.myOtherButton: // doStuff
break;
}
}
}
findViewById(R.id.myButton).setOnClickListener(handler);
findViewById(R.id.myOtherButton).setOnClickListener(handler);
If the only thing distinguishing the buttons is their title text, well, you could use that to distinguish between them in the master onClick method.
Also, not shure, I once had a problem like that on a TextView and it was because I didnt add setClickable(true)
My code was something like
TextView text = new TextView(this);
text.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
text.setText("***");
text.setClickable(true);
text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//My action
}
});
myViewGroup.addView(text );
Hope this helps
If you are using Donut or Eclair, you can use common click listener, registered in your Activity and hooked with your buttons in the layout XML.
For reference, look here, the category Easier click listeners.
Am I right in assuming that the following line:
glideMenuTray.addView(sliderButton,100,40);
Adds the view to the coords x:100,y:40 onto some View extending ViewGroup?
In that case you are stacking 12 buttons on top of each other, only the last Button (labeled Button11) will be visible (and clickable).
And provided that the question is 3 years old I really hope you already resolved this by now :)
set the setOnClickListener before adding the view.
for(int i = 0; i < 12; i++) {
String title = "Button" + i;
Button sliderButton = new Button(this);
sliderButton.setText(title);
sliderButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Log.d("gm", "Tapped ");
}
glideMenuTray.addView(sliderButton,100,40);
}

Categories

Resources