Android-background change - android

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.

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

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.

Android Studio, adding multiple images on click

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);

Android multi stroke gesture not working

I modified an existing app,GestureBuilder,to accept multiple strokes.Hence to accept the letter A,D, F and so on. I created a simple app which recognises if the letter is correct or not.My problem is:
1.I have successfully recorded multi stroke letter in a file called gesture with gestureBuilder.
2.My application does not accept multi stroke gesture.But i have set fadeoffset to 1000 and gesturestroketype as multiple.I do not know why this is happening.The moment I enter next xtroke to write capital A my previous stroke dissapears.I cant write multi stroke letters/symbols.
Code:
xml file:
<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"
tools:context=".MainActivity"
>
<android.gesture.GestureOverlayView
android:id="#+id/gestures_overlay"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:fadeOffset="5000"
android:eventsInterceptionEnabled="true"
android:gestureStrokeType="multiple" />
</LinearLayout>
java file:
package com.example.gesture_letter;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity implements OnGesturePerformedListener {
GestureLibrary mLibrary;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GestureOverlayView gestureoverlayview=new GestureOverlayView(this);
View inflate=getLayoutInflater().inflate(R.layout.activity_main,null);
gestureoverlayview.setGestureColor(Color.rgb(0, 255, 0));
gestureoverlayview.setGestureVisible(true);
gestureoverlayview.setUncertainGestureColor(Color.rgb(0,0,255));
gestureoverlayview.addView(inflate);
gestureoverlayview.addOnGesturePerformedListener(this);
mLibrary=GestureLibraries.fromRawResource(this,R.raw.gestures);
if(!mLibrary.load())
{
Log.i("debug","FAILED");
}
else
{
Log.i("debug","LOADED");
}
Log.i("debug","OnCreate");
setContentView(gestureoverlayview);
}
#Override
public void onGesturePerformed(GestureOverlayView arg0, Gesture arg1) {
// TODO Auto-generated method stub
ArrayList<Prediction> predictions=mLibrary.recognize(arg1);
if(predictions.size()>0)
{
Prediction prediction=predictions.get(0);
if(prediction.score>1.0)
{
String s=prediction.name;
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_LONG).show();
}
else
if(prediction.score<1.0)
{
Toast.makeText(getApplicationContext(), "None",Toast.LENGTH_LONG).show();
}
}
}
}
Thank you.
I also would like a good resource for gesture application building.thank you
I do not know why, android:GestureStrokeType="multiple" did not work. It struck me to try it from the java side. I set :
gestureoverlayview.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
gestureoverlayview is an object of GestureOverlayView(you can name it anything)
(in main_activity.java file for newbies)
Then you'll see it works.
I think its a bug, I'm not sure.I spent weeks hovering on this :(
The problem is that you create a new GestureOverlayView :
GestureOverlayView gestureoverlayview=new GestureOverlayView(this);
You should call the view defined in the xml with findViewById(R.id.gesture_overlay);

setOnClickListener makes Force Close

When I launch the application and click the button to go to the Activity, I suddenly get a Force Close. I've tried to erase it all and write it again, but it is still there. What could be the problem?
package com.alexgascon.formuladora;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Matematicas extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
final Button BtnEcSegundoGrado = (Button)findViewById(R.id.ecsegundogrado);
final Button BtnFracciones = (Button)findViewById(R.id.fracciones);
final Button BtnMCD = (Button)findViewById(R.id.maximocomunBoton);
BtnMCD.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
Intent MCDintent = new Intent(Matematicas.this,Maximocomun.class);
startActivity(MCDintent);
}
});
}
}
And here is the layout code
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/pizarramatesverde"
android:gravity="center" >
<Button
android:id="#+id/ecsegundogrado"
android:layout_height="wrap_content"
android:layout_width="265sp"
android:text="#string/ecsegundo"
android:textColor="#color/Negro"
/>
<Button
android:id="#+id/fracciones"
android:layout_height="wrap_content"
android:layout_width="265sp"
android:text="#string/fracciones"
android:layout_below="#id/ecsegundogrado"
android:textColor="#color/Negro"
/>
<Button
android:id="#+id/maximocomunBoton"
android:layout_height="wrap_content"
android:layout_width="265sp"
android:text="#string/maximocomun"
android:layout_below="#id/fracciones"
android:layout_marginBottom="80sp"
android:textColor="#color/Negro"
/>
</RelativeLayout>
You need to call setContentView before findViewById:
setContentView(R.layout.yourlayoutxmlname);
final Button BtnEcSegundoGrado = (Button)findViewById(R.id.ecsegundogrado);
final Button BtnFracciones = (Button)findViewById(R.id.fracciones);
final Button BtnMCD = (Button)findViewById(R.id.maximocomunBoton);
You need to call setContentView(R.layout.yourxmlLayout); to have the layout being referenced on your activity class.
Try to pop a toast from the click listener and see if it works. If it does - then the problem is in the activity that you want to launch.
You need to add the setContentView(R.layout.yourlayoutxmlname);.
Try the following code:
package com.alexgascon.formuladora;
import android.app.Activity;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Matematicas extends Activity {
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayoutxmlname);
final Button BtnEcSegundoGrado = (Button)findViewById(R.id.ecsegundogrado);
final Button BtnFracciones = (Button)findViewById(R.id.fracciones);
final Button BtnMCD = (Button)findViewById(R.id.maximocomunBoton);
BtnMCD.setOnClickListener( new OnClickListener(){
#Override
public void onClick(View arg0) {
Intent MCDintent = new Intent(Matematicas.this,Maximocomun.class);
startActivity(MCDintent);
}
});
}
}

Categories

Resources