Android colored view containing textviews - android

Ive made a program which has a view in the top op the window (right under the title bar).
According to what is goin on in the program, the color of the view will change.
This works just fine.
Now my problem is: I want 2 textviews inside it, next to each other. So probably: View, Tablelayout, tablerow, textview, textview, tablerow end, tablelayout end, view end.
But this does not seem to work. It gave me the error "Java.lang.RuntimeException: Unable to start activity ComponentInfo" and "View can not be cast to viewgroup".
Nowhere in the java code do i touch any of the new views in the xml code, the only java that touches that XML, is TopView = (View)findViewById(R.id.TopView); and TopView.setBackgroundColor(Color.GREEN ); Topview is the outer view, and works perfectly without anything inside it.
This is the XML code
...
<View
android:id="#+id/TopView"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:background="#8E8E8E" >
<TableLayout
android:id="#+id/tableTrolo"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/TableRow000"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left"
android:orientation="vertical" >
<TextView
android:id="#+id/lbl1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="Vare :"
android:textSize="22dp" />
<TextView
android:id="#+id/lbl2"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:text="du grim" />
</TableRow>
</TableLayout>
</View>
...

You can not have child-views inside a View.
A View is always the leaf node in a layout hierarchy.
1.Re-write the 1st line of your xml-layout as:
<LinearLayout
android:id="#+id/TopView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#8E8E8E">
2.Change corresponding Java code as below:
TopView = (LinearLayout)findViewById(R.id.TopView);

You are placing multiple elements inside a View tag. View's don't hold children Views, there's ViewGroup class for that. Use one of FrameLayout,LinearLayout,RelativeLayout instead.

Don't use View. Change your View to ViewGroup. The difference between View and ViewGroup is that ViewGroup CAN contain other Views. If you want to put some TextViews and other things inside it, you should use ViewGroup or some kind of Layout, like LinearLayout or RelativeLayout.

Related

View's Background Color Removed When Using RelativeLayout In ListView

While working with ListViews I ran into an interesting issue. So my goal was to write an xml layout for a custom ListView item design.
First of all, here is the problematic xml layout (problem described below):
<?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"
android:background="?android:attr/activatedBackgroundIndicator" >
<TextView
android:id="#+id/notebook_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceLarge"
android:layout_toRightOf="#+id/notebook_color_tag"
/>
<View
android:id="#+id/notebook_color_tag"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
/>
</RelativeLayout>
The layout is fairly basic, just a View, that's used to display a color, and a TextView.
I have a subclass of ListFragment which uses an instance of a subclass of CursorAdapter in its setListAdapter() method. In that custom CursorAdpater's bindView() I set the View's color and the TextView's text (that code is not the problem, you can ignore it, it's just here for referrence):
#Override
public void bindView(View view, Context context, Cursor cursor) {
TextView notebookName = (TextView) view.findViewById(R.id.notebook_title);
View colorTag = view.findViewById(R.id.notebook_color_tag);
String name = cursor.getString(NOTEBOOK_NAME_POS);
int color = cursor.getInt(NOTEBOOK_COLOR_POS);
notebookName.setText(name);
colorTag.setBackgroundColor(color);
}
Now, the problem is that if the View's android:layout_height is set to match_parent or wrap_content, the View simply does not appear in the ListView's items. It seems as it has some margin at the top. If a concrete width value is specified, for example: android:layout_height="40dp", everything works fine.
Here is how the bogus layout looks:
Here is how it should look:
As explained above, the blue rectangle does not seem to be inside the ListView's item container at all (but the element is there, it's accessible via findViewById() and it's color does get changed via the setBackgroundColor()call, the View simply seems not to be displayed).
The follwoing change to the layout fixes the problem:
<?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="horizontal"
android:background="?android:attr/activatedBackgroundIndicator" >
<View
android:id="#+id/notebook_color_tag"
android:layout_marginTop="0dp"
android:layout_width="20dp"
android:layout_height="match_parent"
/>
<TextView
android:id="#+id/notebook_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceLarge"
/>
</LinearLayout>
I understand that LinearLayout makes more sense here than the RelativeLayout, but I'm curious what was wrong in the RelativeLayout's case.
You have duplicate 'notebook_color_tag' ID's. You are adding it twice in the first layout XML (as a parameter in the TextView and also as a child of the layout).
Also, I would reverse the order, like so:
<View
android:id="#+id/notebook_color_tag"
android:layout_width="20dp"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
/>
<TextView
android:id="#+id/notebook_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:textAppearanceLarge"
android:layout_toRightOf="#id/notebook_color_tag"
/>
Since the RelativeLayout will create and place in the order of the XML, this is more efficient (it does not have to remeasure to place the views correctly.

Android Layout proplem

I have a layout contain one image and 3 text field
I've tried to align the image to right and text field to left but I've failed
I've used
android:layout_gravity="right" for image and left to text but it did not work also I've used end and start in gravity with no success
this is the layout code:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp"
android:orientation="vertical"
android:background="#drawable/card_background">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/listthumb"
android:layout_width="80dp"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:contentDescription="Rss video thumbnail"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/listtitle"
style="#style/listTitle"
android:maxLines="3"/>
</LinearLayout>
<TextView
android:id="#+id/shortdescription"
android:layout_width="wrap_content"
android:ellipsize="end"
android:maxLines="2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/listpubdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="11dp"/>
</LinearLayout>
</FrameLayout>
Try to use a <RelativeLayout> instead of a <LinearLayout>
With the RelativeLayout you could place a widget depending on the position of another widget
Here the Relative Layout description
Hope this will help, I have not had time to test....
One linear layout should have vertical orientation and contain the 3 text fields.
One linear layout should have horizontal orientation and contain both the above linear layout and the image.
To push two views to the edges of the screen, you can also give each a left/right margin and then put a blank view with weight = 1 in between them.
Please read a bit more on how layouts work on Android and the different types available to you. A LinearLayout will stack the containing Views either Horizontally or Vertically one after the other. A FrameLayout is simply a container and the items within have to position themselves. RelativeLayout allow you to position your views with a relative reference to other views (in your case, you can position your ImageView, and then your 3 TextViews relative to where the ImageView is).
If you can use LinearLayout instead of RelativeLayout, you should do so, as RelativeLayout is always slower, due to having to perform two passes prior to rendering as it needs to measure each view and then also perform the layouts based on that. You might be looking for something like (pseudo-code):
<LinearLayout orientation=horizontal>
<LinearLayout orientation=vertical>
<TextView />
<TextView />
<TextView />
</LinearLayout>
<ImageView />
</LinearLayout>
You have not described your question well . Check below code if it works .
You just forgot to add orientation in linear layout containing one text view and a Image view .
Add Orientation to Your Linear Layout.

Android: Using Object to define View in Layout

My post is based on a previous post and greatly simplified.
(Android: Two Views On Top of Each Other Using XML)
The file/object DrawV populates the screen with pink circles and allows one to touch a circle to make it disappear. In another file, private DrawV drawView = new DrawV(this); This populates the screen but does not participate in the layout.
setContentView(drawView) shows the dots,so I know it works. I want to use a layout named setContentView(R.layout.activity_title); which includes two buttons at the top of the screen and dots below. In other words, I was wondering if there is a method to put the dots shown in some sort of View that can be included with buttons in the same layout.
Any help? Please?
Tell me if you need anything.
If DrawV is an Android View (or extends View), you can include it in a regular xml layout file, and then use that layout file with setContentView(int).
To reference the DrawV class in your layout, you'll need to use the fully-qualified name (with the package).
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button_one"
android:text="One"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
<Button
android:id="#+id/button_two"
android:text="Two"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1.0" />
</LinearLayout>
<com.example.views.DrawV
android:layout_below="#id/buttons"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
Above, the RelativeLayout is your root view. The LinearLayout, buttons, is a ViewGroup just to hold the two buttons and keep them of equal width (note the layout_width=0dp and equal layout_weight). Your DrawV view will be laid out below the buttons View, and then will match the parent container's width and height (fill it).
If you save this under src/main/res/layout/activity_circles.xml, you'll be able to use setContentView(R.layout.activity_circles) in your Activity to set the layout.

android-How to repeat a lineaLayou several times without using listView

What I mean is this , you know we've custom listView that we can make several rows with every thing we want in .
i want to do the same but I don't want to use listView . I want to make 10 rows that contain some text . something like this :
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/layoutround"
android:gravity="right"
android:orientation="vertical"
android:padding="10dp" >
<TextView
android:id="#+id/dama"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="20"
android:gravity="center"
android:text="2"
android:textSize="#dimen/font_size_weather" />
<TextView
android:id="#+id/maxt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:text="Medium Text"
android:textSize="#dimen/font_size_weatherother" />
</LinearLayout>
I want to repeat this layout in my java file ,How is that possible ?
The reason I don't want to use listView is because I have an image as my background , If I use listView I can't use this background .
How can I do that ?
thanks
You might inflate this xml file, edit the contents accordingly and then append it to a parent ViewGroup. Another idea would be creating a compound ViewGroup which encapsulates your element.
Also, you probably can actually insert a background on a listview (which would be even better).
parent.xml
<LinearLayout
...
android:id="#+id/parentLayout />
sibling.xml <- your example
view.java
public View onCreateView(...) {
ViewGroup root = findViewById(R.id.parentLayout);
LayoutInflater inflater = getLayoutInflater();
for ( MyData data : dataList ) {
ViewGroup sibling = (ViewGroup) inflater
.inflate(R.layout.sibling,null);
EditText dama = (EditText)sibling.findViewById(R.id.dama);
dama.setText(data.getTheText());
root.addView(sibling);
}
Like #Der Golem said, you could make your ListView transparent.
Or, to answer your question, you need to have a root XML:
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/layoutround"
android:orientation="vertical" >
and in your Fragment or Activity, you need to
- get a reference to this layout
- inflate the layouts you want to display in the root XML
- or, manually create Views like TextView etc
- then use container.addView(myView)

Android Separate Empty VIews for ListVIews

I want to make multiple empty views for a list view and set them programmatically.
So I 've got a listview in an ListActivity. The way my client wants the app, I have a header bar in the app, so the layout looks like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/providerListLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<include
android:id="#+id/headerBar_ref"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/header_with_dashboard_button" />
<include
android:id="#+id/loadingView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
layout="#layout/loading_view" />
<RelativeLayout
android:id="#+id/listViewWrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/headerBar_ref" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/headerBar_ref" >
</ListView>
</RelativeLayout>
So I have the 2 empty views in separate xml files. In the list Activity I try to set the empty view like so:
RelativeLayout rootLayout = (RelativeLayout) findViewById(R.id.listViewWrapper);
RelativeLayout noFavsLayout = (RelativeLayout) this
.getLayoutInflater().inflate(emptyViewLayoutId,
rootLayout);
getListView().setEmptyView(noFavsLayout);
But when I do this, the empty view is there ALL the time. I've also tried to to add the view using addContentView(), but that takes over the whole screen. I've not been able to find a solution on S/O yet
Based on reading http://wiresareobsolete.com/2010/08/adapting-to-empty-views/, the actual mechanism for showing the empty view is that the adapter checks to see if the list is empty, and then sets the visibility of either the ListView or empty view to View.GONE, then sets the other one to View.VISIBLE. For this to work properly, both views have to be in the same parent view. In your example, this would mean something like
<RelativeLayout
android:id="#+id/listViewWrapper"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#id/headerBar_ref" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="This list is empty."
/>
</RelativeLayout>
(Note that I removed "layout_below" from your ListView, it was the only item in the relative layout, so it didn't need that reference. Also, the view has been added in the XML, you should not have to inflate it in the java.)
Now if you want to programmatically set a different empty view (for instance after performing a search), you could add another view to your relative layout, with another id (such as noResults)... and discover that it's always displayed.
<TextView
android:id="#+id/noResults"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="No results were returned."
/>
So in your onCreate() you'll need to find that view and set its visibility to gone as well.
ListView listView = (ListView)findViewById(android.R.id.list);
View empty = findViewById(android.R.id.empty);
listView.setEmptyView(empty);
View noResults = findViewById(R.id.no_results);
noResults.setVisibility(View.GONE);
Then whenever you change the empty view for your list, you'll want to set the visibility of the other view to GONE to make sure only one is getting displayed.
listView.setEmptyView(noResults);
empty.setVisibility(View.GONE);
I hope this helps!

Categories

Resources