I am randomizing the location of a button on a Relative View everytime a button is clicked. I am doing this by changing the margins of the buttons programatically as shown in the code:
Button testbtn = findViewById(R.id.testbtn);
testbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Get Screen size
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
//Save dimensions in variables
int widthScreen = metrics.widthPixels;
int height = metrics.heightPixels;
TextView width_height = findViewById(R.id.width_height);
//Declare random
Random randomDimension = new Random();
//Declare layout params
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT);
params.rightMargin = 608;
params.leftMargin = 428;
params.topMargin = randomDimension.nextInt(height) +1;
//params.setMargins();
buttonIDs[1].setLayoutParams(params);
width_height.setText(params.leftMargin+" "+params.rightMargin);
}
});
In this case, "buttonIDs[]" is an array i declared earlier in the class and "testbtn" is the button that when pressed, randomizes the position of my button.
However, these specific margins produce the following image
enter image description here
and I need:
enter image description here
Does anyone know why this is happening and how to fix it?
You set:
params.rightMargin = 608;
params.leftMargin = 428;
So the device must be 608+428+width of the button at least wide. Is it?
Related
The purpose of this is to create SimonSays game and do the logic behind generating and displaying a pattern that needs to be repeated
sequenceArray is already populated with random values from 0 to 3, using Random class and for loop, then what I want to do is read depending on the case of each element of the sequenceArray array, I want to change one of 4 views in a way that the animation lasts 200 ms and it should give the end user a feeling of pressing a button, or in this case, a button flashing. Here's how I did this:
for(int j = 0; j < sequenceArray.length; j++){
switch(sequenceArray[j]){
case 0:
Drawable backgrounds_yellow[] = new Drawable[2];
Resources res_yellow = getResources();
backgrounds_yellow[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_yellow);
backgrounds_yellow[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_yellow_blink);
TransitionDrawable crossfader_yellow = new TransitionDrawable(backgrounds_yellow);
ImageView ivYellow = (ImageView) findViewById(R.id.iv1);
ivYellow.setImageDrawable(crossfader_yellow);
crossfader_yellow.startTransition(0);
crossfader_yellow.reverseTransition(200);
case 1:
Drawable backgrounds_red[] = new Drawable[2];
Resources res_red = getResources();
backgrounds_red[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_red);
backgrounds_red[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_red_blink);
TransitionDrawable crossfader_red = new TransitionDrawable(backgrounds_red);
ImageView ivRed = (ImageView) findViewById(R.id.iv2);
ivRed.setImageDrawable(crossfader_red);
crossfader_red.startTransition(0);
crossfader_red.reverseTransition(200);
case 2:
Drawable backgrounds_blue[] = new Drawable[2];
Resources res_ = getResources();
backgrounds_blue[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_blue);
backgrounds_blue[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_blue_blink);
TransitionDrawable crossfader_blue = new TransitionDrawable(backgrounds_blue);
ImageView ivBlue = (ImageView) findViewById(R.id.iv3);
ivBlue.setImageDrawable(crossfader_blue);
crossfader_blue.startTransition(0);
crossfader_blue.reverseTransition(200);
case 3:
Drawable backgrounds_green[] = new Drawable[2];
Resources res_green = getResources();
backgrounds_green[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_green);
backgrounds_green[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_green_blink);
TransitionDrawable crossfader_green = new TransitionDrawable(backgrounds_green);
ImageView ivGreen = (ImageView) findViewById(R.id.iv4);
ivGreen.setImageDrawable(crossfader_green);
crossfader_green.startTransition(0);
crossfader_green.reverseTransition(200);
default:
return;
}
I've tried the same code with OnClickListener shown bellow and it works, but for some reason, when I run the code above, nothing happens, no animation change takes place.
I've tested the sequenceArray afterwards, it isn't empty, but for some reason beyond my comprehension the current code just won't do what I want it to. What's the main difference between the for loop shown below with the logic above?
iv1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Drawable backgrounds[] = new Drawable[2];
Resources res = getResources();
backgrounds[0] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_yellow);
backgrounds[1] = ContextCompat.getDrawable(MainActivity.this, R.drawable.simonsays_yellow_blink);
TransitionDrawable crossfader = new TransitionDrawable(backgrounds);
ImageView image = (ImageView) findViewById(R.id.iv1);
image.setImageDrawable(crossfader);
crossfader.startTransition(0);
crossfader.reverseTransition(200);
}
});
EDIT: CODE NOW FIXED
I'm trying to create a dynamically populated frame animation for a simple slot reel. I create a random sequence of 50 animated images to make it look like the reel is spinning, then add the final three symbols at the end.
My problem is that each time this method is run, it adds the frames onto the previous Animation. The first time it is run, the animation has 53 frames, the second time it has 106 frames, etc. I want to reset/clear the animation each time it is run, but I cannot figure out how to do so. If anyone has any ideas it would be greatly appreciated!
private void animate(int r1c1, int r2c1, int r3c1, int r1c2, int r2c2, int r3c2, int r1c3, int r2c3, int r3c3) {
ImageView imgView = (ImageView)findViewById(R.id.animationImage);
imgView.setVisibility(ImageView.VISIBLE);
AnimationDrawable frameAnimation = new AnimationDrawable();
BitmapDrawable cherry = (BitmapDrawable)getResources().getDrawable(R.drawable.cherry);
BitmapDrawable grape = (BitmapDrawable)getResources().getDrawable(R.drawable.grape);
BitmapDrawable lemon = (BitmapDrawable)getResources().getDrawable(R.drawable.lemon);
BitmapDrawable lucky7 = (BitmapDrawable)getResources().getDrawable(R.drawable.lucky7);
BitmapDrawable watermelon = (BitmapDrawable)getResources().getDrawable(R.drawable.watermelon);
BitmapDrawable orange = (BitmapDrawable)getResources().getDrawable(R.drawable.orange);
BitmapDrawable[] symbols = {cherry,watermelon,grape,lemon,orange,lucky7};
// frameAnimation = (AnimationDrawable) imgView.getBackground();
for(int i = 0; i < 50; i++) {
frameAnimation.addFrame(symbols[(int)(Math.random() * 6)], 50);
}
frameAnimation.addFrame(symbols[r3c1], 200);
frameAnimation.addFrame(symbols[r2c1], 200);
frameAnimation.addFrame(symbols[r1c1], 200);
imgView.setBackgroundDrawable(frameAnimation);
if (frameAnimation.isRunning()) {
frameAnimation.stop();
frameAnimation.selectDrawable(0);
frameAnimation.start();
}
else {
frameAnimation.start();
}
}
Here is my frameanimation.xml file
<animation-list
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="true">
</animation-list>
To clear the animation from any view you can use view.clearAnimation()
Provide your complete code for more clearification.
You can try this: Reset its drawable resource and restart animation.
if (frameAnimation.isRunning()) {
frameAnimation.stop();
imgView.setBackgroundDrawable(null);
imgView.setBackgroundResource(R.drawable.frame_animation);
frameAnimation.start();
}
Or you can use selectDrawable:
if (frameAnimation.isRunning()) {
frameAnimation.stop();
frameAnimation.selectDrawable(0);
frameAnimation.start();
}
Hope this helps.
In my application I have 2 seekbars. The error is that they are on each other! I just cant put them 2 next to each other across all the width of the screen! They are alignd to the left! I want one aligned to th left, one to the right!
The code is:
public View wrapLabelAndSeekbar(String labelText, int width)
{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(250, 250);
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); // Doesn't work
seekerWidth = new SeekBar(this);
seekerHeight = new SeekBar(this);
seekerWidth.setProgress(Main.seekerPlaceWidth);
seekerHeight.setProgress(Main.seekerPlacHeight);
layout.addView(seekerHeight, params);
layout.addView(seekerWidth, params);
return layout;
}
I can only change their size up to half the screen. No more.
What can I do?
A minor mistake, as usual... apparently... I just divided the width of the screen in two so all was on the same position...
RelativeLayout ll = new RelativeLayout(this);
ll.setId(99);
seekerWidth = new SeekBar(this);
seekerHeight = new SeekBar(this);
seekerWidth.setProgress(Main.seekerPlaceWidth);
seekerHeight.setProgress(Main.seekerPlacHeight);
seekerWidth.setId(1);
seekerHeight.setId(2);
RelativeLayout.LayoutParams lay = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
lay.addRule(RelativeLayout.ALIGN_PARENT_TOP);
ll.addView(seekerWidth, lay);
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, seekerWidth.getId());
ll.addView(seekerHeight, p);
I want to fetch Images from my URL and add it to the radio button that is further added to a radio group. However, i could not find metod such as setText to set text for radio button. I wish to add image to my radio button in similar fashion. Is there any hint/method that can do the same for me? Any hint/code example would be really helpful for me,
RadioGroup rg = new RadioGroup (this);
rg = (RadioGroup) findViewById(R.id.radioGroup1);
RadioButton rb[]= new RadioButton[children.size()];
for (int i = 0; i < var.size(); i++)
{
Element movieAtt = (Element)doc.getRootElement().getChild("movies").getChildren("movie").get(i);
MovieName[i]=movieAtt.getAttributeValue( "Title" );
MovieCover[i]=movieAtt.getAttributeValue( "cover" );
ShowTime[i]=movieAtt.getAttributeValue( "showtime" );
rb[i] = new RadioButton(this);
rb[i].setText(movieAtt.getAttributeValue("Title"));
rg.addView(rb[i]);
//Calling this func to get Images
//LoadImageFromWebOperations(movieAtt.getAttributeValue( "cover" ));
//rb[i].buildDrawingCache(LoadImageFromWebOperations(movieAtt.getAttributeValue( "cover" )));
public static Drawable LoadImageFromWebOperations(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}
catch (Exception e)
{
return null;
}
}
Is the problem with the image or the text? As for the image, I believe this should work:
Drawable d = LoadImageFromWebOperations(movieAtt.getAttributeValue("cover"));
rb[i].setButtonDrawable(d);
Make sure to set gravity to Gravity.CENTER or your image won't be centered around the radio button.
You should find information about the text in the RadioButton reference.
I am making a icon for my app.. The app is basically a friend finder. I am creating a overlay that looks much like the icons from Google Latitude. I have an image that changes due to the user and I have the boarder. I've been able to do the layered drawable and overlay fine, but the problem is, the image stretches to the size of the border. This is a problem because, if you've never seen the Google Lat icons, it has a point on the bottom with open space between it.
What I need to do is, somehow restrict the size of the changing image to the bounds of the square portion of the border. Any Help would be much appreciated. Here is my snippet:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 25;
Bitmap bit = BitmapFactory.decodeFile(photo, options);
draw = new BitmapDrawable(bit);
Resources r = getResources();
Drawable[] layers = new Drawable[2];
layers[0] = draw;
layers[1] = r.getDrawable(R.drawable.border);
LayerDrawable layerDrawable = new LayerDrawable(layers);
draw = layerDrawable;
}else{
draw = this.getResources().getDrawable(R.drawable.androidmarker);
}
HelloItemizedOverlay itemizedoverlay = new HelloItemizedOverlay(draw, this);
GeoPoint point = new GeoPoint(lat,lon);
OverlayItem overlayitem = new OverlayItem(point, username, avail + " : " + status + " : Position updated at : " + update_at);
items.add(overlayitem);
itemizedoverlay.addOverlay(overlayitem);
mapOverlays.add(itemizedoverlay);
Funny that shorty after I posted this, I found the answer. I was looking in all of the wrong places to resize the image. I tried the bitmap, the drawable, the layers inside of the layerdrawable. But, what I never tried was the layerdrawable itself. The solution is below:
Resources r = getResources();
Drawable[] layers = new Drawable[2];
layers[0] = draw;
layers[1] = r.getDrawable(R.drawable.border);
LayerDrawable layerDrawable = new LayerDrawable(layers);
layerDrawable.setLayerInset(0, 0, 0, 0, 12);
draw = layerDrawable;
The layerDrawable inset method is as follows:
layerDrawable.setLayerInset(*index of layer*, *left inset*, *top*, *right*, *bottom*);
Thanks guys!