I followed this to create a simple animation.
The example creates manually an ImageView in the main layout, having a resource like this android:src="#anim/anim_android
The problem is, when I make a dynamic ImageView, and I set the resource like this myImageView.setImageResource(R.anim.anim_android);, the animation doesn't work, just views the first image of the sequence.
Any help?
Thanks.
you sould start the animation from you imageView :
myImageView.setImageResource(R.anim.anim_android);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myImageViewn.getDrawable();
myImageView.post(
new Runnable(){
#Override
public void run() {
myAnimationDrawable.start();
}
});
}
EDIT : i think you didn't add the ImageView to your Layout ( contentView of your Activity ) ; try this :
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RelativeLayout rootLayout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
rootLayout.setLayoutParams(params);
ImageView myAnimation = new ImageView(this);
RelativeLayout.LayoutParams paramsImage = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
paramsImage.addRule(RelativeLayout.CENTER_IN_PARENT);
myAnimation.setLayoutParams(paramsImage);
myAnimation.setImageDrawable(getResources().getDrawable(R.anim.anim_android));
rootLayout.addView(myAnimation);
setContentView(rootLayout);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable();
myAnimation.post(new Runnable() {
#Override
public void run() {
myAnimationDrawable.start();
}
});
}
Related
I have a issue trying to dynamically add some linear layout into a linearlayout container after user have clicked on a button.
private void AddView() {
MyView myView1 = new MyView("Name");
this.mainLinearLayout.addView(myView1);
}
This code works great in activity's onCreate method but not after handling user event.
Do you have any idea why it's not working ? (I mean nothing appears on UI)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AddView(); => works great
}
playButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AddView(); => not working
}
});
Thanks,
I think that AddView is wrong.
private void addView() {
LayoutParams lparams = new LayoutParams(
LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
MyView myView1 = new MyView("Name");
myView1.setLayoutParams(lparams);
this.mainLinearLayout.addView(myView1);
}
My usecase is to load images from a URL, and I am using picasso, in the following way to acheive that.
ll = (LinearLayout) findViewById(R.id.tasklayout);
ImageView imageView = new ImageView(this);
Picasso.with(this).load(taskFieldDao.data).into(imageView);
ll.addView(imageView, new LinearLayout.LayoutParams(800, 800));
What I see is a 800 * 800 whitespace with no image in it. Looking at the logs, there doesn't seem to any problem. Atleast picasso doesn't record anything.
I replicate your problem with button click and it is working. You can as well check the your path is correct
downloadButton = (Button)findViewById(R.id.download_button);
downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);
ImageView imageView = new ImageView(PicassoDownloadActivity.this);
Picasso.with(PicassoDownloadActivity.this).load(Helper.imageDownloadPath).into(imageView);
relativeLayout.addView(imageView, new LinearLayout.LayoutParams(800, 800));
}
});
Picasso won't typically give errors when something goes wrong, i suggest doing something like this:
ll = (LinearLayout) findViewById(R.id.tasklayout);
final ImageView imageView = new ImageView(this);
Picasso.with(this).load(taskFieldDao.data).into(imageView, new Callback() {
#Override
public void onSuccess() {
ll.addView(imageView, new LinearLayout.LayoutParams(800, 800));
}
#Override
public void onError() {
Log.e("Picasso","Image load failed);
}
});
How do I make a single button width to fill parent programmatically? I have done this but it cannot seem to work it is still located on top left with width just wrapping the content... here is some of the code on the button creation...
public class TEST_GIFDisplay extends Activity implements View.OnClickListener {
SurfaceView sview;
GifRun gr = new GifRun();
Button btnBack;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sview = new SurfaceView(this);
sview.setZOrderOnTop(true);
gr.LoadGiff(sview, this, R.drawable.smiley);
LinearLayout linearLayout1 = new LinearLayout(this);
linearLayout1.setOrientation(LinearLayout.VERTICAL);
linearLayout1.setBackgroundColor(Color.parseColor("#27ae60"));
linearLayout1.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
LinearLayout linearLayout2 = new LinearLayout(this);
linearLayout2.setOrientation(LinearLayout.VERTICAL);
linearLayout2.setBackgroundColor(Color.parseColor("#27ae60"));
linearLayout2.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
LinearLayout.LayoutParams p = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
p.weight = 1.0f;
btnBack = new Button (this);
btnBack.setText("Back");
btnBack.setBackgroundColor(Color.parseColor("#f1c40f"));
btnBack.setGravity(Gravity.CENTER_HORIZONTAL);
btnBack.setLayoutParams(p);
btnBack.setOnClickListener(this);
linearLayout2.addView(btnBack);
linearLayout1.addView(linearLayout2);
linearLayout1.addView(sview);
setContentView(linearLayout1);
}
#Override
public void onClick(View view) {
btnBack = (Button) view;
btnBack.setBackgroundColor(Color.parseColor("#2980b9")); //color change
new CountDownTimer(100, 50) {
#Override
public void onTick(long arg0) {
// TODO Auto-generated method stub
}
#Override
public void onFinish() {
btnBack.setBackgroundColor(Color.parseColor("#f1c40f")); // original color
}
}.start();//end color changed
finish();
}
}
You are adding the Button to linearLayout2. You should change the linearLayout2's width to MATCH_PARENT
linearLayout2.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
I hope this helps.
P.S: You can create Button's selectors for pressed and selected states, instead of using timer to show a pressed button effect. Here is a basic link that can help you in achieving this : android button selector
I'm trying to use a RelativeLayout to produce two rows of items, one row of three buttons up top with a scrollview below them. I want the scrollView to occupy as much of the space as possible.
I believe a RelativeLayout is the best approach here. I'm trying to do this at runtime and not via an xml layout file. My code is as follows:
RelativeLayout.LayoutParams exitparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
exitparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams zimparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams zoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams scrollparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
zimparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
scrollparams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM):
// Exit button
Button exitbutton = new Button(this);
exitbutton.setText("Exit");
exitbutton.setGravity(Gravity.CENTER);
exitbutton.setWidth(200);
exitbutton.setLayoutParams(exitparams);
// Zoom In button
Button zoomOutButton = new Button(this);
zoomOutButton.setText("Zoom Out");
zoomOutButton.setGravity(Gravity.CENTER);
zoomOutButton.setWidth(200);
zoomOutButton.setLayoutParams(zimparams);
// Zoom In button
Button zoomInButton = new Button(this);
zoomInButton.setText("Zoom In");
zoomInButton.setGravity(Gravity.CENTER);
zoomInButton.setWidth(200);
zoutparams.addRule(RelativeLayout.LEFT_OF, zoomInButton.getId());
zoomInButton.setLayoutParams(zoutparams);
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
layout1.addView(exitbutton);
layout1.addView(zoomInButton);
layout1.addView(zoomOutButton);
exitbutton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
zoomInButton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
increaseZoom();
onResume();
}
});
zoomOutButton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
reduceZoom();
onResume();
}
});
drawView = new DrawView(this, height, width, SMD, zoomLevel);
drawView.setBackgroundColor(Color.WHITE);
ScrollView scrollView = new ScrollView(this);
scrollView.addView(drawView);
scrollView.setLayoutParams(scrollparams);
layout1.addView(scrollView);
setContentView(layout1);
Yet the only thing that shows up is the scrollView. I'm guessing I'm missing something small here... I read the documentation and I believe I did this correctly but the fact that it is not working suggests I either did something wrong or cannot accomplish what I want to do.
A visual of how I want this to look is as follows:
You need to call setContentView(layout1) at the end to actually add the layout.
EDIT:
Okay so Iv'e fixed you code up and it's working on my phone. However I couldn't' work with DrawView obviously so if there is something wrong with that I can't help you. Give this a go.
RelativeLayout.LayoutParams exitparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
exitparams.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
RelativeLayout.LayoutParams zimparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams zoutparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
RelativeLayout.LayoutParams scrollparams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
zimparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
// Exit button
Button exitbutton = new Button(this);
exitbutton.setText("Exit");
exitbutton.setGravity(Gravity.CENTER);
exitbutton.setWidth(200);
exitbutton.setLayoutParams(exitparams);
exitbutton.setId(2);
// Zoom In button
Button zoomOutButton = new Button(this);
zoomOutButton.setText("Zoom Out");
zoomOutButton.setGravity(Gravity.CENTER);
zoomOutButton.setWidth(200);
zoomOutButton.setLayoutParams(zimparams);
zoomOutButton.setId(1);
// Zoom In button
Button zoomInButton = new Button(this);
zoomInButton.setText("Zoom In");
zoomInButton.setGravity(Gravity.CENTER);
zoomInButton.setWidth(200);
zoutparams.addRule(RelativeLayout.LEFT_OF, 1);
zoutparams.addRule(RelativeLayout.RIGHT_OF, 2);
zoomInButton.setLayoutParams(zoutparams);
RelativeLayout layout1 = new RelativeLayout(this);
layout1.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.FILL_PARENT, RelativeLayout.LayoutParams.FILL_PARENT));
layout1.addView(exitbutton);
layout1.addView(zoomInButton);
layout1.addView(zoomOutButton);
exitbutton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
finish();
}
});
zoomInButton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
//increaseZoom();
onResume();
}
});
zoomOutButton.setOnClickListener (new View.OnClickListener() {
public void onClick(View v) {
//reduceZoom();
onResume();
}
});
//drawView = new DrawView(this, height, width, SMD, zoomLevel);
// drawView.setBackgroundColor(Color.WHITE);
ScrollView scrollView = new ScrollView(this);
//scrollView.addView(drawView);
scrollparams.addRule(RelativeLayout.BELOW,1);
scrollView.setLayoutParams(scrollparams);
layout1.addView(scrollView);
setContentView(layout1);
I want to put elements in my view depending on the position of the other ones. Let me explain : First, I have two textviews and I've put successfully the second (villeOffre) one under the first one (titreOffre) :
titreOffre = (TextView) findViewById(R.id.offreTitre);
titreOffre.setText(capitalizedString(offre.getTitle()));
villeOffre = (TextView) findViewById(R.id.offreVille);
villeOffre.setText(offre.getLocality());
infos = (TextView) findViewById(R.id.infos);
ViewTreeObserver vto = titreOffre.getViewTreeObserver();
vto.addOnGlobalLayoutListener(new OnGlobalLayoutListener(
{
#Override
public void onGlobalLayout()
{
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) villeOffre.getLayoutParams();
params.setMargins(15,titreOffre.getBottom()+12,15,0);
villeOffre.setLayoutParams(params);
titreOffre.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
});
This works perfectly, but now I want to put a third textview (infos) which is depending on villeOffre position. How to do that ?
Thanks a lot.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rr = new RelativeLayout(this);
b1 = new Button(this);
b1.setId(R.id.Button01);
recent = b1;
b1.setText("Click me");
rr.addView(b1);
setContentView(rr);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tv1 = new TextView(can.this);
tv1.setId((int)System.currentTimeMillis());
lp = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.BELOW, recent.getId());
tv1.setText("Time: "+System.currentTimeMillis());
rr.addView(tv1, lp);
recent = tv1;
}
});
}