Hi i'm trying to add a relative view to a merge adapter but it's currently scrolling separetly to the lists so does anyone know how i can add a relative layout with an image view and a text view to a merge adapter?
my aim is to have it look like this
header(relativelyout red bar and title);
List
header(relativelyout red bar and title);
List
header(relativelyout red bar and title);
List
and to have this all scroll as if it was all one list
heres my attempt so far
arrayAdapter = new ArrayAdapter<String>(this, R.layout.single_item, newsList);
arrayAdapter2 = new ArrayAdapter<String>(this, R.layout.single_item, newsList2);
arrayAdapter3 = new ArrayAdapter<String>(this, R.layout.single_item, newsList3);
ListView list = getListView();
list.setTextFilterEnabled(true);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE );
View header = inflater.inflate( R.layout.redcell, list, false);
setListAdapter (arrayAdapter);
adapter = new MergeAdapter();
adapter.addView(header);
adapter.addAdapter(arrayAdapter);
adapter.addView(header);
adapter.addAdapter(arrayAdapter2);
adapter.addView(header);
adapter.addAdapter(arrayAdapter3);
setListAdapter(adapter);
}
redcell.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="wrap_content"
android:orientation="vertical"
android:id="#+id/redcelllayout" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="30dp"
android:src="#drawable/titlerect" />
<TextView
android:id="#+id/redheadertext"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:paddingLeft="15dp"
android:paddingTop="3dp"
android:textColor="#color/white"
android:textSize="15dp"
android:textStyle="bold"/>
</RelativeLayout>
Instead of creating them in the program, create an xml file for that view, inflate it, then add it to the adapter.
Like this:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View header1 = inflater.inflate(R.layout.redbar_title);
View header2 = inflater.inflate(R.layout.redbar_title);
View header3 = inflater.inflate(R.layout.redbar_title);
adapter = new MergeAdapter();
adapter.addView(header1);
adapter.addAdapter(arrayAdapter);
adapter.addView(header2);
adapter.addAdapter(arrayAdapter2);
adapter.addView(header3);
adapter.addAdapter(arrayAdapter3);
setListAdapter(adapter);
Set each list layout_height to wrap_content, and wrap everything within a ScrollView.
Perhaps it would be easier if you could make your headers part of the adapter itself, as separators, and use a single list instead of 3.
Related
I am trying this cool library that uses Ken Burn effect here (his github page)
I got it to work but I cant add different Icons on different rows. I tried to use Mkyongs CustomAdaptor example but what he used is a layout with TextView and ImageView. What I am using is ListView. I am already able to change the text font and size of the ListView but lost trying to add icons. How do you do this?
Below is my Layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/blur4"
tools:context="com.gio.hia.hiarewired.NoBoringActionBarActivity">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" />
<FrameLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="#dimen/header_height">
<com.gio.hia.hiarewired.KenBurnsView
android:id="#+id/header_picture"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/picture0" />
<ImageView
android:id="#+id/header_logo"
android:layout_width="#dimen/header_logo_size"
android:layout_height="#dimen/header_logo_size"
android:layout_gravity="center"
android:src="#drawable/ic_header_logo" />
</FrameLayout>
</FrameLayout>
This is my Activity Class
private void setupListView() {
ArrayList<String> FAKES = new ArrayList<String>();
//for (int i = 0; i < 1000; i++) {
FAKES.add("Row 1");
FAKES.add("Row 2");
FAKES.add("Row 3");
FAKES.add("Row 4");
//}
mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
mListView.addHeaderView(mPlaceHolderView);
//mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FAKES));
mListView.setAdapter(new ArrayAdapter<String>(this, R.layout.list_text_config, FAKES));
mListView.setOnScrollListener(new AbsListView.OnScrollListener() { ...
I used a custom R.layout.list_text_config to change the font
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:paddingTop="10dip"
android:paddingBottom="10dip"
android:paddingLeft="15dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="35dp"
android:textColor="#color/colorWhite"/>
In his example he used a layout inflater How do I use my Custom Adapter with a layout inflator or am I looking at the problem on the wrong direction?
I usually just do this
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
but how do I do that when he used a layout inflater like below
mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
mListView.addHeaderView(mPlaceHolderView);
mListView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, FAKES));
Like #Arslan said this can be done with a custom adapter. You are actually doing it correct
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
list.setAdapter(adapter);
That actually creates the list with the icons. What you need to add to that is the header image to the mPlaceHolderView but instead of using mListView use the list that you created with your "custom adapter". I placed the modified line of code from the original line of code so you can easily see the difference.
CustomListAdapter adapter=new CustomListAdapter(this, itemname, imgid);
list=(ListView)findViewById(R.id.list);
mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, list, false);
//mPlaceHolderView = getLayoutInflater().inflate(R.layout.view_header_placeholder, mListView, false);
list.setAdapter(adapter);
Now and since your no longer using the mListView you should probably lookout for instances of mListView and replace it with your list
I'm trying to implement a code in which a list of linear layouts are inserted in the list view by using the array adapter. Each linear layout contains two subviews. Here's the xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/list_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"/>
<TextView
android:id="#+id/list_label"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:textSize="16sp" />
</LinearLayout>
Now in the fragment I created a code snippet like this.
final String[] my_page_items = getResources().getStringArray(R.array.my_page_items);
int[] my_page_icons = {
R.mipmap.ic_about,
R.mipmap.ic_app_settings,
R.mipmap.ic_account_settings,
R.mipmap.ic_logout
};
LinearLayout[] items = new LinearLayout[my_page_items.length];
TextView[] labels = new TextView[my_page_items.length];
ImageView[] icons = new ImageView[my_page_icons.length];
for(int i=0; i<my_page_icons.length; i++) {
items[i] = (LinearLayout) LayoutInflater.from(getActivity().getApplication()).inflate(R.layout.list_item, null);
labels[i] = (TextView) items[i].findViewById(R.id.list_label);
icons[i] = (ImageView) items[i].findViewById(R.id.list_icon);
labels[i].setText(my_page_items[i]);
icons[i].setBackground(getResources().getDrawable(my_page_icons[i]));
}
setListAdapter(new ArrayAdapter<>(getActivity(), R.layout.list_item, items));
lv = getListView();
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
});
When I run this code, I get the the following in the logcat.
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
How should this problem be solved?
In
new ArrayAdapter<>(getActivity(), R.layout.list_item, items)
R.Layout.list_item must be the id of a xml layout file containing only a TextView(the TextView can't be wrapped by another layout, like a LinearLayout, RelativeLayout etc!),
EDIT possible solution :
If you want your list row layout to be something a little different then a simple TextView widget use this constructor:
new ArrayAdapter<>(getActivity(), R.layout.list_item, R.id.id_of_your_textview items)
where you supply the id of a layout that can contain various views, but also must contain a TextView with and id(the third parameter) that you pass to your ArrayAdapter so it can know where to put the Strings in the row layout.
I am trying to get multiple items checked in a list view I have in my dialog box.
This is the Java code:
String names[] ={"A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P"};
AlertDialog.Builder alertDialog = new AlertDialog.Builder(activity);
LayoutInflater inflater = activity.getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.test, null);
alertDialog.setView(convertView);
alertDialog.setTitle("List");
ListView lv = (ListView) convertView.findViewById(R.id.my_list);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
lv.setItemsCanFocus(false);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity, android.R.layout.simple_list_item_1, names);
lv.setAdapter(adapter);
alertDialog.show();
It shows the list in my dialog box okay, but when I click/press an item nothing happens. I don't see any tick or anything visually to indicate it's been selected/checked.
Here's my XML code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ListView
android:id="#+id/my_list"
android:layout_height="match_parent"
android:layout_width="match_parent" />
</LinearLayout>
What am I doing wrong?
You need to change :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
android.R.layout.simple_list_item_1, names);
To :
ArrayAdapter<String> adapter = new ArrayAdapter<String>(activity,
android.R.layout.simple_list_item_mutiple_choice, names);
Im very new in android programming, and my task is to load up the items in my shared preference to a listview, and i just want to change the design of my listview, like changing its font color, font size and etc.. I have tried this solution but it doesn't work, please tell me where I did wrong. Thanks..
heres my code in loading the listview in a listactivity
Map<String,?> datakeys = datapref.getAll();
for(Map.Entry<String,?> entry : datakeys.entrySet()){
Log.d("values123",entry.getKey() + ": " + entry.getValue().toString());
lvlist.add(entry.getValue().toString());}
lvadapter = new ArrayAdapter<String>(this, R.layout.mytextview , R.id.text1, lvlist);
//lvadapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,android.R.id.text1, lvlist);
setListAdapter(lvadapter);
heres the mytextview xml..
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10dip"
android:textColor="#666666"
android:textStyle="italic" />
and my listview xml
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView3"
android:background="#b6fcd5" >
</ListView>
here's the screenshots
You need other constructor of ArrayAdapter :
public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)
so Change this line to :
lvadapter = new ArrayAdapter<String>(this, R.layout.mytextview, lvlist);
To :
lvadapter = new ArrayAdapter<String>(this, R.layout.mytextview , R.id.text1, lvlist);
And mytextview xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10dip"
android:textColor="#666666"
android:textStyle="italic" />
If this doesn't work , then Go with Custom Adapter
one more approach: have a look
String [] yourStringarrayorlist= {"Lionssss","Cats", "yeaaahhh :)"};
ListView listView =new ListView(
getApplicationContext());
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, yourStringarrayorlist) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
// if you want to change any specific items color
int textColor = textView.getText().toString().equals("Cats") ? R.color.any : android.R.color.black;
textView.setTextColor(VideoviewActivity.this.getResources().getColor(textColor));
//If you want to change the text color of all use below code
// textView.setTextColor(VideoviewActivity.this.getResources().getColor( R.color.any));
return textView;
}
});
layoutrela.addView(listView);
You can change the textcolor easily. However for just a check i've created ListView through code and added to the layout :) but it works like awesome..
Inspired by : How to change text color of simple list item
not a new answer so i would not expect any vote or so.
I understand how to do this in design view, but I'm not sure how to create a style based on programmatically created elements. I followed a tutorial and here's where I'm at:
I have a grid view which is populated by a string array as in the code below:
...
gridView = (GridView) findViewById(R.id.gridView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strData);
gridView.setAdapter(adapter);
Each element of the string array populates the grid just fine.
How would I set the text color of the items added to the gridView?
2 solutions: dynamically or with a custom layout.
Dynamically: you can set a text color by using setTextColor(...) when you Override the getView() method in your ArrayAdapter, something like this:
gridview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strData) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTextColor(getResources().getColor(R.color.my_color));
return view;
}
});
Custom Layout: this is the simplest way, build a custom layout as:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tv"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:textColor="#color/my_color"
android:background="#drawable/my_background"
android:padding="5sp"
android:singleLine="true"
android:gravity="center" />
Then, set it to your adapter:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_layout_above, strData);
Even if, you wanted this dynamically, I'm not sure but I think it's better to do with a custom layout.
Hope this helps.
By creating a custom layout and supplying it to your ArrayAdapter. Currently you are supplying the default layout android.R.layout.simple_list_item_1. Create a new layout xml like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
And style it however you want.
You can use it like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_layout, R.id.tvText, strData);