i want to add a code where when a user tap the screen in his/her screen in android, there will be some dialog box showing that the screen is tapped. i tried few codings but it makes my project crash. any suggestions for coding? thank you.
Implement on the root view an OnClickListener interface.
//TestActivity
public class TestActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test);
findViewById(R.id.ll_root).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showDialog();
}
});
}
private void showDialog() {
new AlertDialog.Builder(this)
.setTitle("Screen tapped")
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
}
}
//Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/ll_root"
android:layout_height="match_parent"
android:orientation="vertical">
</LinearLayout>
Use onInterceptTouchEvent() method.
From documentation
Implement this method to intercept all touch screen motion events.
This allows you to watch events as they are dispatched to your children,
and take ownership of the current gesture at any point.
Example:
Related
I want to know how to get the same activity to show 2 layouts. I explain, I have an activity with a login, and within this same are 2 buttons (one called "login" and another "SingUp") the idea is that when you click on one of these 2 shows a different layout for each button, but without having to change all activity in one. I achieved it partially by adding the SetContentView to the Onclic of each button, and it works but only the first time, without the activity it starts with the login layout and then I click on the singUp button, if it changes layout but if later I want to return to the login layout I can not anymore I have to restart the app.What I want to achieve is to change layout in the same activity using buttons, whatever method they mention or they help me to know which part of my code is wrong :(Thank you.
public class MainActivity extends AppCompatActivity {
private Button btnLogin,btnSignup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnLogin=(Button)findViewById(R.id.btnLogin);
btnSignup=(Button)findViewById(R.id.btnSignup);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_main);
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_sign_up);
}
});
}
}
with the previous code and managed to alternate between 2 layouts in the same activity, but the buttons only work the first time if I try to go to a layout by pressing a button for the second time, this simply does not work until I restart the app.
Option 1
Try recreating the Activity. Save which view to load then recreate.
private Button btnLogin,btnSignup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if(preference.getInt(ACTIVITY)==1)
setContentView(R.layout.activity_main);
else
setContentView(R.layout.activity_sign_up);
btnLogin=(Button)findViewById(R.id.btnLogin);
btnSignup=(Button)findViewById(R.id.btnSignup);
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
preference.setInt(ACTIVITY, 1);
recreate();
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
preference.setInt(ACTIVITY, 2);
recreate();
}
});
}
Option 2
Use fragments. Create 2 fragments and inflate them into activity on onclick of button
Option 3
Use VISIBLE/GONE with predefined layouts inside activity_main
Create one layout with two separate views that match_parent's height and width.
Hide / show each one when switching views
<LinearLayout
android:id="#+id/parent"
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/main"
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical"
android:visibility="visibile">
<!-- Add child views here -->
</LinearLayout>
<LinearLayout
android:id="#+id/sign_up"
android:width="match_parent"
android:height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<!-- Add child views here -->
</LinearLayout>
</LinearLayout>
Then programmatically...
btnLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mainView.setVisibility(View.VISIBLE);
loginView.setVisibility(View.GONE);
}
});
btnSignup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mainView.setVisibility(View.GONE);
loginView.setVisibility(View.VISIBLE);
}
});
You can approach it in other ways using Fragments, which one could argue that's the more appropriate way.
Switch between two fragments
I am working on an app using geolocation and I want to set a layout VISIBLE when close to a point and GONE when too far from the point.
This is my xml :
<LinearLayout
android:visibility="gone"
android:id="#+id/slidePane"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
Then I use the ID in the activity as usual :
declare it in the class attribute :
private LinearLayout slidepane;
set it in the onCreate() method :
slidepane = (LinearLayout) findViewById(R.id.slidePane);
And then I try to update it in onLocationChanged() method as follows :
if (InterestPoint.CalculationByDistance(location, new LatLng(47.247801, -1.551883)) < 0.03) {
Toast.makeText(MainActivity.mContext, "InterestPoint close", Toast.LENGTH_SHORT).show();
slidepane.setVisibility(View.VISIBLE);
} else {
slidepane.setVisibility(View.GONE);
}
So the condition is correct since the Toast appears but the layout does not become visible, why?
Thank you.
EDIT : I am using https://github.com/umano/AndroidSlidingUpPanel and try to set the visibility of the second child (the sliding panel). I didn't see anything in the documentation about visibility. I can still access the children of the LinearLayout but not the container itself (and especialy the visibility).
Replace this:
slidepane.setVisibility(View.VISIBLE);
with this:
runOnUiThread(new Runnable() {
#Override
public void run() {
slidepane.setVisibility(View.VISIBLE);
}
});
Make sure you set the visibility of view at the time of initialization.
#Override
protected void onCreate(Bundle savedInstanceState) {
........
slidepane = (LinearLayout) findViewById(R.id.slidePane);
slidepane.setVisibility(View.GONE);
.......
//onLocationChanged() add ->
if (InterestPoint.CalculationByDistance(location, new LatLng(47.247801, -1.551883)) < 0.03) {
Toast.makeText(MainActivity.mContext, "InterestPoint close", Toast.LENGTH_SHORT).show();
slidepane.setVisibility(View.VISIBLE);
} else {
slidepane.setVisibility(View.GONE);
}
I'm wrinting an application for andorid and I have difficulties with change buttons background. I have a simple button and when the users click on the button change the background. I'd like to make the button to turn back into the original form when the user click on it again.
I have no idea how to do that, if anyone has one please response!
use the android.R.drawable.btn_default in order to change the button to default color
#Howlett Logan : You can try this way,
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextButton"
/>
Then
public boolean flag=true; // Global
Button buttonTest;
#Override
protected void onCreate(Bundle savedInstanceState)
{
buttonTest=(Button)findViewById (R.id.button);
buttonTest.setBackgroundResource(R.drawable.your_drawble);
buttonTest.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
if (flag)
{
buttonTest.setBackgroundResource(R.drawable.your_image_1);
}else
{
buttonTest.setBackgroundResource(R.drawable.your_image_2);
}
flag=!flag;
}
});
}
Well I have an activity which contains nothing but a relative layout with brown background,
and I want to start another activity if the users clicks anywhere on the screen, how would i do that ??
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mainscreen);
}
First, give ID to your RelativeLayout by putting android:id="#+id/relativeLayout" in your layout file then.
RelativeLayout l = (RelativeLayout) findViewById(R.id.relativeLayout1);
l.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
// startActivity here
}
});
or without using your RelativeLayout just implement the onTouchEvent(MotionEvent) of activity
public boolean onTouchEvent(MotionEvent event)
{
if(event.getAction() == MotionEvent.ACTION_UP)
{
// start activity
}
return super.onTouchEvent(event);
}
Note: Not yet tried this code.
Add onClick attribute to your layout xml, and implement onClick method in your activity to start a new activity.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="onClick"
android:orientation="vertical" >
In your activity add an onClick method.
public void OnClick(View v) {
startActivity(new Intent(this,MyNewActivity.class));
}
I think your Mainscreen has Parent layout either Linear,Relative,Frame. Take reference of that and handle click listener.
Ex:
If your Parent layout is Linear.
LinearLayout linear=(LinearLayout)findViewById(R.id.linear);
linear.setOnClickListener(this);
how can I catch the event when click occurs somewhere on the app screen?
It doesn't matter if there is a button or something else. I just need to apply an onClick() event listener on the whole application screen.
How to do this?
setonclicklistner for main layout of your layout file....
Like....main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainlayout">
<!- Your other view->
</Relativelayout>
and set click listener for mainlayout...
RelativeLayout rlayout = (RelativeLayout) findViewById(R.id.mainlayout);
rlayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
You should just override Activity's dispatchTouchEvent(ev: MotionEvent) method
Look at this example in Kotlin. This approach will not affect any onClickListenters in your app
/**
* On each touch event:
* Check is [snackbar] present and displayed
* and dismiss it if user touched anywhere outside it's bounds
*/
override fun dispatchTouchEvent(ev: MotionEvent): Boolean {
// dismiss shown snackbar if user tapped anywhere outside snackbar
snackbar?.takeIf { it.isShown }?.run {
val touchPoint = Point(Math.round(ev.rawX), Math.round(ev.rawY))
if (!isPointInsideViewBounds(view, touchPoint)) {
dismiss()
snackbar = null // set snackbar to null to prevent this block being executed twice
}
}
// call super
return super.dispatchTouchEvent(ev)
}
Or check out the full gist
I believe the easiest way is to override onUserInteraction in your Activity
Just be aware that it won't fire when users focus on/off EditTexts and Spinners (probably other Widgets too). But it will fire every time the user touches the screen or presses a Button.
But this makes it unnecessary to build listeners into your layouts or write additional methods to handle those listeners, so I believe it's the most trouble-free way to get what you want.
If you want to know, that the user clicked anywhere on the screen
then the trick is to override OnUserUnteraction():
"Called whenever a key, touch, or trackball event is dispatched to the activity.Implement this method if you wish to know that the user has interacted with the device in some way while your activity is running. This callback and {#link #onUserLeaveHint} are intended to help activities manage status bar notifications intelligently; specifically, for helping activities determine the proper time to cancel a notification...."
public abstract class BaseActivity extends Activity {
...
#Override
public void onUserInteraction() {
Log.d(DEBUG_TAG,"Touch anywhere happened");
super.onUserInteraction();
}
I am using it to set up app screen block after some inactivity.
Give your main layout an id, then
LinearLayout root = (LinearLayout) findViewById(R.id.main_layout);
root.setOnClickListener(new OnClickListener() {
public void onClick()
{
}
});
You can also directly set a callback from the onClick xml attribute of the root layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:onClick"myMethod">
<!- Your other view->
</Relativelayout>
And in your activity:
public void myMethod(View pView) {
//XXX do something
}
Less bloated code this way :)
Maybe you can add a listener to the Layout instance, getting the layout with
RelativeLayout l = (RelativeLayout)findViewById(R.id.mylayout);
l.setOnClickListener(click);