Android imageView Switching Problem - android

i have imageview and i have to display different image in
that with time interval, but when i change the ImageResource
the last image is displayed
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.test);
try {
Thread.sleep(2000) ;
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
image = (ImageView) findViewById(R.id.test_image);
image.setImageResource(R.drawable.test2);
}
Kindly Suggest the Right way to do that
Thanks in Advance

Perhaps this thread is what you are looking for:
how to change images with timer

Whenever you want to update the user interface without performing any action or event, handlers should be used.
This is the sample code
Main.java
package com.balaji.handler;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
public class Main extends Activity {
private ImageView txtStatus;
int i=0;
int imgid[]={R.drawable.icon,R.drawable.back,R.drawable.slider,R.drawable.forward};
private RefreshHandler mRedrawHandler = new RefreshHandler();
class RefreshHandler extends Handler {
#Override
public void handleMessage(Message msg) {
Main.this.updateUI();
}
public void sleep(long delayMillis) {
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
private void updateUI(){
//int currentInt = Integer.parseInt((String) txtStatus.getText()) + 10;
if(i<imgid.length){
mRedrawHandler.sleep(1000);
txtStatus.setBackgroundResource(imgid[i]);
i++;
}
}
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
this.txtStatus = (ImageView)this.findViewById(R.id.txtStatus);
updateUI();
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/txtStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
</ImageView>
</RelativeLayout>

Related

Animating android progress bar

I've created ProgressBar in my view, and I would like it to animate when it's value is updated. I'm runnig the code on API 23. How to animate progressBar when It's value is updated?
Here is my activity:
package com.brylkowski.cleaner.cleaner.Errands;
import android.os.AsyncTask;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.widget.ProgressBar;
import com.brylkowski.cleaner.cleaner.R;
public class UploadErrandActivity extends AppCompatActivity {
public static final String PROGRESS = "progress";
UploadRequestTask task;
private ProgressBar progressBar;
private Handler handler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_upload_errand);
progressBar = (ProgressBar) findViewById(R.id.upload_progress);
progressBar.setMax(6);
task = new UploadRequestTask();
handler = new Handler() {
public void handleMessage(android.os.Message msg) {
progressBar.incrementProgressBy(1);
}
};
task.execute();
}
public class UploadRequestTask extends AsyncTask<Void, Void, Boolean> {
UploadRequestTask() {
}
#Override
protected Boolean doInBackground(Void... params) {
for (int i = 1; i <=6; i++){
Message msg = new Message();
Bundle bundle = new Bundle();
bundle.putInt(PROGRESS, i);
msg.setData(bundle);
handler.sendMessage(msg);
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
return true;
}
#Override
protected void onPostExecute(final Boolean success) {
}
#Override
protected void onCancelled() {
}
}
}
and my view:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:gravity="center"
tools:context="com.brylkowski.cleaner.cleaner.Errands.UploadErrandActivity">
<ProgressBar
style="?android:attr/progressBarStyleHorizontal"
android:indeterminate="false"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/upload_progress"
android:layout_gravity="bottom"
android:longClickable="false"/>
</LinearLayout>
Check this for how to make ProgressBar animation. The only modification you need is instead of applying the animation once when view is initialized, you will need to call
ProgressBarAnimation anim = new ProgressBarAnimation(progressBar, progressBar.getProgress(), to);
anim.setDuration(1000);
progressBar.startAnimation(anim); // play animation immediately
every time you want to update value. Here is documentation for view animation.

Android: Implementing Slideshow

I am trying to implement a slideshow in my android app. I have an imageview and a text view in my xml file. I want that imageview to show a slideshow of some images. The images are stored in drawable-hdpi. When I am opening this page, only text view is showing up and not the slideshow. Not a single image is showing.
Following is the java file for the same.
package abc.xys;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.widget.ImageView;
import android.widget.TextView;
public class Wishes extends Activity {
ImageView slide;
TextView msg;
int i=0;
int imgid[]={R.drawable.friends01,R.drawable.friends02,R.drawable.friends03,R.drawable.friends04,R.drawable.friends05};
RefreshHandler refreshHandler=new RefreshHandler();
#SuppressLint("HandlerLeak")
class RefreshHandler extends Handler{
#Override
public void handleMessage(Message msg) {
// TODO Auto-generated method stub
Wishes.this.updateUI();
}
public void sleep(long delayMillis){
this.removeMessages(0);
sendMessageDelayed(obtainMessage(0), delayMillis);
}
};
public void updateUI(){
int currentInt=Integer.parseInt((String)msg.getText())+10;
if(currentInt<=100){
refreshHandler.sleep(2000);
msg.setText(String.valueOf(currentInt));
if(i<imgid.length){
slide.setImageResource(imgid[i]);
// slide.setPadding(left, top, right, bottom);
i++;
}
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.wishes_template);
slide = (ImageView) findViewById(R.id.slideshow);
msg = (TextView) findViewById(R.id.message);
}
}
The xml file (wishes_template) is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/slideshow_layout"
>
<ImageView android:id="#+id/slideshow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingTop="20dp"
></ImageView>
<TextView android:text="10"
android:id="#+id/message"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
Thanks in advance!

Animation Translate using LayoutParams

Basically i want to create translate animation but for some reason i cannot use TranslateAnimation class. Anyway this is my code
main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher"/>
</RelativeLayout>
MainActivity.java
package com.test2;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RelativeLayout;
import android.widget.RelativeLayout.LayoutParams;
import android.widget.ImageView;
public class MainActivity extends Activity
{
private RelativeLayout layout;
private ImageView image;
private LayoutParams imageParams;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (RelativeLayout) findViewById(R.id.layout);
image = (ImageView) findViewById(R.id.image);
imageParams = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
image.setLayoutParams(imageParams);
layout.setOnClickListener(new ClickHandler());
}
class ClickHandler implements OnClickListener
{
public void onClick(View view)
{
// This is the animation part
for (int i = 0; i < 100; i++)
{
imageParams.leftMargin = i;
image.setLayoutParams(imageParams);
// repaint() in java
try
{
Thread.sleep(100);
}
catch (Exception e)
{
}
}
}
}
}
My problem is I need to do repaint() before using Thread.sleep otherwise the animation will not work and the object will just move to its final position. I've tried invalidate, requestLayout, requestDrawableState but none works. Any suggestion?
EDIT:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</RelativeLayout>
MainActivity.java
public class MainActivity extends Activity
{
private RelativeLayout layout;
private ImageView image;
private LayoutParams imageParams;
private Timer timer;
private int i;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
layout = (RelativeLayout) findViewById(R.id.layout);
image = new ImageView(this);
image.setImageResource(R.drawable.ic_launcher);
imageParams = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
image.setLayoutParams(imageParams);
layout.addView(image);
i = 0;
timer = new Timer();
runOnUiThread
(
new Runnable()
{
public void run()
{
timer.scheduleAtFixedRate
(
new TimerTask()
{
#Override
public void run()
{
imageParams.leftMargin = i;
image.setLayoutParams(imageParams);
i++;
if (i == 15)
{
timer.cancel();
timer.purge();
}
}
}, 0, 100
);
}
}
);
}
}
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
</RelativeLayout>
package com.example.example;
import java.util.Timer;
import java.util.TimerTask;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.app.Activity;
public class MainActivity extends Activity {
ImageView imageView;
RelativeLayout.LayoutParams params;
int i=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView=new ImageView(this);
RelativeLayout main=(RelativeLayout)findViewById(R.id.main);
params=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
imageView.setBackgroundResource(R.drawable.ic_launcher);
imageView.setLayoutParams(params);
main.addView(imageView);
final Timer timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
public void run() {
params.leftMargin=i;
imageView.setLayoutParams(params);
i++;
if(i>=100){
timer.cancel();
timer.purge();
}
}
});
}
}, 0, 100);
}
}
you can use timer task instead of sleep
Like:-
int i=0;
Timer timer=new Timer();
timer.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
// TODO Auto-generated method stub
imageParams.leftMargin = i;
image.setLayoutParams(imageParams);
i++;
if(i==100){
timer.cancel();
timer.purge();
}
}
}, 0, 1000);

android imgeview pause and clear

I have this code and I'm trying to read 2 images from the SD card and I don't know how to pause between the 2 images that I can have enough time to see each 1 of them. Also, I don't know how to clear the ImgeView after I display the 2 images
This is how I want it to go:
load first image -> wait for 5 sec -> load sec image-> wait for 5 sec clear the ImageView
MainActivity.java
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.widget.ImageView;
public class ReadfromSD extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
String imageInSD = "/sdcard/Hanud/AD2.jpg";
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
ImageView myImageView = (ImageView) findViewById(R.id.imageview1);
myImageView.setImageBitmap(bitmap);
String imageInSD2 = "/sdcard/Hanud/AD1.jpg";
Bitmap bitmap2 = BitmapFactory.decodeFile(imageInSD2);
ImageView myImageView1 = (ImageView) findViewById(R.id.imageview1);
myImageView1.setImageBitmap(bitmap2);
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<ImageView
android:id="#+id/imageview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:scaleType="center" />
</LinearLayout>
public class ImgLoadAct extends Activity {
/** Called when the activity is first created. */
Timer timer;
ImageView myImageView;
TextView titleText;
Drawable img1Drawable;
Drawable img2Drawable;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myImageView = (ImageView)findViewById(R.id.imageview1);
titleText=(TextView)findViewById(R.id.img_title);
img1Drawable=getResources().getDrawable(R.drawable.android_logo);
img2Drawable=getResources().getDrawable(R.drawable.apple_logo);
//Load Img1 android_logo
myImageView.setImageDrawable(img1Drawable);
titleText.setText("Img2 android_logo.png loaded");
timer=new Timer();
//delay amount of time(5s here) in milliseconds before first execution.
timer.schedule(loadImg2, 5000);
}
TimerTask loadImg2 = new TimerTask(){
#Override
//Load Img2
public void run() {
runOnUiThread(new Runnable(){
public void run() {
titleText.setText("Img2 apple_logo.png loaded");
myImageView.setImageDrawable(img2Drawable);
timer.cancel();
timer=new Timer();
timer.schedule(clearImg, 5000);
}
});
}
};
TimerTask clearImg = new TimerTask(){
#Override
public void run() {
// TODO Auto-generated method stub
runOnUiThread(new Runnable(){
public void run() {
timer.cancel();
titleText.setText("Img cleared");
myImageView.setImageDrawable(null);
}
});
}
} ;
}
Src dowload:
http://www.everbox.com/f/UtxQ4gzfXBPTFZlcIXi3HQHGU4

codes in onClickListener can't be executed

I'm experiencing problem with the following code, I created two buttons with onClickListener added.
It works fine for the Button -- btnAbout, I'm trying to use another way to handle btnIntro, instead of by using findViewById(...) method, but the statement will not be executed when I click btnIntro.
Why does this happen?
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class TestActivity extends Activity {
private final String TAG = "tag";
private Button btnIntro;
private Button btnAbout;
private View layoutView;
private ViewWrapper wrapper;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btnAbout = (Button) findViewById(R.id.btnAbout);
if (btnIntro == null) {
LayoutInflater inflator = getLayoutInflater();
layoutView = inflator.inflate(R.layout.main, null, false);
wrapper = new ViewWrapper(layoutView);
layoutView.setTag(wrapper);
} else {
wrapper = (ViewWrapper) layoutView.getTag();
}
btnIntro = wrapper.getButton();
Log.e(TAG, Integer.toHexString(layoutView.getId()) + "");
Log.e(TAG, btnIntro.getText().toString());
btnIntro.setOnClickListener(new OnClickListener() {
{
Log.e(TAG, "static");
}
#Override
public void onClick(View arg0) {
Log.e(TAG, "btnIntro clicked");
Toast.makeText(TestActivity.this, "btnIntro", Toast.LENGTH_SHORT)
.show();
}
});
btnAbout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Log.e(TAG, "btnAbout clicked");
Toast.makeText(TestActivity.this, "about", Toast.LENGTH_SHORT).show();
}
});
}
class ViewWrapper {
View base;
Button btn1;
ViewWrapper(View base) {
this.base = base;
}
Button getButton() {
if (btn1 == null) {
btn1 = (Button) base.findViewById(R.id.btnIntro);
Log.e(TAG, btn1.getText().toString());
}
return btn1;
}
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MAIN_LAYOUT_XML"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="30dip">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Test" />
<Button
android:id="#+id/btnIntro"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="btnIntro" />
<Button
android:id="#+id/btnAbout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="btnAbout" />
</LinearLayout>
Problem is that you use to different content views to obtain buttons, one is created when you invoke setContentView() and second you create by invoking inflator.infate(). Thus buttons you get are placed in different content views (only one of which is shown - that created by setContentView). Try that instead:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LayoutInflater inflator = getLayoutInflater();
layoutView = inflator.inflate(R.layout.main, null, false);
setContentView(layoutView);
//..anything you need..
}

Categories

Resources