MarginLeft doesn't work: Why? - java

i have this layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView2"
android:layout_marginLeft="13dp"
android:layout_marginRight="0dp"
android:layout_marginBottom="0dp"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="16sp"
android:text="#string/text"
/>
</ScrollView>
</RelativeLayout>
I would like to know why on some phones or versions of Android (2.3 Gingerbread mainly) the margins are pratically 0, in the sense that it is not detached from the left margin but it is attached, as if there were no android:layout_marginLeft="13dp"

Try to add android:layout_gravity="left" to your textView. Value of layout_gravity can be any of valid list.

Try the following in your xml .I have added android:layout_gravity="left"
<TextView
android:id="#+id/textView2"
android:layout_marginLeft="13dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:textSize="16sp"
android:text="text"
/>

Related

How to use scrollview over codeview?

i'm building an tutorial app where im displaying code snippet to user, but scrolling is just working on white area of the screen but not over the code snippet, may be codeview is overlapping the scrollview, How to resolve it so the codeview also scroll:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="activity_main.xml"
android:textColor="#android:color/black"
android:textStyle="bold"
android:layout_marginTop="5dp"
android:textSize="18sp"/>
<io.github.kbiakov.codeview.CodeView
android:id="#+id/code_view_xml"
android:layout_marginTop="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="create layout resource file inside layout directory with name:"
android:layout_marginTop="10dp"
android:textColor="#000000"
android:textStyle="italic"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="single_item.xml"
android:textColor="#android:color/black"
android:textStyle="bold"
android:layout_marginTop="5dp"
android:textSize="18sp"/>
<io.github.kbiakov.codeview.CodeView
android:id="#+id/code_view_item"
android:layout_marginTop="7dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
</ScrollView>
here the output :
You are missing padding. Try adding padding.
Also your linearlayout height is match_parent. Change that to wrap and then try

Android - Issue with size of EditText in Layout xml

I am having a problem with the layout xml for my fragment... I think it may be because the layout was originally made for just an activity, and then later it was changed to a fragment. It looks like this in my editor:
But in my phone and emulator, it ends up like this:
Can anyone think of a solution for me? I've tried out multiple things but cannot seem to fix it, as i think maybe the rules are a little different when working with fragments? Thank you for all and any help!!
<?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">
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title" />
<EditText android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"/>
</LinearLayout>
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/body" />
<EditText android:id="#+id/body" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" />
<Button android:id="#+id/confirm"
android:text="#string/confirm"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
Layout file for the activity:
<?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:orientation="vertical" >
<FrameLayout
android:id="#+id/frame1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="130dp"
android:layout_toRightOf="#+id/textView1" >
</FrameLayout>
</LinearLayout>
Just change android:layout_width to match_parent. This will fill up all the available space horizontally.
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/body" />
<EditText android:id="#+id/body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scrollbars="vertical" />
<Button android:id="#+id/confirm"
android:text="#string/confirm"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
For Layout file of activity :
<FrameLayout
android:id="#+id/frame1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="130dp"
android:layout_toRightOf="#+id/textView1" >
</FrameLayout>
You can use the property "ems" to change the size.
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
for your text views and edit views try to match_parent instead of wrapping content
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title" />
<EditText android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"/>

Android Custom ViewGroup not rendering correctly

I have a ViewGroup that has the following xml layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/ticketBuy_viewLayout"
android:paddingLeft="16dp"
android:paddingRight="16dp">
<ImageView
android:id="#+id/ticketBuy_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/ticketBuy_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/ticketBuy_icon"/>
<TextView
android:id="#+id/ticketBuy_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/ticketBuy_icon"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
Later, I put these Views into a ListView. And, well, don't really get the desired result.
What I want it to be is a small rectangular view with an image to the left, a title to the top right and the date to the bottom left. I am not posting the ListView xml because it does not have anything that would be of use (just a width and height set to match_parent) However, all I get is this (This ListView has about 8 elements):
I think you just messed up with the layout.
Try this layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ticketBuy_viewLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="16dp"
android:paddingRight="16dp" >
<ImageView
android:id="#+id/ticketBuy_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="#+id/ticketBuy_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/ticketBuy_icon"
/>
<TextView
android:id="#+id/ticketBuy_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/ticketBuy_name"
/>
</RelativeLayout>

Bring content of FrameLayout to front

I have this activity layout where I have a header (RelativeLayout), content (FrameLayout) and a footer (RelativeLayout) all inside a LinearLayout (please refer to the attached screenshot below) :
The problem is that sometimes when content (which is actually a Fragment) is large enough, it gets clipped by the footer (e.g. in the attached image one can see that some button are being clipped). I have tried setting the background of footer layout to transparent (via xml and via code as well), calling the FrameLayout.bringToFront() method but nothing seems to work!
Pasted below is the layout xml I am using:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/layoutMain"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical"
tools:context=".BatwaSelectLanguage" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutHeader"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="top" >
<ImageView
android:id="#+id/imBatwa"
android:layout_width="209dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:layout_marginTop="20dp"
android:src="#drawable/batwa" />
<Spinner
android:id="#+id/spLang"
android:layout_width="140dp"
android:layout_height="60dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="#000000"
android:prompt="#string/english" />
<TextView
android:id="#+id/tvSelectLanguage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/spLang"
android:drawablePadding="30dp"
android:drawableRight="#drawable/line"
android:gravity="center"
android:text="#string/select_your_language"
android:textColor="#color/tvWhite"
android:textSize="#dimen/tvSizeSelectLang" />
</RelativeLayout>
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</FrameLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layoutFooter"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="bottom" >
<ImageView
android:id="#+id/imNFC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="0dp"
android:layout_marginLeft="30dp"
android:contentDescription="#string/nfcimg"
android:src="#drawable/nfc" />
<ImageView
android:id="#+id/imNadra"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginBottom="00dp"
android:layout_marginRight="10dp"
android:contentDescription="#string/nadra"
android:src="#drawable/nadralogo" />
</RelativeLayout>
</LinearLayout>

Two TextViews on the same line

Hoping this is a simple one
I want two textviews on the same line aligned to the left.
Currently my layout is as shown:
I want this layout(note i modified the diagram on paint):
here is the xml i currently have:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtSetItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Large Text"
android:layout_weight="2"
android:textAppearance="?android:attr/textAppearanceLarge"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"/>
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusableInTouchMode="false"
android:focusable="false" />
<TextView
android:id="#+id/txtSetAmount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false" />
<TextView
android:id="#+id/txtSetPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:focusableInTouchMode="false"
android:clickable="false"
android:focusable="false"/>
hmm your layout shouldn't compile-- there is no attribute android:layout_width on the TextView objects.
To fix this, remove layout_weight and set layout_width to wrap_content
Anyways, here:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="this"
/>
</LinearLayout>
Change your first TextView's weight to 0 (or to something smaller than 1). You're giving them equal weight and LinearLayout places them in a way that gives them equal width, which isn't what you want.
Don't use weight.
Use wrap_content on the layout_width.
This will get you what you are looking for:
<LinearLayout 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"
tools:context=".MainActivity" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This"
android:paddingLeft="10dp" />
</LinearLayout>
one way to go about this would be to use relative layout .
You can change how "close" or "far" apart the two textviews will be using changing the android:layout_marginLeft property on textView2.
The following property:
android:layout_alignBaseline="#+id/textView1"
makes sure that tv2 is aligned with tv1.
<?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" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="26dp"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_marginLeft="32dp"
android:layout_toRightOf="#+id/textView1"
android:text="TextView" />
</RelativeLayout>

Categories