How to implement click the certain area to do to action? - android

This is an example of facebook sliding menu.
When sliding, there is 20% space user can see which is similar with facebook. Facebook implements it with clicking anywhere of this 20% and the menu is slide back.
How to implement this?

One way of doing that is as following with OnTouchListener on your activity. You can detect exactly where is touched on the screen.
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.Toast;
public class AndroidTestActivity extends Activity implements OnTouchListener {
LinearLayout main;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
main = (LinearLayout) findViewById(R.id.main_layout);
main.setOnTouchListener(this); // you need to set the touch listener for your view. And every element around the detection area.
}
public boolean onTouch(View v, MotionEvent e) {
if(e.getX() <= main.getWidth() / 5) {
Toast.makeText(this, "In the %20..", Toast.LENGTH_SHORT).show();
return true;
}
return false;
}
}
You also need to id your main layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

Related

Creatig a pop up menu when an image clicked Android

I'm trying to show a menu when I clicked on an image.
This is my full_screen_image.xml.I want to show when I clicked on #+id/photo_menu.
<ImageView
android:contentDescription="verter"
android:id="#+id/photo_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_more_vert_black_30dp"
android:tint="#android:color/white"
android:layout_marginTop="6dp"
android:layout_marginEnd="5dp"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"/>
</RelativeLayout>
This is menu_photo.xml.
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/savePhoto"
android:title="Kaydet" />
<item android:id="#+id/sharePhoto"
android:title="Paylaş" />
</menu>
This is fullScreenImageActivity.java:
package com.example.ahmetbesli.circles;
import android.content.Intent;
import android.net.Uri;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class FullScreenImageActivity extends AppCompatActivity {
private ImageView arrowBack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_full_screen_image);
arrowBack = (ImageView) findViewById(R.id.arrow_back);
arrowBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
ImageView fullScreenImageView = (ImageView) findViewById(R.id.fullScreenImageView);
Intent callingActivityIntent = getIntent();
if (callingActivityIntent != null) {
Uri imageUri = callingActivityIntent.getData();
if (imageUri != null && fullScreenImageView != null) {
Glide.with(this)
.load(imageUri)
.into(fullScreenImageView);
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu){
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_photo,menu);
return true;
}
}
I couldn't understand how to connect that image and the menu. Thanks for your helps...
Set a click listener to the image and in onclick method for that image view create any of the below 4 entities based on your requirement.
You have many options in your use case two of them would be.
1.Toast(only shows message no click events)
2.SnackBar (dismisses itself after some time, prefer this if you want user to respond quickly with some click action)
3.Using AlertDialog
4.Dialog Fragment
and in the both of the above methods(3 & 4) you can reference your menu items for click events or describe your own layout for dialog box and define buttons in those layouts for click events.
creation steps documentation: Android Dialogs
Dialog offers more functionalities and stays there till user closes it unlike toast which dismisses after some time if no action is taken.

Android Google Map API V2 - Buttons above fragment

I have managed to get a map to show on the screen. I am trying to create buttons above the map and then add actions to this. E.g. the button will change the location of the map. I am basically trying to adapt my code to this: https://www.youtube.com/watch?v=awX5T-EwLPc. However, I am using "extends Fragment Activity" which messes everything up.
My problem: I cannot create a button above the map, it is placed inside the map. I am trying to create a button above the map, and assign code so I can move to a location on the map etc. Here is my code:
MainActivity.java
package com.example.theapp;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener
{
Button button;
TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
text=(TextView)findViewById(R.id.textView1);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text.setText("The button worked!");
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.SupportMapFragment" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/map"
android:layout_alignTop="#+id/map"
android:text="Button" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/button1"
android:layout_alignBottom="#+id/button1"
android:layout_alignRight="#+id/map"
android:layout_marginRight="39dp"
android:text="TextView" />
</RelativeLayout>
From the code, my app displays a button on the map and a text view. When you click the button, its meant to change the text, but it doesn't do anything. I have researched tutorials and by the looks of it, I will have to create multiple fragments?
Question: How would I be able to create a button on that page, that will then pin point the user on the map?
Thank you in advance.
You are not setting any functionality to the Button... You should use
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
text.setText("that's how you do it!");
}
});
You need not to worry about "FragmentActivity" until and unless you use fragment class.
Just use this as normal Activity. it will work well.
package com.example.theapp;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends FragmentActivity implements OnClickListener
{
Button button;
TextView text;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button=(Button)findViewById(R.id.button1);
button.setOnClickListener(this);
text=(TextView)findViewById(R.id.textView1);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
text.setText("The button worked!");
}
}

Android multi stroke gesture not working

I modified an existing app,GestureBuilder,to accept multiple strokes.Hence to accept the letter A,D, F and so on. I created a simple app which recognises if the letter is correct or not.My problem is:
1.I have successfully recorded multi stroke letter in a file called gesture with gestureBuilder.
2.My application does not accept multi stroke gesture.But i have set fadeoffset to 1000 and gesturestroketype as multiple.I do not know why this is happening.The moment I enter next xtroke to write capital A my previous stroke dissapears.I cant write multi stroke letters/symbols.
Code:
xml file:
<LinearLayout 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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<android.gesture.GestureOverlayView
android:id="#+id/gestures_overlay"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1.0"
android:fadeOffset="5000"
android:eventsInterceptionEnabled="true"
android:gestureStrokeType="multiple" />
</LinearLayout>
java file:
package com.example.gesture_letter;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.gesture.Gesture;
import android.gesture.GestureLibraries;
import android.gesture.GestureLibrary;
import android.gesture.GestureOverlayView;
import android.gesture.GestureOverlayView.OnGestureListener;
import android.gesture.GestureOverlayView.OnGesturePerformedListener;
import android.gesture.Prediction;
import android.graphics.Color;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Toast;
public class MainActivity extends Activity implements OnGesturePerformedListener {
GestureLibrary mLibrary;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GestureOverlayView gestureoverlayview=new GestureOverlayView(this);
View inflate=getLayoutInflater().inflate(R.layout.activity_main,null);
gestureoverlayview.setGestureColor(Color.rgb(0, 255, 0));
gestureoverlayview.setGestureVisible(true);
gestureoverlayview.setUncertainGestureColor(Color.rgb(0,0,255));
gestureoverlayview.addView(inflate);
gestureoverlayview.addOnGesturePerformedListener(this);
mLibrary=GestureLibraries.fromRawResource(this,R.raw.gestures);
if(!mLibrary.load())
{
Log.i("debug","FAILED");
}
else
{
Log.i("debug","LOADED");
}
Log.i("debug","OnCreate");
setContentView(gestureoverlayview);
}
#Override
public void onGesturePerformed(GestureOverlayView arg0, Gesture arg1) {
// TODO Auto-generated method stub
ArrayList<Prediction> predictions=mLibrary.recognize(arg1);
if(predictions.size()>0)
{
Prediction prediction=predictions.get(0);
if(prediction.score>1.0)
{
String s=prediction.name;
Toast.makeText(getApplicationContext(), s,Toast.LENGTH_LONG).show();
}
else
if(prediction.score<1.0)
{
Toast.makeText(getApplicationContext(), "None",Toast.LENGTH_LONG).show();
}
}
}
}
Thank you.
I also would like a good resource for gesture application building.thank you
I do not know why, android:GestureStrokeType="multiple" did not work. It struck me to try it from the java side. I set :
gestureoverlayview.setGestureStrokeType(GestureOverlayView.GESTURE_STROKE_TYPE_MULTIPLE);
gestureoverlayview is an object of GestureOverlayView(you can name it anything)
(in main_activity.java file for newbies)
Then you'll see it works.
I think its a bug, I'm not sure.I spent weeks hovering on this :(
The problem is that you create a new GestureOverlayView :
GestureOverlayView gestureoverlayview=new GestureOverlayView(this);
You should call the view defined in the xml with findViewById(R.id.gesture_overlay);

move the upper image layout with animation to get down image click

I have two images overlapping each other, i am animating(sliding) the above image to the right of the screen up to some part of it remaining on the screen so that i can animate it back to its original position, Now i want to get click of the below image to start my new activity but i am getting the click of above image only, and i know that animating the upper image will not move its layout.
But i have tried to move the layouts of upper image but after moving the layouts the above image disappears.
Any help?
After translating the above image,set its clickability to false,like
aboveImage.setClickable(false);
Try the below code ,it will work
AnimationActivity.java:
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.ImageView;
public class AnimationActivity extends Activity {
ImageView aboveImage;
ImageView belowImage;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
aboveImage=(ImageView) findViewById(R.id.image_above);
belowImage=(ImageView) findViewById(R.id.image_below);
aboveImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
/*belowImage.setVisibility(View.VISIBLE);
overridePendingTransition(R.anim.move_to_right,R.anim.move_to_left);*/
Animation animation = new TranslateAnimation(0, 100,0, 0);
animation.setDuration(1000);
animation.setFillAfter(true);
aboveImage.startAnimation(animation);
aboveImage.setVisibility(0);
aboveImage.setClickable(false);
}
});
belowImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
startActivity(new Intent(getApplicationContext(),Activity2.class));
}
});
}
}
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image_below"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/cherry" />
<ImageView
android:id="#+id/image_above"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/amalapaul" />
</RelativeLayout>

Pop-Up Window at inital start up, of android application

I am trying to find a code that will do a popup at the initial start up on an installed app. Much like a changelog that is starting to appear in more and more apps.
I have found some similar codes, but being a beginner I haven't been able to figure out where to exactly put the code in and I always have tons of errors that still do not work once I try and fix them.
I am working in Eclipse with an android project, and I'm using a webview to show a website.
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">
<WebView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/webview"
android:layout_height="fill_parent" android:layout_width="fill_parent" android:scrollbarAlwaysDrawVerticalTrack="false"/>
</LinearLayout>
Java File:
package com.A2Ddesigners.WhatThe;
import android.app.Activity;
import android.os.Bundle;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.view.KeyEvent;
public class Whatthe extends Activity {
WebView webview;
/** Called when the activity is first created. */
#Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if ((keyCode == KeyEvent.KEYCODE_BACK) && webview.canGoBack()) {
webview.goBack();
return true;
}
return super.onKeyDown(keyCode, event);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
webview = (WebView) findViewById(R.id.webview);
webview.setWebViewClient(new HelloWebViewClient());
webview.getSettings().setJavaScriptEnabled(true);
webview.setInitialScale(50);
webview.getSettings().setUseWideViewPort(true);
webview.loadUrl("http://mdsitest2.com/");
}
private class HelloWebViewClient extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
}
Using My this code you can show popup on any type of view on intial time of activity.
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.PopupWindow;
public class PopupDispaly extends Activity {
private PopupWindow outComePopup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tiwter_login);
final View anyView = findViewById(R.id.popo_up); // define heade your view
anyView.postDelayed(new Runnable() {
#Override
public void run() {
callFirstToolTip(anyView);
}
}, 5000); // hear you can put your delay time like 5000 mls
}
public void callFirstToolTip(View view) {
Log.i("Started Info","popup");
View layout = LayoutInflater.from(PopupDispaly.this).inflate(R.layout.popup_copy_delete, null); // define hear your layout file id.
LinearLayout layCopyDelete = (LinearLayout) layout.findViewById(R.id.layCopyDelete);// hear define your id of sub lay like LinearLayout.
outComePopup = new PopupWindow(layout);
outComePopup.setFocusable(true);
layCopyDelete.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
outComePopup.setWidth(layCopyDelete.getMeasuredWidth());
outComePopup.setHeight(layCopyDelete.getMeasuredHeight());
outComePopup.setBackgroundDrawable(getResources().getDrawable(android.R.color.transparent));
outComePopup.setOutsideTouchable(true);
outComePopup.showAsDropDown(view);
}
}
And my Custome View layout file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/got_it_bg"
android:layout_gravity="center_horizontal"
android:id="#+id/layCopyDelete"
android:layout_marginLeft="100dp"
android:orientation="horizontal">
</LinearLayout>
Sounds like you should use a custom dialog box. http://developer.android.com/guide/topics/ui/dialogs.html
There's a code example at the bottom of the page. To show it, you can call onCreateDialog(int) from inside your main activity in its onCreate() method.
PopupWindow.showAsDropDown() in a delayed process can solve this problem, you can execute the show popupWindow in onCreate() method by 0.5s delay.
You should use PopupWindow.showAtLocation but use the post method of the one of the views on your activity. you can also use the root view of the activity using findViewById(android.R.id.content).
You code should look like this:
View anyView = findViewById(R.id.anyView);
anyView.post(new Runnable()
{
#Override
public void run()
{
// Create and show PopupWindow
}
});

Categories

Resources