Start Activity from ListView item click Android example
How start certain Activity if user clicked to ListView item
Start Activity from list – launches other activities from list - latest variant!!!!
Game class Game.java game.xml
You have to add class Game to AndroidManifest.xml
Start Activity from list – launches other activities from list - latest variant!!!!
public class Main extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
try {
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();
String sText = ((TextView) view).getText().toString();
Intent intent = null;
if(sText.equals("Game"))
intent = new Intent(getBaseContext(),
Game.class);
//else if(sText.equals("Help")) ..........
if(intent != null)
startActivity(intent);
}
});
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Game class Game.java game.xml
You have to add class Game to AndroidManifest.xml
<activity android:name=".Game" android:label="GameLabel">
</activity>
package cz.okhelp.listview;
import android.app.Activity;
import android.os.Bundle;
public class Game extends Activity{
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// you have to create game.xml
setContentView(R.layout.game);
}
}
396LW NO topic_id
AD
Další témata ....(Topics)
Wiktionary - SDK samples Android
about.xml
protected void showAbout() {
// Inflate the about message contents
View messageView = getLayoutInflater().inflate(R.layout.about, null, false);
// When linking text, force to always use default color. This works
// around a pressed color state bug.
TextView textView = (TextView) messageView.findViewById(R.id.about_credits);
int defaultColor = textView.getTextColors().getDefaultColor();
textView.setTextColor(defaultColor);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setIcon(R.drawable.app_icon);
builder.setTitle(R.string.app_name);
builder.setView(messageView);
builder.create();
builder.show();
}

about.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2009 The Android Open Source Project
//www.apache.org/licenses/LICENSE-2.0
-->
<LinearLayout xmlns:android="//schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dip">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="@string/app_descrip"
android:textColor="?android:attr/textColorPrimaryInverse" />
<TextView
android:id="@+id/about_credits"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="20dip"
android:textSize="16sp"
android:text="@string/app_credits"
android:autoLink="web"
android:textColor="?android:attr/textColorPrimaryInverse" />
</LinearLayout>
// warnning
private List list = new ArrayList();
// ok /put type of added object
private List<String> list = new ArrayList<String>();
If a button have focus, marquee will run.
Important:
android:singleLine="true"
android:ellipsize="marquee"
Optional:
android:marqueeRepeatLimit="1"
Important:
android:singleLine="true"
android:ellipsize="marquee"
Optional:
android:marqueeRepeatLimit="1"
// main.xml
<Button
android:layout_width="150dip"
android:layout_height="wrap_content"
android:text="My button with a long text for marquee as a example source code"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit="1"/>
int nf = Math.round(5.789f);
System.out.print(nf); // 6
float f = 28.611f;
int n3 = Math.round(f);
System.out.println(n3); // 29
double d = 1234.56;
long lon = Math.round(d);
System.out.println(lon); // 1235
int diff = 90 - 40;
// float fDeleni = diff / 10; // error code
float fDeleni = (float)diff / 10.f; // ok
int nRound = Math.round(fDeleni);
// Caution:
int n2 = (int) 8.999f;
System.out.println(n2); // 8
Warning in AndroidManifest.xml:
tag should specify a target API level (the highest verified version; when running on
later versions, compatibility behaviors may be enabled) with android:targetSdkVersion="?"
Solution:
later versions, compatibility behaviors may be enabled) with android:targetSdkVersion="?"
Solution:
<uses-sdk android:minSdkVersion="4"
android:targetSdkVersion="16" />
Editace: 2016-03-02 07:14:49
Počet článků v kategorii: 396
Url:start-activity-from-listview-item-click-android-example