ListView add item click item basic Android example code
Definition of ListView in layout main.xml file Android example
Code in Main.java ListView example source code Java Android
<ListView android:id="@+id/idListView"
android:background="#7700CC00"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
Code in Main.java ListView example source code Java Android
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ListView mlistView = (ListView) findViewById(R.id.idListView);
mlistView.setAdapter(new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
new String[] {"Game", "Help", "Home site"}));
mlistView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text Game, Help, Home
Toast.makeText(getApplicationContext(), ((TextView) view).getText(),
Toast.LENGTH_SHORT).show();
}
});
}
}
396LW NO topic_id
AD
Další témata ....(Topics)
Change table row background color if user click on row Android example code.
MainActivity.java
main.xml
strings.xml
MainActivity.java
public class MainActivity extends Activity {
Boolean bColorYellow = true;
TextView hTextView;
TableRow hTableRow;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hTextView = (TextView)findViewById(R.id.idTextView);
hTableRow = (TableRow)findViewById(R.id.idTableRow1);
} // end onCreate
public void myTableRowClickHandler(View view) {
switch (view.getId()) {
case R.id.idTableRow1:{
if(bColorYellow){
hTableRow.setBackgroundColor(Color.GREEN);
bColorYellow = false;
}
else{
hTableRow.setBackgroundColor(Color.YELLOW);
bColorYellow = true;
}
}
break;
}
}
}
main.xml
<?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"
>
<TableLayout android:layout_height="wrap_content" android:layout_width="fill_parent" android:id="@+id/tableLayout1">
<TableRow android:id="@+id/idTableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#5655AA"
android:onClick="@string/myTableRowClick"
android:focusable="true">
<TextView
android:id="@+id/idTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
</TableRow>
</TableLayout>
</LinearLayout>
strings.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string name="hello">Hello World!</string>
<string name="app_name">TableRow</string>
<string name="myTableRowClick">myTableRowClickHandler</string>
</resources>
Best of SQLite explorer and admin download for SQLite 2.x and SQLite 3.x in separated folders.
Download Explorers SQLite 2.x and SQLite 3.x in separated folders
Download Explorers SQLite 2.x and SQLite 3.x in separated folders
If you change the package name, you have to add new package name in:
DictionaryProvider.java
Do not forget change package name in all java class and xml/searchable.xml
In my project I changed like this:
DictionaryProvider.java
public class DictionaryProvider extends ContentProvider {
String TAG = "DictionaryProvider";
// public static String AUTHORITY = "com.example.android.searchabledict.DictionaryProvider";
// change to your new package name
public static String AUTHORITY = "com.myweb.mysubdomen.searchabledict.DictionaryProvider";
public static final Uri CONTENT_URI = Uri.parse("content://" + AUTHORITY + "/dictionary");
// in AndroidManifest.xml
//change com.example.android to your package e.g. com.myweb.mysubdomen
<!-- Provides search suggestions for words and their definitions. -->
<provider android:name="com.example.android.searchabledict.DictionaryProvider"
android:configChanges="keyboard|keyboardHidden|orientation"
android:authorities="com.example.android.searchabledict.DictionaryProvider" />
<!-- Points to searchable activity so the whole app can invoke search. -->
<meta-data android:name="android.app.default_searchable"
android:configChanges="keyboard|keyboardHidden|orientation"
android:value=".SearchableDictionary" />
// I change like this:
<!-- Provides search suggestions for words and their definitions. -->
<provider android:name=".DictionaryProvider"
android:configChanges="keyboard|keyboardHidden|orientation"
android:authorities="cz.okhelp.android.searchabledict.DictionaryProvider" />
<!-- Points to searchable activity so the whole app can invoke search. -->
<meta-data android:name="android.app.default_searchable"
android:configChanges="keyboard|keyboardHidden|orientation"
android:value=".SearchableDictionary" />
Do not forget change package name in all java class and xml/searchable.xml
In my project I changed like this:
<searchable xmlns:android="//schemas.android.com/apk/res/android"
android:label="@string/search_label"
android:hint="@string/search_hint"
android:searchSettingsDescription="@string/settings_description"
android:searchSuggestAuthority="cz.okhelp.android.searchabledict.DictionaryProvider"
android:searchSuggestIntentAction="android.intent.action.VIEW"
android:searchSuggestIntentData="content://cz.okhelp.android.searchabledict.DictionaryProvider/dictionary"
android:searchSuggestSelection=" ?"
android:searchSuggestThreshold="1"
android:includeInGlobalSearch="true"
>
</searchable>
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();
Change the title associated with this activity. If this is a top-level activity, the title for its window will change. If it is an embedded activity, the parent can do whatever it wants with it.
String sTitle = "My new title";
setTitle(sTitle);
Editace: 2011-09-16 11:48:11
Počet článků v kategorii: 396
Url:listview-add-item-click-item-basic-android-example-code