onClick event not working on Android Blank Activity - android

I have a blank activity in Android Studio, and I have performed the following instructions but it is not working when I run the program.
Select the button and look for properties/attributes panel on the right.
Assign the name onClick to the android:onClick property of your Button.
Implement the following method in the Main_Activity file:
public void onClick (View view) {
Toast.makeText(this, "Button 1 pressed", Toast.LENGTH_LONG).show();
}
When I try to run this I get errors like:
expecting a member declaration
function declaration must have a name

Button yourButton = (Button)findViewById(R.id.your_button_id)
yourButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(this, "Button 1 pressed", Toast.LENGTH_LONG).show();
}
});
Please try this in Java.

1)in Xml file of you Activity set onClick property of button with specified function name
android:onClick:"onClick"
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:onClick="onButtonClick"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
2)after this put same function inside your activity but with parameter like this
public void onClick (View view)
public void onButtonClick(View view){
Toast.makeText(this, "Button 1 pressed", Toast.LENGTH_LONG).show();
}
3) when you click on button provided method gets called
Note: function name can be anything but access specifier, return type and parameter has to be same.

Using the XML attribute android:onClick to trigger a click event. Only need two steps:
1.assign android:onClick to button like this:
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="btnClicked" />
2 In activity define a function named btnClicked like this:
public void btnClicked(View v) {
Log.d("MR.bool", "Button1 was clicked ");
}
Notice: don't mix this way with setOnClickListener,just only above two steps.

Related

Android: How to release an onClick event handler for all the elements of an UI

I do have multiple buttons in a View:
<Button
android:id="#+id/button"
android:layout_width="176dp"
android:layout_height="126dp"
android:text="Button 1"
/>
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Überspringen"/>
I do want to trigger an onClick Event if any of this buttons is clicked, so called View-wide onClick Event Handler wrapping.
What i tried: In order to release an onClick event, it is needed to add an onclick - event to each button, than to put the onClick Event in the controller etc.
Issue: I can have up to 7-17 etc buttons, those don't want to have too many onclick event functions.
Question: How to add an onClick Event handler for everysingle element/button of an UI ?
You could create function like this:
private void applyClickListener(View.OnClickListener listener, View view) {
if (view instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) view).getChildCount(); i++) {
applyClickListener(listener, ((ViewGroup) view).getChildAt(i));
}
} else if (view instanceof Button) {
view.setOnClickListener(listener);
}
}
...and then use it like this:
applyClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("zzz", "click just happened");
}
}, findViewById(android.R.id.content).getRootView());
However I reserve right to mention your task sounds pretty weird.
I don't know if this is a good practice, but i would do it this way:
Create your method in the .java file.
public void myMethod(View view) {
}
Go to your XML layout file and find your buttons. Then add the following attribute to your buttons in your XML code.
android:onClick="myMethod"
Or you can just simply find this attribute in attributes panel in graphic designer.

Custom View Snackbar from XML file will not display bottom of screen

We have tried two ways to display a Custom Snackbar (1) as a masquerading Dialog which will not move to the bottom of the screen It does however not dismiss the current Activity view just makes it opaque. I know why it is in the center of the screen but I am not able to move it to the bottom. (2) next is a view that takes over the entire screen because it is a new content view that I am guessing dismisses the current Activity view BUT it is at the bottom of the screen.
So my question is how to use design number 1 and move the Dialog to the bottom of screen?
Second question how to stop the new view in design number 2 from dismissing the view of the current Activity? After careful reading and little thought and extreme testing I do not think this is possible! I have posted the code for my two methods below. The XML file uses a Relative Layout as the base container.
public void seeSB(){
setContentView(R.layout.custom_snackbar);
// Line of Code above shows XML file
// Line of code tested but no control over the "viewMyLayout"
//LayoutInflater inflater = LayoutInflater.from(ListActivity.this);
//final View viewMyLayout = inflater.inflate(R.layout.custom_snackbar, null);
//viewMyLayout.setEnabled(true);
Button btnAB = (Button) findViewById(R.id.btnAB);
btnAB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// viewMyLayout.setEnabled(false);
// Line above does not function
// CODE BELOW WORKS BUT FAR FROM elegant
setContentView(R.layout.activity_list);
//Intent intent = new Intent(ListActivity.this, ListActivity.class );
//startActivity(intent);
Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show();
}
});
}
public void displaySB(){
final Dialog openSnack = new Dialog(context);
openSnack.setContentView(R.layout.custom_snackbar);
Button btnAB = (Button)openSnack.findViewById(R.id.btnAB);
TextView tvSB =(TextView)openSnack.findViewById(R.id.tvSB);
//Dialog dialog = new Dialog(ListActivity.this);
//dialog.setContentView(Bottom);
// if YES delete Master Password from TABLE_MPW
btnAB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openSnack.dismiss();
Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show();
}
});
openSnack.show();
}
This is far from functional in my book because the method design has just one Custom Snackbar to look at so you need to work on how to have multiple fixed Custom Snackbars. One suggestion might be to have multiple sub views in your parent view and call the sub view you want. I will post just the sub view I added to the parent XML file and the not so real dynamic method to implement which is implemented in this case with a button click. For this to work in a real application the code would need be called from some method or event.
You might consider a switch statement for multiple views ? ? ?
TAKE NOTE THE RELATIVE LAYOUT has its visibility set to GONE at the start
<RelativeLayout
android:id="#+id/hold_snackbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_Black"
android:visibility="gone"
android:orientation="vertical">
<TextView
android:id="#+id/tvSB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:paddingBottom="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:text="#string/snackbar_text"
android:textColor="#color/color_Yellow"
android:textSize="18sp"
android:textStyle="bold" />
<Button
android:id="#+id/btnAB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="350dp"
android:layout_marginTop="5dp"
android:background="#color/color_Transparent"
android:focusable="false"
android:text="#string/snackbar_action"
android:textColor="#color/color_Red"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
Notice the View subViewGroup is declared when the Activity starts
View subViewGroup;
public void makeSB(View view) {
subViewGroup = findViewById(R.id.hold_snackbar);
subViewGroup.setVisibility(View.VISIBLE);
seeSB();
}
public void seeSB(){
Button btnAB = (Button) findViewById(R.id.btnAB);
btnAB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
subViewGroup.setVisibility(View.GONE);
Toast.makeText(getApplicationContext(), "I WAS Clicked", Toast.LENGTH_SHORT).show();
}
});
}
Countdown Timer to close a Snackbar with no Action Button
public void makeCDT(View view) {
cdt = new CountDownTimer(5000, 100) {
// 5 sec 5000,100
// 10 sec 10000,100
#Override
public void onTick(long secsUntilFinished) {
etCPW.setText(String.valueOf(secsUntilFinished / 1000));
//etCPW.setText("seconds remaining: " + secsUntilFinished / 1000);
subViewGroup = findViewById(R.id.SB_NO_ACTION);
subViewGroup.setVisibility(View.VISIBLE);
}
#Override
public void onFinish() {
etCPW.setText("Counter Done");
subViewGroup.setVisibility(View.GONE);
if(cdt!=null){
cdt.cancel();
}
}
};
cdt.start();
}

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

Touch and Multitouch events android

Consider a relative layout with three buttons of variable sizes with no anchoring arguments resulting in all being drawn at the top-left corner of the screen. Consider a single physical touch event being generated. Which of these buttons will see this event? If that all three have registered as onTouch listeners. How is the process behind this behavior ?
RelativeLayout creates a hierarchy between elements inside of it. The item you put to the lowest line will be on top of all others. And when you click, its onClick method will be called. For example you have two buttons inside of RelativeLayout like:
<Button
android:id="#+id/button_a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="You won't see this" />
<Button
android:id="#+id/button_b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="You will see this" />
And let's say your Java code is:
findViewById(R.id.button_a).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(this, "Button A clicked.", Toast.LENGTH_LONG).show();
}
});
findViewById(R.id.button_b).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(getContext(), "Button B clicked", Toast.LENGTH_LONG).show();
}
});
In this case, you will always see the Button B clicked Toast on the screen, when you tap on these buttons.

Keep on clearing text present in edit text when I hold the button

I have a text view in which the user can enter data at run time using the custom buttons that I have created.
My delete button is able to delete one character at a time but if i hold the button then it stops.
I want the text field to get cleared when i hold the button.
Is there any solution to this....??
Please help me out.
This is my xml,
<RelativeLayout
android:id="#+id/btnClear"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
>
<ImageView
android:id="#+id/imgClear"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/img_clear" />
</RelativeLayout>
This is my code,
imgClear.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String getNumber;
if(isFirstNum){
getNumber = txtFirstNumber.getText().toString();
if(getNumber.length() > 0)
txtFirstNumber.setText(getNumber.substring(0, getNumber.length()-1));
} else if(!isFirstNum){
getNumber = txtSecondNumber.getText().toString();
if(getNumber.length() > 0)
txtSecondNumber.setText(getNumber.substring(0, getNumber.length()-1));
}
}
});
You can use the onLongClickListener to check if the delete button is pressed for a long time
imgClear.setOnLongClickListener(new OnLongClickListener()
{
#Override
public boolean onLongClick(View v)
{
txtFirstNumber.setText("");
return true;
}
});
This will set your txtFirstNumber to blank but the onClickListener will not be called.
Your question is not clear. But as per my understanding you want to clear the text of the text view then in your button click listener just set the text for textview to empty.
eg:
public void onClick(View v){
textview.setText("");
}

Categories

Resources