Android RelativeLayout UI not displayed properly - android

I am trying to use RelativeView as part of the RecyclerView. But the UI is not displayed properly. Below is the xml which I am using for the relativelayout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<CheckBox
android:id="#+id/list_item_crime_solved_check_box"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:padding="4dp"/>
<TextView
android:id="#+id/list_item_crime_title_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/list_item_crime_solved_check_box"
android:textStyle="bold"
android:padding="4dp"
tools:text="Crime Title"/>
<TextView
android:id="#+id/list_item_crime_date_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/list_item_crime_solved_check_box"
android:layout_below="#id/list_item_crime_title_text_view"
android:padding="4dp"
tools:text="Crime Date"/>
</RelativeLayout>
When I run it in emulator I get below display
As I am learning android programing I am not getting what could be the cause that the item are not listed below each item and there is lot of gap coming up between two items.

Just change the height of RelativeLayout from match_parent to wrap_content. i.e.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content">

Just update layout height to wrap_content, Refer below code snippet,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff0000"
android:orientation="vertical">

Related

Scrolling just by very little

I have a problem in Android studio. I need to make this layout to scroll, however, it is scrolling by very little.
What could be the problem?
Have tried several of solutions till now, but nothing yet works.
using setNestedScrollingEnabled(false) doesn't work, since i am tackling older API's
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="35dp"
android:orientation="vertical"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="39dp"
android:orientation="horizontal">
<one.triangle.me.element.TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="3dp"
android:layout_marginRight="3dp"
android:id="#+id/element_1"
android:textSize="26sp"
android:layout_gravity="center_vertical"
android:text="•"
/>
</LinearLayout>
<TextView
android:id="#+id/multi_item_selected"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:src="#drawable/whatever"/>
</RelativeLayout>
</ScrollView>
Remove height from ScrollView, as their childs have greater height than parent view. So either make it wrap_content or match_parent.
Update your xml code like below :
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">

MPAndroid adjustment to screen size without fixed height

So I have been trying every mix and match with different layouts and settings and still cant figure out how to make this work. I want to have a spinner on the Same Screen as an MPAndroid Line Chart, I know I can fix the height Manually and adjust it to fit properly, but fill parent or match parent does not account for the spinner and just pushes it off the screen! If I load the application on my phone with height of 290dp Great fits perfectly! Load it on the 7" tablet not so pretty... Half the screen is empty. How do I contain the Chart to fill a layout that accounts for spinner? Wrap Content on the chart makes it unusable.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".history"
android:background="#303030">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.github.mikephil.charting.charts.LineChart
android:id="#+id/chart"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner4"
android:entries="#array/list3"
android:popupBackground="#303030"
android:layout_centerHorizontal="true" />
</LinearLayout>
Try using this I have removed some attributes which were missing at my end. This is where spinner is on top and rest is filled with line chart.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#303030"
android:orientation="vertical">
<Spinner
android:id="#+id/spinner4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:popupBackground="#303030" />
<com.github.mikephil.charting.charts.LineChart
android:id="#+id/chart"
android:layout_width="fill_parent"
android:layout_below="#+id/spinner4"
android:layout_height="fill_parent" />
</RelativeLayout>
Try this,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
tools:context=".history"
android:background="#303030">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="70">
<com.github.mikephil.charting.charts.LineChart
android:id="#+id/chart"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</LinearLayout>
<Spinner
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="30"
android:id="#+id/spinner4"
android:entries="#array/list3"
android:popupBackground="#303030"
android:layout_centerHorizontal="true" />
</LinearLayout>
Because of using weight, on any screen "map and spinner" will look like in same screen height ratio.

android GridView issue show rows

I'm working with gridView, each item has an image and a TextView in the footer.
All images have the same size, so I use match_parent or wrap_content as required.
But the screen only show one line and a half on items, it scroll, and that is perfect, but I would like to show two complete rows and then scroll.
Also, when I run the app in small devices, only show 1 line, I would like to show 2 lines always.
I tried to use this example, but it doesn't work to me because stop scrolling.
Here, some of my code XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg_app">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black"
android:id="#+id/header"
android:padding="10dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/icon_back"
android:src="#drawable/ic_back" />
</RelativeLayout>
<com.xetux.util.ExpandableHeightGridView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center"
android:horizontalSpacing="15dp"
android:isScrollContainer="true"
android:numColumns="3"
android:padding="10dp"
android:id="#+id/family_grid_view"
android:layout_below="#+id/header"
android:verticalSpacing="15dp"/>
<!--<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:id="#+id/family_grid_view"
android:horizontalSpacing="15dp"
android:verticalSpacing="15dp"
android:layout_below="#+id/header"
android:numColumns="3"/>-->
</RelativeLayout>
and each item XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/family_image"
android:background="#drawable/bg_family">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/black_transparency"
android:text="FAMILIA"
android:gravity="center"
android:id="#+id/family_name"
android:padding="10dp"
android:layout_alignParentBottom="true"
android:textColor="#color/white"
android:textSize="15sp"/>
</RelativeLayout>
I think your image size is big. You can checkout for supporting multiple screen tutorials. Try using small images for small screen.

Android: tile bitmap in ListView item

I am trying to add a sidebar image to some of the items in a ListView.
The image should occupy the full height of the item and be repeated vertically if necessary.
Stretching the image is not an option.
After trying a few things I ended up modifying the code found here.
Unfortunately, it does not seem to work within a ListView and the image is not repeated:
Main layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
ListView item layout:
<?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" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical">
<ImageView
android:id="#+id/listitem_sidebar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/sidebar"
android:visibility="invisible"/>
</LinearLayout>
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:text="Test"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Background drawable:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/sidebar"
android:tileMode="repeat" />
Background image:
Setting the sidebar globally for the whole ListView is not an option as I am using different View types for the items in my production code.
Edit: formatting
Alight, finally got it to work just the way I wanted. I added a transparent View below the TextView to force the ListView to measure the item's height and do a layout pass. Then I used the invisible View as an anchor for my side bar.
The only file that needed to be modified was listview_item.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" >
<TextView
android:id="#+id/textview"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:layout_alignParentRight="true"
android:text="Test"/>
<View
android:id="#+id/dummy"
android:layout_below="#id/textview"
android:layout_width="match_parent"
android:layout_height="0dp"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/background"
android:layout_alignParentTop="true"
android:layout_above="#id/dummy"
android:orientation="vertical">
<ImageView
android:id="#+id/listitem_sidebar"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:src="#drawable/sidebar"
android:visibility="invisible"/>
</LinearLayout>
</RelativeLayout>
Here's a screenshot of the result:
I removed the code for my previous workaround as it was more of a hack than anything and didn't work as well as this.

Android TextView not show Complete long Text

<?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="match_parent"
android:padding="2dp"
android:background="#drawable/cell_background">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="15dp"
android:text="New Text"
android:gravity="center"
android:background="#color/white"
android:id="#+id/textView"/>
</LinearLayout>
I came across a problem,TextView not showing the complete text and I absolutely cannot limit text length in code (not other things either!).
When I use setText with the following string:
qwertyuyiopasdfghjklkjsdfhvrtcgwrgcrcrfcwerm,fixcumreiovtreuiocfmruefcmeroiwurewmiohgxiehgiocwrhhhhwemoggruiohwgcioweruhcmrehxrmeihouxe,horhxihuxchfjkhjklsdfhdlkfhdsjflkasdhjfhaskldfhdkljfhsadjfkhdsjfdsakflajdasghsdlkacvnsadohlkfjhdjkflsadhkjfhasdlkfhdsjlkfhdjlkfhasdjfhicnerjsdklfhdjsklfhdsjklfhasdjklvcnsldkjfldksfhajsldkfhdjklsjfhasdjklf789456123
it only shows:
qwertyuyiopasdfghjklkjsdfhvrtcgwrgcrcrfcwerm,fixcumreiovtreuiocfmruefcmeroiwurewmiohgxiehgiocwrhhhhwemoggruiohwgcioweruhc
but getText returns the complete string as shown on the Hierarchy Viewer screenshot!
Your text is too long try to put the text view in scroll
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.NestedScrollView
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:layout_marginTop="40dp"
android:layout_marginBottom="40dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.bala.computer.thenaliramanstories.ScrollingActivity">
<WebView
android:id="#+id/txtStory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/text_margin"/>
</android.support.v4.widget.NestedScrollView>
Change your LinearLayout's android:layout_width="match_parent"
Your parent view has a width of 100dp.
android:layout_width="100dp"
android:layout_height="50dp"
Change them to match_parent and it should be fine

Categories

Resources