1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 | public class LocationTestActivity extends Activity implements LocationListener { private LocationManager service; private TextView tv; private String provider; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); tv = (TextView) findViewById(R.id.display); // Get the location manager service = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Criteria criteria = new Criteria(); provider = service.getBestProvider(criteria, false); Location location = service.getLastKnownLocation(provider); // Initialize the location fields if (location != null) { onLocationChanged(location); } else { tv.setText("Location not available"); } boolean enabled = service.isProviderEnabled(LocationManager.GPS_PROVIDER); if (!enabled) { Intent intent = new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS); startActivity(intent); } } public void onLocationChanged(Location arg0) { //int lat = (int) (location.getLatitude()); //int lng = (int) (location.getLongitude()); //latituteField.setText(String.valueOf(lat)); //longitudeField.setText(String.valueOf(lng)); String lat = String.valueOf(arg0.getLatitude()); String lon = String.valueOf(arg0.getLongitude()); Log.e("GPS", "location changed: lat="+lat+", lon="+lon); tv.setText("lat="+lat+", lon="+lon); } public void onProviderDisabled(String arg0) { Toast.makeText(this, "Disabled provider " + provider,Toast.LENGTH_SHORT).show(); Log.e("GPS", "provider disabled " + arg0); } public void onProviderEnabled(String arg0) { Toast.makeText(this, "Enabled new provider " + provider, Toast.LENGTH_SHORT).show(); Log.e("GPS", "provider enabled " + arg0); } public void onStatusChanged(String arg0, int arg1, Bundle arg2) { Log.e("GPS", "status changed to " + arg0 + " [" + arg1 + "]"); } /* Request updates at startup */ @Override protected void onResume() { super.onResume(); service.requestLocationUpdates(provider, 400, 1, this); } /* Remove the locationlistener updates when Activity is paused */ @Override protected void onPause() { super.onPause(); service.removeUpdates(this); } } |
Friday, August 31, 2012
Location service (android)
Simple ListView 2 - with Dialog (android)
public class HelloListViewActivity extends ListActivity {
String[] data;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
data = getResources().getStringArray(R.array.data);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_cell, R.id.label, data));
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
createDialog(data[position]);
}
});
}
private void createDialog(String s)
{
new AlertDialog.Builder(this)
.setTitle("Message")
.setMessage("Go to " + s + " page ?")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
})
.show();
}
}
Thursday, August 30, 2012
Simple ListView (android)
res/layout/list_cell.xml
<?xml version="1.0" encoding="utf-8"?>
<!-- Single List Item Design -->
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/label"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:textSize="16dip"
android:textStyle="bold" >
</TextView>
res/values/list.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="data">
<item>Apple</item>
<item>Orange</item>
<item>Banana</item>
<item>Milk</item>
</string-array>
</resources>
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class HelloListViewActivity extends ListActivity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// storing string resources into Array
String[] data = getResources().getStringArray(R.array.data);
// Binding resources Array to ListAdapter
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.list_cell, R.id.label, data));
}
Wednesday, August 29, 2012
ImageView from URL
public class ImageLoadActivity extends Activity{
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout m_linear = new LinearLayout(this);
m_linear.setOrientation(LinearLayout.VERTICAL);
ImageView i = new ImageView(this);
m_linear.addView(i);
new DownloadImage(i).execute("http://www.mobisights.com/wp-content/uploads/2012/08/001028312.jpg");
setContentView(m_linear);
}
private class DownloadImage extends AsyncTask<String, Void, Bitmap> {
ImageView image;
public DownloadImage(ImageView image) {
this.image = image;
}
protected Bitmap doInBackground(String... urls) {
String urldisplay = urls[0];
Bitmap bitmap = null;
try {
InputStream in = new java.net.URL(urldisplay).openStream();
bitmap = BitmapFactory.decodeStream(in);
} catch (Exception e) {
Log.e("Error", e.getMessage());
e.printStackTrace();
}
return bitmap;
}
protected void onPostExecute(Bitmap result) {
image.setImageBitmap(result);
}
}
}
Find UIViewController from UIView (iphone)
- (UIViewController*)viewController
{
for (UIView* next = [self superview]; next; next = next.superview)
{
UIResponder* nextResponder = [next nextResponder];
if ([nextResponder isKindOfClass:[UIViewController class]])
{
return (UIViewController*)nextResponder;
}
}
return nil;
}
Tuesday, August 28, 2012
UIButton - Programmetically (iphone)
#import "ViewController.h"
@interface ViewController ()
@end
@implementation ViewController
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
UIButton *myButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
myButton.frame = CGRectMake((320-100)/2, (460-44)/2, 100, 44);
[myButton setTitle:@"Click Me" forState:UIControlStateNormal];
[myButton addTarget:self action:@selector(buttonClicked:) forControlEvents:UIControlEventTouchUpInside];
[self.view addSubview:myButton];
myButton.tag = 100;
}
-(void)buttonClicked:(id)sender
{
NSArray *subviews = [self.view subviews];
if ([subviews count] == 0) return;
for (UIView *subview in subviews) {
NSLog(@"%@", subview);
}
}
- (void)viewDidUnload
{
[super viewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
@end
Monday, August 27, 2012
Rotate Image with SeekBar - Programmetically
public class ImageLoadActivity extends Activity implements OnSeekBarChangeListener{
private ImageView i;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout m_linear = new LinearLayout(this);
m_linear.setOrientation(LinearLayout.VERTICAL);
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.akb48);
Matrix matrix = new Matrix();
matrix.postRotate(0);
SeekBar bar = new SeekBar(this);
bar.setOnSeekBarChangeListener(this);
m_linear.addView(bar);
i = new ImageView(this);
rotateImage(0);
m_linear.addView(i);
setContentView(m_linear);
}
private void rotateImage(int degree)
{
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.akb48);
Matrix matrix = new Matrix();
matrix.postRotate(degree);
Bitmap rotated = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
i.setImageBitmap(rotated);
}
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser)
{
rotateImage((int)Math.round(progress*3.6));
}
public void onStartTrackingTouch(SeekBar arg0) {
}
public void onStopTrackingTouch(SeekBar arg0) {
}
}
Sunday, August 26, 2012
ImageView - rotate image
public class ImageLoadActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout m_linear = new LinearLayout(this);
m_linear.setOrientation(LinearLayout.VERTICAL);
Bitmap image = BitmapFactory.decodeResource(getResources(), R.drawable.akb48);
Matrix matrix = new Matrix();
matrix.postRotate(30);
Bitmap rotated = Bitmap.createBitmap(image, 0, 0, image.getWidth(), image.getHeight(), matrix, true);
ImageView i = new ImageView(this);
i.setImageBitmap(rotated);
m_linear.addView(i);
setContentView(m_linear);
}
}
ImageView - Programmetically
public class ImageLoadActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout m_linear = new LinearLayout(this);
m_linear.setOrientation(LinearLayout.VERTICAL);
ImageView i = new ImageView(this);
i.setImageResource(R.drawable.akb48);
i.setPadding(50, 50, 50, 50);
m_linear.addView(i,newLayoutParams(
LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
//m_linear.addView(i);
setContentView(m_linear);
}
}
Add Event to Button - onClickListener
public class ButtonClickActivity extends Activity {
private Button m_button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout m_linear = new LinearLayout(this);
m_linear.setOrientation(LinearLayout.VERTICAL);
m_button = new Button(this);
m_button.setText("Hello World");
m_button.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0) {
m_button.setText("click");
}
});
m_linear.addView(m_button);
setContentView(m_linear);
}
}
private Button m_button;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout m_linear = new LinearLayout(this);
m_linear.setOrientation(LinearLayout.VERTICAL);
m_button = new Button(this);
m_button.setText("Hello World");
m_button.setOnClickListener(new Button.OnClickListener(){
public void onClick(View arg0) {
m_button.setText("click");
}
});
m_linear.addView(m_button);
setContentView(m_linear);
}
}
Add a Button - Programmetically
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
public class HelloActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout m_linear = new LinearLayout(this);
m_linear.setOrientation(LinearLayout.VERTICAL);
Button m_button = new Button(this);
m_button.setText("Hello World");
m_linear.addView(m_button);
setContentView(m_linear);
}
}
import android.os.Bundle;
import android.widget.Button;
import android.widget.LinearLayout;
public class HelloActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout m_linear = new LinearLayout(this);
m_linear.setOrientation(LinearLayout.VERTICAL);
Button m_button = new Button(this);
m_button.setText("Hello World");
m_linear.addView(m_button);
setContentView(m_linear);
}
}
Subscribe to:
Posts (Atom)