Textview not skipping to next line android - android

I am a complete beginner in android programming, and I am working with TextViews. I want two TextViews with one on top and one on bottom. But it doesn't work, and I am not sure why. Here is the xml
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView
android:text="#string/hello_world"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
So I have the first TextView as fill_parent for width to skip a line, but instead it just takes it out. I can't add a picture as a I just made this account for this question, but it just displays the first TextView and not the next. What is the mistake I made?

In a LinearLayout, you need to set the orientation.
android:orientation="vertical"

Use this code, here orientation was missing in your code
<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" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context=".MainActivity">
<TextView
android:text="#string/hello_world"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>

Related

How to set my Child Layout also starts from parent's position?

I try to set my LinearLayout starts from screen edge(like Parent layout).I dont want the White Space between two layouts.
i need to start from left side from screen edge and top also edge . or follow the parent left and top properties.
NOTE:
suppose i dont want to change the Relative layout properties only changes in linear layout means how to fix it?
<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="com.example.zd.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/preview"
android:focusable="true">
<requestFocus />
</LinearLayout>
</RelativeLayout>
DO LIKE BELOW.
<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"
tools:context="com.example.zd.MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/preview"
android:focusable="true">
<requestFocus />
</LinearLayout>
</RelativeLayout>
remove these lines.. these are creating padding for the child.
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
But according your question.
<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="#color/gray"
android:clipToPadding="false" // this will do the trick here
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
// suppose you have 16dp padding
<LinearLayout
android:layout_marginTop="-16dp"
android:layout_marginBottom="-16dp"
android:layout_marginLeft="-16dp"
android:layout_marginRight="-16dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:id="#+id/preview"
android:background="#color/green"
android:focusable="true">
<requestFocus />
</LinearLayout>
</RelativeLayout>

Android: ListView has strange margins

I'm trying to make an app with an action bar, floating button and ListView.
When I add the ListView in Android Studio it has always big margins to all sides.
Whatever I change in the layout code it doesn't help it always looks like this:
My layout code looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
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="appenzeller.de.glimpse.MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/mainList" />
<LinearLayout
android:id="#+id/ly_bar_bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="right"
android:orientation="horizontal">
<android.support.design.widget.FloatingActionButton
android:id="#+id/button_addc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_margin="28dp"
android:src="#drawable/ic_add_white_18dp"
android:onClick="addNewEntry"
app:borderWidth="0dp"
app:elevation="6dp"
app:pressedTranslationZ="12dp" />
</LinearLayout>
</RelativeLayout>
What am I doing wrong?
Try to remove
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
from your RelativeLayout. You said "all sides", I can only see left/right. But if it's all sides you also have to remove "paddingTop" and "paddingBottom".
Your relative layout is using padding which means you are leaving some space from sides.
Removing the following lines will remove padding
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
if you want to remove padding from top and bottom as well you can remove following tag as well
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingTop="#dimen/activity_vertical_margin"

Android studio layout background error

<?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"
android:background="#drawable/background"
tools:context="com.example.black.myfirstproject.MainActivity">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
android:background="#drawable/background"
i wanted background in MainActivity so added background and clicked "Run"
-> app is down
why my app is down?
// before added background.png in res ->drawable folder
You should use an ImageView - Caution: there are serious memory implications if large images aren't loaded correctly.
You should also read: Displaying Bitmaps Effectively
<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="com.example.black.myfirstproject.MainActivity">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/background"/>
</RelativeLayout>
Also refer to:
android:scaleType=""
for options on positioning the image and specifying how it is displayed within the ImageView.

Remove space between app border and a List view

I'm developing an android app which uses a list view using android studio.But when i use it a space is being displayed between list view and app borders.how can i remove that space?? Like in this pic I need my app to be designed .The following code is XML code for listview i got in my editor.Suggest the changes for space removal.
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
</RelativeLayout>
The padding is due to the padding provided to RelativeLayout
From your XML code remove padding by making you code look like
<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"
tools:context=".MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

Background image at the right bottom corner?

I have the following simple layout:
<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" >
<TextView
android:id="#+id/textview_app_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_description" />
</RelativeLayout>
How could I add there background image at the right bottom corner (it should be semi-transparent, since may be behind the text - I've tried to use ImageView, but it doesn't work with such scenario)?
Would this work?
<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" >
<ImageView
android:id="#+id/xxx"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:alpha="0.4"
android:src="#drawable/xxx"/>
<TextView
android:id="#+id/textview_app_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_description" />
</RelativeLayout>
You can use an XML Bitmap drawable as a background. Say the image (with desired transparency) is stored in bg.png. If you just use it as the background, it will stretch to fill the view; however, you can define an XML bitmap drawable with a gravity attribute. Let's put it in /res/drawable/bottom_right_bg.xml:
<?xml version="1.0" encoding="utf-8"?>
<bitmap
xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/bg"
android:gravity="bottom|right" />
Then you can add an android:background attribute to the RelativeLayout:
<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="#drawable/bottom_right_bg"
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" >
<TextView
android:id="#+id/textview_app_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_description" />
</RelativeLayout>

Categories

Resources