Inflating Custom View into Layout - android

I am trying to inflate a custom view object into my Layout XML, but I am getting an inflate exception, can someone help me?
Here is my Layout XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<ViewGroup
android:id="#+id/desenha_foto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#+id/button_capture"/>
<Button
android:id="#+id/button_capture"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="snapIt"
android:text="#string/Capture"
android:layout_alignParentBottom="true"/>
</RelativeLayout>
Here is my code, where I try to inflate the ViewGroup:
public class Desenha_Foto extends ActionBarActivity {
private DesenhaFotoView draw;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Bitmap bitmap = Global.getInstance().getBitmap();
draw = new DesenhaFotoView(this, bitmap);
setContentView(R.layout.activity_desenha_foto);
final ViewGroup viewGroup = (ViewGroup) findViewById(R.id.desenha_foto);
viewGroup.addView(draw);
Global.getInstance().getProgressDialog().dismiss();
}
}

You can't use a ViewGroup tag in the xml layout because ViewGroup is an abstract class. You either use one of ViewGroup's children(LinearLayout, RelativeLayout etc) or you use a custom implementation(a class that extends ViewGroup). You have your own view, DesenhaFotoView use that instead of the ViewGroup.

Related

Classcastexception: Widget cannot be cast to layout

Though this tends to be very basic question, i cannot solve this issue. I searched the similar kind of issue but none solves my issue.
I created my own class where i created some basic controls and i called this class in my xml as
<com.mypackagename.classname
..
..
/>
and some views goes inside this. and before this now i would like to add relativelayout as
activity_main1.xml:
<RelativeLayout xmlns:android="schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android1:layout_width="match_parent"
android1:layout_height="wrap_content" >
<com.test.MainLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/drawer_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_drawer" />
</LinearLayout>
</com.test.MainLayout>
</RelativeLayout >
and i my main activity i declared as
MainActivity:
Myview view;
view = (Myview)this.getLayoutInflater().inflate(R.layout.activity_main1, null);
setContentView(view);
where MyView is the class where i have my own controls.
After adding relative layout i tried something like
MainActivity1.java:
public class MainActivity1 extends FragmentActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
view = (Myview)this.getLayoutInflater().inflate(R.layout.activity_main1, null);
item.addView(view);
setContentView(item); // facing an error here
.......... rest of the code
}
and my layout activity is
public class MainLayout1 extends LinearLayout
{
....
}
while running it is throwing classcastexception and the error is
java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to com.view.layout.MainLayout
view = (Myview)this.getLayoutInflater().inflate(R.layout.activity_main1, null);
At this point in your java class, you are getting MyView Object in view, but it is actually a layout file instance which returns its parent layout here its a RelativeLayout.
Since inflate(R.layout.activity_main1, null); returns the object of RelativeLayout. Instead of this you have to get instance of your MainLayout1 like(R.id.mainlayout) and then cast it into the MyView object like:
RelativeLayout item = (View)this.getLayoutInflater().inflate(R.layout.activity_main1,null);
view = (Myview) item.findViewById(R.id.mainlayout);
item.addView(view);
setContentView(item);

GraphView in Fragment

I want to display a graph in a fragment, but I didn't found many information about how to use GraphView in general. Can someone please help me to display a simple graph?
I started with this: http://android-graphview.org/#doc_createsimplegraph but don't get it working in a fragment.
What I did was to create a fragment_graph.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="250dip"
android:id="#+id/graph1"
android:background="#102f65" />
</LinearLayout>
and add the relevant code in the Fragment class:
public class GraphFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_graph, container, false);
// add the data here if necessary
// ...
LinearLayout layout = (LinearLayout) view.findViewById(R.id.graph1);
layout.addView(graphView);
return view;
}
// ...
}
Then you can use the fragment "as is".
Most credit actually goes to vogella.
define a empty LinearLayout with fixed width and height in your layout.xml file and then add the graphview view to it.
Layout
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="250dip"
android:id="#+id/graph1" />
Java
LinearLayout layout = (LinearLayout) findViewById(R.id.graph1);
layout.addView(graphView);
Just for the record:
You cannot use it in connection with ScrollViews. If a ScrollView is wrapping your LinearLayout the lib doesn't show anything.

Why does onScrollListener think ListView is not created?

So the error that is thrown is
java.lang.IllegalStateException: Content view not yet created
This is thrown when I uncomment the getListView().setOnScrollListener(this); line.
public class MyCategoryFragment extends ListFragment implements OnScrollListener {
private ArrayList<Article> m_articles = new ArrayList<Article>();
public ArticleAdapter m_artadapter;
public MyCategoryFragment() {
super();
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_category, container, false);
m_articles = manager.loadCategory(getActionBar().getTitle().toString(), 10);
m_artadapter = new ArticleAdapter(this.getActivity(), R.layout.article_button, m_articles);
this.setListAdapter(m_artadapter);
//getListView().setOnScrollListener(this);
return rootView;
}
Here is the layout just so you can see it is the correct listview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<TextView
android:id="#android:id/empty"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:gravity="center_vertical|center_horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="It's not you it's us..."/>
</LinearLayout>
I have researched this extensively and I believe in must be something so simple but it is taking more than a day to fix this. Thanks
as the onCreateView doc stays:
creates and returns the view hierarchy associated with the fragment
so since the method does not return, you will are not able to access the ListView through getListView(). You can obtain a valid reference in the OnActivityCreate callback.

Adding Layout to a ViewGroup Android

I have a flying in menu which based on ViewGroup.
I want that there will be a basic layout and in any activity i would be able to insert to the view group new layout and afterwards to erase it.
But it doesn't work!!! Can you help me please.
class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.root = (FlyOutContainer) this.getLayoutInflater().inflate(R.layout.activity_main, null);
this.setAdditionalLayout(findViewById(R.id.physical_layout));
this.setContentView(root);
}
ViewGroup:
<com.nurielweizmann.calculator.view.viewgroup.FlyOutContainer xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#444488"
android:orientation="vertical"
android:id="#+id/menu">
............
</RelativeLayout>
</com.nurielweizmann.calculator.view.viewgroup.FlyOutContainer>
Function:
FlyOutContainer root;
public void setAdditionalLayout(View view){
root.addView(view,1);
}
Thanks in Advance
Try overriding the setContentView(int) instead of the onCreate(Bundle).
Make sure your base layout XML has a ViewGroup (FrameLayout, for example) available to put each Activity's content.
When overriding the setContentView(int), inflate your base layout first then inflate the Activity's layout and place the Activity's layout on the FrameLayout available.

Adding dynamic ExpandableListView inside main.xml

Is there a site where I can find the inner workings of the BaseExpandableListAdapter? I read the API and I still don't understand how it loops through the whole array that is supplied to provide the view. I'm having problems with my own implementation. I can't create a whole list of expandable lists without using ExpandableListActivity, even though both are the same. It's supposed to retrieve strings from the database and create an expandablelistview out of each one, and add all the created expandablelists to a linearlayout inside main.xml. What happens is that only the expandablelistview for the first string is shown. Here's the snippet
Main Class:
public class MainActivity extends Activity implements OnClickListener {
DBAdapter groupTable = new DBAdapter(this);
ExpandableListView groupLabel;
GroupAdapter adapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
groupTable.open();
adapter = new GroupAdapter(groupTable.getAllGroups());
retrieveExpandables(adapter);
groupTable.close();
Button addGroupButton = (Button)findViewById(R.id.addGroup);
addGroupButton.setOnClickListener(this);
}
public void retrieveExpandables(GroupAdapter adapter) {
LinearLayout layout = (LinearLayout)findViewById(R.id.grouplist);
LinearLayout groupLayout =
(LinearLayout)getLayoutInflater().inflate(R.layout.grouplistview, null);
ExpandableListView groupExpandableList =
(ExpandableListView)groupLayout.findViewById(R.id.groupLabel);
groupExpandableList.setAdapter(adapter);
layout.addView(groupLayout);
}
BaseExpandableListAdapter class:
#Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
if (convertView instanceof ViewGroup)
return (ViewGroup) convertView;
Context context = parent.getContext();
LayoutInflater inflater = LayoutInflater.from(context);
ViewGroup item = (ViewGroup)inflater.inflate(R.layout.grouplisttext, null);
TextView groupLabel = (TextView)item.findViewById(R.id.groupLabel);
groupLabel.setText(groups[groupPosition].name);
groupLabel.setVisibility(View.VISIBLE);
Log.d("A1", "This part repeating");
return item;
}
XML file for the TextView that will be the expandable list title
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:id="#+id/groups"
android:layout_height="wrap_content">
<TextView android:id="#+id/groupLabel"
android:textSize="24sp"
android:text="asdf"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="40dip"
android:layout_marginLeft="45dip"
android:gravity="center_vertical" />
</LinearLayout>
XML file for the ExpandableListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:id="#+id/groups"
android:layout_height="wrap_content">
<ExpandableListView android:id="#+id/groupLabel"
android:textSize="24sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_weight="1"
android:layout_height="40dip" />
</LinearLayout>
Sorry if I posted too much code, I tried removing as much unnecessary information as possible.
I found out where the problem went wrong. Apparently I got the LinearLayout and the ScrollView inside the main.xml interchanged, and the method for getViewGroup class was wrong. It's all good now

Categories

Resources