The magic button that changes the picture!

​This android developer course is actually pretty fun! There are several hard parts, and a few “scratch my head” moments, but Rob does a great job of explaining what to do.
Here’s my latest creation:
https://www.mediafire.com/download/168pmpux75y8w9u

It is a simple app that changes the picture and gives you a popup (toast) when you click the button.
I am a bit proud of myself on this one, because I went above and beyond the task at hand (changing the picture) but also added the toast (from a previous lesson) and changed the icon (which I posted a “how to” on earlier). The great part is, Rob will teach you some principles, and then give you a related task that he didn’t specifically cover.
In this case, he showed us how to display an image, and then asked us to figure out how to change it. Fortunately, there is Google, or I would not have made it work! After you finish the challenge, you can continue through the video and watch Rob explain it, step by step. It’s funny how things that take me an hour he does in about 3 to 5 minutes!
Here is my ANDROIDMANIFEST.XML

[CODE]

<?xml version=”1.0″ encoding=”utf-8″?>

<manifest xmlns:android=”http://schemas.android.com/apk/res/android&#8221;

    package=”com.mycompany.images” >
    <application

        android:allowBackup=”true”

        android:icon=”@drawable/kdf”

        android:label=”@string/app_name”

        android:theme=”@style/AppTheme” >

        <activity

            android:name=”.MainActivity”

            android:label=”@string/app_name” >

            <intent-filter>

                <action android:name=”android.intent.action.MAIN” />
                <category android:name=”android.intent.category.LAUNCHER” />

            </intent-filter>

        </activity>

    </application>
</manifest>

[/CODE]
Then my MAIN.XML

[CODE]

<?xml version=”1.0″ encoding=”utf-8″?>

<LinearLayout

xmlns:android=”http://schemas.android.com/apk/res/android&#8221;

android:layout_width=”match_parent”

android:layout_height=”match_parent”

android:gravity=”top|center”

android:orientation=”vertical”

android:background=”#F0FBFC”>
<TextView

android:text=”This button magically changes the picture!”

android:layout_width=”match_parent”

android:layout_height=”wrap_content”

android:id=”@+id/mainTextView1″

android:background=”#A4CCF9″

android:layout_gravity=”center”

android:gravity=”center”

android:textSize=”20sp”/>
<ImageView

android:src=”@drawable/kdf”

android:layout_height=”wrap_content”

android:layout_width=”wrap_content”

android:scaleType=”fitXY”

android:visibility=”visible”

android:id=”@+id/mainImageView1″

style=”@style/AppTheme”

android:background=”#FFFFFF”

android:padding=”57dp”

android:layout_margin=”27dp”/>
<Button

android:layout_height=”wrap_content”

android:layout_width=”wrap_content”

android:text=”Button”

android:id=”@+id/button”

android:onClick=”change”

android:padding=”10dp”

android:layout_margin=”32dp”/>
</LinearLayout>
[/CODE]
Finally, my MAINACTIVITY.JAVA

[CODE]

package com.mycompany.images;
import android.app.*;

import android.os.*;

import android.view.*;

import android.app.Activity;

import android.os.Bundle;

import android.view.View;

import android.view.View.OnClickListener;

import android.widget.Button;

import android.widget.ImageView;

import android.widget.*;
public class MainActivity extends Activity 

{

Boolean flag=false;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.main);

}
//mess method is declared in XML file

//This function will call when we click on button

//and we have to pass View object in this method

//which will take id of clicked button
public void change(View v)

{

Toast toast = Toast.makeText(getApplicationContext(), “Changing Picture…”, Toast.LENGTH_SHORT);

toast.show();

ImageView iv=(ImageView)findViewById(R.id.mainImageView1);

//use flag to change image

if(flag==false)

{

iv.setImageResource(R.drawable.ic_launcher);

flag=true;

}

else

{

iv.setImageResource(R.drawable.kdf);

flag=false;

}

}

}

[/CODE]
To God be the glory, it even works! Thankfully, there were several helpful finds on stack overflow and other blogs from my Google search. I certainly do not want to take credit for another’s work! I just put all the pieces together.
Another important part of the equation is to put ic_launcher.png and kdf.png (or whatever you name your icons) into the drawable folder in your source.

Linux – keep it simple.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s