Ripple animation on button press not occurring when java code is present - java

I am attempting to create a calculator app. With only XML present a ripple animation like this occurs
However when I add java code, the ripple animation does not occur, everything else functions normally. Is their a way to fix this?
One button XML (foreground tag houses animation)
<ImageButton
android:id="#+id/one_button"
android:foreground="#drawable/ripple_animation_grey"
android:padding="0dp"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_width="85dp"
android:layout_height="85dp"
app:srcCompat="#drawable/one_button_500_500"
android:layout_below="#+id/four_button"
android:layout_alignParentStart="true" />
One button java code
one_button_IB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!is_additon_selected) {
updateScreen("1");
} else {
values.add(current_screen_value);
clearScreen("1");
is_additon_selected = true;
additionButtonActions();
System.out.println(values);
}
}
});

Related

How to create a clickable Edittext?

I know a there is a lot of similar questions out there, but none of them work for me. I want to make a EditText that acts like a Button. I want it to be not editable (and no keyboard should popup). However, when I set an onClickListener(), the EditText doesn't react to my onClick() event. How should I do this correctly?
In my xml I disabled the focusableInTouchMode and I set the clickable to be true. My resulting EditText is not editable, but it doesn't response to my onClickListener().
My xml of the EditText:
<EditText
android:id="#+id/taskviewer_date"
android:focusableInTouchMode="false"
android:clickable="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textStyle="normal"
android:textColorHint="#color/colorShadow"
android:inputType="none"
android:hint="No Due Date"/>
My Code:
EditText text = (EditText) findViewById(R.id.taskviewer_date);
text.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("taskviewer", "OnClickListener called !");
}
}
);
Use onTouch events or put a clickable FrameLayout over your EditText to catch click events. Make FrameLayout's visibility Gone when you want your edittext to be editable
Use of EditText's setOnClickListener will work if you set setTextIsSelectable to true.
In the XML:
<android.support.design.widget.TextInputLayout
android:id="#+id/textInpuLayout"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="#string/hint_edit_text"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<android.support.design.widget.TextInputEditText
android:id="#+id/textInpuEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:enabled="false"
android:inputType="date"/>
</android.support.design.widget.TextInputLayout>
In the appropriate fragment or the activity:
mTextInpuEditText.setEnabled(true);
mTextInpuEditText.setTextIsSelectable(true);
mTextInpuEditText.setFocusable(false);
mTextInpuEditText.setFocusableInTouchMode(false);
mTextInpuEditText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "Edit Text is clicked");
}
});

Android buttons in xml with onClickListeners, have to click a second time for the button to Click

Have tried various solutions including those in here:
Have to click a button twice for it to work in Android Studio and here:
I have to click the button twice for it to work
And have tried " android:focusableInTouchMode="false" " on the buttons in the xml file which did not work either.
My current code is as follows:
SplashActivity.java
//THIS BIT IS IN THE ONCREATE METHOD
Button enterButton = (Button) findViewById(R.id.enter_button);
enterButton.setOnClickListener(OnClick);
Button bookButton = (Button) findViewById(R.id.book_button);
bookButton.setOnClickListener(OnClick);
}
//THIS BIT IS OUTSIDE OF THE ONCREATE METHOD
private OnClickListener OnClick = new OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(SplashActivity.this, MainActivity.class);
switch (v.getId()) {
case R.id.enter_button:
handler.removeCallbacksAndMessages(null);
startActivity(intent);
break;
case R.id.book_button:
handler.removeCallbacksAndMessages(null);
intent.putExtra("source", "onClick");
startActivity(intent);
break;
default:
break;
}
}
};
}
activity_splash.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<Button
android:id="#+id/enter_button"
android:theme="#style/AppTheme.DevButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Enter"
android:stateListAnimator="#null"
/>
<Button
android:id="#+id/book_button"
android:theme="#style/AppTheme.DevButton"
android:layout_width="100dp"
android:layout_height="50dp"
android:text="Book"
android:stateListAnimator="#null"
/>
</LinearLayout>
Really can't work out how to work around this!
UPDATE to - activity_splash.xml
So I've narrowed this down to the Java file, it doesn't seem to be the xml code causing the issue as I've trimmed down the xml file to the following and the symptoms are the same:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<Button
android:id="#+id/enter_button"
android:layout_width="100dp"
android:layout_height="50dp"
/>
<Button
android:id="#+id/book_button"
android:layout_width="100dp"
android:layout_height="50dp"
/>
</LinearLayout>
I would normally extend SplashActivity to include View.OnClickListener and implement
Button button = (Button)findViewById(R.id.button);
button.setOnClickListener(this);
#Override
public void onClick(View view_) {
...
}
Not sure if this would make a difference or not?
I think the focus is the parent view so add the android:descendantFocusability= attribute to your parent view
So after all of the testing I've managed to find out what the problem was.
It was this line of code here which helps with display the image as full screen:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
The SYSTEM_UI_FLAG_HIDE_NAVIGATION flag does not register touch events so you need to change it to SYSTEM_UI_FLAG_IMMERSIVE so it reads as follows:
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_IMMERSIVE);
Note however this flag was only released after Android 4.4 API Level 19 so won't work in Android versions before this.
Thanks guys for looking into this as well for me, most appreciated.

Android, Button don't working at all

This code represent my real issue...
I'm working on an U.I. more complexe.
I have a custom view that need to have a button out of his leftSide. Why? Because i have four of this custom view aside . And those button's view to create some intercalary view !
I have a simple layout that contains a button.
I had to make him out of his layout's parent, with the property clipChildren="false"
But the button don't response to the onClickListener.
I certainly miss something, but what?
The animation click isn't played at all...
Even the Android's click's song isn't played...
The java code have no effect there..
The button's id button2 work.
The button's id button don't work..
Here is my xml code.
<LinearLayout
android:orientation="vertical"
android:background="#0000FF"
android:clipChildren="false"
android:layout_marginLeft="80dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:textColor="#FF0"
android:layout_marginLeft="-20dp"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:id="#+id/button"
android:layout_marginLeft="-80dp"
android:layout_width="80dp"
android:layout_height="80dp" />
<Button
android:id="#+id/button2"
android:layout_width="80dp"
android:layout_height="80dp" />
</LinearLayout>
and the onCreate Methode:
Button buton1 = (Button) rootView.findViewById(R.id.button);
buton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.wtf("-----", "Click");
Toast.makeText(getActivity(), "button1", Toast.LENGTH_LONG).show();
}
});
Button buton2 = (Button) rootView.findViewById(R.id.button2);
buton2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(), "button2", Toast.LENGTH_LONG).show();
}
});
Try putting the button back inside its parent.
If it works there, have a very long hard think about why you want this button to not live inside its parent. This is a bad idea almost all of the time as it breaks things (both practically and conceptually).
I would suspect the issue is that something else which has a better claim to the space the button is occupying is consuming the touch event.

Determinate Circular Progressbar around Button

I need to give scanning functionality in my android app. This is how my design looks like:
I want to put circular progressbar (Yellow in colour) around the Scan Button. I have seen may tutorials but all of these put text inside the progressbar.
This is what I have achieved so far:
Through this code:
XML:
<ProgressBar
android:id="#+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminate="false"
android:max="100"
style="?android:attr/progressBarStyleLarge"/>
<Button
android:id="#+id/scanbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:contentDescription="#string/scan"
android:background="#drawable/scan_button"
android:layout_gravity="center_horizontal"
android:layout_centerInParent="true"/>
</RelativeLayout>
NOTE: Setting android:layout_centerInParent="true" for <ProgressBar> makes it hidden behind the <Button>.
JAVA:
Button ScanButton = (Button)findViewById(R.id.scanbutton);
ProgressBar progressbar = (ProgressBar)findViewById(R.id.progressbar);
ScanButton.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
new Thread(new Runnable()
{
public void run()
{
while(status < 100)
{
status += 1;
ScanScreen.this.progressbar.setProgress(status);
Thread.sleep(100);
}
}
}).start();
}
});
PROBLEM 1: progressbar is indeterminate and placement is not right.
PROBLEM 2: JAVA code works for horizontal progressbar but crashes for circular one when the button is clicked.
I know my problem is quite detailed. You don't need to solve it completely, just give me right directions.

OnClick of bitmap

can you do an onClick of a Bitmap picture that you have created in the code or do I need to make my "zombie" the background of a texview etc. in order to preform the action on click?
You need an ImageButton like this
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="left"
android:onClick="functionToCall"
android:src="#drawable/to_top_button"
/>
And then define the function
public void functionToCall(View v) {
...
}

Categories