Android Layout XML file with class - java

I created a class that extends RelativeLayout.
I also created a xml layout file.
How can I 'connect' between them as I connect Activities with their xml files
(setContentView(R.layout.main);)
Thanks!

I mean you can use LayoutInflater to load the xml in the relative layout, but am not sure that what you should be doing. I am not sure what your trying to do but your wrapping your layout inside another view by doing this. When you can just load the xml straight into your view using the same tactic. So you lose one level of complexity.
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.view,null);
addView(view);

You can inflate the layout using the LayoutInflater right in your constructor.
public class RelSub extends RelativeLayout {
public RelSub(Context context) {
super(context);
// TODO Auto-generated constructor stub
LayoutInflater inflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.main, this);
}
}

Related

android - replace layout programmatically and using ViewStub

I am trying to implement a Newsfeed-type layout with multiple feed items.
The newsfeed item would have a certain layout when collapsed, and this layout would be replaced by an 'exploded' version, when the item is clicked.
I accomplished this by using a ListView of custom items. The custom item XML layout file has a ViewStub which is what I used to change the layout back and forth.
Now, though, I wanted to 'migrate' the layout over to RecyclerView, and to also follow a ViewHolder design pattern.
The latter is what I have tried first, and I'm running into all sorts of problems.
My approach has been as follows:
Get reference to collapsed layout (events_list_item_content) and expanded layout (events_list_item_selected_content);
Get reference to a simple layout resource file to be set as the ViewStub layout (view_stub_layout).
Get ViewStub reference, set its layout (view_stub_layout) inflate, and add the collapsed layout view to this layout (when first creating the feed, all of its items are going to be collapsed).
(After initialisation, when an item is clicked) Remove previous view (layout) from the ViewStubLayout, add the other type of layout.
Here is my custom adapter class:
public class FeedRecyclerAdapter extends BaseAdapter {
public class ViewHolder {
View inflatedViewStub1;
ViewStub viewStub;
LinearLayout viewStubLayout;
LinearLayout listItemContent, listItemContentSelected;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final FeedItem item = feedItems.get(position);
final ViewHolder viewHolder;
if (convertView == null) {
viewHolder = new ViewHolder();
convertView = inflater.inflate(R.layout.events_list_item_content_new_container, parent, false);
View view = null;
view = inflater.inflate(R.layout.events_list_item_content, null);
viewHolder.listItemContent = (LinearLayout) view.findViewById(R.id.events_list_item_content);
view = inflater.inflate(R.layout.events_list_item_selected_content, null);
viewHolder.listItemContentSelected = (LinearLayout) view.findViewById(R.id.events_list_item_content_selected);
view = inflater.inflate(R.layout.view_stub_layout, null);
viewHolder.viewStubLayout = (LinearLayout) view.findViewById(R.id.view_stub_layout);
viewHolder.viewStub = (ViewStub) convertView.findViewById(R.id.list_item_feed);
(viewHolder.viewStubLayout).addView(viewHolder.listItemContent);
viewHolder.viewStub.setLayoutResource(R.layout.view_stub_layout);
viewHolder.inflatedViewStub1 = viewHolder.viewStub.inflate();
convertView.setTag(viewHolder);
} else viewHolder = (ViewHolder) convertView.getTag();
if (item.getExploded()) {
viewHolder.viewStubLayout.removeAllViews();
viewHolder.viewStubLayout.addView(viewHolder.listItemContentSelected);
} else {
viewHolder.viewStubLayout.removeAllViews();
viewHolder.viewStubLayout.addView(viewHolder.listItemContent);
}
return convertView;
}
However, when testing, the page where the Newsfeed is supposed to appear is blank.
ViewStub stub = (ViewStub) findViewById(R.id.layout_stub);
LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
stub.setLayoutResource(layoutId);
stub.inflate(); // inflate 1st layout
ll.removeAllViews(); // remove previous view, add 2nd layout
ll.addView(LayoutInflater.from(context).inflate(secondLayoutId, ll, false));
Android ViewStub change layouts programatically

android: creat layout dynamically and setContentView it dynamically

Suppose there is an Activity called "m1" and there are two layouts called "m2" and "m3" both have few buttons is dynamically how i can setContentView dynamically whay
You have a method to set view to the setContentView
So you can pass the root parent view to this method to achieve your requirement.
You can pass layout Resource ID or View in setContentView(); method.
try this..
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if(viewlayout1){
view = mInflater.inflate(R.layout.layout1,null);
}else{
view = mInflater.inflate(R.layout.layout2,null);
}
setContentView(view);
/*******************/
}
The view hierarchy can have only one root. What setContentView() essentially does is that it sets the root view.
In your case,
Method 1
You'll have to either make one of the layouts as the root and add the other as a child. And call setContentView(root).
Or
Method 2
Create a dummy container layout. Set that as root.
And add both your layouts as children to that container layout.

What is the correct way to get layout inflater in Android?

There is a way to get layoutInflater:
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
and another way is:
LayoutInflater inflater = LayoutInflater.from(context);
a third one (when I am in an Activity) is:
LayoutInflater inflater = getLayoutInflater();
So what is the difference between them?
Note that when I sent the third inflater to my adapter, my application worked. But when I sent the context and created the inflater via the second way, it didn't!
use outside of your activity
LayoutInflater inflater = (LayoutInflater) context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE );
Within your activity
LayoutInflater inflater = getLayoutInflater();
Check this
If you open up the Android source you can see that the LayoutInflator.from method looks like so:
/**
* Obtains the LayoutInflater from the given context.
*/
public static LayoutInflater from(Context context) {
LayoutInflater LayoutInflater =
(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (LayoutInflater == null) {
throw new AssertionError("LayoutInflater not found.");
}
return LayoutInflater;
}
and there is no difference
As long as the Activity or Window that calls getLayoutInflater() has the same Context that would call getSystemService(), there is no difference.
There is not much of a difference between them.
As doc says public abstract Object getSystemService (String name)
A LayoutInflater for inflating layout resources in this context.
And for the public static LayoutInflater from (Context context)
Obtains the LayoutInflater from the given context.
You can check this thread Is there any difference between getLayoutInflater() and .getSystemService(Context.LAYOUT_INFLATER_SERVICE)
The only difference is the context that you use. If the context that you use with LayoutInflater.fromContext() or context.getSystemService(...) is actually an Activity, it should be equivalent to Activity.getLayoutInflater(). If it's the application object, you might have problems inflating views that contain fragments, IIRC.
Actually I think that the getLayoutInflater() - Method of Activity is a convenience - method.
Remember that Activity subclasses Context, so all of the Methods available within Context are also available in the Activity Class.
Internally there will be a call to LayoutInflater.fromContext() or context.getSystemService(), so I would stick to context.getSystemService both to avoid the unnecessary method call as well to clarify that I am making a call to a system service.

how to access to widgets of a View in Android?

I'm making a View by inflating an xml layout:
LayoutInflater inflater = (LayoutInflater) getBaseContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(com.example.R.layout.aaa, null);
in aaa.xml I have a RelativeLayout that itself includes a TextView and a Button. now how can I access to that TextView on view object?
First of all the TextView you are trying to reach must have an id (e.g. #android:id="#+id/yourTxtId"), and then from your rootView, in your case the View you are actually inflating all you have to do is:
View view=inflater.inflate(com.example.R.layout.aaa, null);
TextView txt = (TextView)view.findViewById(R.id.yourTxtId);
//And here you have the reference...
Regards!

access to another XML file

I want to have an access to another XML file in an inner class but I can't get a reference to the other XML components, this an inner class code:
class ItemsClass extends ArrayAdapter<String>
{
public ItemsClass ()
{
super(ListActivity.this, R.layout.itemslist);
}
public View getView (final int position, View convertview, ViewGroup parent)
{
//setContentView(R.layout.itemslist);
final String s = this.getItem(position);
LayoutInflater inflater= getLayoutInflater();
View row= inflater.inflate(R.layout.itemslist, parent, false);
// get ref to each component in itemList.xml
TextView itemName= (TextView) row.findViewById(R.id.textView1);// here i can't access to the TextView in itemslist.xml
}
}
View row= inflator.inflate(R.layout.itemslist, null);
read this
http://www.vogella.de/articles/AndroidListView/article.html
I am not sure why you are trying to using the same xml for both the main activity & the row component.I think you need to reiterate your code,check about the neccesity of the usage of the constructor here.Cheers.

Categories