How to show progress spinner in toolbar - android

I want to show progress spinner on toolbar while performing background task in UIfragment. I want to use material design for my app, so I set theme with no action bar and used toolbar as action bar. I have tried setProgressBarIndeterminateVisibility(Boolean.TRUE) but its not working.

i solved it by just placing the progress bar inside the toolbar in parent activity layout and then from child fragment accessed the progress bar and showed it when needed
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:elevation="3dp"
android:minHeight="?attr/actionBarSize"
android:theme="#style/Base.ThemeOverlay.AppCompat.Dark"
local:popupTheme="#style/ThemeOverlay.AppCompat.Light" >
<ProgressBar
android:id="#+id/toolbar_progress_bar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:indeterminateTint="#cccccc"
android:indeterminateTintMode="src_in"
android:indeterminate="true"
android:layout_gravity="right"
android:visibility="gone"
/>
</android.support.v7.widget.Toolbar>

If you extends from a ActionBarActivity, try this:
public class MainActivity extends ActionBarActivity {
boolean showUp=true;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
setContentView(R.layout.activity_main);
setSupportProgressBarIndeterminateVisibility(Boolean.TRUE);
Button b = (Button) findViewById(R.id.myButton);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(showUp){
setSupportProgressBarIndeterminateVisibility(Boolean.FALSE);
}else {
setSupportProgressBarIndeterminateVisibility(Boolean.TRUE);
}
showUp=!showUp;
}
});
}

Related

Dynamically change the FAB in MainActivity for Android

I have the following XML layout
<androidx.coordinatorlayout.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/fragment_content"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.bottomappbar.BottomAppBar
android:id="#+id/bottomAppBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:fabCradleMargin="20dp"
app:fabCradleVerticalOffset="10dp"
app:fabCradleRoundedCornerRadius="20dp"
android:layout_gravity="bottom">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginEnd="16dp"
android:background="#android:color/transparent"
app:menu="#menu/bottom_nav_menu" />
</com.google.android.material.bottomappbar.BottomAppBar>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="#id/bottomAppBar" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
The associated Java file is the following:
public class MainActivity extends AppCompatActivity {
BottomNavigationView bottomNavigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bottomNavigationView = findViewById(R.id.bottomNavigationView);
FloatingActionButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Here's a Snackbar", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
My question is, I would like to assign an image source dynamically as well as an action based on which of the navigation fragment the user is on.
So when the app opens it would default to the fab button having a '+' which allows users to add items to list.
If they hit the setting nav button I would like for the image to be gear and if pressed it opens the user setting prefs.
I am just not sure how to achieve this?
UPDATE:
if (bottomNavigationView.getMenu().findItem(R.id.miHome).isChecked()) {
// Set icon image for FAB
fab.setImageResource(R.drawable.ic_add);
} else if (bottomNavigationView.getMenu().findItem(R.id.miSettings).isChecked()) {
fab.setImageResource(R.drawable.ic_settings);
fab.setBackgroundColor(Color.RED);
}
The behavior is that it will show the + sign on load, since the dashboard is the default at app launch, but when I hit the settings nav button, the FAB icon does NOT change to the gear and the background color does NOT turn red!?
Can anyone advise what I am doing wrong?
Using if (bottomNavigationView.getMenu().findItem(R.id.miHome).isChecked()) condition will be triggered only once, but you need to register a listener to the lifetime of the lifecycle owner of the bottomNavigationView which is the MainActivity.
This is typically the OnNavigationItemSelectedListener
bottomNavigationView.setOnNavigationItemSelectedListener(
new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
if (item.getItemId() == R.id.miHome) {
fab.setImageResource(R.drawable.ic_add);
} else if (item.getItemId() == R.id.miSettings) {
fab.setImageResource(R.drawable.ic_settings);
fab.setBackgroundColor(Color.RED);
}
return true;
}
});

How to add "UP" back button in fragment (Fragment to Activity)

I want to go from fragment to activity using back button using toolbar back icon.
The fragment is my navigation drawer item & activity is my MainActivity.
How do I do it?
You can use app:navigationIcon="?attr/homeAsUpIndicator" for that back navigation icon.
<android.support.v7.widget.Toolbar
android:id="#+id/toolbarId"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:navigationIcon="?attr/homeAsUpIndicator"/>
For navigation:
Toolbar toolbar = (ToolBar) getActivity().findViewById(R.id.toolbarId);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
getActivity().onBackPressed();
}
});
Call this method in your fragment onCreateView
public void showBackButton() {
if (getActivity() instanceof ActionBarActivity) {
((ActionBarActivity) getActivity()).getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
Try this worked for me :
in XML:
<android.support.v7.widget.Toolbar
android:id="#+id/profileToolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
Create an back arrow icon in drawable folder. Name it 'ic_back_button'.
Not sure how :-
just right click on drawable > new > ImageAsset > Clip Art > Search back > select > OK > Finish (don't forget to change the name).
then Inside your fragment in onCreateView :
Toolbar toolbar = view.findViewById(R.id.profileToolbar);
toolbar.setNavigationIcon(R.drawable.ic_back_button);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().onBackPressed();
}
});
It might help in case if you want to come back from fragment to previous location.
Put this in your class with navController.
#Override
public boolean onSupportNavigateUp() {
navController.navigateUp();
return super.onSupportNavigateUp();
}
don't forget implement relevant dependency such as navigation and navigation UI.
Add this xml code to your fragment and try
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#FFFFFF"
android:layout_gravity="center"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title Here"
android:typeface="serif"
android:layout_gravity="center"
android:id="#+id/toolbar_title"
android:textSize="20sp"
android:textColor="#android:color/black"/>
<ImageView
android:id="#+id/ivback_water"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:padding="15dp"
android:scaleType="fitCenter"
android:layout_gravity="left"
android:background="#drawable/ic_arrow_back_black_24dp" />
</android.support.v7.widget.Toolbar>
You can easily do that, if you are using a Custom back button that is placed on your Custom top app bar, in the button's onClick() function you can call.. getActivity().onBackPressed();
it would work the same as if you have clicked the android navigation's back button...

How to go back by using action bar in android

I have to get this as a result:
Moreover, how do I add colour in the action bar's background?
Try this
Create toolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/orange"
android:theme="#style/AppTheme.Toolbar.Theme"/>
Include this toolbar in your activitys xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/app_bg"
android:orientation="vertical"/>
<include
android:id="#+id/tool_bar"
layout="#layout/toolbar" />
</LinearLayout>
create your activity.java
public class MainActivity extends AppCompatActivity {
protected ActionBar actionBar;
protected Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_profile);
toolbar = (Toolbar) findViewById(R.id.tool_bar);
setSupportActionBar(toolbar);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
finish(); //here add your back function code.
return true;
default:
return super.onOptionsItemSelected(item);
}
}
}
Click here to add back action button in your actionbar and add onClick methocd for that button.
For adding background to your actionbar refer this.
Hope this helps.

Visible ActionBar with opened NavDrawer

Is it possible to keep the actionbar visible using the mikepenz's material drawer library?
Yes this is possible. Just see the sample application. This contains the CustomContainerActivity which showcases how you can have a Drawer which is below the Toolbar
public class CustomContainerActivity extends AppCompatActivity {
//save our header or result
private Drawer result = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sample_custom_container_dark_toolbar);
// Handle Toolbar
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setTitle(R.string.drawer_item_custom_container_drawer);
//Create the drawer
result = new DrawerBuilder(this)
//this layout have to contain child layouts
.withRootView(R.id.drawer_container)
.withToolbar(toolbar)
.withActionBarDrawerToggleAnimated(true)
.addDrawerItems(
new PrimaryDrawerItem().withName(R.string.drawer_item_home).withIcon(FontAwesome.Icon.faw_home),
new PrimaryDrawerItem().withName(R.string.drawer_item_free_play).withIcon(FontAwesome.Icon.faw_gamepad),
new PrimaryDrawerItem().withName(R.string.drawer_item_custom).withIcon(FontAwesome.Icon.faw_eye),
new SectionDrawerItem().withName(R.string.drawer_item_section_header),
new SecondaryDrawerItem().withName(R.string.drawer_item_settings).withIcon(FontAwesome.Icon.faw_cog),
new SecondaryDrawerItem().withName(R.string.drawer_item_help).withIcon(FontAwesome.Icon.faw_question).withEnabled(false),
new SecondaryDrawerItem().withName(R.string.drawer_item_open_source).withIcon(FontAwesome.Icon.faw_github),
new SecondaryDrawerItem().withName(R.string.drawer_item_contact).withIcon(FontAwesome.Icon.faw_bullhorn)
)
.withSavedInstance(savedInstanceState)
.build();
}
#Override
protected void onSaveInstanceState(Bundle outState) {
//add the values which need to be saved from the drawer to the bundle
outState = result.saveInstanceState(outState);
super.onSaveInstanceState(outState);
}
#Override
public void onBackPressed() {
//handle the back press :D close the drawer first and if the drawer is closed close the activity
if (result != null && result.isDrawerOpen()) {
result.closeDrawer();
} else {
super.onBackPressed();
}
}
}
Also note the different xml layout which is used in this Activity.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:elevation="4dp" />
<!-- the layout which will contain (host) the drawerLayout -->
<FrameLayout
android:layout_below="#id/toolbar"
android:id="#+id/drawer_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- the layout which will be the content of the activity (which will be hosted inside the drawer (NOT the list of the drawer)) -->
<FrameLayout
android:id="#+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtLabel"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test"
android:textSize="16sp" />
</FrameLayout>
</FrameLayout>
</RelativeLayout>
This will produce the following:

Access Toolbar (and its children) from fragment?

I'm building an application using the v7.widget.Toolbar component.
I'm able to add the toolbar to my activity, but I don't know how (and what is the right way?) to access it from the activity's fragment.
Here's the toolbar
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
app:theme="#style/MainActionBar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
tools:context=".MainActivity">
<ImageView
android:background="#drawable/icon_1"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:layout_marginLeft="19dp"
android:id="#+id/menu_1"
android:layout_gravity="center_vertical|left"
android:src="#drawable/menu_burger"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageView
android:layout_marginRight="19dp"
android:paddingLeft="1dp"
android:paddingRight="1dp"
android:id="#+id/menu_2"
android:layout_gravity="center_vertical|right"
android:src="#drawable/icon_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Here's how I use it in the activity:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<include layout="#layout/view_home_toolbar" />
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
...
...
</LinearLayout>
In my activity's onCreate:
Toolbar actionBar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(actionBar);
And now, for example, I want to access "#id/menu_1" from the Toolbar in my fragment, How do I do that?
getSupportActionBar() gives me the ActionBar, but I need the Toolbar
Thanks in advance!
no need to code a lot: follow this
in Kotlin:
var toolbar = activity.findViewById<Toolbar>(R.id.toolbar)
toolbar.setTitle("Rashid")
in Java:
Toolbar toolbar = getActivity().findViewById(R.id.toolbar);
toolbar.setTitle("Rashid")
I'm not sure if this works, but you can try it.
final AppCompatActivity act = (AppCompatActivity) getActivity();
if (act.getSupportActionBar() != null) {
Toolbar toolbar = (Toolbar) act.getSupportActionBar().getCustomView();
}
Toolbar is essentially a ViewGroup.
The communication between Activity and Fragments (the same holds for any Java components) should be done through an Interface.
For your case define an interface
public interface IOperateOnToolbar {
//methods allowing to communicate with toolbar
public void setToolbarColor(Color c);
public void setImageView1Resource(int resID);
public void setImageView2Resource(int resID);
//.....
}
So you have an Activity
public class MainActivity extends AppCompatActivity implements IOperateOnToolbar {
//onCreate, onDestroy ...
public void setToolbarColor(Color c) {
//implementation follows
}
public void setImageView1Resource(int resID) {
imageView1.setBackgroundResource(resID);
}
public void setImageView2Resource(int resID) {
imageView2.setBackgroundResource(resID);
}
}
And your Fragment
public class MyFragment extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
IOperateOnToolbar toolbarCallback = (IOperateOnToolbar) getActivity();
}
}
Using the interfaces for communication between components has the following benefits:
If you replace the "MainActivity" with another one ("ActivityX" which also implements IOperateOnToolbar), you don't have to edit anything in "MyFragment"
"MyFragment" has access only to those methods, which are exposed by IOperateOnToolbar interface.
Please chceck this docu, where google advises the communication (between fragments, but should be the in principle the same) using interfaces.
this will do the job for you
((MainAcivity)this.getActivity()).getToolbar();
Here you can access toolbar children:
TextView toolbarTextView = (TextView) ((MainActivity) this.getActivity()).findViewById(R.id.toolbarTitle);
toolbarTextView.setText("Hello");
In my case I has an activity and a navigation drawer that replaces this activity fragment with other fragments.
If you implement toolbar views (spinners, Buttons..) in the main activity,
the fragments that will be replaced in this activity can see toolbar and interact with its views.
else if, you need to handle actions depending on this views, you can use: getActivity.findViewById(); inside fragment.

Categories

Resources