FloatingActionButton on top of action bar - java

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

Related

Button align to bottom, is been hidden from ImageView

I want to place a button in bottom of my layout. Also i have place an image view with layout_alignParentTop, my proble is that my imageview is hide my button.
How can i solve that?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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">
<Button
android:id="#+id/btnGetMoreResults"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:text="Get more"
android:layout_alignParentBottom="true" />
<ImageView
android:layout_alignParentTop="true"
android:src="#android:drawable/ic_menu_gallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/btnGetMoreResults"
android:id="#+id/imageView1" />
</RelativeLayout>
Look the picture below
Should i use CoordinatorLayout instead of RelativeLayout?
In your ImageView, don't use
android:layout_alignParentBottom="true"
You must use
android:layout_above="#+id/your_button"
This is the final code, don't forget to modify it for your need
<RelativeLayout
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_alignParentTop="true"
android:layout_above="#id/button"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<Button
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Use this in imageview
android:margin_bottom="50dp";
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/next_btn"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/next_btn"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

ScrollView doesn't scroll in a ViewPager

In my app I use a TabLayout under a Toolbar, and a ViewPager filling the rest of the space. I need one of the fragments under the tab to be able to scroll. tried using ScrollView, NestedScrollView and android:fillViewport="true" with no success.
I've looked at other questions about the same problem, tried anything that was suggested in the answers, but they all didn't solve my problem.
the activity that contains the ViewPager's code:
<?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"
android:orientation="vertical"
tools:context=".UserProfile">
<include
android:id="#+id/userToolbar"
layout="#layout/user_info_toolbar_layout" />
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/userToolbar"
android:background="#android:color/holo_blue_light"
app:tabSelectedTextColor="#android:color/white"
app:tabTextColor="#android:color/black">
<com.google.android.material.tabs.TabItem
android:id="#+id/dashboardTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/dashboard_tab_text_en" />
<com.google.android.material.tabs.TabItem
android:id="#+id/statisticsTab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/statistics_tab_text_en" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager.widget.ViewPager
android:id="#+id/userInfoViewPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/tabLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
the fragment's code:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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/user_dashboard_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
tools:context=".Dashboard">
<androidx.constraintlayout.widget.ConstraintLayout
android:padding="40dp"
android:id="#+id/userInfoContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.github.abdularis.civ.CircleImageView
android:id="#+id/userImage"
android:layout_width="match_parent"
android:layout_height="0dp"
android:padding="20dp"
android:scaleType="fitCenter"
android:src="#drawable/male_user_sample1"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/username_sample"
android:textColor="#color/colorPrimaryDark"
android:textSize="30sp"
app:layout_constraintTop_toBottomOf="#id/userImage" />
<com.ramijemli.percentagechartview.PercentageChartView
android:id="#+id/userLevelProgress"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="20dp"
android:outlineAmbientShadowColor="#color/colorPrimary"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/username"
app:pcv_animDuration="1000"
app:pcv_animInterpolator="anticipate_overshoot"
app:pcv_drawBackground="false"
app:pcv_drawBackgroundBar="false"
app:pcv_mode="ring"
app:pcv_orientation="counter_clockwise"
app:pcv_progress="100"
app:pcv_startAngle="270"
app:pcv_textColor="#color/colorPrimary"
app:pcv_textShadowRadius="100"
app:pcv_textSize="50sp" />
<TextView
android:id="#+id/userLevel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/level_sample"
android:textColor="#color/colorPrimary"
android:textSize="40sp"
app:layout_constraintTop_toBottomOf="#id/userLevelProgress" />
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
I can't find the problem in my code that causes the fragment under the ViewPager being unscrollable.
Rty this
In your ViewPager
<androidx.viewpager.widget.ViewPager
android:id="#+id/userInfoViewPager"
android:layout_width="match_parent"
android:layout_height="0dp" // <- ****
app:layout_constraintBottom_toBottomOf="parent" // <- ****
app:layout_constraintTop_toBottomOf="#id/tablayout"
/>
Then, in fragment xml
<ScrollView
....
android:layout_width="match_parent"
android:layout_height="match_parent" // <- ****
>
<androidx.constraintlayout.widget.ConstraintLayout
android:padding="40dp"
android:id="#+id/userInfoContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" // <- ****
>
it's seems like there some bug with ConstraintLayout inside ScrollView ,Don't forget that If you constraint some view's bottom to constraint layout's bottom. Scrollview could not scroll. Use NestedScrollView with viewport true is working good for me
<android.support.v4.widget.NestedScrollView>
......
......
</android.support.v4.widget.NestedScrollView>

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.

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

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

Categories