Having some problems trying to put views in the right places - layout

I'm generating dynamic tablerows in this XML
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" >
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutPrincipal1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:stretchColumns="1">
</TableLayout>
</ScrollView>
The problem is: I would like to have some views in a specific position using gravity, but when a define for example, button1.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT,Gravity.CENTER_VERTICAL)); it doesn't work, but if I do this directly in the xml It works(with layout_gravity), someone knows what to do?

Related

android studio expandable bottom sheet with recycleview

I'm trying to achieve something like this:
as you can see the bottom sheet is expandable when there is more than one line of the recycleview.
I want to implement the same thing in the profile page so that if posts are more than three lines then I can expand the bottom sheet, then I can scroll the recycleview (when the sheet is expanded).
my current xml:
<LinearLayout
android:id="#+id/allPosts"
android:layout_below="#+id/userinfo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:paddingTop="5dp"
android:paddingRight="5dp"
android:paddingLeft="5dp"
android:background="#drawable/rounded_behind_recycleview"
android:orientation="vertical"
android:elevation="1dp">
<com.taimoorsikander.myapplication.Widgets.B_RecycleView
android:id="#+id/recycle_view"
android:overScrollMode="never"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
</LinearLayout>
^
it looks like the image I attached but it is not expandable yet.
is this possible in Android Studio? if yes, please help.
In this tutorial they teach you how to set the Behaviour in the BottomSheetFragment,
take a look it's really helpful, there you will find how to set the Behaviour that you want with BottomSheetBehaviour.from(...)...
Kotlin language
Basically you have to set the height(Ex: peekHeight = 50) you want than and set "false" to hide the bottomSheet and "true" to (isDraggable).
There is 2 ways of doing this declarative in the XML or programmatically, I'll show the first:
1º Your first layout needs to be CoordinatorLayout:
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".MainActivity">
<include layout="#layout/bottom_sheet" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
2º The bottomSheetLayout:
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:behavior_hideable="false"
app:behavior_peekHeight="100dp"
app:behavior_skipCollapsed="true"
app:layout_behavior="#string/bottom_sheet_behavior">
<androidx.cardview.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardBackgroundColor="#81D4FA" />
</LinearLayout>
Pay attention to this TAGS, which states the behavior and state of the BottomSheet:
app:behavior_hideable="false"
app:behavior_peekHeight="100dp"
app:behavior_skipCollapsed="true"
app:layout_behavior="#string/bottom_sheet_behavior"

how to make sure the color fill in the entire bottom navigation view?

I'm facing a problem right now that i tried to imply the color function background in the app:itemBackground="#color/purpleBoo"
but the color seems like not to fill in the entire bottom navigation? why is this happening to me?
my bottom navigation code
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/purpleBoo"
app:menu="#menu/bottom_nav_menu"
app:itemIconTint="#color/bottom_nav_color"
app:itemTextColor="#color/bottom_nav_color"/>
</RelativeLayout>
For the current layout that you are using the bottom navigation panel can be maximized to width "match_parent" so the color will also be available inside the width section.
Well, in case the white space around the panel is disturbing the look of your app then you can change the background of the layout as a whole then that would seem better.
Just add the android android:background="#color/purpleBoo" in BottomNavigationView
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:itemBackground="#color/purpleBoo"
app:menu="#menu/bottom_nav_menu"
android:background="#color/purpleBoo"
app:itemIconTint="#color/bottom_nav_color"
app:itemTextColor="#color/bottom_nav_color"/>
</RelativeLayout>

App on phone looks very different to android studio rendering

I'm trying to get a fragment to appear, as rendered in android studio on my phone. I have a Samsung S7 which has 1440 x 2560 pixels, so I've selected the Nexus 6 rendering option in Android Studio.
I have setup my fragment so that it is contained in a content file called content_add_edit_recipe.xml. My activity, activity_add_edit_recipe.xml includes this file, shown below.
<FrameLayout 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/fragment"
android:orientation="vertical"
android:name="com.jcg.grocerylistconverter.AddEditRecipeActivityFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:layout="#layout/fragment_add_edit_recipe"/>
<?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"
tools:context="com.jcg.grocerylistconverter.AddEditRecipeActivity">
here is activity file:
<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_add_edit_recipe"/>
here is the fragment xml for the layout tag (I don't think the rest is relevant right now):
<android.support.constraint.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:id="#+id/content_add_edit_recipe"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
tools:context="com.jcg.grocerylistconverter.AddEditRecipeActivityFragment"
tools:showIn="#layout/activity_add_edit_recipe">....
And here is how the fragment looks in the preview:
while here is how it appears on the phone:
I suspect that this is to do with my actual java code in starting the fragment, but I can't figure out for the life of me where/how this is occurring, after tracing through my code the xmls that are inflated.
If nothing is evident from the xmls I will post the code initiating the activity/fragment, but first can someone please point out if something appears off with my xml?
I think you didn't consider text size and length of text inside while designing layout. You should test your layout with different text length and size inside spinner

How to remove extra padding at top of window?

I have this strange padding-top when I run my App on Android. On iOS it looks as expected.
When I check the padding is 0 for all directions. My last thought is that it could be related to some of my layouts, but I don't understand what that could be.
Toolbar layout:
<?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="?attr/actionBarSize"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_scrollFlags="scroll|enterAlways"/>
Tabs layout
<?xml version="1.0" encoding="UTF-8"?>
<android.support.design.widget.TabLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?attr/colorPrimary"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:tabIndicatorColor="#android:color/white"
app:tabGravity="fill"
app:tabMode="fixed" />
I am using Xamarin.Form's MasterDetailPage with my pages being ContentPage.
Does this combination of pages create this error on Android? Please help me fix it.
Here is a screenshot of what I see:
]1
You're able to change lots of settings in Android Solution (MainActivity.cs) just underneath your namespace.
For example:
[Activity(Theme = "#style/AppStyle.Light", Label = "ProjectName", MainLauncher = false,
ScreenOrientation = ScreenOrientation.Portrait, ConfigurationChanges = ConfigChanges.ScreenSize | ConfigChanges.Orientation)]

How to add ButtonOverlay on a Map(osmdroid)

I want to add a button overlay but it shouldn't be based on the geopoint but instead based on the screen. So whenever i scroll the map, the button should still exists and will remain in that position, How do i do that?
Just add it to your android layout for the activity/fragment
Example here:
https://github.com/osmdroid/osmdroid/blob/master/OpenStreetMapViewer/src/main/res/layout/sample_cachemgr.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<org.osmdroid.views.MapView
android:id="#+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
tilesource="Mapnik" />
<Button android:id="#+id/btnCache"
android:layout_width="fill_parent"
android:text="Cache Manager"
android:layout_height="wrap_content"></Button>
</RelativeLayout>

Resources