Determinate Circular Progressbar around Button - java

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.

Related

Android Studio: how progress bar can fill up based on user input

I am using a progress bar sample from Github however I noticed that the progress bar fill is set to a fixed value. For example, if the step count goal is 10 (.java class) the man should be "10" in the .XML
My goal: when the user inputs their goal step count, the "10" should be variable and change depending on user input.
java snippet:
ProgressBar progressBar = (ProgressBar) this.findViewById(R.id.progressBar);
ObjectAnimator animation = ObjectAnimator.ofInt(progressBar, "progress", lastStep, stepCounter); //animate only from last known step to current step count
animation.setDuration(5000); // in milliseconds
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
lastStep = stepCounter;
XML snippet
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="376dp"
android:layout_height="392dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:max="10"
android:progress="0"
android:progressDrawable="#drawable/circular" />
EDIT********************EDIT
Sorry maybe my question wasn't clear. To provide an example...if the users step goal is 500. I need the progress bar to fill respectively. Therefore, IF step_count = 250 then progress bar should be half full, IF step_count = 750 then should be 3/4 full. I need the progression to be respective to a variable value.
Set android:indeterminate="false". See this fully implemented code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent">
<ProgressBar
android:id="#+id/pBar"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:layout_marginTop="200dp"
android:minHeight="50dp"
android:minWidth="200dp"
android:max="100"
android:indeterminate="false"
android:progress="0" />
<TextView
android:id="#+id/tView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/pBar"
android:layout_below="#+id/pBar" />
<Button
android:id="#+id/btnShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="130dp"
android:layout_marginTop="20dp"
android:text="Start Progress"
android:layout_below="#+id/tView"/>
public class MainActivity extends AppCompatActivity {
private ProgressBar pgsBar;
private int i = 0;
private TextView txtView;
private Handler hdlr = new Handler();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pgsBar = (ProgressBar) findViewById(R.id.pBar);
txtView = (TextView) findViewById(R.id.tView);
Button btn = (Button)findViewById(R.id.btnShow);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
i = pgsBar.getProgress();
new Thread(new Runnable() {
public void run() {
while (i < 100) {
i += 1;
// Update the progress bar and display the current value in text view
hdlr.post(new Runnable() {
public void run() {
pgsBar.setProgress(i);
txtView.setText(i+"/"+pgsBar.getMax());
}
});
try {
// Sleep for 100 milliseconds to show the progress slowly.
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}).start();
}
});
}
}

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

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

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.

How to make an ImageView overlay another View and hide it after a certain period of time?

I'm making a very simple Andriod app and I was wondering if I could get some help with my app.
I would like to show an ImageView over the full ListView (including the action bar) for 3 seconds, and then remove the ImageView (or hide it, anything), to go back to the list view.
How can this be done? I have tried a few things but it either breaks my code or doesn't show anything at all.
Thanks in advance everyone - let me know if you need any further explanation.
EDIT: As per the question below, I'd like the ImageView to be shown as soon as the ListView is shown, for 3 seconds, then disappear.
Allright. What you want is actually quite easy.
Simply create a RelativeLayout that contains a ListView and an ImageView above it.
Then inside your onCreate(...) method, you use a Handler and set the Visibility of the ImageView to GONE after 3 seconds.
Here is the layout.xml file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ListView>
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitXY"
android:src="#drawable/your_image" />
</RelativeLayout>
And inside the onCreate(...) method:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.yourlayout);
final ImageView iv = (ImageView) findViewById(R.id.imageView1);
Handler h = new Handler();
h.postDelayed(new Runnable() {
#Override
public void run() {
// EITHER HIDE IT IMMEDIATELY
iv.setVisibility(View.GONE);
// OR HIDE IT USING ANIMATION
hideImageAnimated(iv);
// DONT use both lines at the same time :)
}
}, 3000); // 3 seconds
}
In order to make things a bit smoother, you could use an AlphaAnimation on your ImageView:
public void hideImageAnimated(final ImageView iv) {
Animation alpha = new AlphaAnimation(1.0f, 0.0f);
alpha.setDuration(1000); // whatever duration you want
// add AnimationListener
alpha.setAnimationListener(new AnimationListener(){
#Override
public void onAnimationEnd(Animation arg0) {
iv.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation arg0) { }
#Override
public void onAnimationStart(Animation arg0) { }
});
iv.startAnimation(alpha);
}

Categories