How update View TextView with timer Android runnable example
Update TextView by runnable. Handler, runnable, timer Android example.
main.xml
public class TimerActivity extends Activity {
TextView hTextView;
Button hButton, hButtonStop;
private Handler mHandler = new Handler();
private int nCounter = 0;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
hTextView = (TextView)findViewById(R.id.idTextView);
hButton = (Button)findViewById(R.id.idButton);
hButton.setOnClickListener(mButtonStartListener);
hButtonStop = (Button)findViewById(R.id.idButtonStop);
hButtonStop.setOnClickListener(mButtonStopListener);
} // end onCreate
View.OnClickListener mButtonStartListener = new OnClickListener() {
public void onClick(View v) {
try {
mHandler.removeCallbacks(hMyTimeTask);
// Parameters
// r The Runnable that will be executed.
// delayMillis The delay (in milliseconds) until the Runnable will be executed.
mHandler.postDelayed(hMyTimeTask, 1000); // delay 1 second
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
private Runnable hMyTimeTask = new Runnable() {
public void run() {
nCounter++;
hTextView.setText("Hallo from thread counter: " + nCounter);
}
};
/**
*
*/
View.OnClickListener mButtonStopListener = new OnClickListener() {
public void onClick(View v) {
mHandler.removeCallbacks(hMyTimeTask);
}
};
}
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"
>
<TextView
android:id="@+id/idTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello"
/>
<Button android:text="Button"
android:id="@+id/idButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></Button>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/idButtonStop"
android:text="Stop"></Button>
</LinearLayout>
396LW NO topic_id
AD
Další témata ....(Topics)
Insert into your default start up activity tag inten-filter tag with action MAIN and category LAUNCHER
AndroidManifest.xml
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="//schemas.android.com/apk/res/android"
package="com.example.blabol"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" />
<application
android:allowBackup="true"
android:icon="@drawable/ic_launcher"
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>
/data/data/<package>/shared_prefs/<package>_preferences.xml
String TAG = "MyActivityName";
File f = new File("/data/data/"+this.getPackageName()
+"/shared_prefs/"+this.getPackageName()+"_preferences.xml");
if(f.exists())
Log.d(TAG, "exist");
else
Log.d(TAG, "not exist");
Error for example:
webcoreglue(2075): The real object has been deleted
Solution:
If this error message shows if orientation the screen is changed
try insert into AndroidManifest.xml this code:
android:configChanges="keyboard|keyboardHidden|orientation"
webcoreglue(2075): The real object has been deleted
Solution:
If this error message shows if orientation the screen is changed
try insert into AndroidManifest.xml this code:
android:configChanges="keyboard|keyboardHidden|orientation"
<activity
android:label="@string/app_name"
android:name=".MyActivity"
android:screenOrientation="portrait"
android:configChanges="keyboard|keyboardHidden|orientation"
>
This version of the rendering library is more recent than your version of ADT plug-in. Please update ADT plug-in
Click on menu Help > Install New Software.
In the Work with field, Add: https://dl-ssl.google.com/android/eclipse/
Select: Developer Tools / Android Development Tools.
Click Next to complete the wizard.
If you have problem try download all sdk + eclipse in one pack , rename old folder for example Andorid_old, create new folder Android and unpack sdk + eclipse from this adress:
//developer.android.com/sdk/index.html
Click on menu Help > Install New Software.
In the Work with field, Add: https://dl-ssl.google.com/android/eclipse/
Select: Developer Tools / Android Development Tools.
Click Next to complete the wizard.
If you have problem try download all sdk + eclipse in one pack , rename old folder for example Andorid_old, create new folder Android and unpack sdk + eclipse from this adress:
//developer.android.com/sdk/index.html
Editace: 22.5.2020 - 17:00
Počet článků v kategorii: 396
Url:how-update-view-textview-with-timer-android-runnable-example