How can I add a Button to my wear OS app? - android

I am adding a Button to my first Wear OS application. When I follow a model from
my existing Android applications, there appears to be difference due to
"WearableActivity" versus "Activity". I cannot define the OnClickListener.
In my on create is this:
bottomButton = findViewById(R.id.bottomButton);
setListener();
later in the main activity source is this
void setListener()
{
bottomButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) // toggle between report and graph display
{ if (showGraph)
{showReport=true; showGraph=false;bottomButton.setText("show Report");}
else
{showReport=false;showGraph=true;bottomButton.setText("show Graph");}
} // end, onClick
}); // end, setOnClickListener
} // end, setListener()
In both cases the button is defined in XML and found and used as follows
OkButton = (Button) findViewById(R.id.OkButton);
OkButton.setOnClickListener(this);

it does compile anyway. And I can access the items in the button such as text. But when I change the text, it doesn't appear on the screen. And when I push the button in the emulator, it doesn't register any action.
The problem is not from your code but from the Android wear emulator. Your wear emulator is in the ambient(low power) mode. Click in the top of the emulator window to toggle between interactive(full power) and ambient modes(low power).
How can I define and use a button in the wear os application.
The same way it is done on the mobile side. Below is an example
public class MainActivity extends WearableActivity implements View.OnClickListener {
Button clickMeButton;
TextView textView;
int count = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
setAmbientEnabled();
clickMeButton = findViewById(R.id.click_me_button);
textView = findViewById(R.id.test_textview);
clickMeButton.setOnClickListener(this);
}
#Override
public void onClick(View view) {
count++;
textView.setText("I am clicked: "+count);
}
#Override
public void onEnterAmbient(Bundle ambientDetails) {
// Handle entering ambient mode
super.onEnterAmbient(ambientDetails);
Log.e("Hello", "I'm ambient");
}
#Override
public void onExitAmbient() {
// Handle exiting ambient mode
super.onExitAmbient();
Log.e("Hello", "exit ambient");
}
#Override
public void onUpdateAmbient() {
// Update the content
super.onUpdateAmbient();
Log.e("Hello", "update ambient: " + isAmbient());
}
}
Below is the results of the above activity (NB: Click in the top of the emulator window to toggle between interactive and ambient modes):
EDIT
To avoid implementing View.OnClickListener in your activity you can pass an instance of View.OnClickListener when calling setOnClickListener for the OkButton like you are already doing for the bottomButton.
OkButton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//do something when OkButton is clicked
}
});

Related

How to get Click Event of futuresimple library's FloatActionButton?

I am using this library this library.I used the code described in its description on github. looks like this I have event for the menu buttons, not its main Button. i want to get its main button's click event for make rest layout blur when its clicked/expended. even want to hide or set its default state when user click or touch somewhere else on screen.
FloatingActionButton actionC = new FloatingActionButton(getBaseContext());
actionC.setIcon(R.drawable.notes);
actionC.setTitle("Add note");
actionC.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//
}
});
FloatingActionsMenu floatingActionsMenu = (FloatingActionsMenu)findViewById(R.id.multiple_actions);
floatingActionsMenu.addButton(actionC);
// ((FloatingActionsMenu) findViewById(R.id.multiple_actions)).addButton(actionC);
final FloatingActionButton actionA = (FloatingActionButton) findViewById(R.id.action_a);
actionA.setIcon(R.drawable.event);
actionA.setTitle("Make life Event");
actionA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//
}
});
and the answer is .
floatingActionsMenu.setOnFloatingActionsMenuUpdateListener(new FloatingActionsMenu.OnFloatingActionsMenuUpdateListener() {
#Override
public void onMenuExpanded() {
Toast.makeText(Launcher.this, "Fdf", Toast.LENGTH_LONG).show();
}
#Override
public void onMenuCollapsed() {
}
});

Unable to start the Android application - NullPointerException on Click Listener

I'm having some problems trying to navigate between screens im my Android Project. I'm not creating other Activities classes yet, I'm just trying to open other XML files by the SetContentView(R.layout.XXX). Here is my main Activity:
package com.android.eduardo.navegacao;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class NavegacaoActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
chamaTelaPrincipal();
Button btCadastro = (Button) findViewById(R.id.btCadastro);
btCadastro.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamaCadastro();
}
});
Button btConsulta = (Button) findViewById(R.id.btConsulta);
btConsulta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamaConsulta();
}
});
Button btVoltar1 = (Button) findViewById(R.id.btVoltar);
btVoltar1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamaTelaPrincipal();
}
});
}
public void chamaCadastro(){
setContentView(R.layout.activity_cadastro);
}
public void chamaConsulta(){
setContentView(R.layout.activity_consulta);
}
public void chamaTelaPrincipal(){
setContentView(R.layout.activity_navegacao);
}
}
As you can see, the "R.layout.activity_navegacao" is my main layout. When I try to execute this code the application closes and I receive a NullPointerException error, indicating some problems on SetContentView.
When I cut the code of the last setOnClickListener (the button "btVoltar") it works and I can open the two other screens. The button "btVoltar" is being used by the other XML to return to the main screen (activity_navegacao).
I already checked the id of the XML on the R class, and it's ok. I also don't receive any error notifications until I execute the project. Sorry for the bad english, if you guys can help me, I appreciate.
You are getting the NullPointerException because you are referencing a button that is on an XML layout which has not been displayed. (ie, the button is not found on activity_navegacao.xml).
For this reason, you should not call setContentView multiple times to change the view, like you are doing in this code. Instead, you should consider either making each screen a different activity (and calling setContentView only once per activity), or look into Fragments and FragmentTransactions. Fragments will allow you to replace the view, like you are attempting to do here.
You Can Initialize views only view is present in screen.In Your You clearly saying that the Button "btVoltar" not present in your current screen(activity_navegacao). So Initialize the view after its screen come to present.So Change your code like below
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
chamaTelaPrincipal();
Button btCadastro = (Button) findViewById(R.id.btCadastro);
btCadastro.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamaCadastro();
}
});
Button btConsulta = (Button) findViewById(R.id.btConsulta);
btConsulta.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamaConsulta();
}
});
}
public void chamaCadastro(){
setContentView(R.layout.activity_cadastro);
}
public void chamaConsulta(){
setContentView(R.layout.activity_consulta);
}
public void chamaTelaPrincipal(){
setContentView(R.layout.activity_navegacao);
Button btVoltar1 = (Button) findViewById(R.id.btVoltar);
btVoltar1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
chamaTelaPrincipal();
}
});
}
}

Android - How to increase EditText value?

I'm new to android development..
I have this code in my main class:
Button prevBtn, pauseBtn, nextBtn;
EditText counterTxt;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_affirmations);
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
prevBtn = (Button)findViewById(R.id.prevBtn);
pauseBtn = (Button)findViewById(R.id.pauseBtn);
nextBtn = (Button)findViewById(R.id.nextBtn);
counterTxt = (EditText)findViewById(R.id.counterTxt);
prevBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t-1));
}
});
nextBtn.setOnClickListener(new View.OnClickListener() {
int t = Integer.parseInt(counterTxt.getText().toString());
public void onClick(View v) {
counterTxt.setText(String.valueOf(t+1));
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_affirmations, menu);
return true;
}
When I click "Previous", the text field value becomes 19.
When I click "Next", the text field value becomes 21.
But it only displays these two values, nothing else, no matter if i click again. I want to subtract or add 1 whenever i click the appropriate buttons.
I think this happens because the event Listeners are inside onCreate() method? Any idea on how to make it update each time I click?
You need to move your parseInt inside your onClick:
nextBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t+1));
}
});
In both cases, t is defined as a member variable of the listener, and never changed. move it inside the onClick method instead, like this (in both cases):
public void onClick(View v) {
int t = Integer.parseInt(counterTxt.getText().toString());
counterTxt.setText(String.valueOf(t-1));
}

One button used for two Action

Can any one tell me? how can i use the same button for two Action?
Like em using one button in my activity that calculate some values and after calculating the when i again press same button then reset the all fields. Like in this Application
http://www.craziness.com/games/play-love-tester/ when i test the love by pressing the button
then i again press the same button then all fields reset.
what should i use in my activity for the above problem?
You can create a global variable which indicates the state of the program and then change this value when needed. In the OnClickListener of your Button you create an if statement which checks this variable and does the needed things for the associated value.
Example:
public class MainActivity extends Activity {
private int state = 0;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (state == 0) {
// State 1
}
else if (state == 1) {
// State 2
}
else {
// Default state
}
}
});
// Rest of your code including state changing part
}
}
You can do it by changing the button text.
If you do not want that someone sees the text change. Test (example "click" and the other state "click " (1 blank at the end) or another solution.
....
Button bt = (Button) findViewById(R.id.button);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// todo: check for expected instance (Button)
Button btc = (Button)v;
String bText = btc.getText().toString();
if (bText == "open") {
btc.setText("close");
}
else if (bText == "close") {
btc.setText("open");
}
}
[...]
You can do so by using a toggle button.
In your xml file add a toggle button
<ToggleButton
android:id="#+id/tbtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textOff="Reset"
android:textOn="Calculation"
android:background="#drawable/icon"/>
Java file
public class Reviews extends Activity implements OnClickListener {
private ToggleButton tbtn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tbtn = (ToggleButton) findViewById(R.id.tbtn);
tbtn.setOnClickListener(this);
public void onClick(View view) {
if (tbtn.isChecked()) {
//calculate the result
}
else {
//Reset your global calculation variable;
}
}
}
}

Button on Custom Dialog Not Responding to Click Events

I created a custom dialog that extends Dialog. One button on that the dialog is an “OK” button which the user is expected to press when finished entering information in other fields. I cannot get any listeners set to that button to fire.
public class HeightDialog extends Dialog {
private Button okButton;
…
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.heightdialog);
this.okButton = (Button)this.findViewById(R.id.userOkWithHeight);
this.okButton.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View v) {
// Does not fire
HeightDialog.this.dismiss();
return;
}
});
this.okButton.setOnLongClickListener(new OnLongClickListener() {
public boolean onLongClick(View v) {
// Does not fire
HeightDialog.this.dismiss();
return true;
}
});
this.okButton.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// Does not fire
HeightDialog.this.dismiss();
return true;
}
});
…
}
I also attempted an implementation where the Dialog class implemented the listeners(http://www.androidcompetencycenter.com/2009/01/android-basics-dialogs-and-floating-activities/) instead of using inner classes(http://about-android.blogspot.com/2010/02/create-custom-dialog.html):
Still no luck.
public class HeightDialog extends Dialog implements View.OnClickListener {
private Button okButton;
…
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.heightdialog);
this.okButton = (Button)this.findViewById(R.id.userOkWithHeight);
this.okButton.setOnClickListener(this);
public void onClick(View view){
HeightDialog.this.dismiss();
return;
}
…
}
I have set breakpoints inside each of the listeners in both versions of the implementation, and the debugger never stops execution. I have attempted to use inner classes for the listeners which did not solve the problem.
Any clues?
Thanks
I found a solution here:
Handling buttons in custom dialogs
It works in my case.
dialog = new Dialog(this);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Custom Dialog");
Button dialog_btn = (Button) dialog.findViewById(R.id.dialog_button);
dialog_btn.setOnClickListener(new View.OnClickListener()
{
// Perform button logic
}
Why I am not sure why following the two examples mentioned in my post did not work, I figured out how to get it to work. I had to move the attachment of my listener to the button in the dialog's onStart() method from the dialog's onCreate() method.
It appear this is related to me also overriding the onStart() method in my custom dialog:
public void onStart() {
super.onStart();
setContentView(R.layout.heightdialog);
...
}
That code must have "zeroed" out my listeners which were in the onCreate() method.
In order to intercept button clicks HeightDialog must implement View.OnClickListener
public class HeightDialog extends Dialog implements View.OnClickListener
{
}

Categories

Resources