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.

Date Calendar Java Android Example


AD MOB

Calendar cal = new GregorianCalendar(), int year = cal.get(Calendar.YEAR),
ERA, MONTH, DAY_OF_MONTH, DAY_OF_WEEK, Android example code.

public class MainActivity extends Activity {
TextView txtV;
	Context cntx;
	
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        txtV = (TextView)findViewById(R.id.idLabel);
        cntx = this;
        Calendar cal = new GregorianCalendar();

     int era = cal.get(Calendar.ERA);  // 0 B.C. before Christ, 1 Anno Domini              
     txtV.setText(txtV.getText() +"

" + era); int year = cal.get(Calendar.YEAR); // 2011 txtV.setText(txtV.getText() +"
" + year); int month = cal.get(Calendar.MONTH); // 0 is Januar txtV.setText(txtV.getText() +"
" + month); int day = cal.get(Calendar.DAY_OF_MONTH); // 1 to 31 txtV.setText(txtV.getText() +"
" + day); int dayOfWeek = cal.get(Calendar.DAY_OF_WEEK); // 1=Sunday, 2=Monday ... txtV.setText(txtV.getText() +"
" + dayOfWeek); } }

397LW NO topic_id




AD

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


15

int java android example | int-java-android-example


int occupy 4 bytes (32 bits) in memory

int in Java example of using Integer and Array of Integer and Iteger to String

        // get max end min values of int in Java example 4 bytes (32 bits) PC architecture
	System.out.println(Integer.MAX_VALUE); // 2147483647
	System.out.println(Integer.MIN_VALUE); // -2147483648


// members variable
private int mProgress = 10;

//integer to string java
int myInteger = 8;
String myString = Integer.toString(myInteger);

// a final variable can only be initialized once
static final int NUM_PARTICLES = 15;

 for (int i = 0; i < NUM_PARTICLES ; i++) { 
// do something
}

// int as return value of function
public int getCount() {
return 5;
}

// int as a parametr of function
public float getFloatFromInt(int i) {
float fRet = (float) i;
 return fRet;
}

//array of int
int[] anArray;              // declares an array of integers
          anArray = new int[2];      // allocates memory for 2 integers
          anArray[0] = 100; // initialize first element
          anArray[1] = 200; // initialize second element

for (int i = 0; i < anArray.length; i++) {
// print out values from anArray
System.out.println("Index: " + i);
System.out.println("Value: " + anArray[i]); 
}


295

Shuffle array int string Java Android | shuffle-array-int-string-java-android



String [] arOfStrings = {"one", "two", "three"};
List<String> mListOfStrings = new ArrayList<String>(Arrays.asList(arOfStrings));
Collections.shuffle(mListOfstrings);
String str = mListOfStrings.get(2);// 2 is index of pozition an element


int [] arOfInt = {1, 2, 3};
List<Integer> mListOfInt = new ArrayList<Integer>(Arrays.asList(arOfInt));
Collections.shuffle(mListOfInt);
136

Draw Path drawPath draw the trajectory of shot Android basic example | draw-path-drawpath-android-basic-example


Draw the trajectory of shot.

public class MainActivity extends Activity {
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new SampleView(this));
	}

	private static class SampleView extends View {

		// CONSTRUCTOR
		public SampleView(Context context) {
			super(context);
			setFocusable(true);

		}
	       private static void makePath(Path p) {
	            p.moveTo(10, 0);
	            p.cubicTo(100, -50, 200, 150, 300, 0);
	        }
		@Override
		protected void onDraw(Canvas canvas) {

			canvas.drawColor(Color.CYAN);
			Paint p = new Paint();
			// smooths
			p.setAntiAlias(true);
			p.setColor(Color.RED);
			p.setStyle(Paint.Style.STROKE); 
			p.setStrokeWidth(5);
			// opacity
			//p.setAlpha(0x80);
 
			p.setColor(Color.BLACK);
			Path mPath = new Path();
            makePath(mPath);
			canvas.drawPath(mPath, p); //(rectF, 90, 45, true, p);
		}

	}
}
55

How update View TextView with timer Android runnable example | how-update-view-textview-with-timer-android-runnable-example


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>




50

How get package resource apk path on device Android example | how-get-package-resource-path-android-example


On Android device is path to *.apk like this example:
/data/app/cz.okhelp.my_package.apk


How get package *.apk path on device dynamically Android example

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        String sPackagePath = getPackageResourcePath();
     }


Editace: 2011-09-29 13:32:10
Počet článků v kategorii: 397
Url:date-calendar-java-android-example

AD
11 Share
Share
Tweet