I am attempting to use the layout tricks #3 described in Android Layout Tricks #3: Optimize, Part 1 and I am getting an error when its trying to find the png file that I am using as the button obj my XML file for the button bar is the following .....
<?xml version="1.0" encoding="UTF-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<include
layout = "#drawable/button_yes"
android:id="#+id/okLabel" />
<include
layout="#drawable/button_no"
android:id="#+id/cancelLabel" />
</merge>
and this is called by the java script......
package com.bobocode.culliganapp;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.widget.FrameLayout;
public class OkCancelBar extends FrameLayout {
public OkCancelBar(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.okcancelbar, this, true);
}
}
which is being instantiated by ..............
<merge
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:okCancelBar="http://schemas.android.com/apk/res/com.bobocode.culliganapp">
<FrameLayout android:background="#drawable/culliganapp1"
android:id="#+id/secondpage"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="20dip"
android:layout_gravity="center|center"
android:padding="12dip"
/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_gravity="center|center"
android:padding="12dip"
android:background="#color/white"
android:textSize="24sp"
android:textColor="#color/black"
android:text="Does your water have an unpleasant taste or odor?"
android:id="#+id/question"
/>
</FrameLayout>
<com.bobocode.culliganapp.OkCancelBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingTop="8dip"
android:gravity="center_horizontal"
android:background="#AA000000"
okCancelBar:okLabel="Yes"
okCancelBar:cancelLabel="No"
/>
</merge>
any help would surely be appreciated
If you want to specify a drawable for your background you'll need to include the png in your drawable directory and specify it on your OkCancelBar with android:background="drawable/somepng".
Use normal character like "yesbutton" . And i don't know why you want to do :
Layout ="#drawable/button_yes"
just do something like android:background="#drawable/yesbutton"
i don't know what
layout is, is not even android because all attributs starts with "android:".
Related
I am trying to show a simple listView with an image, a large text and a small text in each list item. I have coded the following but when I run the app, it gives a NullPointerException. For the method i have used, I do not need to findviewbyid so I'm not sure what I am doing to give this error. Anyone know?
This is my MenuPage.java file
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
public class MenuPage extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[]values=new String[]{"Coffee","Steak","Ice Cream"};
String[]prices=new String[]{"$4.99","$21.99","$12.99"};
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.values, values));
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.prices, prices));
}
}
this is my menu.xml for the java file above:
<?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">
<ImageView
android:id="#+id/ic_launcher_movies"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="4dp"
android:layout_marginRight="10px"
android:layout_marginTop="2dp"
android:src="#drawable/items"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ImageView>
<TextView
android:id="#+id/values"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/values"
android:textSize="25sp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="64dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:minHeight="120dp">
</TextView>
<TextView
android:id="#+id/prices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/prices"
android:textSize="10sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="120dp"
android:layout_marginLeft="65dp">
</TextView>
also, I created an xml with the drawables for the images i want to use;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:duration="30"
android:drawable="#drawable/coffee"/>
<item
android:duration="30"
android:drawable="#drawable/steak"/>
<item
android:duration="30"
android:drawable="#drawable/icecream"/>
and my logcat gives this error
any help would be appreciated! thank you
I think the problem in the menu layout file
android:text="#+id/values"
try to write some text or specify link to string resource like
android:text="#string/my_button_text"
And when you call setListAdpate two times you ovirwrite first adapter with new. Have a look to example here
ListActivity
To be able to select the item (the row) AND sub items on the row, I use android:descendantFocusability="blocksDescendants". And I'm using the onItemSelectevent....
I as well use a custom background for my row.
Now the problem is, when I touch a row, the checkbox get's it's selected background as well (the blue default background, when you select a checkbox), how do I avoid this? I only want the row itself to adjust it's background to the item state, when I touch the row.
I'm using following row layout as a row in a listview in an adapter:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rlMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/list_gradient"
android:descendantFocusability="blocksDescendants" >
<CheckBox
android:id="#+id/cbSelected"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/tvDayName"
style="#style/text_list_info_big"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/cbSelected"
android:text="Tag" />
<RelativeLayout
android:id="#+id/drag_handle"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" >
<ImageView
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:src="#drawable/grabber" />
</RelativeLayout>
<LinearLayout
android:id="#+id/lvData"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/s"
android:layout_toRightOf="#+id/tvDayName"
android:orientation="vertical" >
<TextView
android:id="#+id/tvCount"
style="#style/text_list_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="" />
<TextView
android:id="#+id/tvInfo"
style="#style/text_list_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="" />
<TextView
android:id="#+id/tvInfo2"
style="#style/text_list_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:text="" />
</LinearLayout>
</RelativeLayout>
PS: I know I could handle the onClick event of the row and the sub items in my adapter instead of using onItemClick, but I'm using the DragAndSortList view and doing it that way does not work with the drag&drop there...
I found a solution. Overriding SetPressed of the row didn't work.
Now I'm using following RelativeLayout and that's solving my problem:
import android.content.Context;
import android.util.AttributeSet;
import android.widget.RelativeLayout;
public class UnpressableRL extends RelativeLayout
{
public UnpressableRL(Context context, AttributeSet attrs)
{
super(context, attrs);
}
#Override
protected void dispatchSetPressed(boolean pressed) {
// avoid handing on the event to the child views
}
}
I want to create a composite view from a bunch of controls, by extending a LinearLayout.
I'm using an XML file to specify the inner layout of my custom control. I'm inflating the custom control from the code via LayoutInflater.
The Problem is:
If i replace the root LinearLayout element to a merge element in the XML, my whole layout fall apart. If I don't, then I only encapsulated a LinearLayout with my custom one.
The question is:
What do i have to change in the merge layout, so my view looks like how a linear layout should?
How it looks like:
With LinearLayout (this is how i want it to look like):
http://oi45.tinypic.com/2pqwby1.jpg
With merge (this is the tag i want to use):
http://i45.tinypic.com/155j4o0.png
The code:
TowerLayout.java (no problems here, just in case):
public class TowerLayout extends LinearLayout implements OnClickListener {
public TowerLayout(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.tower_layout, this, true); //this is what i want to use.
//((LinearLayout)findViewById(R.id.root)).setOnClickListener(this); //this is the current ugly workaround.
this.setClickable(true);
setOnClickListener(this);
}
#Override
public void onClick(View v) {
Toast.makeText(getContext(), "Foo", Toast.LENGTH_SHORT).show();
}
}
What i have in Tower_Layout.xml:
<?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:clickable="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TowerA"
android:textAppearance="?android:attr/textAppearanceLarge"
android:duplicateParentState="true"
android:textSize="32sp" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="0.28"
android:duplicateParentState="true"
android:src="#drawable/icon" />
</LinearLayout>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Basic information"
android:duplicateParentState="true"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_marginLeft="15dip"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:text="Lorem ipsum dolor sit amet, consectetuer adipiscing elit." />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:duplicateParentState="true"
android:text="16m"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="100sp" />
What i want in Tower_Layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true">
...
</LinearLayout>
...
</merge>
One more thing:
I know, the screenshots are from the eclipse designer, but believe me: they look like this on android phone as well.
You should explicitly set the android:orientation tag for your layouts(and any LinearLayout, ever). Android supposedly uses horizontal by default, but in my experience it just messes it up if it's not set, and leads to some very strange-looking layouts.
I am new to Android/java programming. I really have no clue what I am doing, and would like a little help. What I am trying to do is build a form very similar to the one below. I have already put in all of the layout, and strings information, but I am having trouble with the functions. The error that I am getting is View cannot be resolved to a type line 14. I am using eclipse to help me program this app. Here is my main.xml, and MadisonStudios.java file's contents as well.
http://mobile.tutsplus.com/tutorials/android/android-sdk-creating-forms/
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/welcome" />
<EditText
android:id="#+id/EditTextName"
android:layout_height="wrap_content"
android:hint="#string/name"
android:inputType="textPersonName"
android:layout_width="fill_parent">
</EditText>
<EditText
android:id="#+id/EditTextEmail"
android:layout_height="wrap_content"
android:hint="#string/email"
android:inputType="textEmailAddress"
android:layout_width="fill_parent">
</EditText>
<Spinner
android:id="#+id/SpinnerStatus"
android:layout_height="wrap_content"
android:prompt="#string/status"
android:layout_width="fill_parent"
android:entries="#array/statuslist">
</Spinner>
<EditText
android:id="#+id/EditTextFeedbackBody"
android:layout_height="wrap_content"
android:hint="#string/changebody"
android:inputType="textMultiLine"
android:lines="5"
android:layout_width="fill_parent">
</EditText>
<Button
android:id="#+id/ButtonSendChange"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="sendChange"
android:text="#string/changebutton">
</Button>
</LinearLayout>
MadisonStudios.java
package com.madisonstudios.supportapp;
import android.app.Activity;
import android.os.Bundle;
public class MadisonStudios extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
public void sendChange(View button) {
// Do click handling here
}
}
import android.view.View;
Add this line in import statements..
You should import it. If you are using Eclipse, press Ctrl+Shift+o.
I am working on a custom list view. I want to show a CheckBox at the custom view. There is no text for the CheckBox. I found it always have some spaces at the right of the CheckBox.
Here is my layout xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:background="#fa9654"
android:paddingTop="65dp" android:paddingBottom="65dp">
<TextView android:id="#+id/bus_route_list_item_num"
android:layout_height="wrap_content" android:layout_width="0dip"
android:gravity="center" android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="0.15"></TextView>
<TextView android:id="#+id/bus_route_list_item_station"
android:layout_height="wrap_content" android:layout_width="0dip"
android:gravity="left" android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight=".5"></TextView>
<TextView android:id="#+id/bus_route_list_item_fee"
android:layout_height="wrap_content" android:layout_width="0dip"
android:gravity="center" android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight=".15"></TextView>
<CheckBox android:id="#+id/bus_route_list_item_reminder" android:layout_height="wrap_content"
android:layout_width="0dip" android:layout_weight=".20" android:gravity="center"
android:layout_gravity="center" android:paddingRight="0dp" android:paddingLeft="0dp"
android:paddingTop="0dp" android:paddingBottom="0dp" android:background="#0066ff"
android:text=""
/>
</LinearLayout>
The result looks like:
As you can see there are some space at the right of the checkbox. What I want is put the checkbox at the middle of the blue area.
Is it possible to remove the unwanted space? thanks
by default, the Checkbox has minWidth and minHeight value
you can set its value to 0
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="0dp"
android:minHeight="0dp" />
The result will be like that without any extra spaces
You can wrap CheckBox in LinearLayout and then use android:gravity="center" on that layout.
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="0dip"
android:layout_weight=".20"
android:background="#ff0000"
android:gravity="center">
<CheckBox android:id="#+id/bus_route_list_item_reminder"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
/>
</LinearLayout>
As another alternative, you can use RelativeLayout. This would greatly simplify you layout and you will be able to get rid of layout_weight.
Neither of previous solutions worked for me, but I've tried applying a translation to the content and it worked pretty well, no need in additional layout hierarchy, nor implementing own views:
<CheckBox
...
android:text="#null"
android:translationX="12dp" />
Also, it keeps bounds of the element in proper place, so touch area is not shifted.
The translationX seems to work. But it causes problem if you want to support RTL layouts. Another solution would be to set the width of checkbox to a fixed length (e.g. 26dp):
<CheckBox
android:layout_width="26dp"
android:layout_height="wrap_content"
android:text="#null" />
To remove extra space at right of the image (when there is no text) extend CheckBox class and override getSuggestedMinimumWidth() method in order to return there image width. Complete solution:
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.widget.CheckBox;
public class CheckBoxWithoutText extends CheckBox
{
private Drawable buttonDrawable;
public CheckBoxWithoutText(Context context)
{
super(context);
}
public CheckBoxWithoutText(Context context, AttributeSet attrs)
{
super(context, attrs);
}
#Override
protected int getSuggestedMinimumWidth()
{
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1)
{
return getCompoundPaddingLeft() + getCompoundPaddingRight();
}
else
{
return buttonDrawable == null ? 0 : buttonDrawable.getIntrinsicWidth();
}
}
#Override
public void setButtonDrawable(Drawable d)
{
buttonDrawable = d;
super.setButtonDrawable(d);
}
}
I would use a relative layout here. Aligning checkbox on parent right...
Regards,
Stéphane