I have this ImageView that displays a picture from a URL. I want to have a button that when clicked will display the image from the URL. Could anyone teach me how to do this?
Here's my code:
String url = "https://www.google.com.ph/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=0ahUKEwiGzJm-l5PWAhVJl5QKHegTAg0QjBwIBA&url=https%3A%2F%2Fwww.enterprise.co.uk%2Fcontent%2Fdam%2Fglobal-vehicle-images%2Fcars%2FVAUX_INSI_2014.png&psig=AFQjCNGerQpF4NHcx50OFhQ2AGUlJYQCpQ&ust=1504877444951754";
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView);
loadImageFromUrl(url);
}
private void loadImageFromUrl(String url) {
Picasso.with(this) .load(url) .placeholder(R.mipmap.ic_launcher)
.error(R.mipmap.ic_launcher)
.into(imageView, new com.squareup.picasso.Callback() {
#Override
public void onSuccess() {
}
#Override
public void onError() {
}
});
}
Picasso:
compile 'com.squareup.picasso:picasso:2.5.2'
Called when a view has been clicked.
imageView = (ImageView)findViewById(R.id.imageView);
imageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Do something here
loadImageFromUrl(url);
}
});
Add a button to your xml file. Let it be with id: btn_load;
Inside the activity add
Button btn = findViewById(R.id.btn_load);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
loadImageFromUrl(url);
}
});
Create a button in your xml layout file. Then in onCreate call:
final String url = "https://www.google.com.ph/url?sa=i&rct=j&q=&esrc=s&source=images&cd=&ved=0ahUKEwiGzJm-l5PWAhVJl5QKHegTAg0QjBwIBA&url=https%3A%2F%2Fwww.enterprise.co.uk%2Fcontent%2Fdam%2Fglobal-vehicle-images%2Fcars%2FVAUX_INSI_2014.png&psig=AFQjCNGerQpF4NHcx50OFhQ2AGUlJYQCpQ&ust=1504877444951754";
ImageView imageView;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
imageView = (ImageView)findViewById(R.id.imageView);
button = findViewById(R.id.button_id);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
loadImageFromUrl(url);
}
});
}
Note: for this to work you will have to declare your url String as final, because you're using it inside an anonymous class (OnClickListener).
Note2: The image url you have may not work. Use this one instead: https://www.enterprise.co.uk/content/dam/global-vehicle-images/cars/VAUX_INSI_2014.png
Related
I am creating a profile for my user but i dont know why i am getting this error
pls can you look ate the code and tell me what is wrong thank you very much
public class Profile extends AppCompatActivity {
EditText editText;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile2);
editText =(EditText)findViewById(R.id.nome);
imageView=(ImageView) findViewById(R.id.foto);
imageView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
};
}
}
Try this:
public class Profile extends AppCompatActivity {
EditText editText;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile2);
editText =(EditText)findViewById(R.id.nome);
imageView=(ImageView) findViewById(R.id.foto);
imageView.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
}
});
// the closing parathesis ')' of setOnClickListener was missing
}
}
I have researched similar problems but none is a fit for this problem. Am trying to access the arrays of drawables from the method name called nextQuestion(), but i keep getting the error cannot resolve symbol 'ballArray' any help please. I just feel that this should work but don't know why. Here is my code.
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
The reason you can't access ballArray from within nextQuestion() is because it's declared in a separate method. If you want it accessible from within nextQuestion(), you would need to make it visible at that level:
ImageView mBallDisplay;
final int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
Do like this:-
public class MainActivity extends AppCompatActivity {
ImageView mBallDisplay;
int[] ballArray = {
R.drawable.ball1,
R.drawable.ball2,
R.drawable.ball3,
R.drawable.ball4,
R.drawable.ball5
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
int quote;
mBallDisplay = (ImageView) findViewById(R.id.image_eightBall);
Button nextbutton = (Button) findViewById(R.id.nextbutton);
Button prevbutton = (Button) findViewById(R.id.prevbutton);
//next button
nextbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
nextQuestion();
}
});
//previous button
prevbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
prevQuestion();
}
});
}
private void nextQuestion(){
mBallDisplay.setImageResource(ballArray[4]);
}
private void prevQuestion(){
}
}
I'm trying to create a simple android app, which has multiple buttons but just 1 imageView. Each button will have a unique image. It displays in the imageView when clicked on. Here is my java code:
public void poundedYamFunction(View view){
ImageView swallowImage = (ImageView) view;
swallowImage.setImageResource(R.drawable.poundedyam);
}
public void ebaFunction(View view){
ImageView swallowImage = (ImageView) view;
swallowImage.setImageResource(R.drawable.eba);
}
public void semoFunction(View view){
ImageView swallowImage = (ImageView) view;
swallowImage.setImageResource(R.drawable.semo);
}
As per the details and code , multiple buttons are associated with different views and the View view reference here is the reference to the Buttons not the imageview so you need to initialize the imageView reference and set the image accordingly.
like
ImageView img;
...oncreate(..){
setContentView(..);
img = (ImageView)findViewById(R.id.id_of_image_view);
}
public void poundedYamFunction(View view){
img.setImageResource(R.drawable.poundedyam);
}
public void ebaFunction(View view){
img.setImageResource(R.drawable.eba);
}
You can achieve it with below code. Implement activity with View.OnClickListener and on onClick(View view) change image.
public class MainActivity2 extends AppCompatActivity implements View.OnClickListener {
ImageView imageView;
Button button1, button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.image_view);
button1 = findViewById(R.id.button_1);
button2 = findViewById(R.id.button_2);
imageView.setOnClickListener(this);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
}
#Override
public void onClick(View view) {
int id = view.getId();
switch (id) {
case R.id.button_1:
imageView.setImageResource(R.drawable.poundedyam);
break;
case R.id.button_2:
imageView.setImageResource(R.drawable.eba);
break;
}
}
}
I am semi-ok with the understanding of how you store and retrieve data through intents, what I am not sure about however is the code implementation. What I want to accomplish is to pass stored data (name/value pairs and a bundle object) to my ThirdActivity class from MainActivity class. Here is a snippet what i have attempted thus far (which when the user clicks on the a3button it just displays a blank page with no string):
MainActivity from which I want to store data to send to ThirdActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button a2_button = (Button) findViewById(R.id.a2_button);
a2_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a2intent = new Intent("com.tester.lab.x");
startActivityForResult(a2intent, 1);
}
});
Button a3_button = (Button) findViewById(R.id.a3_button);
a3_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent a3intent = new Intent(v.getContext(), ThirdActivity.class);
a3intent.putExtra("greeting","my Name");
startActivity(a3intent);
}
});
}
}
ThirdActivity:
public class ThirdActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
getIntent().getStringExtra("greeting");
}
}
How would I display the string "my Name" once the user clicks ThirdActivity button? Any help is wonderful, thanks!
Everything is good, just add String type for value that you are getting and use this String as you want (to show in TextView else..):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
EditText text = (EditText) findViewById(R.id.your_textView_id);
String value = getIntent().getStringExtra("greeting");
text.setText(value);
}
Or by using Button:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
EditText text = (EditText) findViewById(R.id.your_textView_id);
Button three = (Button) findViewById(R.id.your_button_id);
three.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String value = getIntent().getStringExtra("greeting");
text.setText(value);
}
});
}
public class saeidactivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.saeid);
Button btn=(Button) findViewById(R.id.saeidbtn1);
TextView str=(TextView) findViewById(R.id.saeidtxtv1);
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
str.setTextColor(0xFF00FF00);
}
});
}
}
Try to add final modifier.
final TextView str=(TextView)
instead of
TextView str=(TextView)
Or, you can make the TextView local.
btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
TextView str=(TextView) findViewById(R.id.saeidtxtv1);
if(str != null)
str.setTextColor(0xFF00FF00);
}
});