I have 2 textviews and one imageview and i want to bind them into a single control so that i can implement horizontalScrollView on them...
Is there a way to merge different controls so that we can use them???
Or is there a way to implement horizontalScrollView on multiple controls simulataneously??
Thanks in advance
You can build an xml layout that you inflate into a GaleryView. Android allows you to build any mashup of controls into a layout and then use that layout for each row in a listview or each item in a sliding galery.
in a ListAdapter you can inflate the layout and set all of the properties.
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater factory = LayoutInflater.from(context);
View row = factory.inflate(R.layout.list_row, null);
TextView main = (TextView)row.findViewById(R.id.labelMain);
TextView details = (TextView)row.findViewById(R.id.labelDetails);
ImageView icon = (ImageView)row.findViewById(R.id.imgIcon);
main.setText(_items.get(position).Title);
details.setText(_items.get(position).Description);
icon.setImageResource(R.drawable.pin);
return row;
}
Related
I'm using one from the built-in list item layout in my app.
I want to change the style of this list like text color or size of text .
I'm using simple_list_item_1 from these items .
Can you help me how I can do that.
You should make a custom Layout and inflate it in getView() method of Adapter.
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View row;
row = inflater.inflate(R.layout.custom, parent, false);
TextView title, detail;
ImageView i1;
title = (TextView) row.findViewById(R.id.title);
detail = (TextView) row.findViewById(R.id.detail);
i1=(ImageView)row.findViewById(R.id.img);
title.setText(Title[position]);
detail.setText(Detail[position]);
i1.setImageResource(imge[position]);
return (row);
}
you can look into this thing more on Custom Listview
and from my personal opinion you can use RecyclerView for better Customisation.
Do you mean in the Android Studio preview?
If so, tools:listitem="#layout/list-item-layout" is your friend.
Create your own layout and place it in your app layout directory and refer that in your code instead of simple_list_item_1
Example here
R.layout.<yourLayoutName>
instead of
android.R.layout.simple_list_item_1
And customize the layout as you like it!!
the problems is, that I wan't to make ListView with elements which are containing image, description and two buttons. I'm making them in my own BaseAdapter extension, but fragment which is containing ListView is closing (wihtout errors in logcat..). I've found, that ListView is working well, when I'm not returning layout-type elements. So there is my sample with 'sample linear layout', which is not working.. Is there any possibility, to show layouts in ListView?
Here is my code:
Creating part:
lv = (ListView) getView().findViewById(R.id.main_wall_ambajes_lv);
AmbajAdapter aa = new AmbajAdapter(getActivity().getApplicationContext(), StaticData.ambajes);
lv.setAdapter(aa);
My getView method from adapter:
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout ll = new LinearLayout(getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
ImageView iv = new ImageView(getActivity());
iv.setImageBitmap(placeholderBitmap);
ll.addView(iv);
ll.addView(iv);
ll.addView(iv);
ll.addView(iv);
return ll;
}
I don't know why you don't have any error however I don't think you proceed the correct way.
Usually you create the layout in the xml file of the layout folder and only inflate it in the getView(), for example as follow :
private LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
#Override
public View getView(int position, View view, ViewGroup parent) {
if (view == null) {
view = mInflater.inflate(R.layout.your_custom_layout, parent, false);
}
//your code for setting the image or other things goes here
//for example if you have a textView
TextView textView = (TextView) view.findViewById(R.id.my_textview_id);
textView.setText("my custom text for this cell");
return (view);
}
and your_custom_layout is simply the xml file of your layout.
Note that for performance reason due to cell recycling I only inflate the view when it is null and I only read once the LayoutInflater context and put it in mInflater. However for the best performance you should use a ViewHolder, but it is out of the scope of your question.
I created the Layout design using java code only not from the XML Layout Designs. The code I used is following
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv = new TextView(mContext);
tv.setText(hotelList.get(position).name);
return tv;
}
How to use layoutInflator for creating layout fro this. I need 2 more textviews in a single list item. the whole list contains 10 different list items
Please provide some codes for this. Help appreciated
I have gone through this before by having my static class too. Check this out, it will help:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View rowView = convertView;
if ( rowView == null) {
LayoutInflater inflator = this._activity.getLayoutInflater();
rowView = inflator.inflate(R.layout.todolistlisting, null);
TodoListViewHolder viewHolder = new TodoListViewHolder();
viewHolder._name = (TextView) rowView.findViewById(R.id.tVTLName);
viewHolder._completed = (TextView) rowView.findViewById(R.id.tVTLCCount);
viewHolder._remaining = (TextView) rowView.findViewById(R.id.tVTLRCount);
rowView.setTag(viewHolder);
}
TodoListViewHolder holder = (TodoListViewHolder) rowView.getTag();
VO_TodoList votodolist = this._items.get(position);
holder._name.setText(votodolist._title);
holder._completed.setText(votodolist._completed);
holder._remaining.setText(votodolist._remaining);
return rowView;
}
TodoListViewHolder is my view component holder here. like your TextView.
I guess you know how to make XML layout for this layout. So just make the XML layout and get the object of the main layout using the following code:
LinearLayout mainLayout=(LinearLayout) View.inflate(R.layout.yourlayout); //if yourlayout.xml is the name of the xml file you made and put in the layout folder.
To get the child of the layout, let's say if it's a TextView with the id text, then the code would be:
TextView textView=(TextView)mainLayout.findViewById(R.id.text);
You can add view at runtime by using inflater like this
LinerLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.news_categories_item, null);
TextView categoryValueTextView = (TextView)linearLayout.findViewById(R.id.news_category_item_value);
mMainLinearLayout.addView(categoryValueTextView);
Here i am inflating one text view which is there in another linear layout(this is simple linear layout which holds only textview) at runtime and adding it to my main linear layout.
you can get the inflater object in your acitivity by using getLayoutInflater(). And if you want to get inflater in adapter you have to pass inflater object to constructor of adapter from your activity.
I don't know if it is possible, but actually I wouldn't see why not.
Can we do a grid view not just with ImageView but with a custom view.
I am trying to make a grid view of a view composed of an ImageView and a TextView.
I know that everything happens in my Adapter getView's function but I can't figure out how to do it.
public View getView(int position, View convertView, ViewGroup parent) {
View cases = findViewById(R.id.fileUnitLayout);
if (convertView == null) {
convertView = new View(mContext);
} else {
cases = convertView;
}
return cases;
}
My view has an id of R.id.fileUnitLayout. Let's say my inner TextView has an id of A and my inner ImageView has an id of B. How can I fill them ?
Thank you,
You should not need to override getView to accomplish this, necessarily. GridView is an AdapterView so you can provide an adapter that will display what you want via setAdapter
You could, for example, use SimpleAdapter to provide an xml file that is used for each grid view item.
I have a ListView powered by a custom adapter that dynamically changes the background color of each row based on its contents. The getView looks like this:
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View row = convertView;
Power power = powers.get(position);
if(row == null)
{
row = getLayoutInflater().inflate(android.R.layout.simple_list_item_2, parent, false);
}
TextView text1 = (TextView)row.findViewById(android.R.id.text1);
TextView text2 = (TextView)row.findViewById(android.R.id.text2);
text1.setText(power.name);
text2.setText(power.getAttackLine());
row.setBackgroundColor(0xFF0000FF);
return row;
}
All that works peachy, except that whenever I select an item from the list, the standard red/orange highlight only shows up behind the row, only visible for a few pixels on each side of it. Is there a way to get the selection to show up on top of the background?
Try android:drawSelectorOnTop="true" in the <ListView> element in your layout.
I think the problem is that you're using simple_list_item_2, which has some padding/margin built into it. I recommend making your own version of simple_list_item_2, wherein you have both the height and the width set to fill_parent. You can see the latest simple_list_item_2 here.