Android: Adapting app to small screens with layouts including other layouts - java

I finished my app with all my xml layouts in the "layout" folder, which is used by normal-sized screens. So now I am starting with small screens and create a folder called "layout-small" right under the "layout" folder in "res".
I did some modifications in the smaller layout with smaller buttons and margins and then tried to running it on a normal screen and a small screen to see if each screen would use the layout it should, but both use the normal layout. The small screen doesn't use the small layout.
I believe this is due to the fact that the Java class that uses the setContent() method uses a layout which in turn includes another layout which also includes a third layout.
Here is my code for more clarity. The 3 activity_main.xml app_bar_main.xml and content_main.xml layout files are in res/layout and res/layout-small as well, with differences in button sizes only in content_main.xml.
Home.java :
public class Home extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
/**fields*/
private Button butVentes, butLocations, butRecherche, butFavoris, butContact, butSocial; //Buttons for home screen
private Toolbar toolbar;
private DrawerLayout drawer;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBA_8888);
setContentView(R.layout.activity_main);
/**toolbar*/
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/**drawer*/
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.addDrawerListener(toggle);
toggle.syncState();
/**navigationView*/
navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
}
/*...*/
}
activity_main.xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/activity_main_drawer" />
app_bar_main.xml :
<?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"
android:fitsSystemWindows="true"
tools:context=".MainActivities.Home">
<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_main" />
content_main.xml :
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/home_background_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/app_accueil_background"
android:orientation="horizontal"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivities.Home"
tools:showIn="#layout/app_bar_main">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center|bottom"
android:layout_marginBottom="40dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="240dp"
android:orientation="horizontal">
<Button
android:id="#+id/buttonVentes"
android:layout_width="60dp"
android:layout_height="48dp"
android:layout_marginRight="5dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/ic_view_list"
android:paddingTop="10dp"
android:text="#string/content_main_vente"
android:textColor="#color/colorBlackText"
android:textSize="13sp" />
<Button
android:id="#+id/buttonLocations"
android:layout_width="60dp"
android:layout_height="48dp"
android:layout_marginRight="5dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/ic_view_list"
android:paddingTop="10dp"
android:text="#string/content_main_location"
android:textColor="#color/colorBlackText"
android:textSize="13sp" />
<Button
android:id="#+id/buttonRecherche"
android:layout_width="60dp"
android:layout_height="48dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/ic_search"
android:paddingTop="10dp"
android:text="#string/content_main_recherche"
android:textColor="#color/colorBlackText"
android:textSize="13sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:orientation="horizontal">
<Button
android:id="#+id/buttonFavoris"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_marginRight="14dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/ic_star_accueil"
android:paddingTop="10dp"
android:text="#string/content_main_favoris"
android:textColor="#color/colorBlackText"
android:textSize="13sp" />
<Button
android:id="#+id/buttonContact"
android:layout_width="100dp"
android:layout_height="80dp"
android:layout_marginRight="14dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/ic_contacts"
android:paddingTop="10dp"
android:text="#string/content_main_contact"
android:textColor="#color/colorBlackText"
android:textSize="13sp" />
<Button
android:id="#+id/buttonSocial"
android:layout_width="100dp"
android:layout_height="80dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/ic_share"
android:paddingTop="10dp"
android:text="#string/content_main_social"
android:textColor="#color/colorBlackText"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>

instead of creating other layout folders, just maintain different dimension folders like :
values-sw320dp - 1dp
values-sw360dp - 1.12dp
values-sw410dp - 1.28dp
values-sw480dp - 1.5dp

Related

FloatingActionButton on top of action bar

I'm trying to put a floatingActionButton on top of an actionBar, is it possible? and how can i do it?
Below you will find my current XML:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listframe">
<ImageView
android:id="#+id/arrowTopRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:layout_marginTop="70dp"
android:layout_marginRight="70dp"
android:src="#mipmap/cursor" />
<TextView
android:id="#+id/textNoData"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:layout_marginBottom="240dp"
android:layout_marginRight="80dp"
android:layout_marginLeft="80dp"
android:layout_gravity="center_horizontal"
android:text="#string/empty_list_of_codes" />
<ImageView
android:id="#+id/keyBlurry"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:src="#mipmap/keyblurry"
android:scaleType="center" />
<ImageView
android:id="#+id/LogoDown"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="40dp"
android:layout_marginRight="40dp"
android:src="#mipmap/logoblack" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/buttonAddLocation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|end"
android:layout_margin="16dp"
app:maxImageSize="56dp"
android:elevation="8dp"
app:srcCompat="#mipmap/addlocation" />
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:id="#+id/list"/>
</FrameLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
And in the mail class, I'm adding my actionBar programatically:
actionBar = ((MainActivity)currentActivity).getSupportActionBar();
actionBar.setTitle(R.string.main_title);
I tried playing with the position of my floatingButton but it ends up behind the actionBar. Any help would be really appreciated!
Stop using default ActionBar, use a Toolbar instead.
Place a FloatingActionButton over the Toolbar in your layout.xml.
Below is just for general example. Some details should differ and depend on your actual situation.
Modify themes.xml from:
<!-- Base application theme. -->
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
to:
<!-- Base application theme. -->
<style name="Theme.MyApplication" parent="Theme.MaterialComponents.DayNight.NoActionBar">
layout/activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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">
<androidx.appcompat.widget.Toolbar
android:background="#color/purple_200"
android:id="#+id/toolbar"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:layout_marginEnd="150dp"
android:src="#android:drawable/ic_input_add"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}

How to set the ActionBar title in middle instead of the default left aligned position?

Trying to set the ActionBar title in middle instead of the default left aligned position. To do so, based on other answers this is what I've done:
Toolbar myToolbar = (Toolbar) findViewById(R.id.toolbarDownloads);
setSupportActionBar(myToolbar);
if(getSupportActionBar()!=null)
{
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_layout);
}
actionbar_layout.xml
<?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="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Downloads"
android:layout_centerInParent="true"
android:textColor="#000000"
android:id="#+id/mytext"
android:textSize="18sp" />
</RelativeLayout>
However, this does not produce the intended result (ie this renders the text however still is left aligned), what's wrong with this approach and how to fix this?
try this use custom Toolbar
<android.support.v7.widget.Toolbar
android:id="#+id/ar_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="exitUntilCollapsed"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Center"
android:orientation="vertical">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
now in java file
private Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
TextView mTitle = (TextView) toolbar.findViewById(R.id.toolbar_title);
mTitle.setText("Nilesh Rathod");
setSupportActionBar(toolbar);
Simple approach use android:layout_gravity="center" on your TextView and hide default title using below code after that initialize your TextView and set value
Inside your activity:
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayShowTitleEnabled(false);
toolbar_title = (TextView) findViewById(R.id.toolbar_title);
toolbar_title.setText("My Custom center title");
Custom toolbar XML Code:
<?xml version="1.0" encoding="utf-8"?>
<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/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
app:contentInsetStartWithNavigation="0dp">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Toolbar Title" />
</android.support.v7.widget.Toolbar>
You need to use Toolbar in your xml file and you can use TextView inside Toolbar. You can add custom view into Toolbar. Once you done with xml file then you can define Toolbar into Activity class.
<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
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="58dp"
android:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
android:titleTextColor="#ffffff">
<TextView
android:id="#+id/mytext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Downloads"
android:textColor="#000000"
android:textSize="18sp" />
</android.support.v7.widget.Toolbar>
//Rest of your code
</LinearLayout>
In Activity class you can define as
Toolbar toolbar;
toolbar = (Toolbar) findViewById(R.id.ar_toolbar);
toolbar.setTitle("");
You don't need a Toolbar for this. Just use a theme like Theme.AppCompat.Light.DarkActionBar with ActionBar in your activity and use the following code:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (getSupportActionBar() != null) {
getSupportActionBar().setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM);
getSupportActionBar().setCustomView(R.layout.actionbar_layout);
}
//your code
}
<?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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:minHeight="?attr/actionBarSize"
app:contentInsetStart="0dp">
<TextView
android:id="#+id/back_txt"
style="#android:style/TextAppearance.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:gravity="center"
android:text="Back" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Downloads"
android:layout_gravity="center|center_horizontal|center_vertical"
android:layout_marginTop="20dp"
android:layout_centerInParent="true"
android:textColor="#000000"
android:id="#+id/mytext"
android:textSize="18sp" />
<TextView
android:id="#+id/filter_btn"
style="#style/Base.TextAppearance.AppCompat.Medium"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical|center_horizontal"
android:layout_margin="10dp"
android:drawablePadding="5dp"
android:gravity="center"
android:text="Back"
android:textColor="#android:color/black" />
</android.support.v7.widget.Toolbar>
</LinearLayout>

Navigation View in specific activity

I have a problem with Navigation View located under Toolbar. I've read that to set it up correctly apps should use Fragments instead of Activities, but unfortunately I think that it's not my case, coz the whole app is already written using Activities.
I could make NavView work nice in MainActivity (because toolbar is added in it's xml), but the problem is that I don't need NavView on start screen, only in specific activity.
Is it possible to add NavView to only one activity correctly without moving to Fragments?
MainActivity.xml (where it worked perfectly):
<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:background="#drawable/bg"
tools:context="ru.asmodeoux.g_lounge.MainActivity">
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="59dp"
android:layout_height="59dp"
android:layout_gravity="top|left"
android:layout_margin="#dimen/fab_margin"
app:backgroundTint="#color/FAB_color"
app:elevation="24dp"
app:layout_anchor="#+id/include"
app:layout_anchorGravity="bottom|right"
app:srcCompat="#drawable/call" />
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="44dp"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:elevation="0dp"
android:minHeight="0dp"
android:textAlignment="center"
app:popupTheme="#style/AppTheme.AppBarOverlay" />
</android.support.design.widget.AppBarLayout>
<include
android:id="#+id/include"
layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
aboutProduct.xml (where I need to show NavNiew):
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="false">
<android.support.design.widget.CoordinatorLayout
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/bg">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#drawable/bg"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:id="#+id/productRoot"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:adjustViewBounds="true"
android:id="#+id/productImg"
/>
<TextView
android:id="#+id/productDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/productImg"
android:layout_marginBottom="75dp"
android:layout_marginTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="Place for product description."
android:textAlignment="textStart"
android:textColor="#color/white"
android:textSize="19sp"
android:textStyle="italic" />
</RelativeLayout>
</ScrollView>
<android.support.design.widget.FloatingActionButton
android:id="#+id/productAdd"
android:layout_width="59dp"
android:layout_height="59dp"
android:layout_gravity="bottom|end"
android:layout_margin="#dimen/fab_margin"
android:backgroundTint="#color/FAB_color"
android:elevation="24dp"
app:srcCompat="#drawable/buy" />
<TextView
android:id="#+id/productPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|start"
android:layout_marginBottom="#dimen/fab_margin"
android:background="#drawable/rectangle_rounded_some"
android:layout_marginLeft="#dimen/fab_margin"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="100 руб."
android:textAllCaps="false"
android:textColor="#color/black"
android:textSize="37sp" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
app:headerLayout="#layout/navigation_header"
android:background="#color/white"
android:layout_height="match_parent"
android:layout_gravity="start"
app:menu="#menu/drawer" />
</android.support.v4.widget.DrawerLayout>
Header layout:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/gray">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="70dp"
android:text="Что-то ещё?"
android:layout_marginStart="14dp"
android:textColor="#color/white"
android:textAlignment="textStart"
android:layout_marginBottom="8dp"
android:textSize="18sp"
android:textStyle="bold" />
</RelativeLayout>
And a screenshot of how NavView in "aboutProduct" class looks now here
And how it should look like here
As a result I think that the easiest way is to set toolbar there in Theme Editor to "NoActionBar" and add toolbar to every activity by hands.

RecyclerView Becomes Focused when DrawerLayout is Closed

This is sort of a strange bug I discovered.
But if I have a RecyclerView in a fragment and I open then close my DrawerLayout, my RecyclerView comes in focus. That means closing my DrawerLayout will cause the ScrollView to jump to my RecyclerView. Obviously, I would like the ScrollView's position to not move when the DrawerLayout is closed, and definitely not settle on my RecyclerView.
CODE
Throughout my application, I have RecyclerViews contained within ScrollViews, similar to this setup:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="180dp">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/header_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:tint="#80000000"
android:scaleType="centerCrop"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true">
<TextView android:id="#+id/header_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:paddingTop="2dp"
android:paddingBottom="2dp"
android:textColor="#color/text_SecondaryColor"
android:textSize="#dimen/header_xxlarge"/>
<View android:id="#+id/divider"
android:layout_width="0dp"
android:layout_height="#dimen/hr_thick"
android:background="#color/accent_PrimaryColor"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_margin="20dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button android:id="#+id/button_more"
android:layout_marginTop="6dp"
android:layout_marginBottom="6dp"
android:layout_width="400px"
android:layout_height="100px"
android:layout_gravity="center"
android:gravity="center"
android:background="#drawable/button_dark"
android:textColor="#drawable/button_dark_textcolor"/>
</LinearLayout>
</LinearLayout>
</ScrollView>
And my DrawerLayout's XML design is arranged like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include
android:layout_height="wrap_content"
android:layout_width="match_parent"
layout="#layout/toolbar" />
</LinearLayout>
<FrameLayout
android:id="#+id/contentframe"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#color/background_Secondary">
<!-- Relative Layout : Back Button -->
<RelativeLayout
android:id="#+id/menu_backcontainer"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize">
<ImageView
android:id="#+id/menu_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="20dp"
android:src="#drawable/angle"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
<!-- List View : Menu Drawer Content -->
<ListView
android:id="#+id/menu"
android:layout_below="#id/menu_backcontainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice"
android:background="#color/background_PrimaryDarkColor">
</ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
I am not sure what other Java Code I need to include here but this is how I instantiate my RecyclerView:
recyclerView = (RecyclerView)view.findViewById(R.id.recyclerview);
LinearLayoutManager layoutManager = new LinearLayoutManager(getContext().getApplicationContext());
recyclerView.setLayoutManager(layoutManager);
recyclerView.setNestedScrollingEnabled(false);
And For My DrawerLayout:
drawerlayout = (DrawerLayout)findViewById(R.id.drawer);
menu = (ListView)findViewById(R.id.menu);
MenuAdapter adapter = new MenuAdapter(
this,
R.layout.listview_menuitem,
menuItemArray);
menu.setAdapter(adapter);
menu.setOnItemClickListener(new menuItemClickListener());
menuToggle = new ActionBarDrawerToggle(
this,
drawerlayout,
toolbar,
R.string.app_name,
R.string.app_name);
drawerlayout.setDrawerListener(menuToggle);
menuToggle.syncState();
Gee, it is amazing what a moment away from the computer and a cup of coffee will do.
I simply set the focusableInTouchMode attribute of my FrameLayout to true.
I guess this nullifies the RecyclerViews desire to be the focused element in the ScrollView when I touch anywhere on the screen after the DrawerLayout is opened. Perhaps there is a better, more thorough explanation. But I hope this helps anyone else who may experience this problem.

Image/ViewPager will not correctly use android:layout_gravity="bottom" and android:layout_alignParentBottom="true"

For some reason android:layout_gravity="bottom" and android:layout_alignParentBottom="true" on my ViewPager (the classical button) do not seem to place the image at the bottom of the screen and I'm unsure why.
Any suggestions are much appreciated.
SCREENSHOT:
XML:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- The main content view -->
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- The navigation drawer -->
<ListView
android:id="#+id/left_drawer"
android:layout_width="120dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111"
android:choiceMode="singleChoice"
android:divider="#android:color/transparent"
android:dividerHeight="0dp" />
<com.google.android.youtube.player.YouTubePlayerView
android:id="#+id/youtubeplayerview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<com.idg.omv.ui.widget.VideosListView
android:id="#+id/videosListView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:id="#+id/footer"
android:layout_width="fill_parent"
android:layout_height="70dip"
android:layout_alignParentBottom="true" >
<ScrollView
android:id="#+id/groupScrollView"
android:layout_width="fill_parent"
android:layout_height="match_parent" >
</ScrollView>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:src="#drawable/scroll_lt_arrow" />
<Button
android:id="#+id/fav_up_btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/fav_up_btn1" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:src="#drawable/scroll_rt_arrow" />
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
The Navigation Drawer documentation states (emphasis mine):
To add a navigation drawer, declare your user interface with a DrawerLayout object as the root view of your layout. Inside the DrawerLayout, add one view that contains the main content for the screen (your primary layout when the drawer is hidden) and another view that contains the contents of the navigation drawer.
You cannot have multiple views in your DrawerLayout xml (other than your drawer and content). I would recommend putting your content in a fragment, and then inflating it in code similar to the way the examples do.

Categories