setProgressBarIndeterminateVisibility(true) not working - android

I'm trying to add a progress bar to my actionBar. I'm talking about the spinning circle. I did a request and tried to set is vissible, but nothing happens.
I have read many likely questions but is still couldn't figure out what i'm doing wrong.
My code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);//Above setContentView, very important
setContentView(R.layout.activity_main);
//...other stuff
}
In an other method which i don't call in on create.(It's an onClick method)
public void plus(View view){
setProgressBarIndeterminateVisibility(true);//Nothing happens
//...other stuff
}
I do not understand what's wrong, please help me.
NOTE: I never set it to false
Edit:
I tried mmlooloo second part, but absolutly noting happend. Not even part 3. So I tried part 4, but i gave me an exception.
"This Activity already has an action bar supplied by the window decor.
Do not request Window.FEATURE_ACTION_BAR and set windowActionBar to
false in your theme to use a Toolbar instead."
I removed the Window.FEATURE_ACTION_BAR request, but it gave me the same exception.
I don't think i need to set windowActionBar to false, but I did and it still gave me the same exception.
Any other options?

First:
Window provided Progress Bars are now deprecated with Toolbar.
Second:
you must use:
setSupportProgressBarIndeterminateVisibility(true);
instead of
setProgressBarIndeterminateVisibility(true);
because you extends ActionBarActivity. (because you are using supportRequestWindowFeature instead of requestWindowFeature)
Third:
If it crashes this is a known issue.
If your library is updated sorry but it is now just a no-op:
setSupportProgressBarIndeterminateVisibility() crashing has been fixed
for a future release, in that it will be a no-op.
Fourth:
My solution:
use toolbar with progressBar widget:
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ProgressBar progressBar = (ProgressBar) findViewById(R.id.progress_spinner);
progressBar.setVisibility(View.VISIBLE);
}
Layouts:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/toolbar"/>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
and
toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ProgressBar
android:id="#+id/progress_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:indeterminate="true"
android:visibility="gone" />
</android.support.v7.widget.Toolbar>

Related

Cannot make support bottomsheet not-hideable when in Expanded state

I'm trying to make a bottomsheet using google support library. The goal is to have a sheet that:
Can be hidden programmatically only
Its height is calculated automatically
Is defined statically in xml
So far so good, simple stuff. There is also this promising isHideable() which defaults to false.
But the bottomsheet seems to ignore the isHideable when the sheet is set to STATE_EXPANDED (although its not going to cover the whole screen). The only way to make it unhideable is to set a peek height (which I don't want). Is there a way to have it expanded and not-hideable without setting the height manually (or via layout change triggers)
Here is the (super slim) code used:
Activity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View bottomSheet = findViewById(R.id.bottomsheet);
BottomSheetBehavior behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setHideable(false);
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:id="#+id/bottomsheet"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:behavior_hideable="false"
app:layout_behavior="#string/bottom_sheet_behavior"
android:background="#android:color/white">
<TextView
android:layout_width="match_parent"
android:layout_height="300dp"
android:text=":) :) :)"/>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Behavior
The simplest but hackish way I've found so far:
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
bottomSheet.post(new Runnable() {
#Override
public void run() {
behavior.setPeekHeight(bottomSheet.getHeight());
}
});
And of course when there is need to hide it firstly call setHideable(true).
This is just a workaround that might lead to weird behavior.

Layout behind status bar - Android Lollipop

I want in my application to be able to implement this effect:
where the status bar is semi transparent and the layout is behind the status bar. Every example that I've read on the subject, was mainly associated with the navigation drawer and mostly used the ScrimInsetScrollView (or ScrimInsetsFrameLayout). I tried implementing this with ScrimInsetsFrameLayout.
Basically I have an activity that holds a fragment, and this is my layout (the fragment is later added to the container in the activity's onCreate method):
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:fitsSystemWindows="true"
>
<com.test.app.widget.ScrimInsetsFrameLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:insetForeground="#4000"
android:id="#+id/container"
></com.test.app.widget.ScrimInsetsFrameLayout>
</FrameLayout>
And also I've set the android:statusBarColor to transparent in themes.
The solution does not work for me. Apparently I am doing something wrong here.
Can someone point out where I am mistaken?
Have you tried
#Override
public void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
as described in the Android documentation

Android Toolbar - How to implement Spinner for navigation mode?

What is currently the correct way to implement the View Control (No. 2 from the below screenshot taken from Android's design guide):
I found this example but when I tried to replicate it, I noticed that methods like:
actionBar.setNavigationMode() are already deprecated.
So how should I implement it? I thought at first that it's a Spinner but I see apparently that it's not exactly the same
and can I still use ActionBar or should I better move to use Toolbar (yes, I am confused...)
As you rightly said, the setNavigationMode() method is now considered passé. To get the spinner in API 21, you need to use the Toolbar in this way:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_actionbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:background="?attr/colorPrimary">
<Spinner
android:id="#+id/spinner_toolbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</android.support.v7.widget.Toolbar
Add the above code to your Activity's layout. To set up the Toolbar in this Activity, you need to do this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
setSupportActionBar(toolbar);
}
Try this. This will work.

setSupportProgressBarIndeterminateVisibility raising java.lang.NullPointerException when used with Android 5.0 SDK (API 21)

My activity class extends android.support.v7.app.ActionBarActivity. I am requesting window feature and calling setSupportProgressBarIndeterminateVisibility() in the onCreate() method as follows:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
setSupportProgressBarIndeterminateVisibility(true);
}
I cannot grasp what's wrong with my code and why I am getting java.lang.NullPointerException raised by the setSupportProgressBarIndeterminateVisibility().
My gradle dependencies contains:
compile 'com.android.support:appcompat-v7:21.0.0'
Does anybody know how to use the indeterminate progress bar in the support.v7 action bar with the API 21?
You need to use Toolbar instead of ActionBar and add the ProgressBar into the Toolbar.
Here is an easy solution to insert indeterminate ProgressBar into Toolbar; turns out it's not difficult at all :) Just put your ProgressBar xml element inside your Toolbar like this:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary">
<ProgressBar
android:id="#+id/progress_spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:indeterminate="true"
android:visibility="gone" />
</android.support.v7.widget.Toolbar>
And then in your AppCompatActivity, simply retrieve the ProgressBar after retrieving the Toolbar and set the ProgressBar to visible or invisible when you need it.
protected void onCreate(Bundle savedInstanceState)
{
setContentView(R.layout.toolbar);
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
progressBar = (ProgressBar) findViewById(R.id.progress_spinner);
//Make progress bar appear when you need it
progressBar.setVisibility(View.VISIBLE);
//Make progress bar disappear
progressBar.setVisibility(View.INVISIBLE);
}
Hope this helps :)
EDIT: replaced ActionBarActivity with AppCompatActivity as per the latest Android support libraries guidelines.
Per this comment on Chris Banes' (the author of AppCompat) AppCompat v21 announcement post:
Either way, progress bar's are not supported on Toolbar anymore.
On why:
Because Toolbar is a highly focused widget. If you want a ProgressBar, add it to the Toolbar yourself (it's just a ViewGroup).
I'd expect more information as the official documentation gets updated here shortly.

how to customize titlebar with an image in android app?

I would customize the title bar of every activity of my application with an icon and a text. I've tried some ways but they doesn't work. can anybody help me?
this is an example of my code:
public class TitleBar extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final boolean customTitleSupported = requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.activity_main);
if ( customTitleSupported ) {
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.titlebar);
}
// user can also set color using “Color” and then “Color value constant”
// myTitleText.setBackgroundColor(Color.GREEN);
}
}
in xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/myTitleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="appuntiiiiiii" />
</LinearLayout>`
I know it might be overkill for your needs but if you really want something cool you should try implementing ActionBar. It's avalible officialy from API11 but you can implement ActionBarSherlock which looks and works pretty much the same. I'm loving it!
As for your code, there is no ImageView so how could you possible display an image. You need an imageView before your TextView

Categories

Resources