How to hide white line (picture attached) in Tabbed Activity (ViewPager)? - android

I'm trying to hide actionBar in my tabbed activity, i almost did it, but white line (on the top of the screen ) left on the screen. How to hide it?
My Activity code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(1);
ActionBar bar = getSupportActionBar();
if(bar != null) {
bar.hide();
}
In Manifest i set:
android:theme="#style/Theme.AppCompat.Light.NoActionBar"

Related

Can action bar contain PagerSlidingTabStrip?

I am using Android studio 3.3 canary 8 and my API level is 28. I have an activity which has the action bar and another PagerSlidingTabStrip. The action bar has a title and a close button to go home. Now, I want to get rid of these two toolbars and one toolbar either the action bar or the tabstrip, but I want the close button and the tabs as well. Can the action bar contain tabs? Or can I include the close button in the tabstrip anyhow? If it is not possible then can I do any trick to have something like this? The relevant part of my activity is here:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_info);
ViewPager viewPager = findViewById(R.id.viewPager);
InfoFragmentPagerAdapter adapter = new InfoFragmentPagerAdapter(getSupportFragmentManager());
adapter.addFragment(new InfoAboutFragment (), getString(R.string.about));
adapter.addFragment(new InfoHelpFragment(), getString(R.string.help));
PagerSlidingTabStrip tabsStrip = findViewById(R.id.tabs);
tabsStrip.setViewPager(viewPager);
tabsStrip.setShouldExpand(true);
tabsStrip.setAllCaps(true);
tabsStrip.setTextColor(Color.WHITE);
tabsStrip.setDividerColor(Color.TRANSPARENT);
tabsStrip.setDividerPadding(20);
tabsStrip.setIndicatorColor(Color.WHITE);
tabsStrip.setIndicatorHeight(12);
tabsStrip.setUnderlineColor(Color.TRANSPARENT);
setupActionBar();
}
private void setupActionBar() {
getLayoutInflater().inflate(R.layout.toolbar, findViewById(android.R.id.content));
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setTitle(R.string.about);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_close_white_36dp);
}
}

getSupportActionBar setSubtitle only shows after activity recreation

I have a generic
MyActivity extends AppCompatActivity
I don't override the toolbar with a custom xml defined toolbar, just use the generated one Android provides.
I can set the title via your normal
getSupportActionBar().setTitle("foo");
but setting the subtitle via
getSupportActionBar().setSubtitle("bar");
doesn't set it. It remains blank. I'm doing this onCreate()
(I feel I've done this many times before with no fail)
Although I've noticed if I visit another activity, then return, the subtitle would then show... not on orientation change, not on recreate() but only when I'm returning from an activity.
I'm experiencing this on 5.0 and 7.0
For the time being I'll likely define my own Toolbar and move forward since that seems where most people have solutions for this same problem.
Relevant code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_replenishment_list);
ButterKnife.bind(this);
MyApplication.getInstance().getComponent().inject(this);
setupUI();
}
private void setupUI() {
setupActionBar();
}
private void setupActionBar() {
ActionBar actionBar = getSupportActionBar();
if (actionBar != null) {
//TODO: not working unless activity is recreated...
// explore custom xml defined toolbar
//actionBar.setTitle("different title than what is defined in manifest"); <-- this does work, but not this
actionBar.setSubtitle(UserUtil.getFormattedFirstNameLastName(userService.getUserFromJWT(), this));
}
}
I have put the below code in my onCreate() method.
ActionBar actionBar = getActionBar();
if (actionBar==null) {
System.out.println("TEST NULL");
} else {
System.out.println("TEST NOT NULL");
}
The result is null. When I add the toolbar first it works fine.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getActionBar();
actionBar.setSubtitle("TESTING");
Your getSupportActionBar or getActionBar will return null if you didn't set toolbar to it. You need to set the toolbar to your action bar before using getSupportActionBar or getActionBar.

Change Toolbar & AppBarLayout Background onCreate

I'm trying to have 2 theme options for my app, Dark and Light. When the dark theme is selected I use sharedpreference to save and apply the theme on startup. But when I try to change the background color of the toolbar & appBarLayout like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
long currentTheme = sharedPref.getInt(getString(string.current_theme), 0);
if (currentTheme == 0) {
this.setTheme(R.style.AppTheme_NoActionBar);
}
if (currentTheme == 1) {
this.setTheme(R.style.AppTheme_NoActionBar_Dark);
Toolbar toolbar = (Toolbar) findViewById(id.toolbar);
setSupportActionBar(toolbar);
toolbar.setBackgroundColor(getResources().getColor(ActionbarDark));
}
Logcat returns the error: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v7.widget.Toolbar.setBackgroundColor(int)' on a null object reference
And I cant understand why
Update: Fixed it by using the answer from #Nilesh Rathod and changing the theme of the activity before setting the content view, then after setting content view changing the theme of the Toolbar and AppBarLayout.
You have missed R in findViewById of toolbar
it should like below
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
change your code like this
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
long currentTheme = sharedPref.getInt(getString(string.current_theme), 0);
if (currentTheme == 0) {
this.setTheme(R.style.AppTheme_NoActionBar);
}
if (currentTheme == 1) {
this.setTheme(R.style.AppTheme_NoActionBar_Dark);
getSupportActionBar().setBackgroundDrawable(new ColorDrawable(0xff00DDED));
}
setContentView(R.layout.activity_main);
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
android.app.FragmentManager fragmentmanager = getFragmentManager();
fragmentmanager.beginTransaction()
.replace(R.id.content_frame
, new FirstFragment())
.commit();
DrawerLayout drawer = (DrawerLayout) findViewById(id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, string.navigation_drawer_open, string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
navigationView.getMenu().getItem(0).setChecked(true);
}

Android persistent imageview across tabbed view

I have a tabbed view activity, which lets me scroll through 7 tabs (just the android studio auto generated tabbed view, nothing fancy but included for completeness)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
mSectionsPagerAdapter.addFragment(new Fragment1());
mSectionsPagerAdapter.addFragment(new Fragment2());
mSectionsPagerAdapter.addFragment(new Fragment3());
mSectionsPagerAdapter.addFragment(new Fragment4());
mSectionsPagerAdapter.addFragment(new Fragment5());
mSectionsPagerAdapter.addFragment(new Fragment6());
mSectionsPagerAdapter.addFragment(new Fragment7());
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setCurrentItem(0);
}
Is it possible to place an imageview at the bottom of the tabs that will remain in place when swiping left and right between the tabs? i.e. the imageview stays in place, as though it were floating above the fragments in the tabs, and the rest of the content moves
The answer turned out to be simple, but I'll post it here anyways in the hope that it will help someone
To have an image at the bottom which remains persistent (does not move) across the tabs, just put the image in the layout of the tabbed activity, and it will be drawn over all of the tabs.

Android Navigation Drawer Icon reverting to default when Drawer is open

I'm having a problem with the Navigation Drawer Icon.
We replaced the default "back caret" to use a different icon and it works fine.
However, if the navigation drawer is already open and the user rotates their device, then the icon reverts back to the default caret and won't go back to the custom one until the navigation drawer is closed and the onCreate() method for the activity is called again (usually by rotating the device).
Here is the code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_start);
// set the toolbar as the action bar
Toolbar toolbar = (Toolbar) findViewById(R.id.my_awesome_toolbar);
toolbar.setNavigationIcon(R.drawable.ic_drawer);
setSupportActionBar(toolbar);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new StartFragment())
.commit();
}
//Init the GameLog
GameLog.init(this);
}
/**
* Initializes the DrawerLayout for the particular activity
*/
public static void init(Activity activity) {
mActivity = activity;
mDrawerLayout = (DrawerLayout)activity.findViewById(R.id.drawer_layout);
mRecyclerView = (RecyclerView)activity.findViewById(R.id.left_drawer);
//set adapter
mRecyclerView.setAdapter(mAdapter);
//set layout manager
mRecyclerView.setLayoutManager(new LinearLayoutManager(activity, LinearLayoutManager.VERTICAL, false));
//add a divider between elements
mRecyclerView.addItemDecoration(
new HorizontalDividerItemDecoration.Builder(activity)
.color(Color.WHITE)
.build());
Toolbar toolbar = (Toolbar)((ActionBarActivity)activity).getSupportActionBar().getCustomView();
mDrawerToggle = new ActionBarDrawerToggle(activity, mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close);
// Set the drawer toggle as the DrawerListener
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (activity.getActionBar() != null) {
activity.getActionBar().setDisplayHomeAsUpEnabled(true);
activity.getActionBar().setHomeButtonEnabled(true);
}
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
GameLog.getToggle().syncState();
}
Hopefully this makes sense.
Thanks for any help.
Look into calling ActionBarDrawerToggle#syncState.
https://developer.android.com/reference/android/support/v4/app/ActionBarDrawerToggle.html#syncState()

Categories

Resources