Show Menu Option Automatically At First Activity Of Application - android

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.....

Related

Can I add a button onClick handler via Andriod Studio GUI?

In Android Studio (3.5.3), is it possible to add a button onClick handler via the GUI?
When the onClick dropdown (red box) is clicked, there are no options. When I typed in something manually (as in the picture) no code was generated.
I can add a button handler to my activity in the code, which works fine, but nothing is populated in the "Common Attributes" on this screen.
You have multiple options, but in any one you have to do some effort:
First: you can type anything in onClick attribute and press Enter then will place below in your layout
<Button
android:onClick="onClick"
Your effort: you've either to
Switch to the Text tab of your layout, press ALT + ENTER on the
warning on "onClick" attribute, and then choose Create onClick
Handler, choose the activity and hit OK.
Switch to your activity behavior class and add below method.
public void onClick(View view){
}
Second: you can reverse the order of the first method, so you can first create your click handler callbacks in your activity, and then choose them for buttons on the design tab, and here you can use the drop-down menu of the onClick().
Here I created a couple of callbacks
public class MainActivity extends AppCompatActivity {
...
public void onClick(View view) {
}
public void onClick2(View view) {
}
}
And then you can choose any from the designer tab onClick attribute

Trying to close menu by pressing outside of it

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

How to achieve ripple animation on button without even clicking

I need to show ripple on my button simply in the onCreate() of my activity. Of all the code I have scanned, ripple effect is only visible when button is pressed. Please guide me on how to show ripple by default without the button click.
You still have to click button but programmatically. Use yourButton.performClick() in your onCreate method and make sure when you do this do not run code that is handle on click event for your button for that you can use one boolean variable to check whether you are doing it programmatically or real action is perform
yourbutton clicklistener {
if(isprogrammatic){
// dont do anything
isprogrammatic = false
}
else{
// run your code
}
}
OnCreate
onCreate(Bundle..){ // your on create method
//yes it is programmatic
isprogrammatic = true;
yourbutton.performClick();
}

Clickable TextView only works once

I made a textview clickable then it triggers an intent, it works but only once. After clicking the textview the first time it's no longer clickable and I have no idea why. Your help will be appreciated.
<TextView android:text="Click Me" android:layout_height="wrap_content"
android:layout_width="match_parent" android:id="#+id/textView1"
android:textSize="50dp" android:focusable="false" android:longClickable="true"></TextView>
TextView txt = (TextView) findViewById(R.id.textView1);
txt.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(Example.this, Alert.class));
return false;
}
});
What does the alert class do? If it is an activity it could be that it is being laid over the top of your current activity so although you can see your activity, it's not at the top of the stack/in the foreground so you're not actually pressing the TextView, you're pressing a transparent activity that is over the top of it.
The easiest way to check that is to press the TextView, then press your device's back key and see if the TextView responds to the click.
Or are you sure you're not setting the same layout in Alert.class? That would make it look like it's the same activity but if the Alert class doesn't set the click listener, nothing is going to happen.
The fact that you're starting an activity with an intent and that's making an instance of another class (which I assume is also an activity) stops the click working to me is seriously suggesting that Alert is getting the click somehow instead of Example. When you say things work fine if you remove the intent backs that up as well. Maybe you could post the full source of both classes?
Do you have an onClickListener that disables the textview?
By returning false from onLongClick Android would also invoke the onClick listener if you have one.
Also you could try to remove android:focusable="false"

how to change the view of a button onClick in android

In my app I am trying to calculate an operation using timer. For controlling those operations I am using four buttons as Start, Stop, Pause and resume.
But I want to show only 2 buttons. At the beginning I have only two buttons Start and Pause.
When the start button is clicked timer gets started and immediately in Start button's place I want to show the Stop button.
I want to do the same for the other stop and pause buttons. How to do this please help me......
Using ToggleButton is a good solution for you. Do something like:
ToggleButton first = new ToggleButton(getContext());
ToggleButton second = new ToggleButton(getContext());
first.setTextOff("start");
first.setTextOn("stop");
second.setTextOff("pause");
second.setTextOn("resume");
and use setOnCheckedChangeListener() to implement your actions.
In your onClick(View v), v is the button that gets clicked. You can cast it like:
Button b = (Button) v;
so you can change its text with setText(), and set another listener. You can declare the alternate listeners once as members of the activity, and set them without re-declaring them each time.
Your application needs to maintain states, such as "Idle/Stopped", "In Progress", "Paused", etc. If you want to hide buttons, you can use View.setVisibility, and dynamically show and hide the buttons when your state changes (when other buttons are pressed). You would need to set your layout appropriately so that the buttons display nicely as they are shown/hidden dynamically
Or, you can change the text of the buttons, and their associated click listeners dynamically. This method is not very ideal becuase you may run in to cases where you want different amount of buttons for all your different states, and also, you're associating variable behavior with a single control. Also, you must manage your click listeners, adding and removing them dynamically.
here is a simple implementation
public class Demo extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
button.setText("stop");
}
});
}
}
In the main.xml have a Button widget like this,
<Button android:id="#+id/button"
android:text="start"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>

Categories

Resources