Adding a Camera to my Android App and Initializing it on application start up - android-camera

I'm pretty new to programming but here is a code I have. I'm trying to develop an app and I want it to open the camera feature right away. This is what I'm working with. It's asking me to add more details so I figure I should type more... Any help would be appreciated. This is my only grade for a class that is taught very poorly
import android.content.Context;
import android.hardware.Camera;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
public class CameraSurfaceView extends SurfaceView implements SurfaceHolder.Callback {
private SurfaceHolder sHolder;
public Camera camera= null;{
}
public CameraSurfaceView(Context context) {
super(context);
sHolder= getHolder();
sHolder .addCallback(this);
// TODO Auto-generated constructor stub
}
#Override
public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
Camera.Parameters params= camera.getParameters();
params.setPreviewSize(width, height);
camera.setParameters(params);
camera.startPreview();
}
#Override
public void surfaceCreated(SurfaceHolder holder) {
camera = Camera.open();
try{
camera.setPreviewDisplay(sHolder);
}catch(Exception e){
}
// TODO Auto-generated method stub
}
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
camera.stopPreview();
camera= null;
}
public void capture (Camera.PictureCallback jpegHandler){
camera.takePicture(null, null, jpegHandler);
}
}

Related

How to edit the tree viewer element based on context menu selection

I want to edit the tree viewer element based on my context menu option. Basically i need to update the element value which is displayed. If i double click on tree viewer element i was able to update the value, but through context menu also i should be able to do.
Sample code for adding the context menu:
protected def void createContextMenu(Viewer viewer) {
val MenuManager contextMenu = new MenuManager("Menu"); // $NON-NLS-1$
contextMenu.setRemoveAllWhenShown(true);
contextMenu.addMenuListener(new IMenuListener() {
public override void menuAboutToShow(IMenuManager mgr) {
fillContextMenu(mgr);
}
});
val Menu menu = contextMenu.createContextMenu(viewer.getControl());
viewer.getControl().setMenu(menu);
}
/**
* Fill dynamic context menu
*
* #param contextMenu
*/
protected def void fillContextMenu(IMenuManager contextMenu) {
contextMenu.add(new GroupMarker(IWorkbenchActionConstants.MB_ADDITIONS));
contextMenu.add(new Action("Rename") {
public override void run() {
val selectedElement = (treeViewer.selection as IStructuredSelection).firstElement
}
});
}
See if this helps or let me know otherwise
package myFolder;
import org.eclipse.jface.viewers.ILabelProvider;
import org.eclipse.jface.viewers.ILabelProviderListener;
import org.eclipse.jface.viewers.ITreeContentProvider;
import org.eclipse.jface.viewers.TreeViewer;
import org.eclipse.jface.viewers.TreeViewerColumn;
import org.eclipse.jface.viewers.Viewer;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.MenuEvent;
import org.eclipse.swt.events.MenuListener;
import org.eclipse.swt.graphics.Image;
import org.eclipse.swt.layout.FillLayout;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Menu;
import org.eclipse.swt.widgets.MenuItem;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.TreeItem;
public class trying {
public static void main(String[] args){
Display display = new Display();
Shell shell = new Shell(display);
shell.setLayout(new FillLayout());
shell.setSize(300,300);
TreeViewer viewer = new TreeViewer(shell);
viewer.getTree().setHeaderVisible(true);
viewer.getTree().setLinesVisible(true);
viewer.setContentProvider(new ITreeContentProvider () {
#Override
public void dispose() {
// TODO Auto-generated method stub
}
#Override
public void inputChanged(Viewer arg0, Object arg1, Object arg2) {
// TODO Auto-generated method stub
}
#Override
public Object[] getChildren(Object arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public Object[] getElements(Object arg0) {
// TODO Auto-generated method stub
return new String[]{"Hoshe","Irani"};
}
#Override
public Object getParent(Object arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public boolean hasChildren(Object arg0) {
// TODO Auto-generated method stub
return false;
}
});
viewer.setLabelProvider(new ILabelProvider(){
#Override
public void addListener(ILabelProviderListener arg0) {
// TODO Auto-generated method stub
}
#Override
public void dispose() {
// TODO Auto-generated method stub
}
#Override
public boolean isLabelProperty(Object arg0, String arg1) {
// TODO Auto-generated method stub
return false;
}
#Override
public void removeListener(ILabelProviderListener arg0) {
// TODO Auto-generated method stub
}
#Override
public Image getImage(Object arg0) {
// TODO Auto-generated method stub
return null;
}
#Override
public String getText(Object arg0) {
// TODO Auto-generated method stub
return "hoshe";
}
});
viewer.setInput("Hoshe");
Menu popupMenu = new Menu(viewer.getControl());
MenuItem newItem = new MenuItem(popupMenu, SWT.CHECK);
newItem.setText("New");
MenuItem refreshItem = new MenuItem(popupMenu, SWT.CHECK);
refreshItem.setText("Refresh");
MenuItem deleteItem = new MenuItem(popupMenu, SWT.CHECK);
deleteItem.setText("Delete");
popupMenu.addMenuListener(new MenuListener() {
#Override
public void menuShown(MenuEvent arg0) {
// TODO Auto-generated method stub
}
#Override
public void menuHidden(MenuEvent arg0) {
// TODO Auto-generated method stub
display.asyncExec(new Runnable() {
#Override
public void run() {
// TODO Auto-generated method stub
TreeItem [] arr = viewer.getTree().getSelection();
for(MenuItem item2 : popupMenu.getItems()){
boolean bool1 = item2.getSelection();
if(bool1){
arr[0].setText(item2.getText());
item2.setSelection(false);
}
}
}
});
}
});
viewer.getTree().setMenu(popupMenu);
shell.open();
while(!shell.isDisposed()){
if(!display.readAndDispatch())
display.sleep();
}
display.dispose();
}
}

How to call a class from another activity?

I want to call MainActivity class to another activity. This is my code for the MainActivity.java:
package com.blinkedup.geolocationchat;
import android.location.Criteria;
import android.location.Location;
import android.location.LocationListener;
import android.location.LocationManager;
import android.os.Bundle;
import android.app.Activity;
import android.app.Service;
import android.view.Menu;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
TextView textView;
LocationManager locationManager;
MyLocationListener locationListener = new MyLocationListener();
Criteria criteria;
String bestProvider;
String listOfBestProviders;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
criteria = new Criteria();
textView = (TextView) findViewById(R.id.textView1);
criteria.setAccuracy(Criteria.ACCURACY_COARSE);
criteria.setPowerRequirement(Criteria.POWER_LOW);
criteria.setAltitudeRequired(false);
criteria.setBearingRequired(false);
criteria.setCostAllowed(true);
locationManager = (LocationManager) getSystemService(Service.LOCATION_SERVICE);
//locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, locationListener);
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListener);
bestProvider = locationManager.getBestProvider(criteria, true);
Toast.makeText(getApplicationContext(), bestProvider, 3).show();
}
protected void onPause(){
super.onPause();
locationManager.removeUpdates(locationListener);
}
private class MyLocationListener implements LocationListener{
#Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
textView.setText("Latitude: " + location.getLatitude() +
"Longitude: " + location.getLongitude());
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
}
In another activity I wanted to call the lat and long of the above code but don't want to rewrite the code to the activity. I just want to call it and display the result in another activity. please help. thanks
make your variable static and define it before onCreate() method use that variable in another activity by call like this.
YourMainActivity.yourstaticvariable

How to add the SeekBar for resize the web-view text size Android?

SeekBar seekbar = (SeekBar)root.findViewById(R.id.seekBar);
seekbar.setOnSeekBarChangeListener(new OnSeekBarChangeListener() {
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
webview.getSettings().setTextZoom(progress);
webview.loadData(abst.getAbstract_detail(), "text/html", "UTF-8");
}
public void onStartTrackingTouch(SeekBar seekBar) {
// TODO Auto-generated method stub
}
public void onStopTrackingTouch(SeekBar seekBar) {
}
});

refresh listView by clicking outside of it

I have a viewpager with an adapter that extends FragmentStatePagerAdapter, this has to show some fragment that always repeats the same layout (in whic there is a listView) for every page ... now the problem is how can I update the listView that is currently visible by clicking on a button that is outside of the Fragment? I can refresh only the ones that are in the not visible pages,but not the ones that are displayed or adjacent...
One possible solution is to reset the adapter, but it doesn't seems to be right one, because it' s slow... could you please help me?
public class MainActivity extends FragmentActivity implements OnClickListener{
#Override
protected void onCreate(Bundle arg0) {
// TODO Auto-generated method stub
super.onCreate(arg0);
setContentView(R.layout.layout1);
pagerAdapter = new AdapterPager(getSupportFragmentManager());
ctx=this;
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(pagerAdapter);
pagerTab=(PagerTabStrip) findViewById(R.id.pager_tab_strip);
mViewPager.setCurrentItem(paginaCentrale);
modifica=(ImageView)findViewById(R.id.modifica);
modifica.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO onClick
switch (arg0.getId()){
case R.id.modifica:
//here I would like to call the method caricaLista() of the fragment
break;
}
}
public static class AdapterPager extends FragmentStatePagerAdapter {
public AdapterPager(FragmentManager fm) {
super(fm);
}
#Override
public int getItemPosition(Object object) {
// TODO Auto-generated method stub
return super.getItemPosition(object);
}
#Override
public Fragment getItem(int i) {
Fragment fragment;
Bundle args = new Bundle();
args.putInt("position", i);
fragment=new Fragment1();
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
return 1000;}
#Override
public CharSequence getPageTitle(int i) {
String title=String.valueOf(i);
return title;
}
}
public static class Fragment1 extends Fragment implements OnItemClickListener{
int fragmentPosition;
ListView lv;
List<Lista> lista= new ArrayList<Lista>();
public Fragment1(){
}
public static Fragment1 newInstance(){
return new Fragment1();
}
#Override
public void onResume() {
// TODO Auto-generated method stub
super.onResume();
caricaLista("")}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle
savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container, false);
lv=(ListView) rootView.findViewById(R.id.listViewCompiti);
Bundle args = getArguments();
int position= args.getInt("position");
fragmentPosition=position;
lv.setOnItemClickListener(this);
homeListAdapter= new HomeListAdapterWithCache(ctx,R.layout.list_item_home,lista);
lv.setAdapter(homeListAdapter);
return rootView;
}
You have to store the Current Fragment into your MainActivity , and change every time the page changes.
So you call the method of the current fragments.
Check out how your MainActivity should look like:
public class MainActivity extends FragmentActivity implements OnClickListener{
&Fragment mCurrentFragment;*
protected void onCreate(Bundle arg0) {
super.onCreate(arg0);
setContentView(R.layout.layout1);
pagerAdapter = new AdapterPager(getSupportFragmentManager());
ctx=this;
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(pagerAdapter);
pagerTab=(PagerTabStrip) findViewById(R.id.pager_tab_strip);
mViewPager.setCurrentItem(paginaCentrale);
modifica=(ImageView)findViewById(R.id.modifica);
modifica.setOnClickListener(this);
*mViewPager.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
mCurrentFragment = pagerAdapter.getItem(position);
}*
});
#Override
public void onClick(View arg0) {
// TODO onClick
switch (arg0.getId()){
case R.id.modifica:
*mCurrentFragment.caricaLista();*
break;
}
}
}

GWT RPC mechanism how to use non void return type

I have a scenario wherein I need to specify a return type to the Synchrnous function, the code is as follows :
#RemoteServiceRelativePath("show_box")
public interface ShowBoxCommandService extends RemoteService{
public ArrayList<String> showBox();
}
The implementation of the method on the server is :
public ArrayList<String> showBox() {
ArrayList<String> box = new ArrayList<String>();
Iterator<Box> boxes = BoxRegistry.getInstance().getBoxes();
while (boxes.hasNext()) {
box.add(boxes.next().toString());
}
return box;
}
I am trying to define the callback variable in the following format at the client side in order to call the method
AsyncCallback<Void> callback = new AsyncCallback<Void>() {
public void onFailure(Throwable caught) {
// TODO: Do something with errors.
// console was not started properly
}
#Override
public void onSuccess(Void result) {
// TODO Auto-generated method stub
// dialog saying that the console is started succesfully
}
};
update with the aync interface code :
public interface ShowBoxCommandServiceAsync {
void showBox(AsyncCallback<ArrayList<String>> callback);
}
But this is causing the definition of the method in the Async method to change.
Any ideas or clues will be helpful.
Thanks,
Bhavya
P.S. Apologies if this is a repetition
The callback should be:
AsyncCallback<ArrayList<String>> callback = new AsyncCallback<ArrayList<String>>() {
public void onFailure(Throwable caught) {
// TODO: Do something with errors.
// console was not started properly
}
#Override
public void onSuccess(ArrayList<String> result) {
// TODO Auto-generated method stub
// dialog saying that the console is started succesfully
}
};
If you don't need to utilize the result then you can ignore it, but if that is the case, you should probably question your design and why you would need the method to return an ArrayList<String> in the first place.
If the service interface looks like this:
public interface ShowBoxCommandService extends RemoteService {
public ArrayList<String> showBox();
}
then you must have an associated async interface:
public interface ShowBoxCommandServiceAsync {
public void showBox(AsyncCallback<ArrayList<String>> callback);
}
Which means, that the type of the callback that you should pass to showBox is AsyncCallback<ArrayList<String>>.
new AsyncCallback<ArrayList<String>>() {
#Override
public void onSuccess(ArrayList<String> list) {
// ...
}
#Override
public void onFailure(Throwable caught) {
// ...
}
}
Your callback should not be Void. If your synchronous method returns a List of Strings, the async callback method should receive the List. You'll have to use the ArrayList, because the class needs to implement the Serializable interface.
AsyncCallback<ArrayList<String>> callback = new AsyncCallback<ArrayList<String>>() {
public void onFailure(Throwable caught) {
// TODO: Do something with errors.
// console was not started properly
}
#Override
public void onSuccess(ArrayList<String> result) {
// TODO Auto-generated method stub
// dialog saying that the console is started succesfully
}
};
Huh? Your method returns an ArrayList and you are declaring void in your call?
Change <Void> to <ArrayList<String>>