view from layout - java

i came across this
http://www.brighthub.com/mobile/google-android/articles/48845.aspx
i understand i can create a view. But i want to define a layout xml and put that into my view. for example in layout i will have a few textviews, i will set value to them dynamically and add them to view and display. is this possible if yes how ? if no whats the alternative ?

Look at LayoutInflater
Sample:
LayoutInflater layoutInflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout dialerLayout = (LinearLayout) layoutInflater.inflate(R.layout.phone_dialer, null);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
dialerLayout.setLayoutParams(params);
LinearLayout tabDialer = (LinearLayout) findViewById(R.id.tabDialer);
tabDialer.addView(dialerLayout);

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

Showing two RelativeLayouts in a LinearLayout not working

I'm trying to display two RelativeLayouts in a LinearLayout, however the best result I'm getting is getting one of the two displayed.
final LinearLayout layoutMain = new LinearLayout(this);
layoutMain.setOrientation(LinearLayout.VERTICAL);
setContentView(layoutMain);
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout layoutAll = (RelativeLayout) inflate.inflate(
R.layout.activity_main, null);
final RelativeLayout menuLayout = (RelativeLayout)inflate.inflate(
R.layout.bottom_menu, null);
menuLayout.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
layoutMain.addView(layoutAll);
layoutMain.addView(menuLayout);
layoutAll is showing, and if I change the order of the addView lines, then menuLayout is showing. So far I've tried different layout parameters (MATCH_PARENT for layoutAll or menuLayout and even both). Any suggestion ? Thanks.

Create ImageView and TextView in HorizontalScrollView

I want to create a TextView over an ImageView in my Android-App. I know I need a RelativeLayout for this, but I don't know how to create this in my main.java (NOT in XML).
It should be dynamic, so I want to create a for-loop which creates many ImageViews with a TextView over it in a HorizontalScrollView.
This is my approach
LinearLayout sv = (LinearLayout) findViewById (R.id.LinearLayout1);
for (int i=1 ; i<=3; i++){
String uri = "drawable/test"; //only one picture several times
int imageResource = getResources().getIdentifier(uri, null, getPackageName());
ImageView iv = new ImageView (this);
iv.setBackgroundResource (imageResource);
sv.addView(iv);
}
This only add ImageViews to my HorizontalScrollView (LinearLayout is located in the HorizontalScrollView), but now I also want to add TextViews over my ImageViews. I tried lot of things but nothing works.. I despair.
Hopefully someone can help me
PS: Sorry for my spelling mistakes, I'm from Germany
If you know it to design in xml, create a XML and inflate it.
Below is the code that explains, How to use it.
Code Snippet
//Parent View
LinearLayout sv = (LinearLayout) findViewById (R.id.LinearLayout1);
//infalter service
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//Loop to create all the child views
for (int i=0; i<=3; i++) {
//Refrence your layout xml here
LinearLayout childView= inflater. inflates (R. layout. your_xml, null);
//Get refrence to your button and imageview using childView
//add child view
sv.addView(childView);
}
//add sv to horizontal scroll view
Dude try custom views. Create one XML file and create one basic layout . And then inflate that view as many time as u want don't do with for loop. I thnk u r new in android.
http://androidexample.com/Dynamically_Create_View_Elements__-_Android_Example/index.php?view=article_discription&aid=115&aaid=137

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!

Can't click to custom item in android-uitableview

I am using android-uitableview
And I have this problem:
When I use this code:
LayoutInflater mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout view = (RelativeLayout) mInflater.inflate(R.layout.custom_view, null);
ViewItem viewItem = new ViewItem(view);
getUITableView().addViewItem(viewItem);
When I click this button, I can't go to onClick(int index) <= the problem
But when I use getUITableView().addBasicItem , I can handle the click.
How can I handle when I click the custom view ?
Regards
I think they have not implement onClick for addViewItem but they do for addBasicItem.
Source Code
You can call getUITableView().setClickListener(...)

Categories