My items in my listView are not aligning properly? - java

rather than explain what's happening, here's a screen-grab:
As you can see my item list is aaaalll over the shop. Going to paste both activity_main.xml and another one that formats the list, list_row.xml
ACTIVITY_MAIN.XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#121212">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/logo" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/main_title"
android:textColor="#33b5e5"
android:background="#1f1f1f"
android:textStyle="bold"
android:textSize="14sp"/>
<ListView
android:id="#+id/android:list"
android:background="#121212"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:dividerHeight="2dip"/>
</LinearLayout>
LIST_ROW.XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:padding="6dip"
android:background="#121212">
<TableLayout
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="fill_parent"
android:stretchColumns="*"
android:background="#121212">
<TableRow>
<ImageView
android:id="#+id/icon"
android:padding="2dip"
android:background="#121212"/>
<TextView
android:id="#+id/description"
android:padding="2dip"
android:textSize="18sp"
android:textColor="#ffffff"
android:singleLine="true"
android:background="#121212"/>
</TableRow>
</TableLayout>
</LinearLayout>

Try giving weights to ImageView and TextView like android:layout_weight as the ratio you want.
And dont forget to make android:layout_width = "match_parent" otherwise weights will have no effect..

remove the Table layout
and use this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="64dp"
android:background="#121212"
android:orientation="horizontal"
android:padding="6dip" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#121212"
android:padding="2dip"
android:src="#drawable/instructions" />
<TextView
android:id="#+id/description"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent"
android:background="#121212"
android:padding="2dip"
android:singleLine="true"
android:text="tomer"
android:textColor="#ffffff"
android:textSize="18sp" />
</LinearLayout>

Related

How to distribute space between a parent element and a child viewgroup

I would like to build a layout with an image that should be 90 percent height of the screen and the last 10% is the 3 text views that i have in a horizontal layout im able to acheive this using setting the height of the image in dp to cover 90 percent of the screen(500dp) i tried layout_weight everywhere and it aintworking is there any way i could acheive this without explicitly setting the height of the imageHers a screenshot! This is my end goal but i have explicitly set image height in this
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"
android:weightSum="2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="500dp"
android:scaleType="centerCrop"
app:srcCompat="#drawable/martin" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
>
<TextView
android:text="Martin garrix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:text="Beleive"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Imagine!!" />
</LinearLayout>
</LinearLayout>
You have to use android:height="0dp" and android:layout_height="9" for you image and android:layout_height="1" for the other container. Also the weightSum should be 10. You can check this for examples of layout_weigth http://abhiandroid.com/ui/linear-layout
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"
android:weightSum="10" >
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="0dp"
android:layout_weight="9"
>
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
app:srcCompat="#drawable/martin" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
>
<TextView
android:text="Martin garrix"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:text="Beleive"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
/>
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Imagine!!" />
</LinearLayout>
</LinearLayout>
You can simply achieve this without using to multiple time weight in 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"
android:orientation="vertical">
<ImageView
android:id="#+id/descripcion_actividad"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/layout"
android:scaleType="fitXY"
android:src="#mipmap/ic_launcher"
/>
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:padding="10dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:id="#+id/hora_inicio"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/descripcion_actividad"
android:layout_alignParentBottom="false"
android:layout_alignStart="#+id/descripcion_actividad"
android:layout_below="#+id/descripcion_actividad"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/descripcion_actividad"
android:layout_alignParentBottom="false"
android:layout_alignStart="#+id/descripcion_actividad"
android:layout_below="#+id/descripcion_actividad"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/descripcion_actividad"
android:layout_alignParentBottom="false"
android:layout_alignStart="#+id/descripcion_actividad"
android:layout_below="#+id/descripcion_actividad"
android:layout_weight="1"
android:text="TextView" />
</LinearLayout>
</RelativeLayout>

Move Layouts when opening the EditText Keyboard

i'm creating a console or terminal thing, and i'm having a problem. Normaly my Activity looks like this:
but when i click in the edittext and the keyboard opens my edittextfield and my send-button are gone:
but i want the edittext and the sendbutton to be above the keybaord, so you see what you're typing and you're able to send it. This is my xml code:
<?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"
android:background="#d1d1d1">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="477dp"
android:layout_below="#+id/title" >>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:text="Console:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="0dip"
android:gravity="top"
android:textSize="20sp"
android:textColor="#000000"
android:typeface="monospace"
android:paddingLeft="0dp"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<EditText
android:layout_marginLeft="5dp"
android:layout_width="163dp"
android:layout_height="wrap_content"
android:id="#+id/consoleText"
android:layout_weight="0.92" />
<Button
android:layout_width="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/sendButton"
android:layout_gravity="right" />
</LinearLayout>
</LinearLayout>
Maybe somebody knows, how i can fix this issue! Thanks in advance, any help is appreciated :)
Setting ScrollView's layout_weight=1 should work. 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"
android:background="#d1d1d1">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textView"
android:text="Console:"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="0dip"
android:gravity="top"
android:textSize="20sp"
android:textColor="#000000"
android:typeface="monospace"
android:paddingLeft="0dp"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:layout_marginLeft="5dp"
android:layout_width="163dp"
android:layout_height="wrap_content"
android:id="#+id/consoleText"
android:layout_weight="0.92" />
<Button
android:layout_width="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp"
android:layout_height="wrap_content"
android:text="Send"
android:id="#+id/sendButton"
android:layout_gravity="right" />
</LinearLayout>
</LinearLayout>

How to scroll all contents of layout in Android?

My marking has some elements. These elements shall be scrolled completely, when scrolling down the calendar shall leave for screen limits if it it is necessary and to scroll LinearLayout contents.
I already tried to add ScrollView, it was ineffectual since all screen entirely but only a calendar, I ask to help was scrolled not.
Here Layout code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout_calendar"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<RelativeLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/calendar_top" >
<RelativeLayout
android:id="#+id/previous"
android:layout_width="40dip"
android:layout_height="30dip"
android:layout_alignParentLeft="true" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/arrow_left" />
</RelativeLayout>
<TextView
android:id="#+id/title_calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dip"
android:textColor="#000000"
android:textSize="18dip"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/next"
android:layout_width="40dip"
android:layout_height="30dip"
android:layout_alignParentRight="true" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/arrow_right" />
</RelativeLayout>
</RelativeLayout>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#android:color/transparent"
android:numColumns="7"
android:stretchMode="columnWidth" >
</GridView>
<LinearLayout
android:id="#+id/linLayout_all_ivents_in_this_day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical" >
</LinearLayout>
</LinearLayout>
You can use scrollView.
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
-> your xml code <-
</ScrollView>
The only think you need to know is that scrollview only can have one child node, preferencialy a LinearLayout.
Use the ScrollView: http://developer.android.com/reference/android/widget/ScrollView.html
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/LinearLayout_calendar"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:scrollbars="vertical" >
<RelativeLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/calendar_top" >
<RelativeLayout
android:id="#+id/previous"
android:layout_width="40dip"
android:layout_height="30dip"
android:layout_alignParentLeft="true" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/arrow_left" />
</RelativeLayout>
<TextView
android:id="#+id/title_calendar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dip"
android:textColor="#000000"
android:textSize="18dip"
android:textStyle="bold" />
<RelativeLayout
android:id="#+id/next"
android:layout_width="40dip"
android:layout_height="30dip"
android:layout_alignParentRight="true" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/arrow_right" />
</RelativeLayout>
</RelativeLayout>
<GridView
android:id="#+id/gridview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:listSelector="#android:color/transparent"
android:numColumns="7"
android:stretchMode="columnWidth" >
</GridView>
<LinearLayout
android:id="#+id/linLayout_all_ivents_in_this_day"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:scrollbars="vertical" >
</LinearLayout>
</LinearLayout>
</ScrollView>

creating a new linear layout

i created button and other...
but now when i want to create a three buttons green,red,blue they are not showing up i want them to be directly down the button1 but they are not showing up i wish you can help me this is the error im getting -xml documents must start and end within the same entity the error is showing in the last last last linearlayout
this is the code im having problem with
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/Greetings"
android:textSize="20sp" />
<Button
android:id="#+id/Button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
</LinearLayout>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/thirdLine"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#ff0000"
android:textColor="#000000"
android:text="red" />
<TextView
android:id="#+id/fourthLine"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#00ff00"
android:textColor="#000000"
android:text="green" />
<TextView
android:id="#+id/fifthLine"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#0000ff"
android:textColor="#000000"
android:text="blue" />
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</RelativeLayout>
</LinearLayout>
this is a picture for the red blue green buttons where to be while mine are down
https://www.dropbox.com/s/73grdc0iff3ql3u/Untitled.png
this is what i reached to till now but i want them to be equally distributed like the picture above
https://www.dropbox.com/s/73grdc0iff3ql3u/Untitled.png
you have not closed the below linear layout tag in your xml.
"<"LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:orientation="horizontal" ">"
You need one more </LinearLayout> at the end of your xml file. At the minute, only 2 of the 3 LinearLayout's are being closed.
EDIT
The reason your Buttons are appearing at the bottom is because of this:
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
</RelativeLayout>
I'm not sure what purpose this serves in your application, but removing that will fix your problem.
Replace your xml with following code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="10" >
<TextView
android:id="#+id/secondLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="20sp" />
<Button
android:id="#+id/Button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="HelloWord" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="5dp"
android:weightSum="10" >
<TextView
android:id="#+id/thirdLine"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.3"
android:background="#ff0000"
android:text="red"
android:textColor="#000000"
android:gravity="center" />
<TextView
android:id="#+id/fourthLine"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.4"
android:background="#00ff00"
android:text="green"
android:textColor="#000000"
android:gravity="center" />
<TextView
android:id="#+id/fifthLine"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3.3"
android:background="#0000ff"
android:text="blue"
android:textColor="#000000"
android:gravity="center"/>
</LinearLayout>
</LinearLayout>
Hope it will help you.

Trouble with ImageView gravity on Java, Android

I have this code:
<LinearLayout
android:layout_weight="0.5"
android:layout_width="0px"
android:id="#+id/linearLayout2"
android:orientation="vertical"
android:layout_height="fill_parent">
<LinearLayout
android:layout_weight="0.45"
android:layout_width="fill_parent"
android:id="#+id/linearLayoutMainLogo"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="0px">
<ImageView
android:src="#drawable/logo"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:id="#+id/imageViewLogo"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I need to set ImageView in center of LinearLayout by horizontal and by vertical simultaneously, but my code doesn't work. Where did I do mistake? Thank you
This is wrong i think:
android:layout_width="0px"
android:layout_height="0px"
Just correct with this:
<LinearLayout
android:layout_width="fill_parent"
android:id="#+id/linearLayout2"
android:orientation="vertical"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:layout_weight="0.45"
android:layout_width="fill_parent"
android:id="#+id/linearLayoutMainLogo"
android:orientation="horizontal"
android:gravity="center"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/icon"
android:gravity="center"
android:layout_width="wrap_content"
android:id="#+id/imageViewLogo"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>

Categories

Resources