android camera intent use another activity - android

actually i'm trying to use camera action(like a 'take a photo') in another activity.
i mean. i got a activity(CameraPreviewActivity). it shows CameraPreview.
if user put the 'draw' button. and then new activity(CanvasActivity) starts. and it show a transparent Canvas.
so user can draw on the Camera preview.
all i want to do is make a 'take a picture' button in the CanvasActivity.
actually its a different activity so i couldn't make a 'take a picture' button in the CavasActivity.
so i use intent.putExtra method to send Camera information. but it's not working at all.
how can i control the CameraPreviewActivity at CanvasActivity.
is my idea wrong ? or is there any good way to figure out this problem.
here's my code.
CameraPreviewActivity.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ex1);
cameraObject = isCameraAvailiable();
showCamera = new ShowCamera(this, cameraObject);
setCameraDisplayOrientation(this, 0, cameraObject);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(showCamera);
}
public void snapIt(View view) {
cameraObject.takePicture(null, null, capturedIt);
}
public void drawIt(View view) {
Intent intent = new Intent(this, DrawingActivity.class);
startActivity(intent);
}
and it's XML.
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
<Button
android:id="#+id/button_capture"
android:layout_width="136dp"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:onClick="snapIt"
android:text="#string/Capture" />
<Button
android:id="#+id/button_draw"
android:layout_width="126dp"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:onClick="drawIt"
android:text="#string/Draw" />
</FrameLayout>
CanvasActivity.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ex2);
findViewById(R.id.myDrawing).setBackgroundColor
(getResources().getColor(android.R.color.transparent));
FrameLayout layout = (FrameLayout) findViewById(R.id.drawingView);
mView = new DrawingView(this);
layout.addView(mView, new LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
init();
}
and it's XML.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myDrawing"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:color="#00000000" >
<FrameLayout
android:id="#+id/drawingView"
android:layout_width="318dp"
android:layout_height="match_parent"
android:layout_gravity="bottom"
android:layout_weight="0.99" >
</FrameLayout>
</LinearLayout>
thanks a lot !!

how can i control the CameraPreviewActivity at CanvasActivity
You don't. Allow the user to take a picture by some means on the CanvasActivity, such as:
via a button, as you originally proposed
via an action bar item
via clicking on the preview itself
via long-clicking on the preview itself
via some other sort of gesture
etc.

Related

Displaying internal intent within a fragmentnot working

I have a code to call the intent to display the running services screen. But i wanted it to be displayed within a fragment. But am not able to do that. I call this intent from the java class "bottompage.java" which is the fragment for bottom half of the page. But still when the button is clicked this intent occupies the full screen.
Activity main:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.app_display);
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.fragmentContainer1, new TopPage());
fragmentTransaction.add(R.id.fragmentContainer2, new BottomPage());
fragmentTransaction.commit();
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
Bottompage.java
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View secondview = inflater.inflate(R.layout.bottom_fragment,container,false);
Button dummy = (Button) secondview.findViewById(R.id.dummy);
dummy.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
final String APP_DETAILS_PACKAGE_NAME ="com.android.settings";
// Here you need to define the package name
final String SCREEN_CLASS_NAME ="com.android.settings.RunningServices";
Intent servicesintent = new Intent();
servicesintent.setAction(Intent.ACTION_VIEW);
//intent.setAction(Intent.ACTION_VIEW);
servicesintent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
servicesintent.setClassName(APP_DETAILS_PACKAGE_NAME, SCREEN_CLASS_NAME);
BottomPage.this.getActivity().startActivity(servicesintent);
}
});
return secondview;
}
Main.xml
The xml file for 2 fragments
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<TextView
android:layout_width="157dp"
android:layout_height="wrap_content"
android:text="Second page"
android:id="#+id/tv2"
android:layout_gravity="center_horizontal"
android:layout_weight="0.06" />
<FrameLayout
android:id ="#+id/fragmentContainer1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="center_horizontal"
android:layout_weight="0.06"></FrameLayout>
<FrameLayout
android:id ="#+id/fragmentContainer2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_below="#+id/fragmentContainer1"></FrameLayout>
The xml file for the bottom fragment
BottomPage.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/bottom_fragment">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="call services"
android:id="#+id/dummy"
android:layout_gravity="center_horizontal" />
How do i make the intent "servicesintent" in bottompage.java to display the running services within the fragment "fragmentcontainer2". Currently its taking full screen.
I am not shure what you exactly want
I use this code to display the Intent that activated my Fragment in the Log.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
...
Activity parent = this.getActivity();
Intent intent = (parent == null) ? null : parent.getIntent();
if (intent != null) {
Log.d(Global.LOG_CONTEXT, "onCreateView " +
intent.toUri(Intent.URI_INTENT_SCHEME));
}
Instead of adding it to the Log you can set the value to a textview.
How do i make the intent "servicesintent" in bottompage.java to display the running services within the fragment "fragmentcontainer2".
You don't. You cannot embed third-party UIs inside your own, except in very specific circumstances (e.g., app widgets and a home screen).
Also note that there is no requirement that any device have a com.android.settings.RunningServices activity in com.android.settings that is exported.

How to make a button redirect to another XML layout

I'm making a button in xml(res / layout / activity_home.xml), like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity" >
<ImageView
android:id="#+id/imageView1"
android:src="#drawable/schkopwide"
android:contentDescription="#string/HTI"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="Intent i = new Intent(activity_store.xml);
startActivity(i);"
android:text="#string/HTI" />
</RelativeLayout>
so what should I add into this xml to let it redirect to another xml page (res / layout / activity_store.xml)?
Thank you
If you Want to show two different layouts in Same Activity, then ViewSwitcher is best layout.
You can add multiple layouts in ViewSwithcher. And Replace them by using viewswitcher.next(); function.
<ViewSwitcher
android:id="#+id/viewswitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<!-- Add Two View’s Here -- >
</ViewSwitcher>
You can take reference from this link: http://abhiandroid.com/ui/viewswitcher
Try out as below:
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="78dp"
android:onclick="start"
android:text="#string/HTI" />
In your main activity :
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
Here is your ActivityStore Class code:
public class ActivityStore extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
}
Also add the activity into your mainfest file.
<activity
android:name=".ActivityStore"
android:label="#string/app_name"/ >
You can't add the launch of an Intent inside the onclick parameter in XML. You have to do it by code.
In your code:
Button button = (Button)findViewById(R.id.button1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Intent i = new Intent(this, ActivityStore.class);
startActivity(i);
}
});
And in the OnCreate of the ActivityStore class, put this
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_store);
}
NOTE: I supposed that yous activity_store class is called ActivityStore
You need to check on Android documentation :
Activity
OnClickListener
Intent
http://developer.android.com/training/index.html
Good luck.
Try this,
Statically include XML layouts inside other XML layouts. use include. Add the below code in your activity_store.xml
<include layout="#layout/activity_home"/>
Sure you will get solution.
A simple way would be to create an Activity with another xml attached to it and then use intent.

Change SurfaceView Image Depending On Thumbnail

So far I have an Activity where there are a selection of thumbnails, once a thumbnail is clicked it opens up the Camera Activity with the SurfaceView which is being set in the XML below:
I need a way to be able to change the SurafceView depending on which thumbail was choosen in the previous Activity I have the thumbnails set up as buttons. I have given each button the same ID as the image name it should use on the SurfaceView so is there a way to take the button ID and change the RelativeLayout background below.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/overlay"
android:gravity="bottom"
android:orientation="vertical" >
<Button
android:id="#+id/takepicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_margin="10px"
android:layout_marginBottom="10dp"
android:background="#drawable/capture" />
</RelativeLayout>
Java:
View viewControl = controlInflater.inflate(R.layout.control, null);
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
When starting up the Camera activity, put the ID of the background you want into the intent's extras bundle like so:
intent.putExtra("backgroundId", backgroundId);
startActivity(intent);
In the second activity you retrieve the background ID from your intent and assign it to the background of the root view:
#Override
public void onCreate(Bundle bundle) {
...
int backgroundId = getIntent().getIntExtra("backgroundId", 0);
viewControl.setBackgroundResouce(backgroundId);
...
}

Move an image dynamically in android

This is the oncreate from my application.
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
getWindow().setFormat(PixelFormat.UNKNOWN);
surfaceView = (SurfaceView)findViewById(R.id.camerapreview);
surfaceHolder = surfaceView.getHolder();
surfaceHolder.addCallback(this);
surfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
controlInflater = LayoutInflater.from(getBaseContext());
viewControl = controlInflater.inflate(R.layout.control, null);
image =(ImageView) viewControl.findViewById(R.id.img);
LayoutParams layoutParamsControl
= new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT);
this.addContentView(viewControl, layoutParamsControl);
}
control.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
>
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_launcher"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
/>
</LinearLayout>
How can I move this image on a button click.
Actually i want to transalate this image from the current pixel position to another.
I tried this on the button click.
b =(Button)findViewById(R.id.button);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Button clicked");
TranslateAnimation anim = new TranslateAnimation(0,0,200,200);
anim.setDuration(2000);
image.setAnimation(anim);
}
});
but now the image will disappear for two seconds. What am I doing wrong.
please any one help.
Actually i want to move the image on accelerometer change.
If you are using Animation then your Image is back to the start position after it completes the animation.
If you want to achieve like this then just increase Animation duration,
Or If you want to scale image one place to another without animation,
Remove animation and just Use on your button's click method,
ImageView.scrollBy(scrollByX, scrollByY);
For more info look at Android: Scrolling an Imageview.

How to use the xml setting in a view of a activity?

I want to show two views in one activity. If I clicked on button in the first view I want to see the second and other way round.
The views should not have the same size as the screen so I want e.g. to center it, like you see in first.xml.
But if I add the views with
addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
the views are not centered. They are shown at top left.
How can I use the xml settings to e.g. center it?
first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#drawable/background"
android:layout_gravity="center"
android:minWidth="100dp"
android:minHeight="100dp"
android:paddingBottom="5dp"
>
<LinearLayout android:id="#+id/head"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton android:id="#+id/first_button"
android:src="#drawable/show_second"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null" />
</LinearLayout>
second.xml same as first.xml but with
<ImageButton android:id="#+id/second_button"
android:src="#drawable/show_first"
... />
ShowMe.java
public class ShowMe extends Activity {
View mFirstView = null;
View mSecondView = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initFirstLayout();
initSecondLayout();
showFirst();
}
private void initFirstLayout() {
LayoutInflater inflater = getLayoutInflater();
mFirstView = inflater.inflate(R.layout.first, null);
getWindow().addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton firstButton = (ImageButton)mMaxiView.findViewById(R.id.first_button);
firstButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ShowMe.this.showSecond();
}
});
}
private void initSecondLayout() {
// like initMaxiLayout()
}
private void showFirst() {
mSecondView.setVisibility(View.INVISIBLE);
mFirstView.setVisibility(View.VISIBLE);
}
private void showSecond() {
mFirstView.setVisibility(View.INVISIBLE);
mSecondView.setVisibility(View.VISIBLE);
}}
Hope someone can help.
Thanks
Why don't you use setContentView(R.layout.yourlayout)? I believe the new LayoutParams you're passing in addContentView() are overriding those you defined in xml.
Moreover, ViewGroup.LayoutParams lacks the layout gravity setting, so you would have to use the right one for the layout you're going to add the view to (I suspect it's a FrameLayout, you can check with Hierarchy Viewer). This is also a general rule to follow. When using methods that take layout resources as arguments this is automatic (they might ask for the intended parent).
With this consideration in mind, you could set your layout params with:
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(/* wrap wrap */);
lp.setGravity(Gravity.CENTER);
addContentView(mYourView, lp);
But I would recommend setContentView() if you have no particular needs.
EDIT
I mean that you create a layout like:
~~~/res/layout/main.xml~~~
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="....."
android:id="#+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
then in your onCreate() or init...Layout():
setContentView(R.layout.main);
FrameLayout mainLayout = (FrameLayout)findViewById(R.id.mainLayout);
// this version of inflate() will automatically attach the view to the
// specified viewgroup.
mFirstView = inflater.inflate(R.layout.first, mainLayout, true);
this will keep the layout params from xml, because it knows what kind it needs. See reference.

Categories

Resources