Android Studio - ViewFlipper - ImageView - android

I had my viewflipper working, flipping between two images. I messed around with sizes and long story short the app crashes now. I undid everything and tried putting it in again, and I can't get it to not crash anymore. Without the viewflipper it runs fine.
here is my content_main.xml
<?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:id="#+id/content_main"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.melanie.cskiosk.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Welcome!"
android:visibility="visible"
android:textColor="#android:color/background_dark"
android:textStyle="normal|bold" />
<ViewFlipper
android:id="#+id/viewflipper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoStart="true"
android:flipInterval="2000">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/woit_161013_0749" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/woit_161013_0761" />
</ViewFlipper>
</RelativeLayout>
Not sure what I'm doing wrong here. Like I said I had this working just fine a few hours ago

Related

Android Studio does not show layoutpreview

I am trying to create a layout in Android Studio but the preview of the layout is not showing at all. There is no errour though.
How can i get it to work properly? I will add a screenshot.
Preview not loading the layout
Thanks in advance.
<?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:id="#+id/content_main"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.diceout.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Let's Play!" />
<Button
android:id="#+id/rollButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="155dp"
android:layout_marginStart="155dp"
android:layout_marginTop="25dp"
android:text="Roll" />
</RelativeLayout>
TextView
#ivan franken Could you post your #layout/activity_main Xml code. I saw this line tools:showIn:#layout/activity_main
in code on your content_main.xml. I tested it in my project and got similiar result to yours. But when I deleted it, it worked fine.

Android: "Error parsing XML: must not undeclare prefix"

I am very new to android development and was in the process of trying to create my first process when I came across the build error with which the question is titled.
Here is my XML code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app= ""
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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.austinc.clicker.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:id="#+id/textViewCount"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textSize="24dp" />
<Button
android:text="Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ButtonClick"
android:layout_alignLeft="#+id/ButtonClick"
android:layout_alignStart="#+id/ButtonClick"
android:layout_marginTop="61dp"
android:id="#+id/buttonReset" />
<Button
android:text="Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="55dp"
android:id="#+id/ButtonClick"
android:layout_below="#+id/textViewCount"
android:layout_centerHorizontal="true" />
</RelativeLayout>
From what I can infer by double clicking on the error, the problem has to do with the second line of code (the meaning of which I have no idea). Is this a quick fix, or have I made a mistake somewhere in editing some piece of code?
I also faced the same issue, searched for 15 minutes and found the solution, i modified first three lines in content_main.xml
<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:id="#+id/content_main"
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.user.demoapp.MainActivity"
tools:showIn="#layout/activity_main">

TextView to fill space above a ListView

I want to show the word "STATS" above a Listview containing either 2 or 3 list items. I want the ListView at the bottom edge of the screen and the remaining space to be filled by the "STATS" TextView and the text centered vertically and horizontally. Basically a single word floating lonely in the exact middle of the remaining space.
I cannot do this in the "drag and drop" layout editor because it shows 8 placeholder items in the ListView, so I can't snap to the edges of things properly.
I've got the ListView how I want it, but the "STATS" is causing problems.
I've tried all sorts of things (apart from the right one, obviously). Here's my currently broken layout...
<?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: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="uk.co.xxxxxxxxxxx.xxxxxxx.MainActivity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="bottom"
>
<TextView
android:id="#+id/tv_reboot_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:textAlignment="center"
android:text="#string/stats"
/>
<ListView
android:id="#+id/lv_app_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/reboot_screen_items"
android:layout_gravity="center_horizontal|center_vertical"
/>
</LinearLayout>
I'm sure it's something trivial to do but its driving me mad.
Try this, it works well
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:id="#+id/tv_reboot_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="30sp"
android:textStyle="bold"
android:textAlignment="center"
android:text="STATE"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center" />
<ListView
android:id="#+id/lv_app_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/reboot_screen_items"
android:layout_gravity="bottom" />
</LinearLayout>
Result
Can you try 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"
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="uk.co.xxxxxxxxxxx.xxxxxxx.MainActivity"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<TextView
android:id="#+id/tv_reboot_message"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="30sp"
android:textStyle="bold"
android:textAlignment="center"
android:gravity="center"
android:text="STATS"
android:layout_above="#+id/lv_app_info"
/>
<ListView
android:id="#+id/lv_app_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/values"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
/>
</RelativeLayout>
You can use RelativeLayout to fill up the remaining space to show your TextView.
Use gravity and layout_gravity attributes.
Gravity and layout_gravity

Get imageview repeating: looks like titlebar

i try to get this picture at the top of my app as an "titlebar".
So i want that it repeats itself. But i am a little bit stuck here.
I have this code in my background.xml:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/topbar"
android:tileMode="repeat" />
and this is my 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:layout_margin="0dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:background="#drawable/bgColor"
android:gravity="top"
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/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/topbar" />
</RelativeLayout>
Does anyone know how to do so?
Thanks.
You need to set the width of ImageView as match_parent. That will do it. But you can also do that with 9patch images. For more information check out this link.
EDIT
Here is the layout with repeated background image. You need to set the src of ImageView as background.xml. And the reason there is a space between the image parent layout is that parent has padding. If you remove it, it will be fine as well.
<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:layout_margin="0dp"
android:layout_marginBottom="0dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_marginStart="0dp"
android:layout_marginTop="0dp"
android:gravity="top"
tools:context=".MainActivity" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/background" />
</RelativeLayout>

XML documents must start and end within the same entity help me

<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="${packageName}.${activityClass}" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
tools:context=".MainActivity" >
<AnalogClock
android:id="#+id/analogClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="290dp" />
<TextClock
android:id="#+id/textClock1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="146dp"
android:text="#+id/textclock"
android:textColor="#FFFFFF"
android:textSize="100sp" />
</RelativeLayout>
Can I have some help on this I'm really confused would really appreciate some advice. I was just cleaning up my code when this error appeared. I have no idea on how to fix
The error comes because you didn't close RelativeLayout tag in your xml file,so add </RelativeLayout>at the end of xml file.
You don't need to add two relative layout where one parent layout is useless
So simply replace in your code
<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="${packageName}.${activityClass}" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
tools:context=".MainActivity" >
with
<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/bg"
tools:context=".MainActivity" >
Second Option is
Remove your xml code and saved the xml file. After that,replace it with the above code and save. The error will be gone. It is probably a remnant from the time when "automatically build project" enabled.

Categories

Resources