Show menu on button click in android - android

I just want the normal menu which gets displayed on clicking the settings button in android phone instead with Virtual Button Click. I tried openOptionsMenu(); but it didn't help. I hope you understand the problem here: I want for example default "setting" option displayed which we see when I click on button in actionbar but this time I have action bar hidden so I have a button which I want to display the menu on Clicking that button.
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openOptionsMenu();
}
});

You can set its property in xml file to invisible or gone & in the .java file set it to visible see code below:
Note: In my case I'm using Buttons you can use according to your own
.java file:
Button next,btn;
public void newBtnL(View view){
next = (Button) findViewById(R.id.ListenBtn);
btn = (Button) findViewById(R.id.newBtn);
next.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setVisibility(View.VISIBLE);
}
});
}
.xml file:
<Button
android:id="#+id/ListenBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="this"
android:layout_below="#+id/btn_stop_service"
android:onClick="newBtnL"/>
<Button
android:id="#+id/newBtn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/ListenBtn"
android:text="that"
android:visibility="invisible"/>

Related

Android: onClick causing me to go back an Activity

I am trying to build a dynamic UI, but when I add the onClick method to the button whenever I push the button I go back to my previous activity. Any ideas on how to fix it?
my button's code: (the addMenu method is never run in the activities class)
<Button
android:text="New Menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/newButton"
android:layout_weight="1"
android:onClick="addMenu"/>
here is my addmenu code although no matter what goes in here(even if nothing at all) it still won't work
public void addMenu()
{
LinearLayout layout = (LinearLayout) findViewById(R.id.backLayer);
Button newButton = new Button(this);
newButton.setText("menu "+menu);
layout.addView(newButton);
menu++;
}
whenever I push the button I go back to my previous activity.
Sounds like your app is crashing and restarting... read the logcat, and you'd see something along the lines that your method signature is wrong.
android:onClick="addMenu" needs a method of public void addMenu(View v).
Or just use Java to set the button listener and remove android:onClick.
findViewById(R.id.newButton).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addMenu();
}
}
try this
/**
* #param v android:id="#+id/newButton"
*/
public void addMenu(View v)
{
LinearLayout layout = (LinearLayout) findViewById(R.id.backLayer);
Button newButton = new Button(this);
newButton.setText("menu "+menu);
layout.addView(newButton);
menu++;
}

How to hide the keyboard, and add text to EditText

I have a EditText and a Button .
When you press EditText , I want to not show the keyboard , And when you press the Button, I want to type a number 1 on the EditText .
observation
I want to cursor does not disappear .
![
When you press the 1 1 writes
When you press the Del licked
Can be controlled in the text
Without the appearance of the keyboard ]1
Set this in your EditText xml to disable the keyboard
android:focusableInTouchMode="false"
Set a "1" on your edit text.
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mYourEditText.setText("1");
}
});
Next configuration to your EditText:
android:longClickable="false"
android:clickable="false"
android:focusableInTouchMode="false"
Add a onClickListener to your button:
myButton.setOnClickListener(new View.OnCLickListener(){
#Override
public void onClick(View v) {
myEditText.setText( myEditText.getText().toString() + "1");
}
});

Change button background more times

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;
}
});
}

Android Webview with a toggle button

I have two overlapping webviews with in a frame layout. One contains a map and the other (on top of the map) contains the details. I want to let user to on and off the webview contains details with a toggle button. How can I do that?
First get the value of your switch button and check what is user prefer to do.
String val= (String) (switchVal.isChecked()? switchVal.getTextOn() : switchVal.getTextOff());
If he selected to be off the detail view then set not to be visible (setKeepScreenOn(boolean)) your detail webview.
For Toggle Button simply you have to use:
<ToggleButton
android:id="#+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOn="#string/toggle_turn_on"
android:textOff="#string/toggle_turn_off"
android:checked="true" />
Now, you have ID for toggle. Start work on MainActivity.
public void addListenerOnButton() {
ToggleButton toggleButton2 = (ToggleButton) findViewById(R.id.toggleButton2);
Button btnDisplay = (Button) findViewById(R.id.btnDisplay);
btnDisplay.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
StringBuffer result = new StringBuffer();
result.append("\ntoggleButton2 : ").append(toggleButton2.getText());
Toast.makeText(MyAndroidAppActivity.this, result.toString(),
Toast.LENGTH_SHORT).show();
}
});
For Instance have a look there, if toogle is ON that would display like this:

Error in changing button visibility onClick

I have a button which is called Check, I want it to be invisible and visible as I click each time on it, as If its visible and I clicked it will become invisible and verse vies !
But my code doesn't work ! any ideas ?
Button Check ;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Check = (Button)findViewById(R.id.checkButton);
Check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View View) {
if (View.getVisibility() == android.view.View.VISIBLE)
View.setVisibility(android.view.View.INVISIBLE);
else if (View.getVisibility() == android.view.View.INVISIBLE)
View.setVisibility(android.view.View.VISIBLE);
}
});
In my activity its visible at the beginning and when I click on it, it become invisible, BUT when I click again it stays invisible !
Change your code to this,
Check.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (v.isShown())
v.setVisibility(View.INVISIBLE);
else
v.setVisibility(View.VISIBLE);
}
But i think problem is, when button goes invisible, you are not getting any click event on it. First make sure that onClick method get call when button is invisible.
An invisible button will not dispatch any interaction event. So instead of setting button's visibility to the invisible, you can set a transparent or blank background or something like that.
But i personally believe, you should change your use-case because why one will click on the invisible button.
Try This:
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="abcd" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:onClick="abc"
android:text="Button" />
</LinearLayout>
public void abc(View v) {
v.setVisibility(View.INVISIBLE);
}
public void abcd(View v) {
v.findViewById(R.id.button1).setVisibility(View.VISIBLE);
}
Invisible Items don't receive on-click event. So the only way you can receive a click on invisible is by receiving on some other view in place of the invisible view. The above solution wraps the button in a layout, so when button is invisible the on-click is passed on to layout, which handles the event and do accordingly. If you have a high usage of such layout you can also create a custom button with above mechanism.

Categories

Resources