i want to load an image from a url.i have 10 images on my server and i m calling one in every class .it works but there are some pictures that are not opening...is there any wrong on my code?i have checked the url and its ok!thanks
public class modern_clock extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.sites);
ImageView im = (ImageView) findViewById(R.id.image);
Drawable drawable = LoadImageFromWeb("http://kostas-menu.gr/chania/roloi.jpg");
im.setImageDrawable(drawable);
TextView title1 = (TextView) findViewById(R.id.text_title);
title1.setText("The Clock\n" );
TextView info = (TextView) findViewById(R.id.info);
info.setText(".................\n" );
}
private Drawable LoadImageFromWeb(String url) {
// TODO Auto-generated method stub
try{
InputStream is = (InputStream) new URL(url).getContent();
Drawable d = Drawable.createFromStream(is, "src name");
return d;
}catch (Exception e) {
System.out.println("Exc="+e);
return null;
}
}
}
EDIT:
#George:
i have added in my package the LoaderImageView,java class.i also changed my xml file from the simple imageview to
<com.example.android.LoaderImageView
android:layout_marginTop="10px"
android:id="#+id/loaderImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
image="http://developer.android.com/images/dialog_buttons.png"
/>
and in every class of my project i have added that:
final LoaderImageView image = new LoaderImageView(this, "http://kostas-menu.gr/chania/santrivani.jpg");
image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
but unfortunately with that way i m always getting the image from the url in my xml file.i have the same xml for all my classes,so how could i set the image from the java file?i tried to erase the line image:"http...." from the manifest,but this is not returning any image at all!!thanks
Ideally you should fetch the images in a seperate thread different from the UI thread.
Lazy load of images in ListView
This might help too.
use this resources. I am sure this will help you
http://www.anddev.org/novice-tutorials-f8/imageview-with-loading-spinner-t49439.html
do you use this code? Is this something that we should?
public class ImageExampleXML extends Activity {
private final String images[] = {"http://developer.android.com/images/dialog_custom.png", "http://developer.android.com/images/dialog_progress_bar.png"};
private int i = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button loadImage = (Button) findViewById(R.id.button);
final LoaderImageView image = (LoaderImageView) findViewById(R.id.loaderImageView);
loadImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
image.setImageDrawable(getAnImageUrl());
}
});
}
private String getAnImageUrl() {
i++;
if(i >= images.length){
i = 0;
}
return images[i];
}
}
Related
I'm trying to have one TextView switch from one sentence to another alongside my ImageSwitcher. Here is a sample code for my activity
private Integer images[]={R.drawable.image1,R.drawable.image2,R.drawable.image3,R.drawable.image4....};
private int currImage=0;
private Integer text[]={R.string.text0,R.string.text1,R.string.text2,R.string.text3,R.string.text4....};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initializeImageSwitcher();
setInitialImage();
setImageRotateListener();
setInitialText();
}
private void initializeImageSwitcher() {
final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
imageSwitcher.setFactory(new ViewSwitcher.ViewFactory() {
#Override
public View makeView() {
ImageView imageView = new ImageView(MainActivity.this);
return imageView;
}
});
imageSwitcher.setInAnimation(AnimationUtils.loadAnimation(this, R.anim.right_in));
imageSwitcher.setOutAnimation(AnimationUtils.loadAnimation(this, R.anim.left_out));
}
private void setImageRotateListener() {
final ImageButton rightarrow = (ImageButton) findViewById(R.id.next);
rightarrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
currImage++;
if (currImage == 29) {
currImage = 0;
}
setCurrentImage();
setCurrentText();
}
});
}
private void setInitialImage() {
setCurrentImage();
}
private void setInitialText(){
setCurrentText();
}
private void setCurrentImage() {
final ImageSwitcher imageSwitcher = (ImageSwitcher) findViewById(R.id.imageswitcher);
imageSwitcher.setImageResource(images[currImage]);
}
private void setCurrentText() {
final TextView textSwitcher = (TextView) findViewById(R.id.textView);
textSwitcher.setText(Integer.toString(text[currImage]));
}
The it compiles and run just fine. Issue lies in the text output. Instead of the sentences defined in the string, the TextView displays a series of numbers.
For example:
<string name="text1">Sentence one</string>
<string name="text2">Sentence two</string>
<string name="text3">Sentence three</string>
<string name="text4">Sentence four</string>
The output for "text1" would be 8513856, "text2" would be 841363, "text3" would be 18413587, and so on.
If anyone one has an idea of how to solve this issue, your help would be greatly appreciated.
You are displaying the resource id's of the strings, not the strings themselves.
Change to:
private void setCurrentText() {
final TextView textSwitcher = (TextView) findViewById(R.id.textView);
textSwitcher.setText(getResources().getString(text[currImage]));
}
In my Activity I have one ImageView
XML Code:
<!-- FOTO LATERAL! -->
<ImageView
android:id="#+id/simbolo_raca"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<Button
android:id="#+id/button_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ok. Seguinte Pergunta"
android:layout_gravity="right"
android:gravity="right"
android:paddingRight="30dip">
</LinearLayout>
This is the Activity:
public class QuestionarioActivityRaca extends Activity{
ImageView simbolo;
int position;
Button B_Save;
List<Drawable> List_imagens = new ArrayList<Drawable>();
#Override
public void onCreate(Bundle savedInstanceState) {
simbolo = (ImageView) findViewById(R.id.simbolo_raca);
B_Save = (Button) findViewById(R.id.button_save);
position = 0;
List_imagens.add(getResources().getDrawable(R.drawable.p1));
List_imagens.add(getResources().getDrawable(R.drawable.p2));
List_imagens.add(getResources().getDrawable(R.drawable.p3));
List_imagens.add(getResources().getDrawable(R.drawable.p4));
loadNewImage(position);
B_Save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
loadNewImage(position);
position++;
}
});
}
// I Use this method to load the Image
public loadNewImage(int Page)
{
new Thread(new Runnable(){
#Override
public void run(){
simbolo.post(new Runnable(){
#Override
public void run()
{
simbolo.setImageDrawable(List_imagens.get(Page));
}
});
}
}).start();
}
The first time I call this method, (position = 0) the image doesnt loads. After that, the image is loading correctly.
How I have to do to load the image the first time?
(I can not load the image in the XML using android:src="#drawable/x") because the image could be different anytime.
EDITED!
There are a number of issues with the code sample you posted.
like writing int with a capital I.
Int position;
Not sending in a parameter with your method in the onClick:
public void onClick(View v) {
loadNewImage();
}
And several more in both your XML and code.
Is there a spesific reason you want to run a new thread every time for this task?
If you really need a thread, you have to declare your int as final in the method.
However, you should get your desired result with the code-sample below.
You have to modify your onClick to send the appropriate int for whatever drawable you want to use.
Good luck.
public class testactivity extends ActionBarActivity {
ImageView simbolo;
int position;
Button B_Save;
List<Drawable> List_imagens = new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_testactivity);
simbolo = (ImageView) findViewById(R.id.simbolo_raca);
B_Save = (Button) findViewById(R.id.button_save);
position = 0;
List_imagens.add(getResources().getDrawable(R.drawable.ic_drawer));
List_imagens.add(getResources().getDrawable(R.drawable.ic_check));
List_imagens.add(getResources().getDrawable(R.drawable.ic_action_add_package));
List_imagens.add(getResources().getDrawable(R.drawable.ic_action_exception));
loadNewImage(position);
final Random rand = new Random();
B_Save.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
loadNewImage(rand.nextInt(4));
}
});
}
public void loadNewImage(final int page)
{
simbolo.setImageDrawable(List_imagens.get(page));
}
}
first of all before the button gets clicked you call this function to load the initial image with loadNewImage(position); when the button is clicked you call this function loadNewImage(); i am guessing it works when the button is clicked because this loadNewImage(); method is ok and loadNewImage(int page); is not ok, because they are two different function.
Solving your problem if you want an integer object use Integer position or else go with int position and please let go of the thread. now your code will work
public class Serchresult extends Activity implements OnClickListener {
ImageView imageView1;
String Status;
String Reason;
TextView status;
TextView reason;
ImageView statusicon;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.act_serchresult);
Intent intent = getIntent();
Status = intent.getExtras().getString("Status");
Reason = intent.getExtras().getString("Reason");
imageView1 = (ImageView) findViewById(R.id.searchstatus_imgBack);
imageView1.setOnClickListener(this);
status = (TextView) findViewById(R.id.status);
reason = (TextView) findViewById(R.id.reason);
statusicon = (ImageView) findViewById(R.id.imageView1);
reason.setText(Reason.replace("null", ""));
if (reason.equals("ACCEPTED")) {
AQuery aq = new AQuery(getApplicationContext());
statusicon.setImageResource(R.drawable.accept_icon);
} else if (reason.equals("REJECTED")) {
AQuery aq = new AQuery(getApplicationContext());
statusicon.setImageResource(R.drawable.reject_icon);
}
else {
// reason.setCompoundDrawables(null, null, null, null);
statusicon.setImageResource(0);
}
status.setText(Status.replace("null", ""));
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.searchstatus_imgBack) {
finish();
}
}
}
here is my code i want display image from drawable folder accept and reject icon .i am getting ACCEPTED and REJECTED from intent on the basis of this response i want to display image in image view i have apply condition but image is not visible please help me where am doing wrong
Your code seems working. But still you can try following code to display image. Make sure your if-else conditions are working
Bitmap defBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.accept_icon);
statusicon.setImageBitmap(defBitmap);
Maybe the Bitmap is too big for the heap, try use BitmapFactory.Options to reduce the size.
BitmapFactory.Options op = new BitmapFactory.Options();
op.inSampleSize = 4;
statusicon.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.accept_icon, op));
BELOW IS MY CODE. This code is working, but I want to get the value of the display image in array to test, if the display country is correct. Please help me . I'm stuck in this activity. Thanks for all help .
public class MainActivity extends Activity implements OnClickListener {
private boolean blocked = false;
private Handler handler = new Handler();
ViewFlipper flippy;
Button show;
TextView view;
int flags[] = { R.drawable.afghan, R.drawable.albania, R.drawable.algeria };
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.flipper);
flippy = (ViewFlipper) findViewById(R.id.viewFlipper1);
show = (Button) findViewById(R.id.button1);
view = (TextView) findViewById(R.id.textView1);
for (int i = 0; i < flags.length; i++) {
setflipperimage(flags[i]);
}
}
private void setflipperimage(int i) {
// TODO Auto-generated method stub
Log.i("Set Filpper Called", i + "");
ImageView image = new ImageView(getApplicationContext());
image.setBackgroundResource(i);
flippy.addView(image);
}
do not call this in loop,use this on click or on touch:
i+=1;
flippy.setDisplayedChild(flags[0]);
This will get used to get the current child id
viewFlipper.getDisplayedChild();
Hi I'm kind of beginner to android OS programming, and I got stuck with a problem, I cant figure out how to do a dynamic background, based on timers (say each 10 seconds a background changes to a different one) I have some code but it comes up with error, here's a sample:
private static final long GET_DATA_INTERVAL = 10000;
int images[] = {R.drawable.smothie1,R.drawable.omletherb1};
int index = 0;
ImageView img;
Handler hand = new Handler();
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
LinearLayout layout= (LinearLayout)findViewById(R.id.LinearView1);
hand.postDelayed(run, GET_DATA_INTERVAL);
}
Runnable run = new Runnable() {
public void run() {
layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);
if (index == images.length)
index = 0;
hand.postDelayed(run, GET_DATA_INTERVAL);
Any help would be greatly apprieciated :D thanks
EDIT: The errors I get are on this line:
layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);
It says that:
-layout cannot be resolved
-the method getDrawable(int) is undefined for the type Object
This error:
layout.setBackgroundResource(LinearView1).getDrawable(images[index++]);
It says that:
-layout cannot be resolved
-the method getDrawable(int) is undefined for the type Object
Please help :)
I have finally worked it out, after removing a few errors I have came up with this (and its working) :
public class CookBookActivity extends Activity {
/** Called when the activity is first created. */
private static final long GET_DATA_INTERVAL = 1000;
int images[] = {R.drawable.omletherb1,R.drawable.smothie1};
int index = 0;
LinearLayout img;
Handler hand = new Handler();
private LinearLayout layout;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.setContentView(R.layout.main);
layout = (LinearLayout)findViewById(R.layout.main);
hand.postDelayed(run, GET_DATA_INTERVAL);
Typeface tf2 = Typeface.createFromAsset(getAssets(),
"fonts/BPreplay.otf");
TextView tv2 = (TextView) findViewById(R.id.textView2);
tv2.setTypeface(tf2);
Typeface tf = Typeface.createFromAsset(getAssets(),
"fonts/BPreplay.otf");
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setTypeface(tf);
Button mainNext = (Button) findViewById(R.id.nextScreen1);
mainNext.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent();
i.setClassName("com.unKnown.cookbook", "com.unKnown.cookbook.screen1");
startActivity(i);
}
});
}
Runnable run = new Runnable() {
public void run() {
layout.setBackgroundDrawable(getDrawable(index++));
if (index == images.length)
index = 0;
hand.postDelayed(run, GET_DATA_INTERVAL);
}
};
protected Drawable getDrawable(int i) {
// TODO Auto-generated method stub
return getResources().getDrawable(images[i%2]);
}
}