Android XML adapted for all scree sizes - size

I am currently developing an android application and am facing a problem.
I am aware that the most common thing to adapt your application to most of the devices is to create different folder named "layout-small", "layout-normal", etc. in order to handle this issue.
However, this does not solve everything : both Samsung S2 and Samsung S3 and considered "normal" screen size, but the given result is very different, even though I also use "dp" to set height and with of most of my views.
How can I adapt my xml for EVERY device PERFECTLY ? I don't mind having the same xml file for every different device, but how do I do that ?
Here is an example of an XML file that renders welle on S3, but is cut down on S2 (the last button appears partially)
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
android:useDefaultMargins="false">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Me"
android:id="#+id/textView"
style="#style/title_parameters"
android:layout_alignParentTop="true"
/>
<Button
android:id="#+id/btUpdateProfile"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="Update profile"
android:textColor="#drawable/text_color_selected_parameters"
style="#style/button_text"
android:background="#drawable/solo_button_parameters"
android:drawableRight="#drawable/table_view_cell_accessory_disclosure_indicator"
android:layout_below="#+id/textView"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Friends"
android:id="#+id/textView2"
style="#style/title_parameters"
android:layout_below="#+id/btUpdateProfile"/>
<Button
android:id="#+id/btFindFriends"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="Find friends"
android:textColor="#drawable/text_color_selected_parameters"
style="#style/button_text"
android:background="#drawable/solo_button_parameters"
android:drawableRight="#drawable/table_view_cell_accessory_disclosure_indicator"
android:layout_below="#+id/textView2"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginBottom="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Setting"
android:id="#+id/textView3"
style="#style/title_parameters"
android:layout_below="#+id/btFindFriends"/>
<Button
android:id="#+id/btSharing"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="Sharing"
android:gravity="top"
android:textColor="#drawable/text_color_selected_parameters"
style="#style/button_text"
android:background="#drawable/top_corner_button_parameters"
android:drawableRight="#drawable/table_view_cell_accessory_disclosure_indicator"
android:layout_below="#+id/textView3"/>
<Button
android:id="#+id/btPushNotification"
android:layout_width="fill_parent"
android:layout_height="50dp"
style="#style/button_text"
android:textColor="#drawable/text_color_selected_parameters"
android:text="Push notification"
android:layout_marginTop="-2dp"
android:background="#drawable/rectangle_button_parameters"
android:drawableRight="#drawable/table_view_cell_accessory_disclosure_indicator"
android:layout_below="#+id/btSharing"/>
<Button
android:id="#+id/btAutomation"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:gravity="bottom"
android:text="Automation"
style="#style/button_text"
android:background="#drawable/bottom_corner_button_parameters"
android:drawableRight="#drawable/table_view_cell_accessory_disclosure_indicator"
android:layout_below="#+id/btPushNotification"
android:textColor="#drawable/text_color_selected_parameters"
android:layout_marginTop="-2dp"/>
<Button
android:id="#+id/btDisconnect"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:text="Disconect"
android:layout_marginTop="20dp"
android:textColor="#drawable/text_color_selected_parameters"
style="#style/button_text"
android:background="#drawable/solo_button_parameters"
android:drawableLeft="#drawable/disconnect"
android:layout_below="#+id/btAutomation"
android:layout_marginBottom="30dp"/>
</RelativeLayout>
I will upload screen shots as soon as I can.
Once again, my question is : how can I have an XML file for every existing device ?

Can you please post some code. It will be better.
Without define fixed size relative layout and use its positional attributes properly. (This is a comment )

Related

How to make a part of image in textview as clickable using Kotlin?

I found that I can add a image by using android:drawableRight
then how can I make image as clickable in textview?
<TextView
android:id="#+id/tv_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ellipsize="end"
android:maxLines="1"
android:padding="5dp"
android:text="title"
android:drawableRight="#drawable/ic_baseline_delete_24"
android:drawablePadding="5dp"
android:clickable="true"
android:textSize="18sp" />

Is there any way to add a favourite button on the image?

I'm currently working on mobile application by using the kotlin language. May I know is it possible to add a favorite button on the image that the user can click it? If it is possible, then how is it?
<com.google.android.material.button.MaterialButtonToggleGroup
android:id="#+id/toggleButtonGroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/ic_favorite"
style="#style/ToggleButtonWithIconOnly"
app:icon="#drawable/ic_favorite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</com.google.android.material.button.MaterialButtonToggleGroup>
<ImageView
android:id="#+id/cake"
android:layout_width="300dp"
android:layout_height="200dp"
android:layout_marginTop="30dp"
android:layout_marginStart="55dp"
app:srcCompat="#drawable/fgh" />

Put a button at the end of a recyclerview with swiperefreshlayout

i want to to put a button at the end of a scrollview, i know that i can achieve this if i modify my adapter but i don't want to do it in that way because i think there's an easier way. So i tried modifying my layout like this:
<!-- Not important stuff -->
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/refreshIndicatorCoins"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.RecyclerView
android:id="#+id/rvCoinsFull"
android:clickable="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.RecyclerView>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.v4.widget.SwipeRefreshLayout>
This works almost perfect but swipeRefresh's animation freezes
Hope it helps.
After RecyclerView put this code.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</RelativeLayout>

android how to center a TextView within the full width of the parent?

I have a Checkbox and a TextView that are anchored to the left side of a UI. I have another TextView "newcard #" that I would like to center in the full width of the parent. The parent width is the white space running from left to right, shown below. Notice how "newcard #" is to the right of the red center line. Basically I would like "newcard #" to be centered around the red center line.
I have tried multiple combinations of gravity and layout_gravity without any luck. What am I missing here?
item.xml
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginBottom="4dp"
card_view:cardBackgroundColor="#android:color/white"
card_view:cardCornerRadius="6dp"
card_view:cardElevation="4dp" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_selector" >
<CheckBox
android:id="#+id/chkSelected"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_marginLeft="4dp"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:gravity="center"
android:background="#008080" />
<TextView
android:id="#+id/cardType1"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_toRightOf="#+id/chkSelected"
android:layout_toEndOf="#+id/chkSelected"
android:paddingStart="3dp"
android:paddingLeft="3dp"
android:paddingEnd="6dp"
android:paddingRight="6dp"
android:layout_marginTop="4dp"
android:gravity="center"
android:textColor="#ffffff"
android:textStyle="bold|italic"
style="#style/Base.TextAppearance.AppCompat.Subhead" />
<TextView
android:id="#+id/cardBlankText1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_toRightOf="#+id/cardType1"
android:layout_toEndOf="#+id/cardType1"
android:layout_marginTop="4dp"
android:layout_marginLeft="6dp"
android:layout_marginStart="6dp"
android:gravity="center_horizontal"
android:textColor="#color/colorFlLabelFinal"
android:textStyle="bold"
style="#style/Base.TextAppearance.AppCompat.Subhead" />
I kept only RelativeLayout, hope a result won't change.
Deleted:
android:layout_toEndOf="#+id/cardType1"
android:layout_toRightOf="#+id/cardType1"
and inserted:
android:layout_centerHorizontal="true"
So:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_selector">
<CheckBox
android:id="#+id/chkSelected"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="4dp"
android:layout_marginStart="4dp"
android:layout_marginTop="4dp"
android:background="#008080"
android:gravity="center"/>
<TextView
android:id="#+id/cardType1"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_marginTop="4dp"
android:layout_toEndOf="#+id/chkSelected"
android:layout_toRightOf="#+id/chkSelected"
android:gravity="center"
android:paddingEnd="6dp"
android:paddingLeft="3dp"
android:paddingRight="6dp"
android:paddingStart="3dp"
tools:text="Work"
android:textColor="#ffffff"
android:textStyle="bold|italic"/>
<TextView
android:id="#+id/cardBlankText1"
style="#style/Base.TextAppearance.AppCompat.Subhead"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_marginLeft="6dp"
android:layout_marginStart="6dp"
android:layout_marginTop="4dp"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
tools:text="newcard#"
android:textColor="#color/colorFlLabelFinal"
android:textStyle="bold"/>
<TextView
android:id="#+id/cardBlankTextNumsTotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="fggfhfg"
android:layout_toRightOf="#id/cardBlankText1"/>
</RelativeLayout>

Navigating effectively in scrollView

In my android application I have a background and at the bottom a horizontal scrollView. In this scrollView I have 4 buttons each one representing a separate activity plus 2 more which are arrows for scrolling left and right, if the user scrolls right then 4 different buttons will populate the scrollview. In my Current VM, it works correctly but if I switch to one with a larger screen then instead of 4 buttons I have 5 or even 5 and a half, at it scrolls much less. My question is how can I always have 6 buttons in my scrollView (4 activity buttons and the extra two arrows for navigating in the scrollView) independently of the size and the density if the screen. This is the xml I've written so far
<?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"
android:background="#drawable/fonto1" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:fillViewport="true"
android:fitsSystemWindows="true"
android:measureAllChildren="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="93dp"
android:background="#000"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/aristerovelos2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/aristerovelos1"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/ellinika"
android:background="#drawable/aristerovelos3"
android:src="#drawable/aristerovelos3" />
<ImageButton
android:id="#+id/ispanika"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/agglika"
android:layout_marginLeft="9dp"
android:layout_toRightOf="#+id/aristerovelos2"
android:background="#drawable/ispanika"
android:src="#drawable/ispanika" />
<ImageButton
android:id="#+id/italika"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/agglika"
android:layout_marginLeft="14dp"
android:layout_toRightOf="#+id/ispanika"
android:background="#drawable/italika"
android:src="#drawable/italika" />
<ImageButton
android:id="#+id/rosika"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/agglika"
android:layout_marginLeft="14dp"
android:layout_toRightOf="#+id/italika"
android:background="#drawable/rosika"
android:src="#drawable/rosika" />
<ImageButton
android:id="#+id/deksivelos2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/aristerovelos1"
android:layout_marginLeft="80dp"
android:layout_toRightOf="#+id/rosika"
android:background="#drawable/deksivelos3"
android:src="#drawable/deksivelos3" />
<ImageButton
android:id="#+id/aristerovelos1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:background="#drawable/aristerovelos3"
android:src="#drawable/aristerovelos3" />
<ImageButton
android:id="#+id/galika"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/agglika"
android:layout_marginLeft="16dp"
android:layout_toRightOf="#+id/agglika"
android:background="#drawable/agglika"
android:src="#drawable/gallika" />
<ImageButton
android:id="#+id/germanika"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/galika"
android:layout_marginLeft="14dp"
android:layout_toRightOf="#+id/galika"
android:background="#drawable/germanika"
android:src="#drawable/germanika" />
<ImageButton
android:id="#+id/ellinika"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/germanika"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/germanika"
android:background="#drawable/ellinika"
android:src="#drawable/ellinika" />
<ImageButton
android:id="#+id/agglika"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/aristerovelos1"
android:background="#drawable/agglika"
android:src="#drawable/agglika" />
<ImageButton
android:id="#+id/deksivelos1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/aristerovelos2"
android:layout_toRightOf="#+id/ellinika"
android:background="#drawable/deksivelos3"
android:src="#drawable/deksivelos3" />
</RelativeLayout>
</HorizontalScrollView>
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/horizontalScrollView1"
android:layout_alignParentLeft="true"
android:src="#drawable/grammi" />