Trying to close menu by pressing outside of it - android

Hey guys I'm making a pocketbook in Unity. On the upper right side corner there's a menu button that I've coded to make a drop-down list of links appear that will bring you to different pages of the pocketbook. Right now the only way to make the list dissapear is to click on the menu button again, which is inconvenient. I want the user to be able disable the menu by tapping anywhere else on the screen (I've created an invisible button that covers the rest of the screen, only problem is I want it to be active only when the scroll menu is active) like a normal menu screen. This is the code I've created for it. Menu Button is the menu button, close button is a button I created that covers the rest of the screen, I'm struggling to make the button active only when the scroll menu is up otherwise it will stop the user from scrolling through the pages. Thanks
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
using UnityEngine.UI;
public class MenuButton : MonoBehaviour {
public GameObject scrollMenu;
public Button menuButton, closeButton;
public bool active = false;
// Use this for initialization
void Start () {
Button btn = menuButton.GetComponent<Button>();
btn.onClick.AddListener(TaskOnClick);
closeButton.enabled = false;
}
// Update is called once per frame
public void TaskOnClick ()
{
if (active == false)
{
scrollMenu.SetActive(true);
active = true;
closeButton.GetComponent<Button>().interactable = true;
} else
{
scrollMenu.SetActive(false);
active = false;
}
}
}

What you can try is.
1. Make a button part of your menu, it should cover the entire screen
2. it must visible/hide with your menu
3. it must be transparent.
4. on the button click event attach menu hide code.
I have done that and it's working perfectly.
see the image below

Related

How to open the SlidingPanelLayout, only through button?

I'm developing an Android app with GoogleMap and I need to implement a SlidingPanelLayout in this Activity.
I want to open the SlidingPanelLayout only when a certain button is clicked, because if I drag the finger
on the GoogleMap the SlindingPanelLayout appears. So if I fix the open SlidingPanelLayout only when the button is clicked, the problem will be solved.
Are there any way to block the SlidingPanelLayout opening when I drag on the map, or something like this?
If you really want the panel to open only through a button press, you should create a class that extends SlidingPaneLayout and override the onInterceptTouchEvent() method.
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if(!isOpen()){
//The map panel is being shown. We don't want the SlidingPaneLayout to handle the MotionEvent.
return false;
}
else{
//The other panel is being shown... Let the SlidingPaneLayout handle the MotionEvent as normal.
return super.onInterceptTouchEvent(ev);
}
}
Remember to use the custom SlidingPaneLayout class in your code or layout and not the regular one. Also, you should, obviously, place a button that will call the openPane() method of your custom class somewhere.
====
THE APPROACH BELOW IS NOT THE ANSWER TO THE QUESTION, BUT AN ALTERNATIVE SOLUTION
Now, if you want to let the user to freely use the GoogleMap object and let the SlidingPaneLayout open if a drag event occur in a certain region of the screen/map, you can use this approach:
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
//Get the user touch event position in dp units
float xTouchPosDp = ev.getX()/getResources().getDisplayMetrics().density;
if(!isOpen()){
if(xTouchPosDp < 30){
//If the panel is closed (map pane being entirely shown)
//and the touch event occur on the first 30 horizontal dp's
//Let the SlidingPaneLayout onTouchEvent() method handle the
//motion event alone (the GoogleMap object won't receive the event
//and depending on the movement, the panel will open)
return true;
}else{
//Now, if the panel is closed, but the touch event occur
//on the rest of the screen, let the GoogleMap object handle
//the motion event.
return false;
}
}
else{
//If the panel is opened, let the SlidingPaneLayout handle the
//motion event normally.
return super.onInterceptTouchEvent(ev);
}
}
Again, remember to use your custom SlidingPaneLayout class in your code/layout.
The problem with this solution is that if both panels are opened (they together fit the entire screen), you won't be able to move the map laterally.

Android Device Backbutton should not work

First of all i am learning android, I am testing my Andorid app in my Samsung Galaxy S1.
My app Function is: while i am pressing RandomNumber button, it will generate Random numbers and displaying in the screen in TextArea.
But i am facing the below issues.
The Device back button is allow user to go back. How i can avoid that? ( I have buttons defined in the program dynamically, only that Back button should work )
While shaking the phone or change the position of the phone, then the Random numbers are automatically generating. How to avoid that?. Please advise.
Button Creation Dynamic
final Button buttonToAdd = new Button(this);
buttonToAdd.setText("RandomNumber");
Listener:
buttonToAdd.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Strvalue = (String) buttonToAdd.getText();
if (Strvalue.equals("RandomNumber"))
{
Randomnumbergeneration();
}
}
});
You can overwrite the Activities onBackPressed() method to handle the back-button click event.
#Override
public void onBackPressed() {
// put some code here or just do nothing
// don't call super.onBackPressed() if you want to disable the back function
}
But if you want to publish your application you should follow the official design guidelines and do not disable this behaviour because every android user is used to it and will find it unlikely if the back button does not work anymore.

Clicking the top home button doesn't do anything

I am using actionbarsherlock. I have three screens as follows
lists items in list activity
lists items in list activity after an item from first screen is clicked
shows details about the clicked item.
On second screen I want to have home button that takes the user back to first scree, and on third screen I want to have a home button that takes the user back to second screen.
Here is what I'm doing:
//on second activity:
getSupportActionBar().setTitle("First");
getSupportActionBar().setHomeButtonEnabled(true);
//on third activity:
getSupportActionBar().setTitle("Second");
getSupportActionBar().setHomeButtonEnabled(true);
This makes the button on top left show up but nothing happens when I click it.
I found online that this change needs to be made in onOptionsItemSelected as well. But I don't have android.id.home in my code. My IDE throws error on android....
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == /*what should I put here*/) {
finish();
return true;
}
return true;
}
you should put here android.R.id.home - it is an Android home button id

How to refresh the screen when back button is pressed

I have scenario with two screens.
Screen 1 shows data from from API in list format
There is a "+" button in menu bar
Clicking this button takes user to screen 2
User can enter some info on screen 2 and press the "save" button on top of this screen. This does a POST to my API and saves the data.
After saving, I would like to put the user back to screen 1. I've done that with this:
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
if (menuItem.getTitle().toString().equalsIgnoreCase("save")) {
new CreateSomethingTask(this,enteredName.getText().toString(), id).execute();
Intent listscreen = new Intent(getApplicationContext(), ShowListActivity.class);
startActivity(listscreen);
return true;
}
return true;
}
However, the added item is not shown. If I close my app and open it again then the item shows up.
Is there a good way to handle this? I like how the Github Android app handles this when creating a new Gist. But I'm not sure how to implement that.
You should start your screen2 with startActivityForResult(). That way you could send a result back and a code and proceed to refresh your screen1. See example : How to manage `startActivityForResult` on Android?
Below function maybe help you, didn't tried.
#Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
callFunctionToRefreshList();
//or redraw data from api
//setContentView(R.layout.activity_book);
}

Show Menu Option Automatically At First Activity Of Application

I develop a application
and in which i have a Menu option which i invoke from onCreateOptionMenu()
But this is called only when any user press the menu button
so now i want that my application start and first Activity is Welcome.java
then in onCreate(Bundle b)
can i write sone line from which the menu is invoked automatically without pressing Menu button
i used openOptionMenu() but it not works.
2) can i create a Button and simulate it as Menu button and then write button.performClick() so it act as a Menu Button and menu option will visible
So give me some suggestion on this
Thanks
You can request the menu be opened with an Activity method
openOptionsMenu();
If you want to show a menu immediately, you'll have to wait for the window focus to change, rather than using onResume:
#Override
public void onWindowFocusChanged(boolean hasFocusFlag) {
super.onWindowFocusChanged(hasFocusFlag);
if (hasFocusFlag) {
openOptionsMenu();
}
}
See openOptionsMenu()
Hi I will like to provide answer for your question
(" can i create a Button and simulate it as Menu button and then write button.performClick() so it act as a Menu Button and menu option will visible")
Answer:
Step 1-Create a button/ image button in your layout
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/a"
android:onClick="expand"
android:src="#drawable/button" />
Here I have set onClick method as "expand"
Step 2-Now in your MainActivity.java class define your "expand" method which will be call once user click on your button
public void expand(View v)
{
ImageButton imgButton=(ImageButton)findViewById(R.id.imageButton1) ;
imgButton.setVisibility(View.GONE);
openOptionsMenu();
}
In this code I have set visibility as "gone", as I want the button to disappear once menu is shown
Step 3-*(In case you are setting visibility for the button)* You can also write code to set visibility as "visible" once menu is closed using below method
public void onOptionsMenuClosed(Menu menu) {
super.onOptionsMenuClosed(menu);
ImageButton imgButton=(ImageButton)findViewById(R.id.imageButton1) ;
imgButton.setVisibility(View.VISIBLE);
}
Hope this will help you.....

Categories

Resources