Android View View XML Programmatically - android

I'm trying to debug a view that I'm creating programmatically. I'd like to be able to see what the exact XML is of the view after it's created.
Really I'd like to be be able to do something like Log.d(TAG,view.toString())
And get an output like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/activity_main"
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="sampleapp.example.com.tr221test.MainActivity">
<Button android:id="#+id/surveyButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take Survey!"/>
</RelativeLayout>
Is there anyway I can programmatically see what the xml is of a view?

There is nothing built in for that. You would have to write your own View-hierarchy-to-XML code or see if somebody has a library for that.
Or, use Android Studio's layout inspector.
Or, use uiautomatorviewer.
Or, use Stetho.

Related

How to create a custom ListView in Android Studio

I'm quite new to Android Studio and for my app I need to create a custom ListView made of cells that have:
2x textView
a non modifiable ratingView (Stars)
an imageView
the cells should look something like this:
I would really appreciate if you show me a way to do it using the design interface in Android Studio instead of the xml editor.
Up until now I created a simple listView but I don't know how to customize it. This is the xml code:
<?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="dancam.com.chords.TabMainActivity">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
Hopefully you can understand and develop your design now.
you need to add the views for every line.
Add a row.xml in your layout resources.
Create a custom adapter for listview, in getView inflate row.xml.
Here is an example.
If you need a more flexible implementation, considere
using RecyclerView, guide here.
Hope it helps

PorterDuff Color Filters are not supported

I just switched to Android Studio. I face a rendering problem after creating a blank activity. When I go to xml and pick API 19 or below for preview I see this error:
The graphics preview in the layout editor may not be accurate :
- PorterDuff Color Filters are not supported.
Here is the 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: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="ir.iseemedia.trubleshooter.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Title :"
android:id="#+id/ConnectionSpeedTitle" />
</RelativeLayout>
Can anybody please explain What's the problem and How can I solve it?
Apparently changing Rendering API to Upper API levels will make this error disappear. But I still don't know the reason of this problem.

Where the right place to set layout in android?

I have 2 files in the res.
Activity_main.xml is
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.test2.MainActivity"
tools:ignore="MergeRootFrame" />
fragment_main.xml is
<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.test2.MainActivity$PlaceholderFragment" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
I plan to change the layout where I problem in this current layout when I drag and drop all item just overlap on each other how to make a nice form type layout? What adjustment and which file must I adjust?
It sounds like you are using the Eclipse GUI tools to create your layout. I strongly suggest that you read about Android Layouts. If you want, you can edit the XML layout text directly or at the very least this will help you understand the properties you can set for your controls to get the exact look that you want.

Add background image to RelativeLayout in android

I tried to add a background image to an Android app using android:background="#drawable/imageURL". I added the image to the drawable-hdpi folder, but I'm getting an error. I'm trying to add the image to a RelativeLayout. I also tried to use LinearLayout but it didn't work either.
So how to add background image to my app within RelativeLayout in Android?
What is the error?
<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/abc.png"
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:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
replace
android:background="#drawable/abc.png"
with
android:background="#drawable/abc"
From the documentation:
May be a reference to another resource, in the form
"#[+][package:]type:name"
as you can see the extension is not part of the value of the attribute
just write #drawable/abc instead of #drawable/abc.png

Background image not loading for relative layout in android

In my android xml code, I want to draw a background image for a relative layout:
I have a file called homeBackground.png. But its not actually showing up when I run on my phone. If I look in the GUI editor for the layout, I can see the background.
Does anyone know what's wrong here?
Thanks.
<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/homeBackground"
tools:context=".ActivityCourses" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
</RelativeLayout>
EDIT:
Now I get this
[2013-10-13 02:21:10 - NAME] res\drawable-hdpi\homeBackground.png: Invalid file name: must contain only [a-z0-9_.]
Capital letters not allowed in resources files.
Instead of
homeBackground.png
rename the file name to:
home_background.png

Categories

Resources