I'm creating a spinner programmatically in my activity class. The "Spinner" looks like a dropdown rather than a spinner. I guess I want it to look more like a picker (ie date picker / time picker / number picker) where you can spin through all the text options.
I would use a picker type widget if there was a text picker available but I can't seem to find such a widget - only a number picker. Here is the code I'm using in my activity.
ArrayAdapter <String>lv1List = new ArrayAdapter<String>(this.getApplicationContext(),android.R.layout.simple_spinner_item, new String[]{"item 1","item 2","item 3"});
Spinner sp = new Spinner(getApplicationContext());
sp.setAdapter(lv1List);
sp.setOnItemSelectedListener(this);
Take a look at Android Wheel. You can have text or pretty much anything on it. Works perfectly.
Quick ( and inefficient) example if you want to create your own spinner. (Works with List View Adapters):
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<FrameLayout android:id="#+id/container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_gravity="center_vertical"
/>
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="vertical">
<Button android:id="#+id/btn_up"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="up"/>
<Button android:id="#+id/btn_down"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="down"/>
</LinearLayout>
</LinearLayout>
CustomSpinner Class:
public class CustomSpinner extends FrameLayout{
// ------------------------------ FIELDS ------------------------------
private FrameLayout mContainer;
private SpinnerAdapter mAdapter;
private int index = 0;
// --------------------------- CONSTRUCTORS ---------------------------
public CustomSpinner(Context context) {
super(context);
init(context);
}
public CustomSpinner(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
private void init(Context context){
this.addView(LayoutInflater.from(context).inflate(R.layout.custom_spinner,this,false));
findViewById(R.id.btn_up).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
index--;
refresh();
}
});
findViewById(R.id.btn_down).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
index++;
refresh();
}
});
mContainer = (FrameLayout) findViewById(R.id.container);
}
// -------------------------- OTHER METHODS --------------------------
public void setAdapter(SpinnerAdapter adapter) {
this.mAdapter = adapter;
refresh();
}
private void refresh() {
//----needs recycling for better performance---
//---now, we'll just clear up--
mContainer.removeAllViews();
//---do we need to refresh ? -----
if(mAdapter == null || mAdapter.getCount() == 0){return;}
//--clamp index--
index = Math.max(0,index);
index = Math.min(mAdapter.getCount() - 1, index);
//--get view and show it-----
View currentView = mAdapter.getView(index,null,mContainer);
mContainer.addView(currentView);
}
}
Use Case:
Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<com.example.CustomSpinner android:id="#+id/custom_spinner"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
/>
</LinearLayout>
Activity:
public class MyActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,
new String[]{"Monday","Tuesday","Wednesday","Thursday","Friday","Saturday","Sunday"});
((CustomSpinner)findViewById(R.id.custom_spinner)).setAdapter(adapter);
}
}
Related
Main activity contains a spinner and a button. MainActivity.java code is as follows.
public class MainActivity extends AppCompatActivity implements LoadNew.VCallback {
public Spinner dropdown;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dropdown = (Spinner) findViewById(R.id.spinner1);
//Create a list of items for the spinner.
String[] items = new String[]{"select topic", "topic1", "topic2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// TODO Auto-generated method stub
String sp1 = String.valueOf(dropdown.getSelectedItem());
if (sp1.contentEquals("select topic")) {
loadFun("");
}
if (sp1.contentEquals("topic1")) {
loadFun("topic1");
}
if (sp1.contentEquals("topic2")) {
loadFun("topic2");
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView){
// TODO Auto-generated method stub
}
});
Button x = (Button) findViewById(R.id.full_screen);
x.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loadFun("topic2", 0);
}
});
}
public void loadFun(String topicName, int i) {
LoadNew st = new LoadNew(this);
st.display(topicName, i);
}
}
The xml layout for main activity is as follows (activity_main.xml).
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/full_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Screen"
style="?android:attr/borderlessButtonStyle"/>
</LinearLayout>
<LinearLayout
android:id="#+id/full_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
There is another java class LoadNew.java as follows.
public class LoadNew {
public interface VCallback {
//To access methods of MainActivity class
}
public Activity activity;
public LoadNew(Activity _activity) {
this.activity = _activity;
VCallback callerActivity = (VCallback) activity;
}
//Display PDF
public void display(String topicName, int i) {
LinearLayout fullScreen = (LinearLayout) this.activity.findViewById(R.id.full_view);
fullScreen.removeAllViews();//Clear field
LayoutInflater inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_load, null);
if(i ==0) {
this.activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.activity.setContentView(view);
}
TextView tv= (TextView) this.activity.findViewById(R.id.tv1);
tv.setText(topicName);
tv.setTextSize(100);
}
}
The activity_load.xml file is as follows.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout> xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/tv1"
android:layout_below="#+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
When an item is selected in the spinner, the corresponding text is displayed in the textview. Upon pressing the button, the textview appears in full screen mode.
Now I want to return to the previous view (by pressing back button or by single tab on screen), i.e., the view before the button was clicked. How can I achieve this? Thanks in advance for the patience and the help.
Simplest would be to have the spinner and the non-fullscreen TextView on one activity and open a second activity with the fullscreen mode on button click.
GridView is scrolled programmatically, and no new item appears coming up from the bottom.
I tried to update with the following line, but it does not force GridView to load new items.
imageAdapter.notifyDataSetChanged();
gridview.invalidateViews();
gridview.setAdapter(imageAdapter);
Simplified the app, now the scrolling can be fired with button click, but the upcoming empty items are still appearing. Here is some code:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final CustomGridView gridview = (CustomGridView) findViewById(R.id.gridView1);
final ImageAdapter imageAdapter = new ImageAdapter(this);
gridview.setAdapter(imageAdapter);
gridview.setNumColumns(3);
LinearLayout.LayoutParams linearParams = (LinearLayout.LayoutParams)gridview.getLayoutParams();
linearParams.width=66*3;
gridview.setLayoutParams(linearParams);
final Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
gridview.scrollBy(0, 44);
}
});
}
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 300;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(66, 66));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0); // 8 8 8 8
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(R.drawable.asdf);
return imageView;
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.j4nos.moviebuffs12.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<HorizontalScrollView
android:id="#+id/horizontalScrollView"
android:layout_width="198dp"
android:layout_height="match_parent"
android:layout_marginTop="66dp"
android:layout_marginLeft="0dp"
android:layout_marginBottom="0dp">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.j4nos.moviebuffs12.CustomGridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="0dp"
android:columnWidth="66dp"
android:horizontalSpacing="0dp"
android:scrollbarAlwaysDrawHorizontalTrack="true"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbars="horizontal"
android:stretchMode="none"
android:verticalSpacing="0dp"
android:listSelector="#null"
android:scaleType="centerCrop">
</com.j4nos.moviebuffs12.CustomGridView>
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
public class CustomGridView extends GridView {
public CustomGridView(Context context) {
super(context);
}
public CustomGridView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomGridView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
/* ADD THIS */
#Override
public int computeVerticalScrollOffset() {
return super.computeVerticalScrollOffset();
}
}
I think the problem is with the way you are scrolling:
gridview.scrollBy(0, 44);
This moves the gridview, not the content of the gridview. You can verify this by adding the following into your layout inside the CustomGridView component:
android:fastScrollAlwaysVisible="true"
Now you can see it's not the gridview content that is being interacted with on the button press.
Instead, I suggest you try to use:
gridview.scrollListBy(44);
if your API level allows it.
I using custom adapter for displaying a list of items in my project,
Right now it is working fine.but now i got another requirement that i need to put a button on each item.
If put the button like that then the onItemClickListenter() for that item is not working ,instead onClickListener() for that button is working.
But according to my context both onClickListenter() for button and onItemClickListener() for that item should work.Can somebody please help me if know the technique.
Thanks in advance.
As Button is a focusable view thats why onItemClick isn't work. In ur_row.xml (where you placed that Button) add these attributes inside Button tag
android:focusable="false"
android:focusableInTouchMode="false"
Inside your CustomAdapter's getView(...) set onClickListener for that Button this will fire both onItemClick for ListView and onClick for Button.
You need a custom adapter:
This project contains 4 parts:
1.MainActivity
public class MainActivity extends Activity {
private ListView listView;
private CustomAdapter customAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity);
init();
}
/**
* Init Data
*/
public void init(){
List<String> listItems = new ArrayList<String>();
for(int i=0;i<10;i++) {
listItems.add("Test" + i);
}
listView = (ListView)findViewById(R.id.listView);
customAdapter = new CustomAdapter(MainActivity.this,R.layout.row_item,listItems);
listView.setAdapter(customAdapter);
}
}
2.MainActivity layout
<RelativeLayout 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"
tools:context=".main_activity">
<ListView
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
3.CustomAdapter
public class CustomAdapter extends ArrayAdapter<String> {
private Context context;
private TextView itemListText;
private Button itemButton;
private List<String> listValues;
public CustomAdapter(Context context, int resource, List<String> listValues) {
super(context, resource,listValues);
this.context = context;
this.listValues = listValues;
}
/**
* getView method is called for each item of ListView
*/
#Override
public View getView(int position, View convertView, ViewGroup parent) {
String currentValue = listValues.get(position);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.row_item, null);
itemListText = (TextView)convertView.findViewById(R.id.itemListText);
itemListText.setText(currentValue);
itemListText.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,"Text Working",Toast.LENGTH_SHORT).show();
}
});
itemButton = (Button)convertView.findViewById(R.id.itemButton);
//To lazy to implement interface
itemButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context,"Button Working",Toast.LENGTH_SHORT).show();
}
});
return convertView;
}
}
4.Row Item layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/itemListText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Select Text"
android:gravity="center_vertical"
android:layout_weight="0.7"/>
<Button
android:id="#+id/itemButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Push me"
/>
</LinearLayout>
Output:
TextView touch action
Button touch action
rather than putting buttons on row use image views as many required and set on click listener to them, Since imageViews are not focusable
I tried to build my first custom gui element but I get problems when I try to change appearance or adapter (using Gallery) later in code.
My Problem: I can't change Custom Gallery properties
My actual Code:
First I create an XML which is the widget customGallery.xml
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageButton android:id="#+id/toLeft"
android:background="#drawable/arrow_left"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_marginBottom="1dip" />
<Gallery
android:id="#+id/gallery"
android:layout_width="fill_parent"
android:layout_height="40dip"
android:layout_toRightOf="#+id/toLeft"
android:spacing="40dip"
android:scrollbars="horizontal"/>
<ImageButton android:id="#+id/toRight"
android:background="#drawable/arrow_right"
android:layout_width="wrap_content"
android:layout_height="40dip"
android:layout_toRightOf="#+id/gallery" />
</merge>
Later i create a test.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lin_layout"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.Test.GallerySlider
android:id="#+id/choose"
android:layout_span="2"
android:layout_width="300dip"
android:layout_height="wrap_content" />
</LinearLayout>
My next Step was to include this custom Widget into my project and change the adapter from my Widget:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
LinearLayout lineLayout = (LinearLayout) findViewById(R.id.lin_layout);
ViewStub st3 = new ViewStub(TestwidgetActivity.this);
LinearLayout.LayoutParams paramst3 = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lineLayout.addView(st3,paramst3);
st3.setLayoutResource(R.layout.test);
View st3InflaView =st3.inflate();
GallerySlider gSlider= (GallerySlider) st3InflaView.findViewById(R.id.choose);
gSlider.setNewAdapter( new ArrayAdapter<String>(this, android.R.layout.customGallery, new String[] {"1 ","2","3","4"}));
}
This is the Widgetclass I wrote:
public class GallerySlider extends RelativeLayout implements OnClickListener {
private ArrayAdapter<String> adapter;
private Gallery gallery;
private ImageButton toLeftBtn = null;
private ImageButton toRightbtn = null;
public GallerySlider(Context context) {
super(context, null);
init(context);
}
public GallerySlider(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public GallerySlider(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
init(context);
}
public void init(Context ctx){
LayoutInflater inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE) ;
inflater.inflate(R.layout.customGallery, this, true);
toLeftBtn = (ImageButton) findViewById(R.id.toLeft);
toLeftBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(gallery.getSelectedItemPosition() > 0){
gallery.setSelection(gallery.getSelectedItemPosition()-1);
}
}
});
toRightbtn = (ImageButton) findViewById(R.id.toRight);
toRightbtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(gallery.getSelectedItemPosition() < gallery.getAdapter().getCount()-1){
gallery.setSelection(gallery.getSelectedItemPosition()+1);
}
}
});
adapter = new ArrayAdapter<String>(ctx, android.R.layout.simple_gallery_item, new String[] {"1","1",
"1")});
gallery = (Gallery) findViewById(R.id.gallery);
gallery.setBackgroundResource(R.drawable.h_sliderl);
gallery.setAdapter(adapter);
}
#Override
public void onClick(DialogInterface dialog, int which) {
switch(which){
case R.id.toLeft: gallery.setSelection(gallery.getFocusedChild().getId()-1);
break;
case R.id.toRight: gallery.setSelection(gallery.getFocusedChild().getId()+1);
break;
}
}
public void setNewAdapter(ArrayAdapter<String> _adapter){
gallery.setAdapter(_adapter);
((ArrayAdapter) gallery.getAdapter()).notifyDataSetChanged ();
}
}
If I call setNewAdapter(ArrayAdapter _adapter) nothing change.. . I also tried to change the size of the gallery but it also fails(nothig happen). Is my approach false?
greetings marcel
The first thing I can detect is that you are creating your custom view two times.
The first creation occurs with the ViewStub when you set the layout.
And the second one, which doesn't get added to the contentView when you inflate R.layout.test.
You are setting the adapter to the second custom view which it isn't added to the view hierarchy.
im having an issue when i try to make a linearlayout with multiple listviews. originally when i added multiple custom linearlayouts containing a textview and a listview i could not scroll down the page so i found out adding a scrollview would fix this, which it did but for some reason it then cropped the linearlayout view so i could only see the top part of each list.
heres the code for the different parts:
Main layout
<?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"
android:weightSum="1">
<TextView
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/whatsNewTitle"
android:text="Whats New?"
android:layout_width="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="30dip">
</TextView>
<Button android:layout_height="wrap_content"
android:id="#+id/home"
android:text="Home"
android:layout_weight="0.07"
android:layout_width="126dp">
</Button>
<ScrollView
android:id="#+id/sv1"
android:layout_width="fill_parent"
android:fillViewport="true"
android:layout_height="630dp">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:id="#+id/whats_new_content"
android:layout_height="fill_parent">
</LinearLayout>
</ScrollView>
</LinearLayout>
layout im inflating and adding multiple times into main layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="1">
<TextView
android:text="TextView"
android:textAppearance="?android:attr/textAppearanceMedium"
android:id="#+id/new_date"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:textSize="25dip">
</TextView>
<ListView
android:id="#+id/new_files"
android:layout_width="match_parent"
android:layout_height="wrap_content" android:background="#000000">
</ListView>
</LinearLayout>
the main activity that creates the multiple listviews and displays them:
public class WhatsNew extends Activity{
private Button homeBtn;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.whats_new_main);
CreateLastModDateList();
homeBtn = (Button) findViewById(R.id.home);
homeBtn.setOnClickListener(new OnClickListener() {
public void onClick(View v){
Intent myIntent = new Intent(getApplication().getApplicationContext(),
Main.class);
startActivityForResult(myIntent, 0);
}
});
}
private ArrayList<File> GenerateNewFileDates(){
Files.clearFilesList();
Files.buildFilesList(Files.getRoot());
return Files.getAllFiles();
}
private void CreateLastModDateList(){
ArrayList<File> files = GenerateNewFileDates();
ArrayList<String> allDates = new ArrayList<String>();
ArrayList<String> dates = new ArrayList<String>();
Context c = this.getApplicationContext();
LinearLayout l = (LinearLayout) findViewById(R.id.whats_new_content);
/** Builds a list of all file dates **/
for(File file : files){
Date lastModifiedDate = new Date(file.lastModified());
allDates.add(lastModifiedDate.toString());
}
/** For each date, check if we have already put that date into the dates array
if we have then we want to ignore it, otherwise add it to the dates array. **/
for(String date : allDates){
if (exists(dates,date) == false){
dates.add(date);
}
}
for(String date : dates){
ArrayList<String> test = new ArrayList<String>();
test = generateFileList(date, files);
WhatsNewLayout whatsNew = new WhatsNewLayout(c,test, date);
l.addView(whatsNew);
}
}
public ArrayList<String> generateFileList(String date, ArrayList<File> mFiles){
ArrayList<String> filesFromDate = new ArrayList<String>();
for(File file : mFiles){
Date lastModifiedDate = new Date(file.lastModified());
boolean x = lastModifiedDate.toString().equals(date);
if (x == true){
filesFromDate.add(file.getName());
}
}
return filesFromDate;
}
public boolean exists(ArrayList<String> list, String compare){
boolean result = false;
if(list.contains(compare)){
result = true;
}
return result;
}
}
The custom linearlayout i inflate:
public class WhatsNewLayout extends LinearLayout {
private LayoutInflater mInflater;
public WhatsNewLayout(Context context, ArrayList<String> files, String date) {
super(context);
mInflater = LayoutInflater.from(context);
View convertView = new View(context);
/** initialize variables **/
DataHolder holder;
Context c = this.getContext();
ArrayAdapter<String> list = new ArrayAdapter<String>(c,
android.R.layout.simple_list_item_1, files);
convertView = mInflater.inflate(R.layout.whats_new_item, null);
holder = new DataHolder();
holder.modDate = (TextView) convertView.findViewById(R.id.new_date);
holder.FileList = (ListView) convertView.findViewById(R.id.new_files);
convertView.setTag(holder);
holder.modDate.setText(date);
holder.FileList.setAdapter(list);
this.addView(convertView);
}
}
Sorry about the large mass of code, but i didnt want to leave any details out in case it is important, hopefully this is everything, im sure its something really fundermental with my call of the layout but i just cannot see what is casuing the custom layout to be cut.
if any more details are required please let me know. =)
Put your listViews in a vertical scroll. You can have scrollable listView inside of a vertical scroll by the following trick. use the following code and enjoy!
private int listViewTouchAction;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//...
setListViewScrollable(myListView1);
setListViewScrollable(myListView2);
}
private void setListViewScrollable(final ListView list) {
list.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
listViewTouchAction = event.getAction();
if (listViewTouchAction == MotionEvent.ACTION_MOVE)
{
list.scrollBy(0, 1);
}
return false;
}
});
list.setOnScrollListener(new OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view,
int scrollState) {
}
#Override
public void onScroll(AbsListView view, int firstVisibleItem,
int visibleItemCount, int totalItemCount) {
if (listViewTouchAction == MotionEvent.ACTION_MOVE)
{
list.scrollBy(0, -1);
}
}
});
}
listViewTouchAction is a global integer value.
It is not possible to have a ListView inside ScrollView (since the ListView is also a scrollable component) this is why your ListView is cropped to the screen.
If you need to display several List on screen to let user select something, you may use Spinner instead of List