Easiest way to re-use layout in multiple activities - java

I'm trying to use a NavBar (http://imgur.com/a/fygH3) at the bottom of each page/activity. when an image is clicked, it opens a new page. Such as a page containing a countdown timer.
Would the best way to do this be load the navbar layout onto the bottom of a page containing a countdown timer.Or is it possible to just use one navbar layout page that has a mainActivity layout, then load a countdownPage into the main activity layout, upon a button press?
This would be done for 5 different activities/pages.

You could create a bottom layout, then add it on your layout's activities.
Generic Layout my_bottom_layout.xml
<RelativeLayout android:id="#+id/bottom_layout">
<RecyclerView/>
</RelativeLayout>
Layout's Activities
<RelativeLayout
android:id="#+id/layout_1">
...
<include layout="#layout/my_bottom_layout" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/layout_2">
...
<include layout="#layout/my_bottom_layout" />
</RelativeLayout>

Related

Android fragment preloading data

Hello I'm trying to make an application that has multiple bottomnaviagtion tabs.
Currently it works because fragment data (for example, images) are static. However, if I want to make them dynamic, I'll have to create a preloader.
So I want to display a loading layout (for example R.layout.loading) UNTIL (async) function has completed and obtained data from server. Then I want to replace the layout in the fragment with a new layout (R.layout.datafragment)
Fragment onCreateView:
return inflater.inflate(R.layout.loading, container, false);
In summary it should work exactly like youtube bottom tabs.
You should create in your activity layout a placeholder for those dynamic fragments and a loader which is by default gone, but when you start loading data from the server you should make it visible (loader). Then, when you load necessary data, just start fragment transition and place whatever fragment you have loaded.
Your parent activity's layout should be like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--Navigation View and Fragment Container-->
</RelativeLayout>
<RelativeLayout
android:id="#+id/progress_layout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"/>
</RelativeLayout>
</RelativeLayout>
Then you simply show/hide progressLayout based on your requirement. Thanks

Create facebook like menu

I want to open a menu of options similar to that of Facebook. I did a search but I did not find anything ...
You will probably be needing to use Bottom Sheets for that :
I - Make a bottomsheet.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="300dp"
android:orientation="vertical"
android:padding="16dp"
app:layout_behavior="#string/bottom_sheet_behavior">
<!-- add your content here -->
</LinearLayout>
II- now in your main activity implement it in a way it is a direct child of a CoordinatorLayout
<include layout="#layout/bottomsheet" />
III- For the JAVA code use the following:
inearLayout bottomSheetViewgroup = (LinearLayout) findViewById(R.id.bottom_sheet);
BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetViewgroup);
To control your bottom sheet (hiding, expanding, dragging, collapsing):
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
PS- you can handle this by :
STATE_EXPANDED: To completely expand the bottom sheet.
STATE_HIDE: To completely hide the bottom sheet.
STATE_COLLAPSED: To set the bottom sheet height with the value set on the peekHeight attribute.

How to use SwipeRefresh's refresh icon Without Listview?

My app consists of different activities where I use a SwipeRefreshLayout to refresh data in listViews by swiping. When this happen a refresh icon is displayed at the top of the screen.
However, for 2 other specific activities that contain no list view, I only want to display the refresh icon while the data is refreshing by using pullToRefresh.setRefreshing(true); & pullToRefresh.setRefreshing(false);. I don't need to use it for swiping.
This is how it looks in my list view layout xml file:
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/pullToRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/ListView"
android:dividerHeight="8dp"
android:divider="#000000"
android:layout_below="#+id/feedLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
Use SwipeRefreshLayout as a regular layout, and call pullToRefresh.setRefreshing(true) when you want to show the loading wheel, and pullToRefresh.setRefreshing(false) to hide it.

Custom ScrollView scrolls to top when a View is added

I am using a custom View by Matt Clark called TwoDScrollView. It allows me to have vertical and horizontal scrolling.
TwoDScrollView has a child RelativeLayout which is the Scrollable layout.
The problem is that when I add a View to the RelativeLayout programmatically, the TwoDScrollView scrolls to top.
Apparently, this also happens on ScrollViews when a RelativeLayout is used so it is not only from TwoDScrollView.
XML:
<com.example.viewmover.TwoDScrollView
android:id="#+id/twoDScrollView1"
android:drawingCacheQuality="low"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="horizontal|vertical">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scene_container"
android:drawingCacheQuality="low"
android:layout_width="1000dip"
android:layout_height="3000dip"
android:background="#drawable/tiledbackground">
</RelativeLayout>
</com.example.viewmover.TwoDScrollView>
Any ideas of how to solve this problem?

Android: Default layout for listitem containing title and subtitle

I have made a listitem, containing a title and a subtitle. Looking to the result, I think it doesn't look really professional. So that's why I'm asking.
The listitem I'm looking for is pretty common (it's used in a lot of apps i.e. Android's default settings menu and it's also shown when you add a listview in the graphical layout editor tab in eclipse).
So my question is: Where can I find a default layout for a listitem with a title and a subtitle?
Resource id is android.R.layout.simple_list_item_2
Upper text line has id android.R.id.text1 and lower one - android.R.id.text2
Layout is located in the <ANDROID_SDK_ROOT>/platforms/<any_api_level>/data/res/layout folder
OR
You can use TwoLineListItem from the default Android controls list(it is located under "Advanced" tab in Eclipse Layout Editor)
OR
You can build your own layout with anything you like(for example LinearLayout with orientation="vertical" and two TextEdits added
So The Best Way Is:- I took out the simple list item 2 and made an layout in my project and with little editing it took the same layout as you may give in android.R.simple_list_item_2
So the code is:-
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?attr/listPreferredItemPaddingLeft"
android:paddingLeft="?attr/listPreferredItemPaddingLeft"
android:paddingEnd="?attr/listPreferredItemPaddingRight"
android:paddingRight="?attr/listPreferredItemPaddingRight">
<TextView android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView android:id="#id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text1"
android:layout_alignStart="#+id/text1"
android:layout_alignLeft="#+id/text1"
android:textAppearance="?attr/textAppearanceListItemSmall" />

Categories

Resources