Custom View Android Basic Example
Activity.java
\res\layout\main.xml
location TouchImageView\src\cz\okhelp\TouchImageView\TouchImageView.java
public class A extends Activity{
Bitmap bm;
TouchImageView touch;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
bm = BitmapFactory.decodeResource(getResources(), R.drawable.chinese_sky_map);
touch = (TouchImageView)findViewById(R.id.myImageView);
touch.setImageBitmap(bm);
}
}
\res\layout\main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/hello" />
<cz.okhelp.TouchImageView.TouchImageView
android:id="@+id/myImageView"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
location TouchImageView\src\cz\okhelp\TouchImageView\TouchImageView.java
public class TouchImageView extends ImageView {
Context context;
// constructor wihtout using *.xml file
// public TouchImageView(Context context) {
// super(context);
// }
// constructor with xml file
public TouchImageView(Context context, AttributeSet attrs)
{
super(context, attrs);
super.setClickable(true);
this.context = context;
}
}
396LW NO topic_id
AD
Další témata ....(Topics)
Map<Float,String> linkedHashMap = new LinkedHashMap<Float, String>();
linkedHashMap.put(14.f, "text");
linkedHashMap.put(13.f, "text");
linkedHashMap.put(10.f, "text");
linkedHashMap.put(11.f, "text");
linkedHashMap.put(2.f, "text");
linkedHashMap.put(3.f, "text");
linkedHashMap.put(1.f, "text");
linkedHashMap.put(7.f, "text");
linkedHashMap.put(23.f, "text");
for (Entry<Float,String> entry : linkedHashMap.entrySet()) {
System.out.println(entry.getKey());
}
14.0
13.0
10.0
11.0
2.0
3.0
1.0
7.0
23.0
Map<Float,String> hashMap = new HashMap<Float, String>();
hashMap.put(14.f, "text");
hashMap.put(13.f, "text");
hashMap.put(10.f, "text");
hashMap.put(11.f, "text");
hashMap.put(2.f, "text");
hashMap.put(3.f, "text");
hashMap.put(1.f, "text");
hashMap.put(7.f, "text");
hashMap.put(23.f, "text");
for (Entry<Float,String> entry : hashMap.entrySet()) {
System.out.println(entry.getKey());
}
// key :
1.0
14.0
3.0
13.0
2.0
7.0
11.0
23.0
10.0
Context context = getApplicationContext();
Drawable drawable = context.getResources().getDrawable(R.drawable.my_image);
// convert drawable to bitmap
Bitmap bitmap = ((BitmapDrawable)drawable).getBitmap();
// convert bitmap to drawable
Drawable d = new BitmapDrawable(bitmap);
Android example source code.
Screen Android example source code for developers.
Get orientation of screen.
Get orientation of screen.
public int getScreenOrientation() {
if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
return 1; // Portrait Mode
}else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
return 2; // Landscape mode
}
return 0;
}
Emulator window was out of view and was recentred
Emulator] WARNING: Data partition already in use. Changes will not persist!
Emulator] WARNING: SD Card image already in use: C:\Documents and Settings\user_name\.android\avd\hvga21_up1_7.avd/sdcard.img
Emulator] WARNING: Cache partition already in use. Changes will not persist!
Basic step
Try restart Eclipse if using from menu File->Restart
Other solutions
Solution 1: try restart ADB server
//android.okhelp.cz/how-quickly-restart-adb-exe-adb-server-android-emulator-example/
Solution 2:
//android.okhelp.cz/android-emulator-wont-run-application-started-from-eclipse/
Solution 3:
Try delete:
Try solution 4:
Eclipse menu select Window->Preferences->Android
Windows 32
FROM: C:\Program Files\Android\android-sdk
TO: C:\PROGRA~1\Android\android-sdk
Windows 64
FROM: C:\Program Files (x86)\Android\android-sdk
TO: C:\PROGRA~2\Android\android-sdk
Emulator] WARNING: Data partition already in use. Changes will not persist!
Emulator] WARNING: SD Card image already in use: C:\Documents and Settings\user_name\.android\avd\hvga21_up1_7.avd/sdcard.img
Emulator] WARNING: Cache partition already in use. Changes will not persist!
Basic step
Try restart Eclipse if using from menu File->Restart
Other solutions
Solution 1: try restart ADB server
//android.okhelp.cz/how-quickly-restart-adb-exe-adb-server-android-emulator-example/
Solution 2:
Delete Run Configuration
//android.okhelp.cz/android-emulator-wont-run-application-started-from-eclipse/
Solution 3:
Try delete:
c:\Documents and Settings\user_name\.android\avd\my_avd.avd\cache.img
c:\Documents and Settings\user_name\.android\avd\my_avd.avd\userdata-qemu.img
Try solution 4:
Eclipse menu select Window->Preferences->Android
Windows 32
FROM: C:\Program Files\Android\android-sdk
TO: C:\PROGRA~1\Android\android-sdk
Windows 64
FROM: C:\Program Files (x86)\Android\android-sdk
TO: C:\PROGRA~2\Android\android-sdk
Spinner in Android application is equivalent of ComboBox in WinApi.
Spinner get selected item to string example.
Spinner get selected item position to int example.
Main activity class MainComboBox.java
File MyOnItemSelectedListener.java
strings.xml
main.xml
Spinner get selected item to string example.
Spinner get selected item position to int example.
Main activity class MainComboBox.java
public class MainComboBox extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(
this, R.array.countries_array, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new MyOnItemSelectedListener());
Button myButton =(Button)findViewById(R.id.button1);
myButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Spinner sp = (Spinner)findViewById(R.id.spinner);
String spinnerString = null;
spinnerString = sp.getSelectedItem().toString();
int nPos = sp.getSelectedItemPosition();
Toast.makeText(getApplicationContext(), "getSelectedItem=" + spinnerString,
Toast.LENGTH_LONG).show();
Toast.makeText(getApplicationContext(), "getSelectedItemPosition=" + nPos,
Toast.LENGTH_LONG).show();
}
});
}
}
File MyOnItemSelectedListener.java
public class MyOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
Toast.makeText(parent.getContext(), "Item is " +
parent.getItemAtPosition(pos).toString(), Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView parent) {
// Do nothing.
}
}
strings.xml
<resources>
<string name="hello">Hello World, MainComboBox!</string>
<string name="app_name">ComboBox</string>
<string name="prompt">Choose a country</string>
<string-array name="countries_array">
<item>China</item>
<item>India</item>
<item>USA</item>
<item>Indonesia</item>
<item>Brazil</item>
<item>Pakistan</item>
<item>Nigeria</item>
<item>Bangladesh</item>
<item>Russia</item>
</string-array>
</resources>
main.xml
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="@string/prompt"
/>
<Spinner
android:id="@+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="@string/prompt"
/>
<Button android:text="Button" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
Editace: 2012-02-14 21:11:33
Počet článků v kategorii: 396
Url:custom-view-android-basic-example