Hide only the Action bar on Scroll not action bar tabs - android

I am getting an issue at trying to hide the action bar while scrolling down.Then while scrolling up,the action bar have to shown again.
For Eg:
I referred this Tutorial.Here you can see the output and respective codes related to hide and show the action bar.
I am showing the output what I get it till now.
After scrolling down:
The screenshot above shown, it hide the action bar tab also.But it have to hide only the action bar.That's the major issue.
After scrolling up:
The screenshot above shows it displays back the action bar with tabs.
TopRatedFragment.java:
import info.androidhive.tabsswipe.R;
import android.app.ActionBar;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.widget.ScrollView;
public class TopRatedFragment extends Fragment implements ViewTreeObserver.OnScrollChangedListener {
private float mActionBarHeight;
private ActionBar actionBar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
final TypedArray styledAttributes = getActivity().getTheme().obtainStyledAttributes(
new int[] { android.R.attr.actionBarSize });
mActionBarHeight = styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
actionBar = getActivity().getActionBar();
((ScrollView)rootView.findViewById(R.id.parent)).getViewTreeObserver().addOnScrollChangedListener(this);
return rootView;
}
#Override
public void onScrollChanged() {
float y = ((ScrollView)getActivity().findViewById(R.id.parent)).getScrollY();
if (y >= mActionBarHeight && actionBar.isShowing()) {
actionBar.hide();
} else if ( y==0 && !actionBar.isShowing()) {
actionBar.show();
}
}
}
fragment_top_rated.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="?android:attr/actionBarSize"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button5"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button" />
.
.
</LinearLayout>
</ScrollView>
My issue is, While scrolling down it only have to hide the action bar only not action bar tabs.

I think this library and its sample could help you.
Look at the top-right gif animation:
I think the sample file is called "ViewPagerTabScrollViewActivity"

Related

WebView in XML not working with Navigation Drawer Fragment

I am new to android and cannot find a solution online. I have a navigation drawer that leads to the fragment 'RecentNews', which is related to it's XML 'fragment_recent_news'. I have a WebView (webview) setup in the XML, and have set up the fragment so that it should load a page, but it does not. What am I doing incorrectly? I am unsure if the way to properly do this has changed in the past year because nothing online has worked. Thanks
fragment_recent_news(xml):
<RelativeLayout 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" tools:context="com.hardingsoftware.hrcfitness.RecentNews">
<WebView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/webview"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
RecentNews:
package com.hardingsoftware.hrcfitness;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
public class RecentNews extends Fragment {
WebView webView;
public RecentNews() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootView = inflater.inflate(R.layout.fragment_recent_news, container, false);
WebView myWebView = (WebView) rootView.findViewById(R.id.webview);
myWebView.loadUrl("www.hrcfitness.com/recent-news/");
return rootView;
}
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
EDIT: I have the page loading now, however I now have another issue, where the webview seems to ignore the relative layout and part of it is lying under the main bar.

Hide support ActionBar while scrolling

I have an activity that has a support action bar, below that a sliding tab layout, below that a listview of images for the corresponding tab. I just want to hide the support action bar not the sliding tab strip and I am not able to hide it.
Here is my activity:
import android.content.Intent;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewTreeObserver;
import android.view.Window;
import android.view.WindowManager;
public class NewGalleriActivity extends AppCompatActivity implements ViewTreeObserver.OnScrollChangedListener {
Toolbar toolbar;
ViewPager viewPager;
NewGalleriPagerAdapter viewPagerAdapter;
SlidingTabLayout slidingTabLayout;
CharSequence Titles[] = {"Wildlife", "Architecture", "Black & White", "Close Up", "Night"};
int numOfTabs = 5;
private float mActionBarHeight;
private ActionBar mActionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
getWindow().requestFeature(Window.FEATURE_ACTION_BAR_OVERLAY);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_galleri);
final TypedArray styledAttributes = getTheme().obtainStyledAttributes(
new int[]{android.R.attr.actionBarSize});
mActionBarHeight = styledAttributes.getDimension(0, 0);
styledAttributes.recycle();
//Initialise views
viewPager = (ViewPager) findViewById(R.id.pager);
slidingTabLayout = (SlidingTabLayout) findViewById(R.id.tabs);
toolbar = (Toolbar) findViewById(R.id.toolbar1);
setSupportActionBar(toolbar);
mActionBar = getSupportActionBar();
int defaultValue = 0;
int page = getIntent().getIntExtra("ARG_PAGE", defaultValue);
viewPagerAdapter = new NewGalleriPagerAdapter(getSupportFragmentManager(), Titles, numOfTabs);
viewPager.setAdapter(viewPagerAdapter);
viewPager.setCurrentItem(page);
slidingTabLayout.setDistributeEvenly(true);
slidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
#Override
public int getIndicatorColor(int position) {
return Color.parseColor("#FAC80A");
}
});
slidingTabLayout.setViewPager(viewPager);
if (android.os.Build.VERSION.SDK_INT >= 21)
{
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(Color.parseColor("#263238"));
}
FloatingActionButton button = (FloatingActionButton) findViewById(R.id.ddd);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(NewGalleriActivity.this, ImageGridActivity.class);
startActivity(intent);
}
});
findViewById(R.id.parent).getViewTreeObserver().addOnScrollChangedListener(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_home, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public void onScrollChanged() {
float y = findViewById(R.id.parent).getScrollY();
if(y >= mActionBarHeight && mActionBar.isShowing()) {
mActionBar.hide();
} else if(y==0 && !mActionBar.isShowing()) {
mActionBar.show();
}
}
}
Here is my layout for the activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#drawable/black"
android:orientation="vertical">
<include layout="#layout/toolbar2" />
<com.pipipzz.simpleapp.SlidingTabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#231F20" />
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#drawable/drop_shadows" />
<android.support.design.widget.FloatingActionButton
android:id="#+id/ddd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="24dp"
android:layout_marginBottom="10dp"
android:src="#drawable/fab_icons"
app:borderWidth="#null"
app:elevation="4dp"
app:fabSize="normal" />
</FrameLayout>
</ScrollView>
</LinearLayout>
Here is my layout for the toolbar:
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFC80A"
android:fitsSystemWindows="true"
android:minHeight="?attr/actionBarSize"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<ImageView
android:id="#+id/tool_logo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="#string/tool_logo"
android:gravity="center_horizontal"
android:src="#drawable/logo_tool" />
</RelativeLayout>
<ImageView
android:id="#+id/tool_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="8dp"
android:contentDescription="#string/tool_search"
android:src="#drawable/search" />
</android.support.v7.widget.Toolbar>
Edit: Here is my layout for fragment that is shown in the tabs.
<FrameLayout 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="#231F20"
tools:context="com.pipipzz.simpleapp.ArchitectureFragment">
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp"
android:scrollingCache="true"
android:smoothScrollbar="true" />
</FrameLayout>
Any help would be highly appreciated.
Please have a look at this library Android-ObservableScrollView
It has a lot of examples and it will be much easier to use it in your case.
There are several examples of using it with Toolbar, that you are currently using in your code.
It would be better if you prodived all source code to run it, maybe you have issue somewhere else.
First about your code. Don't use findViewById whenever you want, it is really heavy method. So when you are doing this each time onScrollViewChanged it makes your app laggy.
Find your scroll view only once in onCreate method
mMainScrollView = findViewById(R.id.parent);
mMainScrollView.getViewTreeObserver().addOnScrollChangedListener(new OnScrollChangedListener() {#Override
public void onScrollChanged() {
int scrollY = rootScrollView.getScrollY(); //for verticalScrollView
// Hide or show toolbar here
}
});
Why not directly to call hide on toolbar without getting support
action bar ? Try do to it directly on toolbar.
If you call hide
method directly on ActionBar/Toolbar does it work ?
Try to do something like this
To hide toolbar
toolbar.animate().translationY(-toolbar.getBottom()).setInterpolator(new AccelerateInterpolator()).start();
And show it again
toolbar.animate().translationY(0).setInterpolator(new DecelerateInterpolator()).start();

How to put a complex Layout within CollapsingToolbarLayout

I am trying to put a complex layout within a CollapsingToolbarLayout. Attached image describes the details.
What I want - Add the described custom layout in my CollapsingToolbarLayout.
What are the problems
CirclePageIndicator is not visible (screenshot below)
ViewPager swiping through the ViewPager gives blank Screens. This is
depite PagerAdapter configured.
Any attempt to bring my custom layout outside CollapsingToolbarLayout
and make it a direct child of AppBarLayout brings a blank screen.
What I have done -
Tried creating a child LinearLayout of CollapsingToolbarLayout - this does not show the CirclePageIndicator and inconsistently shows the ViewPager
Tried creating a child LinearLayout of AppBarLayout - This shows a blank screen.
How do I go about resolving the issue ??
Any help is really appreciated !
activity_main.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="250dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true" >
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:contentScrim="#android:color/darker_gray"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginEnd="64dp"
android:fitsSystemWindows="true">
<!--<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/charts"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.1" />-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/charts" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.viewpagerindicator.CirclePageIndicator
android:id="#+id/circlePageInd"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin"/>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
app:layout_anchor="#id/appbar_layout"
app:layout_anchorGravity="bottom|right|end"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation = "vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Info
Jarlsberg lancashire edam. Dolcelatte hard cheese brie st. agur blue
cheese caerphilly bavarian bergkase cheese and biscuits mascarpone. Cheeseburger swiss bavarian
bergkase cream cheese fromage frais cheesy feet port-salut airedale. St. agur blue cheese rubber
cheese caerphilly cheddar cheesecake cream cheese manchego lancashire. Roquefort squirty cheese
the big cheese."
android:textAppearance="#style/TextAppearance.AppCompat.Title" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Jarlsberg lancashire edam. Dolcelatte hard cheese brie st. agur blue
cheese caerphilly bavarian bergkase cheese and biscuits mascarpone. Cheeseburger swiss bavarian
bergkase cream cheese fromage frais cheesy feet port-salut airedale. St. agur blue cheese rubber
cheese caerphilly cheddar cheesecake cream cheese manchego lancashire. Roquefort squirty cheese
the big cheese." />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
MainActivity.java
package com.deep.;
import android.content.res.Configuration;
import android.graphics.Color;
import android.support.annotation.Nullable;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBar;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.TableLayout;
import com.viewpagerindicator.CirclePageIndicator;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private static final int VIEW_PAGER_COUNT = 3;
/* private ListView listView;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle actionBarDrawerToggle;*/
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
// toolbar.setBackgroundColor(Color.BLUE);
// toolbar.setTitle("Wealth Tracker");
setSupportActionBar(toolbar);
final ActionBar ab = getSupportActionBar();
ab.setTitle("WealthTracker");
ab.setHomeAsUpIndicator(R.drawable.ic_drawer);
ab.setDisplayHomeAsUpEnabled(true);
ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
ScreenSlidePagerAdapter pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
CirclePageIndicator circlePageIndicator = (CirclePageIndicator) findViewById(R.id.circlePageInd);
circlePageIndicator.setViewPager(viewPager);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return new ScreenSlidePagerFragment();
}
#Override
public int getCount() {
return VIEW_PAGER_COUNT;
}
}
public static class ScreenSlidePagerFragment extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// return super.onCreateView(inflater, container, savedInstanceState);
// View view = inflater.inflate(R.layout.fragment_main, container, false);
// return view;
TabLayout tabLayout = new TabLayout(getActivity());
tabLayout.addTab(tabLayout.newTab().setText("Expense"));
tabLayout.addTab(tabLayout.newTab().setText("Inflow"));
tabLayout.addTab(tabLayout.newTab().setText("Investment"));
tabLayout.setTabMode(TabLayout.MODE_FIXED);
tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);
tabLayout.setTabTextColors(Color.WHITE, Color.YELLOW);
Log.i("ScreenSlidePagerFrag", "Returning tab layout");
return tabLayout;
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content view
// boolean drawerOpen = drawerLayout.isDrawerOpen(listView);
// menu.findItem(R.id.action_websearch).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
// if(actionBarDrawerToggle.onOptionsItemSelected(item)){ return true; }
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Not able to align fragment to parent activity without removing navigation bar

I am in the process of writing an android version of a iOS app I have helped create.
I am using a template navigation menu which puts a bar at the top and a menu at the side when dragged out or a button is pressed. This works fine, however the fragment comes with margins which leaves blank spaces between the bar and the layout.
I have tried removing the margins, but they then prevent the menu at the top from showing.
I am not sure which code snipped to include, so I have included the home.xml (fragment) activity_main.xml and the java files associated.
home.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_marginTop="70dp"
android:layout_marginLeft="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/tvNo"
android:layout_gravity="left" />
</LinearLayout>
activity_main.xml
<!-- The main content view -->
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<!-- The ActionBar -->
<include
layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</include>
</FrameLayout>
<!-- The navigation drawer -->
<ListView android:id="#+id/lvDrawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:paddingTop="24dp"
android:divider="#android:color/darker_gray"
android:dividerHeight="0dp"
android:background="#android:color/background_light" />
</uk.co.mrgyro.cropcirclelocatorandroid.FragmentNavigationDrawer>
Home.java
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import uk.co.mrgyro.cropcirclelocatorandroid.R;
public class Home extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.home, container, false);
return rootView;
}
}
MainActivity.java
package uk.co.mrgyro.cropcirclelocatorandroid;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.ListView;
public class MainActivity extends ActionBarActivity {
private FragmentNavigationDrawer dlDrawer;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Set a ToolBar to replace the ActionBar.
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Find our drawer view
dlDrawer = (FragmentNavigationDrawer) findViewById(R.id.drawer_layout);
// Setup drawer view
dlDrawer.setupDrawerConfiguration((ListView) findViewById(R.id.lvDrawer), toolbar,
R.layout.drawer_nav_item, R.id.flContent);
// Add nav items
dlDrawer.addNavItem("First", R.drawable.ic_one, "First Fragment", Home.class);
dlDrawer.addNavItem("Second", R.drawable.ic_two, "Second Fragment", SecondFragment.class);
dlDrawer.addNavItem("Third", R.drawable.ic_three, "Third Fragment", ThirdFragment.class);
// Select default
if (savedInstanceState == null) {
dlDrawer.selectDrawerItem(0);
}
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
// If the nav drawer is open, hide action items related to the content
if (dlDrawer.isDrawerOpen()) {
// Uncomment to hide menu items
// menu.findItem(R.id.mi_test).setVisible(false);
}
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
// Uncomment to inflate menu items to Action Bar
// inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (dlDrawer.getDrawerToggle().onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
dlDrawer.getDrawerToggle().syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
dlDrawer.getDrawerToggle().onConfigurationChanged(newConfig);
}
}
This is how it looks with the margins added
This is how it looks with both margins in home.xml set to 0
styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
The appearance I would like is like the first image, except with no blank space.
Thank you in advance
I dont understand very well your activities design and your layouts. If you need a navigation drawer and an action bar, I would just create one activity which extends ActionBarActivityand the navigation drawer written in the main xml. then in the content side of the navigation drawer place your activity content. In the code retrieve the action bar and the drawer and customize them as you want. You wont probably have those issues with margins etc and would be much more clean.
Remove
android:layout_marginTop="70dp"
android:layout_marginLeft="20dp"
from your Home layout, and
android:fitsSystemWindows="true"
from your activity layout.

How to Change a size of fragment?

Pretty new to android development, I need to know how I can change the size of my ListView in my fragment pragmatically to cover the entire screen? match_parent, fill_parent didn't work.
I have hard coded values of 500dp
Here is my fragment
<RelativeLayout 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"
tools:context=".MainActivity">
<ListView
android:id="#android:id/list"
android:layout_width="500dp"
android:layout_height="500dp"
android:divider="#null"
android:dividerHeight="0dp"
android:layout_marginTop="0dp">
</ListView>
</RelativeLayout>
and here is JAVA my fragment code:
import android.app.ListFragment;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import java.util.List;
public class MyFragment extends ListFragment {
List<data> flowers = new datadata().getdatas();
public MyFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
dataArrayAdapter adapter = new dataArrayAdapter(getActivity(),
R.layout.data_listitem,
flowers);
setListAdapter(adapter);
}
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.my_fragment, container, false);
return rootView;
}
}
here is my main Activity class
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/myContainer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbarStyle="insideOverlay"
android:scrollbars="none"
android:fadeScrollbars="true"
android:fitsSystemWindows="true"
android:layout_gravity="top"
>
</ScrollView >
and this is my main activity java code:
package mshirvan.example.com.netplex;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Fragment;
import android.content.Intent;
import android.media.Image;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import android.view.View.OnClickListener;
import data.row1;
public class MainActivity extends Activity {
public static float screenx = 0;
public static float screeny = 0;
public static int screenpixelx = 0;
public static int screenpixely = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ScreenUtility utility = new ScreenUtility(this);
screenx = utility.getWidth();
screeny = utility.getHeight();
screenpixelx = utility.getPixelWidth();
screenpixely = utility.getPixelHeight();
MyFragment frag = new MyFragment();
getFragmentManager().beginTransaction()
.add(R.id.myContainer, frag)
.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
ScreenUtility utility = new ScreenUtility(this);
String output = "Width: " + utility.getWidth() + ", " +
"Height: " + utility.getHeight();
return true;
}
return super.onOptionsItemSelected(item);
}
}
Add the following to your fragment RelativeLayout. This should be as follows:
android:layout_width="match_parent"
android:layout_height="500dp"
For example the map layout fragment should be as follows :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="500dp"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
>
<com.google.android.gms.maps.MapView
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true" />
</RelativeLayout>
Then add this fragment layout in the main layout.
put these in your listview too:
android:layout_width="match_parent"
android:layout_height="match_parent"
or you can change the layoutparameters to match_parent in yout fragment
that contains the listview.
you have to set the fragment container height to match parent not ListView. in your case ,ListView will fill it's parent height. ListView's parent is RelativeLayout and the RelativeLayout height is matchParent that means , RelativeLayout will also fill it's container and it's container is your fragment place holder that defined in your activity's layout file. you have to set that container's height to matchParent.
I think the problem is that you are trying to add fragment to ScrollView. use FrameLayout with android:layout_height=match_parent for fragment's container and if you have to add multiple fragments, then define multiple FrameLayouts in your activity's layout or add them dynamically.

Categories

Resources