Visible ActionBar with opened NavDrawer - android

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:

Related

Toolbar not responding to Listview scroll events

I have an activity which contains a custom toolbar and a simple listview. I want the toolbar respond when the listview is scrolled. However, it is not working.
Here is the .xml file
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.abdralabs.talksee.HistoryActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/history_appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/history_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/title"
app:layout_scrollFlags="scroll|enterAlways">
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/lv_history"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</ListView>
Here is the .java file
public class HistoryActivity extends AppCompatActivity {
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_history);
toolbar = (Toolbar)findViewById(R.id.history_toolbar);
/*
setSupportActionBar(toolbar);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
*/
toolbar.setTitle("History");
String[] rcArray = {"A","B","C","D","A","B","C","D","A","B","C","D","A","B","C","D","A","B","C","D","A","B","C","D","A","B","C","D"};
ArrayAdapter adapter = new ArrayAdapter<String>(this,
R.layout.list_history, rcArray);
ListView listView = (ListView) findViewById(R.id.lv_history);
listView.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
/*
switch (item.getItemId()){
case android.R.id.home:
NavUtils.navigateUpFromSameTask(this);
break;
}
*/
return super.onOptionsItemSelected(item);
}
}
What have I done wrong?
EDIT:
In response to rafsanahmad007's answer
I have applied the changes as per your suggestion but still there is no change.
When I scroll through the ListView there is no change to the size of the ToolBar. However, when I click on the ToolBar itself and make an up & down motion the ToolBar moves up & down too. What I want to achieve is, when I scroll the ListView downwards the ToolBar should collapse and when I scroll the ListView upwards the ToolBar should get to its normal size/position.
The following pictures depict how the ToolBar is responding currently.
As you can see in the above pics, the ListView is not being scrolled, but the ToolBar itself is being scrolled. What I want is, when I scroll the ListView the ToolBar should scroll only during that time. I hope I have clarified myself.
try this:
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:minHeight="80dp"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed" />
</android.support.design.widget.AppBarLayout>
you can also get Help from Here

Some questions on Android UI

Please, anybody help me. I have a 2 big problem:
App bar moves my left button to center app, right after string:
setSupportActionBar(mToolbar)
buttons - wrap/wrap, tryed to wrap her in RelativeLayout, tryed to set "anchor" parameter - no effect - title moves her then appears.
Fragments appears higher then fragmentContainer - on my appbar.
My idea was - 1 main activity + 3 fragments changes in her bottom part, and settings-activity. Screen applied.
Also I have a question:
For now I changes fragment using .replace - method. If I do it then I send search query and receive list of elements - it would create a new main fragment with empty list? But now it's not important. Of course, I can get fragments by tag, if one exists.
Code.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
style="#style/match">
<android.support.design.widget.AppBarLayout
style="#style/fill"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="72dp"
android:background="#color/colorPrimary"
app:theme="#style/ToolBarStyle"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:layout_collapseMode="pin">
<RelativeLayout
style="#style/wrap">
<ImageView
android:id="#+id/settings_btn"
android:layout_width="64dp"
android:layout_height="64dp"
android:layout_alignParentLeft="true"
android:src="#drawable/servis"
android:contentDescription="#string/settings.title"
android:fitsSystemWindows="true"/>
</RelativeLayout>
<ImageView
android:id="#+id/logout_btn"
android:layout_width="24dp"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="#dimen/spacing_normal_16"
android:layout_weight="0.5"
android:src="#drawable/off"
android:contentDescription="#string/main.logout"
android:cropToPadding="false"/>
</android.support.v7.widget.Toolbar>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/black"/>
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
style="#style/fill"
app:layout_scrollFlags="scroll|snap">
<android.support.design.widget.TabItem
android:id="#+id/search_tab"
style="#style/wrap"
android:text="#string/main.search"/>
<android.support.design.widget.TabItem
android:id="#+id/saved_tab"
style="#style/wrap"
android:text="#string/main.saved"/>
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content"/>
</android.support.design.widget.CoordinatorLayout>
content.xml (like in books):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/fragmentContainer"
style="#style/match"
tools:context=".ui.activities.MainActivity"/>
fragment_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/match">
<android.support.v7.widget.RecyclerView
android:id="#+id/audio_list"
style="#style/recycler_view"/>
... <buttonsLayout>
</RelativeLayout>
MainActivity:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private DataManager mDataManager;
private Toolbar mToolbar;
private ImageView mSettings, mLogout;
private TabLayout mTabLayout;
private TabItem mSearchTab, mSavedTab;
private int accentColorId, primaryColorId;
private String mQuery;
public void setQuery(String query) {
mQuery = query;
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDataManager = DataManager.getInstance();
mToolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
//mToolbar.setTextAlignment(View.TEXT_ALIGNMENT_CENTER);
mSettings = (ImageView) findViewById(R.id.settings_btn);
mSettings.setOnClickListener(this);
mLogout = (ImageView) findViewById(R.id.logout_btn);
mLogout.setOnClickListener(this);
accentColorId = getResources().getColor(R.color.colorAccent);
primaryColorId = getResources().getColor(R.color.colorPrimary);
mTabLayout = (TabLayout) findViewById(R.id.tab_layout);
mSearchTab = (TabItem) findViewById(R.id.search_tab);
mSavedTab = (TabItem) findViewById(R.id.saved_tab);
mTabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
if (tab.getPosition() == 0) {
//mSearchTab.setBackgroundColor(accentColorId);
//mSavedTab.setBackgroundColor(primaryColorId);
showFragment(SearchFragment.newInstance(mQuery), "search");
} else if(tab.getPosition() == 1) {
showFragment(new SavedFragment(), "saved");
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {}
#Override
public void onTabReselected(TabLayout.Tab tab) {}
});
mTabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
signIn();
}
...
private void showFragment(Fragment fragment, String tag) {
FragmentManager fm = getFragmentManager();
fm.beginTransaction()
.replace(R.id.fragmentContainer, fragment, tag)
.addToBackStack(tag)
.commit();
}

PreferenceActivity with Navigation Drawer Throws Null Pointer Exception

I had a PreferenceActivity which was working fine. Then, I decided to add a navigation drawer to it, and edited the layouts according to my other activity layouts which also have working navigation drawers. However, after doing that, my PreferenceActivity stopped opening and started throwing a NPE on start. I can't even open that activity anymore. Problem is in onCreate, obviously, and also in an onClick method as stated in the logcat. Yet, there are two onClick's in this activities onCreate, and I'm not seeing how those result in a NPE, since they are the exact same in my other working activities. My layout xmls are seperated into 3 xmls according to the default DrawerLayout implementation. Without further due, here are the codes and xmls:
PreferenceActivity:
public class FirstYearCoursesActivity extends PreferenceActivity
implements NavigationView.OnNavigationItemSelectedListener {
private AppCompatDelegate mDelegate;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
getDelegate().installViewFactory();
getDelegate().onCreate(savedInstanceState);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first_year_courses);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar((toolbar));
Window window = this.getWindow();
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));
addPreferencesFromResource(R.xml.prefs);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view2);
navigationView.setNavigationItemSelectedListener(this);
View headerView = navigationView.getHeaderView(0);
ImageButton logoutButton = (ImageButton) headerView.findViewById(R.id.logoutButton);
logoutButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(FirstYearCoursesActivity.this, LoginActivity.class);
startActivity(intent);
}
});
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
protected void onPause() {
final DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
final Handler handler = new Handler();
if (drawer.isDrawerOpen(GravityCompat.START)) {
handler.postDelayed(new Runnable() {
#Override
public void run() {
drawer.closeDrawer(GravityCompat.START);
}
}, 200);
super.onPause();
} else {
super.onPause();
}
}
private void setSupportActionBar(#Nullable Toolbar toolbar) {
getDelegate().setSupportActionBar(toolbar);
}
private AppCompatDelegate getDelegate() {
if (mDelegate == null) {
mDelegate = AppCompatDelegate.create(this, null);
}
return mDelegate;
}
#Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
String key = preference.getKey();
switch (key) {
case "math119":
Intent intent = new Intent(FirstYearCoursesActivity.this, Math119Activity.class);
startActivity(intent);
break;
default:
Toast.makeText(this, "Other Click", Toast.LENGTH_SHORT).show();
break;
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// to be filled
int id = item.getItemId();
if (id == R.id.nav_camera) {
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
return true;
}
}
activity.xml:
<android.support.v4.widget.DrawerLayout
android:id="#+id/drawer_layout"
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">
<include
layout="#layout/app_bar_first_year_courses"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view2"
android:layout_width="250dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer"
android:background="#ffffffff"
app:itemTextColor="#000000"
app:itemTextAppearance="#style/TextAppearance.AppCompat.Body1"/>
</android.support.v4.widget.DrawerLayout>
app_bar.xml:
<android.support.design.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"
android:fitsSystemWindows="true"
tools:context=".activities.FirstYearCoursesActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:popupTheme="#style/AppTheme.PopupOverlay"/>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_first_year_courses"/>
</android.support.design.widget.CoordinatorLayout>
content.xml:
<LinearLayout
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".activities.FirstYearCoursesActivity"
tools:showIn="#layout/app_bar_first_year_courses"
android:orientation="vertical">
</LinearLayout>
nav_header.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="140dp"
android:background="#drawable/profilebanner"
android:gravity="bottom"
android:orientation="vertical"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
android:id="#+id/header">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/logoutButton"
android:src="#drawable/ic_power_settings_new_white_18dp"
android:background="#00ffffff"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="30dp"
android:layout_marginRight="10dp"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/headerClick"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:paddingTop="20dp">
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:id="#+id/imageView"
android:background="#drawable/onur"
android:layout_centerVertical="true"
android:layout_alignParentStart="false"
android:layout_alignBottom="#+id/textView3"
android:layout_marginBottom="-20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Onur Çevik"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:id="#+id/textView2"
android:textSize="13sp"
android:layout_toRightOf="#+id/imageView"
android:layout_marginLeft="25dp"
android:layout_marginTop="45dp"/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Environmental Engineering"
android:textSize="11sp"
android:layout_toRightOf="#+id/imageView"
android:layout_marginLeft="25dp"
android:layout_below="#+id/textView2"
android:layout_alignParentStart="false"/>
</RelativeLayout>
</RelativeLayout>
prefs.xml:
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
android:key="firstyearprefs">
<Preference
android:title="Math-119"
android:key="math119"/>
<Preference
android:title="Math-120"
android:key="math120"/>
<Preference
android:title="Phys-105"
android:key="phys105"/>
<Preference
android:title="Phys-106"
android:key="phys106"/>
<Preference
android:title="Chem-107"
android:key="chem107"/>
<Preference
android:title="Eng-101"
android:key="eng101"/>
<Preference
android:title="Eng-102"
android:key="eng102"/>
<Preference
android:title="Ce-101"
android:key="ce101"/>
</PreferenceScreen>
logcat:
02-08 02:10:35.415 30815-30815/com.theoc.proto E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.theoc.proto, PID: 30815
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ListView.setOnItemClickListener(android.widget.AdapterView$OnItemClickListener)' on a null object reference
at android.preference.PreferenceScreen.bind(PreferenceScreen.java:143)
at android.preference.PreferenceActivity.bindPreferences(PreferenceActivity.java:1419)
at android.preference.PreferenceActivity.access$000(PreferenceActivity.java:123)
at android.preference.PreferenceActivity$1.handleMessage(PreferenceActivity.java:230)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5272)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:909)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:704)
EDIT:
One thing worth mentioning is that I triple checked the activity and it's adapter that leads to this PreferenceActivity on list item click. I reversed all my Drawer Layout implementations, and I was able to open this PreferenceActivity through parent activity. Navigation Drawer code, or layouts are the ones messing with it. I'm getting the listview.onclick(adapter) NPE because my list item click on parent activity goes to a null reference, which in this case it the PreferenceActivity itself. Also debug didn't show me anything null in PreferenceActivity's onCreate, except savedInstanceState.
I was able to reproduce the same NPE using your code and fixed it by replacing the addPreferencesFromResource call (note that it's deprecated) using a fragment instead as described in these threads:
Android: Preference Fragment with a Navigation Drawer's Fragment
What to use instead of "addPreferencesFromResource" in a PreferenceActivity?
Although, on my version, the preferences view overlaps the toolbar and I am not sure what the activity layout should be to fix that, maybe it will be OK in yours.

Add non-swipeable Tab to toolbar in android

I am developing an android app. I was able to add toolbar using support library to the app. Now i want to add tabs to the app. But the tab should be non-swipeable. I searched the internet but all the questions and articles were for swipeable tabs.
toolbar.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.support.v7.widget.Toolbar
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbarsdfs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimaryDark"
app:theme="#style/ThemeOverlay.AppCompat.ActionBar">
</android.support.v7.widget.Toolbar>
</LinearLayout>
ToolbarActivty.Java
public class ToolbarActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.toolbar);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbarsdfs);
if (toolbar != null) {
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
}
}
This is what i want to acheive.
If you are not using swipable tabs try tabs by using custome Tabhoast. but the simplest is adding buttons and checnge the fragment above according to button click.

How to show progress spinner in toolbar

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

Categories

Resources