ImageView change image Android example
How to change the image dynamically in ImageView and button setOnClickListener Android sample.
Main.java
main.xml
Put into res/drawable this pictures:
Main.java
ipackage cz.okhelp.my_game;
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;
public class Main extends Activity {
private ImageView hImageViewSemafor;
private Button hButton;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hImageViewSemafor = (ImageView)findViewById(R.id.idImageViewSemafor);
hButton = (Button) findViewById(R.id.idBtnChangeImage);
hButton.setOnClickListener(mButtonChangeImageListener);
}
View.OnClickListener mButtonChangeImageListener = new OnClickListener() {
public void onClick(View v) {
// setImageResource will change image in ImageView
hImageViewSemafor.setImageResource(R.drawable.semafor_green);
}
};
}
main.xml
Put into res/drawable this pictures:


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<ImageView
android:src="@drawable/semafor_red"
android:id="@+id/idImageViewSemafor"
android:background="#66FFFFFF"
android:adjustViewBounds="true"
android:maxWidth="47dip"
android:maxHeight="91dip"
android:padding="10dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
396LW NO topic_id
AD
Další témata ....(Topics)
Invalid proguard configuration file path
C:\documents\my_android_projects\my_project\proguard.cfg does not exist or is not a regular file
Solution:
Check if exist file proguard.cfg in your project on the path C:\docum.........
If not exist, copy a file proguard.cfg from other project or create file proguard.cfg and insert
this source code to file and save this file.
From more details see:
//developer.android.com/guide/developing/tools/proguard.html
C:\documents\my_android_projects\my_project\proguard.cfg does not exist or is not a regular file
Solution:
Check if exist file proguard.cfg in your project on the path C:\docum.........
If not exist, copy a file proguard.cfg from other project or create file proguard.cfg and insert
this source code to file and save this file.
-optimizationpasses 5
-dontusemixedcaseclassnames
-dontskipnonpubliclibraryclasses
-dontpreverify
-verbose
-optimizations !code/simplification/arithmetic,!field/*,!class/merging/*
-keep public class * extends android.app.Activity
-keep public class * extends android.app.Application
-keep public class * extends android.app.Service
-keep public class * extends android.content.BroadcastReceiver
-keep public class * extends android.content.ContentProvider
-keep public class * extends android.app.backup.BackupAgentHelper
-keep public class * extends android.preference.Preference
-keep public class com.android.vending.licensing.ILicensingService
-keepclasseswithmembernames class * {
native <methods>;
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet);
}
-keepclasseswithmembers class * {
public <init>(android.content.Context, android.util.AttributeSet, int);
}
-keepclassmembers class * extends android.app.Activity {
public void *(android.view.View);
}
-keepclassmembers enum * {
public static **[] values();
public static ** valueOf(java.lang.String);
}
-keep class * implements android.os.Parcelable {
public static final android.os.Parcelable$Creator *;
}
From more details see:
//developer.android.com/guide/developing/tools/proguard.html
onSaveInstanceState, onRestoreInstanceState , save preferences
int mCurrentPhotoIndex = 0;
@Override
protected void onSaveInstanceState(Bundle outState) {
outState.putInt("photo_index", mCurrentPhotoIndex);
super.onSaveInstanceState(outState);
}
@Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
mCurrentPhotoIndex = savedInstanceState.getInt("photo_index");
super.onRestoreInstanceState(savedInstanceState);
}
// or save preferences for new start of Activity in onStop
//onCreate or onResume or onStart etc.
public void loadPreferences() {
SharedPreferences settings = getSharedPreferences(F.PREFERENCES_NAME, 0);
mCurrentPhotoIndex = settings.getInt("mCurrentPhotoIndex",mCurrentPhotoIndex);
// String_sOtazka = settings.getString("_sOtazka", _sOtazka);
}
// onStop
public void savePreferences() {
SharedPreferences settings = getSharedPreferences(PREFERENCES_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
editor.putInt("mCurrentPhotoIndex", mCurrentPhotoIndex);
// String, boolean, float ...
// editor.putString("mButton1", mButton1.getText().toString());
editor.commit();
}
Set focus on a View in Android application example source code for Button, EditText, View, TextView, isFocused(), requestFocus() .
// set focus on Button Android example
private Button mRightButton;
mRightButton = (Button) a.findViewById(R.id.rightButton);
mRightButton.requestFocus();
// boolean isFocused()
boolean b = mRightButton.isFocused(); // true or false
// set focus on TextView directly Android example
((TextView) findViewById(R.id.myText)).requestFocus();
// set focus on View Android example
private View mView;
mView = findViewById(R.id.showAll);
mView.requestFocus();
// set focus on EditText Android example
private EditText mEdit;
mEdit = (EditText)findViewById(R.id.myEdit);
mEdit.requestFocus();
- download any mobile styles for example //www.artodia.com/phpbb-styles/mobile/
- unzip style into your forum style folder for example all art_mobile folder copy into 0:/myweb/forum/styles/
- folow instalation istructions //www.artodia.com/phpbb-styles/mobile/tutorials/mobile-detection/
- check url result on https://www.google.com/webmasters/tools/mobile-friendly
- unzip style into your forum style folder for example all art_mobile folder copy into 0:/myweb/forum/styles/
- folow instalation istructions //www.artodia.com/phpbb-styles/mobile/tutorials/mobile-detection/
- check url result on https://www.google.com/webmasters/tools/mobile-friendly
import android.content.Context;
import android.media.AudioManager;
import android.media.SoundPool;
public class SoundManager
{
private SoundPool soundPool;
private int[] sm;
Context context;
public SoundManager(Context context) {
this.context = context;
soundPool = new SoundPool(4, AudioManager.STREAM_MUSIC, 0);
sm = new int[2];
// fill your sounds
sm[0] = soundPool.load(context, R.raw.my_sound1, 1);
sm[1] = soundPool.load(context, R.raw.my_sound2, 1);
}
public final void playSound(int sound) {
AudioManager mgr = (AudioManager)context.getSystemService(
Context.AUDIO_SERVICE);
float streamVolumeCurrent =
mgr.getStreamVolume(AudioManager.STREAM_MUSIC);
float streamVolumeMax = mgr.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
float volume = streamVolumeCurrent / streamVolumeMax;
soundPool.play(sm[sound], volume, volume, 1, 0, 1f);
}
public final void cleanUpIfEnd() {
sm = null;
context = null;
soundPool.release();
soundPool = null;
}
}
Editace: 2011-09-26 09:53:21
Počet článků v kategorii: 396
Url:imageview-change-image-android-example