How to post a message in android studio - java

I want to make a button which will post a message once the button is hit. Here is my main activity.java :
enter image description here
But I confuse why I cannot choose the onclick option for the button :
enter image description here
or I dont have to choose the onClick button option again ?
Hope you undestand my question, and here is my video reference from youtube :
"Android Tutorial for Beginners 8 # wrap_content, fill_parent, Password Field and Toast in Android" Uploaded by : ProgrammingKnowledge

Replace the onCreate() method as
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addListnerOnButton(); //Add this line to your code.
}

you are doing wrong in android you can set click listener by two ways
1. In Java code
2. In XML
in your example you are doing runtime in java code so it will not show in XML editor
in your case just call your addListenerOnButton in onCreate it will work
if you want it in xml
add onClick function in activity class as below
public void buttonClick(View v)
{
//do what you want
}
then this buttonClick will be shown in editor or you can directly add in xml button onClick attribute
<Button ...
android:onClick="buttonClick"/>

Related

Disable a button by clicking on another button Java/Xml/Android Studio

I wrote this code to disable using another button by clicking this button in Java/Android Studio but it is not working.
public void btnPowerCheck_onClick(View v) {
SendMessage("43");
Button ButtonBtnPowerCheck = findViewById(R.id.btnPowerCheck);
ButtonBtnPowerCheck.setEnabled(false);
}
Thanks for response, It was my problem, I entered tags and ids wrong

Is there any trivial case when `OnclickListener doesn't` work on a activity?

I implemented the interface View.OnclickListener and Override the method onClick.
Problem is, all other buttons and view response on click, but a particular button doesn't work.
Java code for that button is:
if(v.getId() == R.id.btnResetPass){
Log.i("test", "In ResetButton");
resetPassword();
}
and xml file code for this button is:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/btnResetPass"
android:text="Reset"/>
Nothing happens when I click on this button. But all other Views and Buttons in this onClick method
work properly accept this one. Why isn't it working then?
Have you set the Listener
button.setOnClickListener(this);

How to setOnClickListener to a button inside a ListView that also have the id of the item

I am creating an app that tracks the inventory for a shop.
The layout consists of a ListView that has a sale button for each item, and on clicking the sale button, the quantity of the item should be decreased by one and be updated in database.
So my question is how do i set the onClickListener to the button so that it also receives the id of the item for which the sale button was clicked for, as it will enable me to update only the specific item in the database.
This is an sample example
The Button in the fxml file must have an fx:id so that you can use the button properties in the code behind
it looks like this
<Button fx:id="myButton"/>
and then you can use it (e.g. in your controller) like that
myButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
// TODO
}
});
please check this also setOnClickListener

How do I edit properties of components?

Let's say I have a button. How would I programmatically edit its properties? I don't want to change the properties using XML code in the layout.
Declare your Button variable:
Button button;
...
Then write the following in your onCreate() method:
button = (Button) findViewById(R.id.<Your buttons ID>);
button.setHeight(50);
Please rather do research before asking questions.

How to call a method defined in another activity/class onClick() of a Button ellement?

I am working on the Layout of my main activity with Android Studio (lowest API is 15), and have defined a few XML Buttons on it.
The idea of the program is to edit a list of words by adding to, displaying, and clearing it with a set of buttons. (There is an EditText for adding, but that's not important for the question). But with the idea of high cohesion in mind, I have defined this list and the methods that manipulate it in another plain class called WordList (which still extends Activity), and so when trying to invoke the onClick property of a button, it cannot find them.
android:onClick="addWord"
Method 'addWord' is missing in 'MainActivity' or has incorrect signature...
Is there a way to make the layout, or the individual element point (or get its data context) from another class, or is that against the whole structure of Android and I should just put it in its original activity?
Are you using the correct signature for the method?
Methods defines using the onClick attribute must meet the following requirements:
must be public
must have a void return value
must have a View object as parameter (which is the view that was clicked)
like
public void addWord(View view) {
//your action
}
Add an OnClickListener to the button instead of using the XML onClick attribute.
https://stackoverflow.com/a/29479937/1496693
Try this, it should work:
Button btn = (Button) findViewById(R.id.mybutton);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
addWord(v);
}
});
// some more code
public void addWord(View v) {
// does something very interesting
}
XML Implementation
<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="addWord" />
<!-- even more layout elements -->

Categories