Android compound control won't display - android

I'm trying to create a counter as a compound control.
TallyItem.java
package com.anna.mytallykeeper;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.*;
public class TallyItem extends LinearLayout {
private Button decrementButton;
private Button incrementButton;
private TextView descriptionText;
private TextView countText;
public TallyItem(Context context) {
super(context);
LayoutInflater inflate = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflate.inflate(R.layout.tallyitem_layout, this);
/*
String service = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li = (LayoutInflater)getContext().getSystemService(service);
li.inflate(R.layout.tallyitem_layout, this, true);
*/
//Get the fields
decrementButton = (Button)findViewById(R.id.decrement);
incrementButton = (Button)findViewById(R.id.increment);
descriptionText = (TextView)findViewById(R.id.description);
countText = (TextView)findViewById(R.id.count);
decrementButton.setOnClickListener(buttonListener);
incrementButton.setOnClickListener(buttonListener);
}
public TallyItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
private OnClickListener buttonListener = new OnClickListener() {
#Override
public void onClick(View v) {
Button button = (Button)v;
int count = Integer.parseInt(countText.getText().toString());
if (button.getText().equals("-")) {
count--;
} else if (button.getText().equals("+")) {
count++;
}
countText.setText("" + count);
}
};
}
tallyitem.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/decrement"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:gravity="center"
android:text="-"
android:textSize="30sp"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="200dp"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/description"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Item 1"
android:textSize="25sp"
/>
<TextView
android:id="#+id/count"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="0"
android:textSize="60sp"
/>
</LinearLayout>
<Button
android:id="#+id/increment"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:gravity="center"
android:text="+"
android:textSize="30sp"
/>
</LinearLayout>
main.xaml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<com.anna.mytallykeeper.TallyItem
android:id="#+id/tallyItem1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</merge>
I am at a loss as to why it won't show up in main.xml.
Thanks for any help.

your view need a constructor with the AttributeSet passed in. Change your constructor to this:
public TallyItem(Context context, AttributeSet attr) {
super(context, attr);
...
}
and get rid of the constructor with the single argument.

try this in your main.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
/>
<com.anna.mytallykeeper.TallyItem
android:id="#+id/tallyItem1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</merge>

Related

layout_weight property is ignored in Custom Views

I tried implementing a custom view and put it in an expandable card view, which is contained inside a RecyclerView. At the start, I open it and it looks quite nice.
Before scroll
But then, I leave it opened and scroll to the bottom of the RecyclerView. When I scroll back to the item, close it and then open it again, the layout is broken.After scroll
Below is the code of the custom view
<?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="60dp" android:background="?android:attr/selectableItemBackground"
android:clickable="true">
<View
android:layout_width="#dimen/session_padding_left"
android:layout_height="#dimen/session_padding_left"></View>
<TextView
android:id = "#+id/tv_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:textColor="#color/lightGray"
android:gravity="center_vertical"
android:layout_weight="6"
android:maxLines = "2"
android:lines="2"
android:textSize="#dimen/extra_small_text_size"
android:ellipsize="end"
android:paddingTop = "10dp"
android:paddingBottom = "10dp"
android:text = "Word-of-Mouth Behaviour in Mobile Social Media "/>
<TextView
android:id = "#+id/tv_time"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="right"
android:textSize="#dimen/extra_small_text_size"
android:textColor="#color/lightGray"
android:layout_weight = "4"
android:text="11:30 - 12:00"/>
<ImageButton
android:layout_width="#dimen/dimen_image_button_add_remove"
android:layout_height="#dimen/dimen_image_button_add_remove"
android:layout_gravity="center|right"
android:scaleType="fitCenter"
style = "?android:attr/borderlessButtonStyle"
android:src = "#drawable/ic_add_agenda"
/>
</LinearLayout>
And the view class:
package au.com.leremede.acis2016.views;
import android.content.Context;
import android.util.AttributeSet;
import android.widget.LinearLayout;
import android.widget.TextView;
import au.com.leremede.acis2016.R;
public class PresentationView extends LinearLayout {
private TextView tvTitle;
private TextView tvTime;
public PresentationView(Context context) {
super(context);
init(context);
}
public PresentationView(Context context, AttributeSet attrs)
{
super(context, attrs);
init(context);
}
private void init(Context context) {
setOrientation(HORIZONTAL);
inflate(context, R.layout.view_presentation, this);
tvTitle = (TextView)findViewById(R.id.tv_title);
tvTime = (TextView)findViewById(R.id.tv_time);
}
public void setTitle(String title)
{
tvTitle.setText(title);
}
public void setTime(String time)
{
tvTime.setText(time);
}
}
I solved this problem myself using the Percent Support Library
<?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="60dp" xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="?android:attr/selectableItemBackground"
android:clickable="true">
<View
android:id="#+id/empty_view"
android:layout_width="#dimen/session_padding_left"
android:layout_height="#dimen/session_padding_left"
></View>
<ImageButton
android:id="#+id/btn_add_remove_agenda"
android:layout_width="#dimen/dimen_image_button_add_remove"
android:layout_height="#dimen/dimen_image_button_add_remove"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:scaleType="fitCenter"
style="?android:attr/borderlessButtonStyle"
android:src="#drawable/ic_add_agenda"
/>
<android.support.percent.PercentRelativeLayout android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/empty_view"
android:layout_toLeftOf="#id/btn_add_remove_agenda">
<TextView
android:id="#+id/tv_title"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:textColor="#color/lightGray"
android:layout_alignParentLeft="true"
app:layout_widthPercent="60%"
android:maxLines="2"
android:lines="2"
android:textSize="#dimen/extra_small_text_size"
android:ellipsize="end"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:text="Word-of-Mouth Behaviour in Mobile Social Media "/>
<TextView
android:id="#+id/tv_time"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="right"
android:layout_alignParentRight="true"
app:layout_widthPercent="40%"
android:textSize="#dimen/extra_small_text_size"
android:textColor="#color/lightGray"
android:text="11:30 - 12:00"/>
</android.support.percent.PercentRelativeLayout>
</RelativeLayout>

Android Studio (1.5) hangs when using custom component

I am trying to use a custom component in my app. This is the code:
Layout file (select_method_layout):
<?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="#color/grey">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/encabezadometodo"
android:id="#+id/encabezadometodo"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:textStyle="bold"/>
<es.infaplic.aguaics.Componentes.ComponenteSelectMethod
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/methodfile"
android:layout_below="#+id/textexplicacion"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<es.infaplic.aguaics.Componentes.ComponenteSelectMethod
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/methodservice"
android:layout_below="#+id/methodfile"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/layoutmethodservice"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/servicemethod"
android:id="#+id/service"
android:gravity="center"
/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/layoutmethodfile"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#id/layoutmethodservice">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/filemethod"
android:id="#+id/file"
android:gravity="center"
/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/textexplicacion"
android:id="#+id/textexplicacion"
android:layout_centerHorizontal="true"
android:layout_below="#+id/encabezadometodo"
android:gravity="center"/>
The class that controls the custom component:
public class ComponenteSelectMethod extends RelativeLayout {
private ImageView imagen;
private TextView text;
public ComponenteSelectMethod(Context context) {
super(context);
inicializar();
}
public ComponenteSelectMethod(Context context, AttributeSet attrs) {
super(context, attrs);
inicializar();
}
private void inicializar() {
String infService = Context.LAYOUT_INFLATER_SERVICE;
LayoutInflater li =
(LayoutInflater)getContext().getSystemService(infService);
li.inflate(R.layout.componente_select_method_layout, this, true);
imagen=(ImageView)findViewById(R.id.imageiconoselectmethod);
text=(TextView)findViewById(R.id.textmethod);
}
public void setImage(int icono){
this.imagen.setBackgroundResource(icono);
}
public void setText(String text){
this.text.setText(text);
}
}
And, in the main class, I am using it this way:
final AlertDialog.Builder dialog=new AlertDialog.Builder(this);
dialog.setTitle(getString(R.string.firsttime));
LayoutInflater inflater = getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.select_method_layout, null);
dialog.setView(dialoglayout);
ComponenteSelectMethod cFile=(ComponenteSelectMethod)dialoglayout.findViewById(R.id.methodfile);
ComponenteSelectMethod cService=(ComponenteSelectMethod)dialoglayout.findViewById(R.id.methodservice);
LinearLayout layoutFile=(LinearLayout)dialoglayout.findViewById(R.id.layoutmethodfile);
LinearLayout layoutService=(LinearLayout)dialoglayout.findViewById(R.id.layoutmethodservice);
cFile.setImage(R.drawable.icono_archivo_agu);
cFile.setText(getString(R.string.textexplicacionfile));
cService.setImage(R.drawable.icono_directorio);
cService.setText(getString(R.string.textexplicacionservice));
final AlertDialog methodDialog=dialog.show();
layoutFile.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
settings.edit().putString("method", "file").apply();
methodDialog.dismiss();
proceder();
}
});
layoutService.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
settings.edit().putString("method", "service").apply();
methodDialog.dismiss();
proceder();
}
});
EDIT:
componente_select_method_layout:
<?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:layout_width="50dp"
android:layout_height="50dp"
android:id="#+id/imageiconoselectmethod"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text=""
android:id="#+id/textmethod"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/imageiconoselectmethod"
android:layout_toEndOf="#+id/imageiconoselectmethod"
android:layout_marginStart="30dp"
android:layout_marginLeft="30dp"/>
</RelativeLayout>
With this, when I open the xml file, it fails rendering. It stays in "Rendering..." and does nothing more. If I access the Text mode in layout editing, and do anything (like hitting space bar) Android Studio stops responding.
Is something wrong with my custom component? Anybody knows how to avoid Android Studio to hang?
Thank you.

findViewById returns null - why?

I have created a custom view Game4 that extends LinearLayout. When I tried finding them, it field and returned null (and then nullPointerException and then crash/!!)
This is activity_play_game.xml:
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.example.sortthegraph.PlayGameActivity"
tools:ignore="MergeRootFrame" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.example.sortthegraph.Game_4 // A custom View extends LinearLayout
android:id="#+id/game4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
</com.example.sortthegraph.Game_4>
<View
android:layout_width="match_parent"
android:layout_height="1dip"
android:background="#000000" />
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
This is my OnCreate function:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_play_game);
tempGame = (Game_4) findViewById(R.id.game4); // ?? Why null ??
This is Game_4 constructors:
public class Game_4 extends LinearLayout {
public Game_4(Context context, AttributeSet attrs) {
this(context);
}
public Game_4(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.game_4_layout, this, true);
this.setWillNotDraw(false);
}
The game_4_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingBottom="25dp"
android:layout_marginLeft="15dip"
android:layout_marginRight="15dip"
android:gravity="center" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<View android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.example.sortthegraph.BackButton
android:id="#+id/backButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dp" >
</com.example.sortthegraph.BackButton>
<View android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="20dip"
android:layout_marginTop="20dip" >
<com.example.sortthegraph.BackButton
android:id="#+id/backButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</com.example.sortthegraph.BackButton>
<View
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.example.sortthegraph.BackButton
android:id="#+id/backButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
</com.example.sortthegraph.BackButton>
</TableRow>
<TableRow
android:id="#+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<View android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
<com.example.sortthegraph.BackButton
android:id="#+id/backButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp" >
</com.example.sortthegraph.BackButton>
<View android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1" />
</TableRow>
public Game_4(Context context, AttributeSet attrs) {
this(context);
}
You need to pass the attrs to the super constructor, e.g.
super(context, attrs);
Otherwise the attributes consumed by the superclass such as android:id are lost.
You can do your custom init in this two-arg constructor and call it from the 1-arg constructor as well:
public Game_4(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.game_4_layout, this, true);
this.setWillNotDraw(false);
}
public Game_4(Context context) {
this(context, null);
}
Game_4 has not been imported properly. Also in layout , press CTRL and take the cursor to com.example.sortthegraph.Game_4 and click to go to the corresponding class. If it does not direct to that corresponding class, then there may be some issue.

I want to draw using canvas over an existing xml background how can I do it?

This is the class where i draw the picture onto this canvas, and on this class I’m trying to set the canvas's background to an existing xml layout that I designed.
The idea of what I want to do is like this made-up function: canvas.setBackground(R.layout.game); on the onDraw method.
The java class:
package com.example.snakesnladders;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.view.View;
public class MyBringBack extends View {
Bitmap playerW;
public MyBringBack(Context context) {
super(context);
playerW = BitmapFactory
.decodeResource(getResources(), R.drawable.white);
}
#Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(playerW, 0, 0, null);
}
}
The XML file:
<?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="#drawable/easymap"
android:orientation="vertical" >
<TextView
android:id="#+id/whitePlayer"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="#drawable/white"
android:visibility="gone" />
<TextView
android:id="#+id/blackPlayer"
android:layout_width="32dp"
android:layout_height="32dp"
android:visibility="gone"
android:background="#drawable/black" />
<Button
android:id="#+id/btRoll"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/cubePic"
android:background="#drawable/buttonshape"
android:text="Roll"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/cubePic"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="42dp"
android:layout_marginRight="22dp"
android:background="#drawable/cube" />
<TextView
android:id="#+id/tvTurn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cubePic"
android:layout_alignRight="#+id/btRoll"
android:text="Your turn!"
android:textColor="#color/green"
android:textSize="32dp"
android:textStyle="italic" />
</RelativeLayout>
quick and dirty: Insert the the MyBringBack view in your xml and set height and width to match_parent as last element with a transparent Background
Upüdate your MyBringBack with these constructors:
public MyBringBack (Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
initView();
}
public MyBringBack (Context context, AttributeSet attrs) {
super(context, attrs);
initView();
}
public MyBringBack (Context context) {
super(context);
initView();
}
private void initView() {
//DO WHAT YOU WANT ON INIT
playerW = BitmapFactory
.decodeResource(getResources(), R.drawable.white);
}
And XML Like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/easymap"
android:orientation="vertical" >
<TextView
android:id="#+id/whitePlayer"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="#drawable/white"
android:visibility="gone" />
<TextView
android:id="#+id/blackPlayer"
android:layout_width="32dp"
android:layout_height="32dp"
android:visibility="gone"
android:background="#drawable/black" />
<Button
android:id="#+id/btRoll"
android:layout_width="160dp"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/cubePic"
android:background="#drawable/buttonshape"
android:text="Roll"
android:textColor="#FFFFFF" />
<TextView
android:id="#+id/cubePic"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="42dp"
android:layout_marginRight="22dp"
android:background="#drawable/cube" />
<TextView
android:id="#+id/tvTurn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cubePic"
android:layout_alignRight="#+id/btRoll"
android:text="Your turn!"
android:textColor="#color/green"
android:textSize="32dp"
android:textStyle="italic" />
<your.package.MyBringBack
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/my_bring_back"
android:background="#android:color/transparent" />
</RelativeLayout>

Error in implementing a custom view in android:

I'm trying to implement a custom View in my application. I went over this page:
http://developer.android.com/training/custom-views/index.html
and I have created the following:
XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/leftMenu"
android:layout_width="100dp"
android:layout_height="fill_parent"
android:background="#drawable/left_menu_background"
android:orientation="vertical"
android:visibility="gone" >
<ImageButton
android:layout_width="fill_parent"
android:layout_height="52dp"
android:background="#drawable/left_menu_close_button"
android:onClick="showLeftMenu" />
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/left_menu_separator" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/left_menu_buttons_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/left_menu_data_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/left_menu_button_transparent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:onClick="showSelectTab">
<ImageButton
android:id="#+id/left_menu_data_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/transparent_rectangle"
android:scaleType="fitXY"
android:src="#drawable/folder"
android:onClick="showSelectTab"/>
<TextView
android:id="#+id/left_menu_data"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Data"
android:textColor="#color/my_white"
android:onClick="showSelectTab"/>
</LinearLayout>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/left_menu_separator" />
<LinearLayout
android:id="#+id/left_menu_settings"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:onClick="showSettingsPopup">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/transparent_rectangle"
android:scaleType="fitXY"
android:src="#drawable/settings"
android:onClick="showSettingsPopup"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="Settings"
android:textColor="#color/my_white"
android:onClick="showSettingsPopup">
</TextView>
</LinearLayout>
<ImageButton
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/left_menu_separator" />
</LinearLayout>
</ScrollView>
and those classes:
AppViewLinearLayout:
package com.emildesign.sgreportmanager.ui;
import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
public class AppViewLinearLayout extends LinearLayout {
public AppViewLinearLayout(Context context, AttributeSet attrs, int layoutResource)
{
super(context, attrs);
if (isInEditMode())
return;
View view = LayoutInflater.from(context).inflate(layoutResource, null);
addView(view);
}
}
and: LeftSideMenu:
package com.emildesign.sgreportmanager.ui;
import com.emildesign.sgreportmanager.R;
import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
public class LeftSideMenu extends AppViewLinearLayout
{
private LeftSideMenuClickListener mListener;
public LeftSideMenu(Context context, AttributeSet attrs, int layoutResource)
{
super(context, attrs, layoutResource);
findViewById(R.id.left_menu_data).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mListener != null)
mListener.dataOnClick(v);
}
});
findViewById(R.id.left_menu_settings).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (mListener != null)
mListener.settingsOnClick(v);
}
});
}
public interface LeftSideMenuClickListener {
void dataOnClick(View v);
void settingsOnClick(View v);
}
}
Now I try to add it to my main layout like this:
<FrameLayout 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=".LoginScrActivity" >
<com.emildesign.sgreportmanager.ui.LeftSideMenu
android:id="#+id/leftSideMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
....
and for some reason I receive this in the logcat:
E/AndroidRuntime( 1034): java.lang.RuntimeException: Unable to start activity Co
mponentInfo{com.emildesign.sgreportmanager/com.emildesign.sgreportmanager.activi
ties.ReportsTableActivity}: android.view.InflateException: Binary XML file line
#10: Error inflating class com.emildesign.sgreportmanager.ui.LeftSideMenu
UPDATE:
I made the following change, in LeftSideMenu:
public LeftSideMenu(Context context, AttributeSet attrs)
{
super(context, attrs, R.layout.left_menu);
.....
The error was gone, but I still can see my custom layout.
Hirarchy Viewer:
What am I doing wrong?
Any help would be appreciated.
Thanks.
Add this constructor:
public LeftSideMenu (Context context, AttributeSet attrs) {...}

Categories

Resources