Okhelp.cz

Recepty, články, nápady, programování. Dříve dum-zahrada, finance, internet a know-how.okhelp.cz Pro lepší výsledky hledání používejte i diakritiku.

How update View TextView with timer Android runnable example


AD MOB

Update TextView by runnable. Handler, runnable, timer Android example.


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>





397LW NO topic_id




AD

Další témata ....(Topics)


123

R.java not generated - Android project in Eclipse issue | r-java-not-generating-android-project-in-eclipse-issue


If You create new xml file with prefix _ , for example _style.xml and You to clean project (Project->Clean), than package in folder project\gen will deleted with R.java class and new R.java not be created.

For to solving this problem You have to rename file without prefix _ as style.xml or name what You need and rebuild project.

If some ID cannot be resolved or is not a field get error occurence
You have to delete import android.R; in Activity.class if was inserted,
when this error is displayed.
111

Create RadioButton RadioGroup dynamically - Android sample | create-radiobutton-radiogroup-dynamically-android-sample



public class MainActivity extends Activity implements OnClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
            RadioGroup radioGroup = new RadioGroup(this);
            
            LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                    LinearLayout.LayoutParams.FILL_PARENT,
                    LinearLayout.LayoutParams.WRAP_CONTENT
            );
            layout.addView(radioGroup, p);

            RadioButton radioButtonView = new RadioButton(this);
            radioButtonView.setText("RadioButton");
            radioButtonView.setOnClickListener(this);
            radioGroup.addView(radioButtonView, p);
            
            RadioButton radioButtonView2 = new RadioButton(this);
            radioButtonView2.setText("RadioButton2");
            radioButtonView2.setOnClickListener(mThisButtonListener);
            radioGroup.addView(radioButtonView2, p);
        
    }
    
    public void onClick(View view) {
    	 try {
    	  String s = ((RadioButton) view).getText().toString();
    	  Toast.makeText(MainActivity.this, "This is: " + s,
    			  Toast.LENGTH_LONG).show();
    	 }
    	 catch (Exception e1) {
    	   e1.printStackTrace();
    	 }
    	}
    
    private OnClickListener mThisButtonListener = new OnClickListener() {
        public void onClick(View v) {
        	String s = ((RadioButton) v).getText().toString();
        	Toast.makeText(MainActivity.this, "Hello from 2!" + s,
        			Toast.LENGTH_LONG).show();
        }
    };

}
27

Get Context Java Android example | get-context-java-android-example


getContext() getApplicationContext() method Java Android example source






Context myContext_1 = ThisClassName.this; // to open a Dialog
Context myContext_2 = getContext();
Context myContext_3 = this.getContext();
Context myContext_4 = this;
Context myContext_5 = this.getApplicationContext ();


OnClickListener getImageBtnOnClick = new OnClickListener() {
public void onClick(View view) {
Context context = view.getContext();

}
};
// Toast
Toast.makeText(getApplicationContext(), "Context == getApplicationContext "
, Toast.LENGTH_SHORT).show();



// store Context in public class
public class MyActivity extends Activity {
public static Context myCnt = null;
...
protected void onCreate(Bundle icicle) {
...
myCnt = this;
MyStorage.setContext(myCnt);
// or
// MyStorage.setContext(this);

// cntxFromStorage == this
Context cntxFromStorage = MyStorage.getContext();
...
};
};

public class MyStorage
{
private static Context cntStorageContext = null;


public static Context getContext() {
return cntStorageContext;
}

public static void setContext(Context context) {
MyStorage.cntStorageContext = context;
}


};

class DataBaseHelper extends SQLiteOpenHelper {
// get MyActivity context
Context cnt = MyStorage.getContext();

}



381

Android Fragment Examples | android-fragment-examples


Code look up at the Api Demos

android_fragments arguments attributes - three fragments on screen
android_fragments_arguments

android_fragments_alert_dialog
android_fragments_alert_dialog.png

android_fragments_context_menu
android_fragments_context_menu.png

android_fragments_custom_animation
android_fragments_custom_animation.png

android_fragments_dialog
android_fragments_dialog.png

android_fragments_dialog_or_activity
android_fragments_dialog_or_activity.png

fragment_hide_show
fragment_hide_show.png

fragment_layout
fragment_layout

fragments_list_array
fragments_list_array.png

fragment_menu
fragment_menu.png

android_fragments_get_result_from_fragment and tabs in two rows
android_fragments_get_result_from_fragment.png

android_fragments_recive_result
android_fragments_recive_result.png

android_fragments_stack
android_fragments_stack.png

fragment_tabs
fragment_tabs.png





36

Titlebar title bar change text setTile Android example | titlebar-title-bar-change-text-android-example


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: 22.5.2020 - 17:00
Počet článků v kategorii: 397
Url:how-update-view-textview-with-timer-android-runnable-example

AD
11 Share
Share
Tweet