Android Studio, adding multiple images on click - android

JAVA:
package com.example.scott.testapplication;
import android.app.Activity;
import android.media.Image;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
public class HomeScreen extends Activity {
RelativeLayout Backround;
Button InitButton;
ImageView image;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
Backround = (RelativeLayout) findViewById(R.id.Backround);
InitButton = (Button) findViewById(R.id.InitButton);
image = (ImageView) findViewById(R.id.image);
InitButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/*insert code here*/
}
});
}
}
In order to insert the image, i'm thinking that I need to use a command like ImageView image = new (image), and then use another command to draw it on the display. I dont know which commands I should use.
XML:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/Backround"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin">
<ImageView
android:layout_width ="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/image"
android:scaleType="fitXY"
android:src="#drawable/DESERT2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Initiate"
android:id="#+id/InitButton"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_vertical" />
</RelativeLayout>
I would like to create the image on the event of the click of a button, I also created the ImageView in the xml file. Not sure if I should change something there.

you can use
image.setImageResource(R.drawable.yourDesiredImageName);

Related

Android application that downloads images from bing image search not having the correct image

I have an android application that has a button and an image view
when clicking the button the application goes to bing image search and search for images then get the url of the first image and make it the source of the image view so that it has the first image of the search
When I do the image search using the explorer (I use chrome) I find that the first image displayed in my browser is not the same displayed in the image view
when I search for (facebook.com logo) or (google.com logo) the image view is the same of the first image shown in the search result but if I change the search to (yahoo.com logo) the image view is the second
If I change the search to (outlook.com logo) it takes the 10th image of the search and if I search for (hotmail.com logo) it does not show an image related to hotmail at all
I use JSOUP library to parse the HTML of the web page of the search results
here is the xml of the project:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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"
tools:context="arb.myapplication.MainActivity">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:srcCompat="#mipmap/ic_launcher"
android:layout_alignParentBottom="true"
android:layout_marginBottom="131dp"
android:id="#+id/imageView" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginRight="39dp"
android:layout_marginEnd="39dp"
android:layout_marginTop="11dp"
android:id="#+id/button3"
android:elevation="0dp" />
</RelativeLayout>
and this is the Java file of the project:
package arb.myapplication;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import org.jsoup.Connection;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Attribute;
import org.jsoup.nodes.Attributes;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
class DownloadIcon extends AsyncTask<Object, Object, Integer>
{
#Override
protected Integer doInBackground(Object... objects) {
Connection connecion= Jsoup.connect("http://www.bing.com/images/search?q=outlook.com+logo");
try {
Document document=connecion.get();
Elements element=document.select("img.mimg");
String url=element.get(0).absUrl("src");
InputStream inputStream= new URL(url).openStream();
Bitmap bitmap=BitmapFactory.decodeStream(inputStream);
publishProgress(bitmap);
} catch (IOException e1) {
publishProgress(-1);
}
return 0;
}
#Override
protected void onProgressUpdate(Object... values) {
ImageView imageView=(ImageView) findViewById(R.id.imageView);
Bitmap bitmap=(Bitmap) values[0];
imageView.setImageBitmap(bitmap);
}
#Override
protected void onPostExecute(Integer integer) {
Toast.makeText(getBaseContext(),"done",Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button3=(Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
new DownloadIcon().execute();
}
});
}
}
Help me please

Android: ImageView Rotate Animation working only once

I am facing strange Issue
If I click on the image it is rotating for first time,
if you click it again it is not rotating.
I have used toast to check if control is going inside the function, but toast is getting printed all the time.
Why Image is not rotating for second time?
here is my code..
MainActivity:
package com.example.sayantan.myapp1;
import android.annotation.TargetApi;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
public void rotateImage(View view) {
Toast.makeText(getApplicationContext(), "in rotateImage()...", Toast.LENGTH_SHORT).show();
imageView.animate().rotation(1800f).setDuration(1500);
// Toast.makeText(getApplicationContext(), "End of rotateImage()", Toast.LENGTH_SHORT).show();
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = (ImageView) findViewById(R.id.imageView);
}
}
Layout XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.example.sayantan.myapp1.MainActivity">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/imageView"
android:layout_below="#+id/blue"
android:layout_centerHorizontal="true"
android:layout_marginTop="56dp"
android:src="#drawable/bart"
android:onClick="rotateImage" />
I am pretty sure that the rotation is kept at the end of the animation. So the second time you try to animate the image it doesn't move because it already is rotated to 1800.
So either reset your image to 0 or rotate it to currentRotation + 1800f.
Use rotationBy() instead of rotation();
Have you tried to call Start()?
imageView.animate().rotation(1800f).setDuration(1500).start();
hi you can try with this,
imageView.animate().rotation(1800f).setDuration(1500);
imageView.getAnimation().setRepeatCount(Animation.INFINITE);
Thanks hope this will help you.

List Crashes App when updated

I'm trying to get a ListView to show the contents of a simple array but my program crashes at the setAdapeter line. As I understand this is supposed to be because the final value comes out as null but I don't see why that would be the case or how to fix it. I have already tried using ArrayAdapeter instead as well as catching the null exception with a try block. The former changes nothing and the latter simply shows a blank activity with no list to speak of. Please help.
Here's Main Activity
package ballton.fowdeckbuilder;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ListAdapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btnDecks = (Button) (findViewById(R.id.btnDecks));
Button btnCards = (Button) (findViewById(R.id.btnCards));
btnDecks.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, DeckCatalogue.class));
}
});
btnCards.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this, CardCatalogue.class));
}
});
}
}
Here's the activity with the Problem
package ballton.fowdeckbuilder;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
public class DeckCatalogue extends AppCompatActivity {
ListView Ldecks;
String TAG = "TAG";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_deck_catalogue);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
Ldecks = (ListView) (findViewById(R.id.DeckList));
String Decks[] = new String[] {"Faria","Melgis"};
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, Decks));
Ldecks.setAdapter(feed);
}
}
Here's the Layout for that activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="ballton.fowdeckbuilder.DeckCatalogue"
tools:showIn="#layout/activity_deck_catalogue">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/DeckList"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
And Here's the layout XML I use for the list times
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:id="#+id/Deck" />
</LinearLayout>
Use this code instead of yours:
ListAdapter feed = (new ArrayAdapter<String>(this, R.layout.show_deck, R.id.Deck, Decks));
This happened because you must supply the resource ID (as your xml: R.id.Deck) for the TextView you used for ListView.

Fatal Exception : Main on android app

I have a problem with my project. If you can to solve this problem please help me..
This is main code
import android.app.Activity;
import android.app.Dialog;
import android.content.pm.ActivityInfo;
import android.util.FloatMath;
import android.util.Log;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import java.lang.Math;
import com.kalkulator.R;
public class MainActivity extends Activity implements OnClickListener {
private String str1="",str2="",str3="",op="";
private Float num1,num2,num3,num4,num5;
private long a,b=1;
EditText ed;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
ed=(EditText)findViewById(R.id.EditText01);
View[] keys=new View[30];
keys[0]= findViewById(R.id.button1);
keys[1]= findViewById(R.id.button2);
keys[2]= findViewById(R.id.button3);
for(int i=0;i<keys.length;i++)
{
keys[i].setOnClickListener(this);
}
ed.setText("");
}
public void onClick(View v) {
switch(v.getId())
{
case R.id.button1:
str3=ed.getText().toString();
if(str3.contains("E"))
{
Toast.makeText(getApplicationContext(),"Factorial limit is upto 25", Toast.LENGTH_SHORT).show();
break;
}
a=Long.parseLong(str3);
if(a>25)
{
Toast.makeText(getApplicationContext(),"Factorial limit is upto 25", Toast.LENGTH_SHORT).show();
break;
}
for(long i=1;i<=a;i++)
{
b*=i;
}
ed.setText(String.valueOf(b));
b=1;
break;
......
And this is the xml code
<RelativeLayout 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"
tools:context=".MainActivity" >
<EditText
android:id="#+id/EditText01"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:cursorVisible="false"
android:gravity="right"
android:textStyle="italic" >
</EditText>
<TableLayout
android:id="#+id/TableLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/EditText01"
android:layout_below="#+id/EditText01"
android:layout_marginTop="28dp"
android:stretchColumns="*" >
<TableRow
android:id="#+id/TableRow02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="n!" />
.......
I don't know why, but when i run this project on AVD_for_3_7_FWVGA_slide, i have notification on log cat FATAL EXCEPTION : Main.
Thanks
//setContentView(R.layout.main); must not be commented, and you should consider moving the orientation to your AndroidManifest file
Also,
keys[0]= findViewById(R.id.button1);
keys[1]= findViewById(R.id.button2);
keys[2]= findViewById(R.id.button3);
for(int i=0;i<keys.length;i++)
{
keys[i].setOnClickListener(this);
}
ed.setText("");
You're calling findViewById on the keys values with index > which gets you a NullPointerException.

Android-background change

So basically what I want to do is to change the background of the application when integer A value is greater than 50. I've tried using:
setBackgroundResource(R.drawable.imagename);
but it doesn't work.
I would really appreciate your help.
UPDATE:
My code:
package com.example.highhope;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.view.View;
import android.view.View.OnClickListener;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;
public class MainActivity extends ActionBarActivity {
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
addListenerOnButton();
}
public void addListenerOnButton() {
button = (Button) findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
runOnUiThread(new Runnable() {
#Override
public void run() {
view.setBackgroundResource(R.drawable.i0);
}
});
}
});
}
}
My layout:
<RelativeLayout 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"
tools:context="com.example.highhope.MainActivity$PlaceholderFragment" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="90dp"
android:text="Button" />
So for starters I'm just trying to change application background on button click and I've tried everything and nothing seems to work.
You can make that with a Selector, Check this tutorial.
it's pretty easy, you just have to:
Add the images to your app resources
Create your background_button.xml (selector in the tutorial) in the drawable carpet
Set the background_button as your button background in your layout (android:background="#drawable/background_button")
Set an onClickListener to your button
Test it!.
When you do it this way, the background of your button will change with no need of handle it programmatically.
Are you making the background change in the UI thread? Try setBackgroundResource in the UI thread like below.
runOnUiThread(new Runnable() {
#Override
public void run() {
view.setBackgroundResource(R.drawable.bg);
}
});
Also, if you are using a Fragment. you would have to put getActivity(). in front of runOnUiThread.

Categories

Resources