Android: Custom composite class extending LinearLayout thinks it's a LinearLayout - java

I am creating a custom composite layout that consists of a clickable horizontal LinearLayout that contains an ImageView and two TextViews. Ultimately I want to be able to reference the entire thing using a single field and add, subtract, and edit these to my activity depending on user activity. I'm using an XML resource that I am inflating in the constructor. However my phone thinks I'm creating a LinearLayout and I get the following error when i try to implement the custom class:
D/ConnectionButton﹕ Ready to inflate
D/ConnectionButton﹕ constructor 2
D/AndroidRuntime﹕ Shutting down VM
W/dalvikvm﹕ threadid=1: thread exiting with uncaught exception (group=0x417be898)
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exampleapp.app/com.exampleapp.app.ManageFriends}: android.view.InflateException: Binary XML file line #3: Error inflating class com.exampleapp.utils.ConnectionButton
The error when I am setting NameText. If I leave that line out it runs but my later Log tells me android thinks it is LinearLayout, not ConnectionButton
So here's my definition class ConnectionButton:
public class ConnectionButton extends LinearLayout {
ImageView IconView;
TextView NameView;
TextView StatusView;
public ConnectionButton(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.d("ConnectionButton","Ready to inflate");
addView(inflater.inflate(R.layout.connection_row, null));
Log.d("ConnectionButton","Inflated");
IconView = (ImageView) findViewById(R.id.icon);
NameView = (TextView) findViewById(R.id.name);
StatusView = (TextView) findViewById(R.id.status);
}
public ConnectionButton (Context context, AttributeSet attrs) {
super(context, attrs);
Log.d("ConnectionButton","constructor 2");
/* LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.d("ConnectionButton","Ready to inflate");
addView(inflater.inflate(R.layout.connection_row, null));
Log.d("ConnectionButton","Inflated");*/
IconView = (ImageView) this.findViewById(R.id.icon);
NameView = (TextView) this.findViewById(R.id.name);
StatusView = (TextView) this.findViewById(R.id.status);
NameView.setText("From Constructor 2");
}
}
Here's my XML resource:
<com.exampleapp.utils.ConnectionButton xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="50dp"
android:orientation="horizontal"
android:clickable="true"
style="#android:style/Widget.Button">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:adjustViewBounds="true"
android:paddingRight="5dp"
android:id="#+id/icon"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_weight="2"
android:id="#+id/name"/>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="16sp"
android:layout_weight="1"
android:id="#+id/status"/>
</com.exampleapp.utils.ConnectionButton>
And here is where I create it: (N.B. ConnectionList is the existing vertical LinearLayout that should contain these objects)
LinearLayout box = (LinearLayout)findViewById(R.id.ConnectionList);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Log.d("ConnectionButton","Ready to inflate");
View CBv = inflater.inflate(R.layout.connection_row, box);
Log.d("CBv class",CBv.getClass().toString());

You're making the incorrect assumption that inflater.inflate returns the new view. According to the documentation it returns:
The root View of the inflated hierarchy. If root was supplied, this is
the root View; otherwise it is the root of the inflated XML file.
So in your case CBv is a LinearLayout because that's what the parent is. To get access to your view, just use findViewById.

I think that you're trying to inflate a ConnectionButton inside the constructor of ConnectionButton which poses a recursive problem.
I suggest that you change ConnectionButton to make it inherit from FrameLayout instead (since you only have one root child) and change the root type of its XML layout content to LinearLayout. Your control will then be an extended FrameLayout which contains a single LinearLayout.

Related

CardView is not inflating properly using LayoutInflater

I want to add CardView programmatically.
Here is my Main Activity XML Layout (activity_main.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/linearLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
</LinearLayout>
Here is my CardViewTemplate (card_view_template.xml)
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/cardViewTemplate"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp"
android:clickable="true"
android:foreground="?android:selectableItemBackground">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="This is a Card" />
</androidx.cardview.widget.CardView>
Here is my Java Code (MainActivity.java)
LayoutInflater inflater = getLayoutInflater();
ViewGroup parent = findViewById(R.id.linearLayout1);
inflater.inflate(R.layout.card_view_template, parent);
Everything works fine till here.
Now I want to add Card at certain position in my activity_main.xml as I am using multiple CardViews, I want to add Cards at certain position. Hence, instead of the above code, I tried this:
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.card_view_template, null);
ViewGroup parent = findViewById(R.id.linearLayout1);
parent.addView(view, 0);
But it does not inflate properly. Only the Text is visible, The Card does not seem to appear.
When dynamically adding views, we shouldn't inflate the View with null ViewGroup parent.
In this View view = inflater.inflate(R.layout.card_view_template, null); here parent is specified as null, this caused the problem.
Specify the parent to which the View will be attached to and only set attach to parent as false. This way Parent will be specified but not attached.
Hence first declare parent (root) and then create View and specify the parent (root) and set attach to parent (root) false
This is the correct statement
View view = inflater.inflate(R.layout.card_view_template, parent, false);
Hence the complete code will be:
LayoutInflater inflater = getLayoutInflater();
ViewGroup parent = findViewById(R.id.linearLayout1);
View view = inflater.inflate(R.layout.card_view_template, parent, false);
parent.addView(view, 0);
i suggest that you use directly the linear layout ,like:
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.card_view_template, null);
LinearLayout LL = findViewById(R.id.linearLayout1);
LL.addView(view,0);

Android textView.setText() throws exception

I'm trying to create an app where I can put several times the same kind of layout in a LinearLayout.
The layout just has a TextView and i'm trying to use setText but this line of code doesn't seems to work.
I don't know how many layout I must add to the LinearLayout because it depends of how many drones (I have one layout/drone) the user will have upload to the app, so I made a function that read the text to put in the TextView from a SharedPreference and that also read the number of drones (so the number of layouts) in another SharedPreferences.
The TextView i'im putting the string in is from a layout named dronelayoutmain.
I tried sending just "test" in the setText, but it crashes anyway, so I think that android just can't find my TextView, but I don't really know why.
So here is the function I call in OnCreate that is supposed to add one layout for each drone I have
public void affichagedrone(){
LinearLayout mlineairelayout = (LinearLayout)
findViewById(R.id.lineaireLayout);
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Context mContext;
mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
SharedPreferences droneprefs = mContext.getSharedPreferences("dronepref", MODE_PRIVATE);
SharedPreferences applimaindata = mContext.getSharedPreferences("applimaindata", MODE_PRIVATE);
int nbdrone = applimaindata.getInt(String.valueOf(emplacementNbDrone),1);
String string;
TextView textViewnomConstructeurDrone;
if (nbdrone !=0) {
for(int i=0;i<nbdrone;i++) {
textViewnomConstructeurDrone = (TextView) findViewById(R.id.textViewNomConstructeur);
string = droneprefs.getString(String.valueOf(i),"Fail");
textViewnomConstructeurDrone.setText(string);
final View rowView = inflater.inflate(R.layout.dronelayoutmain, null);
// Add the new row before the add field button.
mlineairelayout.addView(rowView);
}
}
}
And here is the layout i'm trying to put in the LinearLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="120dp"
>
<TextView
android:id="#+id/textViewNomConstructeur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="#string/adddrone"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
The function is supposed to check in applimaindata the number of layout to add, and then, in the for(), is supposed to set the Text of the TextView from the droneprefs file and then add the Layout to the main LinearLayout
If this TextView belongs in the newly inflated layout, you must find it after the layout is inflated:
for(int i = 0; i < nbdrone; i++) {
final View rowView = inflater.inflate(R.layout.dronelayoutmain, null);
textViewnomConstructeurDrone = rowView.findViewById(R.id.textViewNomConstructeur);
string = droneprefs.getString(String.valueOf(i),"Fail");
textViewnomConstructeurDrone.setText(string);
mlineairelayout.addView(rowView);
}

How to inflate Android View in LinearLayout class?

I've got a little piece of xml, which I'll be using in a lot of places in my app. For this reason I want to store it in a separate file. So I created mywidget.xml in which I have my xml. I then try to inflate this in mywidget.java, after which I want to include it in a different xml file like so:
<com.mycom.android.ui.widget.AmountWidget android:layout_width="fill_parent" android:layout_height="wrap_content"></com.mycom.android.ui.widget.AmountWidget>
In my java file, I try to inflate the initial xml like this:
public class AmountWidget extends LinearLayout {
public AmountWidget(Context context) {
super(context);
LinearLayout ll = (LinearLayout) findViewById(R.layout.amount_widget);
addView(ll);
}
}
But with the code above I get an error saying that there's an error inflating class com.mycom.android.ui.widget.AmountWiget.
My question: Does anybody know how I can inflate a layout so that I can use it as a class in another xml layout file?
The xml from the widget looks like this:
<?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="wrap_content"
android:layout_margin="10dp"
android:padding="10dp"
android:background="#layout/border"
>
<EditText
android:id="#+id/payment_amount_major"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="35sp"
android:textStyle="bold"
android:inputType="number"
android:digits="0,1,2,3,4,5,6,7,8,9"
android:maxLength="9"
android:gravity="right"
/>
</LinearLayout>
Try this:
// ViewGroup to add views
LinearLayout list = (LinearLayout) findViewById(R.id.linear_layout_list);
// Inflate you custom xml to view
// Ex: res/layout/item_data.xml
View item = getLayoutInflater().inflate(R.layout.item_data, null);
// Link item in list here
list.addView(item);
mContainerView is LinearLayout which contain your EditText and row is your xml filename.
The View class has an inflate method which wraps LayoutInflater.inflate. You should be able to use:
LinearLayout ll = (LinearLayout) inflate(context, R.layout.amount_widget, this);
to inflate your widget from xml. The call to addView() won't be needed, as inflate will add the newly inflated view for you!
Edit: Just a note, because this View is already a LinearLayout, there's no need to have the root of the xml you're inflating also be a LinearLayout. It can increase your performance if you inflate only the EditText and just add that to the parent, rather than nesting a second LinearLayout within the parent. You can set the LinearLayout attributes (such as background and padding) directly on the AmountWidget wherever it's added in xml. This shouldn't matter too much in this specific case, but may be good to know going forward if you have a situation with many nested views.
Edit2: The View class has three constructors: View(Context), View(Context, AttributeSet), and View(Context, AttributeSet, int). When the system inflates a view from xml, it will use one of the latter two. Any custom View will need to implement all three of these constructors. An easy way to do this while reusing your code is like this:
public AmountWidget(Context context) {
super(context);
LinearLayout ll = (LinearLayout) inflate(context, R.layout.amount_widget, this);
}
public AmountWidget(Context context, AttributeSet attrs) {
this(context);
}
public AmountWidget(Context context, AttributeSet attrs, int defStyle) {
this(context);
}
This will work if you don't care what the attributes or style arguments are, and just want the AmountWidget created the same any time it's inflated.
Easiest Solution
LinearLayout item = (LinearLayout )findViewById(R.id.item);//where you want to add/inflate a view as a child
View child = getLayoutInflater().inflate(R.layout.child, null);//child.xml
item.addView(child);
ImageView Imgitem = (ImageView ) child.findViewById(R.id.item_img);
Imgitem.setOnClick(new ...

Extending ListActivity android.R.id.list

I am creating a Calculator. When calculations are made, I need them displayed, then clickable so I can delete individual figures. I followed this tutorial, and got this far. The only problem is when I run it, I get a FC and my logcat says this:
Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'
All of my research say to change my id to android.R.id.list. I tried this, but still getting the same error.
Here is my code:
CustomAdapter.java (extends ArrayAdapter)
private final Context context;
private final ArrayList<Calculations> items = null;
private final LayoutInflater inflater;
public CustomListAdapter(Context context, int textViewResourceId, ArrayList<Calculations>items){
super(context, textViewResourceId, items);
this.context = context;
this.items = items;
this.inflater = LayoutInflater.from(context);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final View v = convertView != null
? convertView
: infalter.inflate(R.layout.list_item, parent, false);
final Calculations c = items.get(position);
final TextView type = (TextView) v.findViewById(R.id.tvType);
final TextView figure = (TextView) v.findViewById(R.id.tvFullFigure);
final TextView amount = (TextView) v.findViewById(R.id.tvAmount);
type.setText(c.getType());
figure.setText(c.getFigure());
amount.setText(c.getFigureAmount());
return v;
}
acitivty_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="15dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:background="#drawable/concret_back"
>
<!--Code remove for brevity-->
<ListView
android:id="#+id/android.R.id.list"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
</RelativeLayout>
I have had this problem with another app I have done and never found a solution. So I though I would ask why do I keep getting the Caused by: java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list' error?
Thanks for any input.
Change your ListView id from
<ListView
android:id="#+id/android.R.id.list"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
to
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />

Inflate custom android widget

I know there are dozens similar post, but it looks to me everything is correct here:
The custom widget:
public class DoubleTextItem extends LinearLayout {
private TextView txtMain;
private TextView txtDescription;
public DoubleTextItem(Context context) {
super(context);
}
public DoubleTextItem(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
((Activity)getContext()).getLayoutInflater().inflate(R.layout.widget_double_text_item, this);
setupViewItems();
}
private void setupViewItems() {
txtMain = (TextView) findViewById(R.id.txtMain);
txtDescription = (TextView) findViewById(R.id.txtDecription);
}
public void setDescription(String text) {
txtDescription.setText(text);
}
}
The custom widget layout xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/txtMain"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/txtDecription"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
ANd here inside an activity function i get a casting error,
LayoutInflater inflater = LayoutInflater.from(this);
DoubleTextItem item = (DoubleTextItem) inflater.inflate(R.layout.widget_double_text_item, layout);
item.setText(som-txt);
item.setDescription("#"+athlete.getString("position"));
Here, the root View is a LinearLayout but you try to cast it your custom class:
DoubleTextItem item = (DoubleTextItem) inflater.inflate(R.layout.widget_double_text_item, layout);
The standard advice is:
All DoubleTextItems are LinearLayouts, but not all LinearLayouts are DoubleTextItems.
Meaning you cannot downcast objects from a LinearLayout to a DoubleTextItem, there are too many assumptions and Java won't let you do it.
If you want a DoubleTextItem in your layout you need to use:
<your.package.name.DoubleTextItem
... />
(Also, calling inflate inside onFinishInflate() seems a little silly especially since you don't save the inflated item... If you want to inflate a different layout, don't inflate the first one.)
Overall it looks like you are trying to recreate the now deprecated TwoLineListItem, perhaps you can learn some pointers from it's source code (or just use the TwoLineListItem.)

Categories

Resources