How to create the Header Inside the Listview(First Row Is Fix which will not move verticaly when listview scroll verticaly) But This listview move Horizontaly.
You can use this method of listview,
ListView l1 = new ListView(this);
TextView tv = new TextView(this);
tv.setText("Header");
l1.addHeaderView(tv);
You can use any View type object as Header
Reference
maybe what you need is to separate the header from the view.
Related
I am learning android and I want to know if I can add a view with out using xml file as template for view
, any examples for help
You can add views to layout programmaticaly by using addView() method provided by the ViewGroup class.
First of all you should take a reference to your layout by using findViewById<>().
Then you must create a view to add:
val listView = findViewById<LinearLayout>(R.id.list_view)
val view = Button(this)
view.layoutParams = LinearLayout.LayoutParams(
LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
listView.addView(view)
Above I added a button lo my linear layout with list_view id.
The layout parameters are the correspondent width and height.
Yes, just use .addView(), if for example you have a ListView that you want to add a TextView to, you can just do:
ListView yourListView = (ListView) activity.findViewById(R.id.yourListView);
TextView newTextView = new TextView(activity);
newTextView.setText("foobar");
yourListView.addView(newTextView);
activity should be this if you are in an Activity, or requireActivity() if you're in a Fragment.
You can set many other properties of this new TextView (or whatever View you are adding), just try typing .set after it, and look at the suggestions.
I have a GridView and I need to add a footer view to. I know this is possible to be do with a ListView using code like this:
LinearLayout layoutFooter = (LinearLayout) getLayoutInflater().inflate(R.layout.footerview, null);
final ListView listView = getListView();
listView.addFooterView(layoutFooter);
But how do I do the same with GridViews? This approach doesn't seem to be working for GridView. Any idea? Cheers.
use relative layout as a parent layout and set your footer ,center and header postion and put your grid view in center position and desired footerview at footer position.
Here's a good soluction to do this.
I've got a LinearLayout which includes among others two ListViews. Each ListView has it's own ArrayAdapter. Now the Scrolling shouldn't be in the ListViews, the user should see the whole Lists and scroll the whole view. Sourrounding the LinearLayout with a ScrollView doesn't work because of the inerhit Scrolling Views... .
How can I expand the ListViews and let the user scroll only the outer view?
You should not use a ListView this way.
List Views are meant to recycle views, which it cannot do if its not the view that is scrolling.
You could simply use a LinearLayout and add every single view to the layout. This would be better than using a ListView.
(This does not mean it is the best solution)
This can be done easily in RecyclerView. But in this case you are using ListViews so try listView.setScrollContainer(false);
may be it works for you
You should inflate Views Like this.
// from content View of activity or fragment
listView = (LinearLayout) findViewById(R.id.sos_list_view);
listView.removeAllViews();
List<SosObject> sosList = batabase.getAllItems();
for(SosObject t: sosList) {
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Item layout
View view = inflater.inflate(R.layout.sos_prewivew_item, null);
TextView comment = (TextView) view.findViewById(R.id.sos_comment_text);
TextView date = (TextView) view.findViewById(R.id.sos_date_text);
TextView id = (TextView) view.findViewById(R.id.sos_answer_id);
TextView tittle = (TextView) view.findViewById(R.id.answer_tittle);
listView.addView(view);
}
}
Your xml Should look like:
<ScrollView>
<LinearLayout>
</LinearLayout > // Fist list
</LinearLayout > // Second list
</LinearLayout>
</ScrollView>
ListViews cannot be overriden to lose the scrolling ability. They seem to be designed as top-level objects that occupy the whole screen.
So basically you have 2 options here:
Convert all your elements into ListView's items
use LinearLayouts instead of ListViews and wrap all elements in a ScrollView
I'm not sure how to ask this question exactly, but I'll give it a try.
Here's what I'm trying to do, in one Activity.
Build a Grid, that contains [x] rows of 3 columns each, with this content
[a TextView (containing a name)] [a Spinner (containing a list of states)] [an EditText]
How to start ? The Views I can create programmatically, that's not a problem, I even store them in 3 array lists for later easy reference, but I can't see how to do it right.
Should I create an xml layout with e.g. (and pseudocode)
LinearLayout (horizontal)
TextView ...
Spinner ...
EditText ...
/LinearLayout
and try to inflate it in the loop I use to create each row, and setting the id of each view in a standard way (e.g. viewName[x] where x is the current "i" from my for, but is it of any use?), as we do for example for an ExpendableList Adapter's groups/childs ?
Or is there a way to actually use a GridView/GridLayout to do that (in this case, being in my Activity, how do I put each specific created View into the correct GridView/GridLayout) ?
Or still another way I don't suspect at all ?
Thanks in advance
if you want to add views programatically then, just create a layout.xml with 3 Linearlayouts(horizontal) in it. Also assign id to those LinearLayouts. Then in your java code, just find the views and call addView() on those LinearLayouts.
Example:
LinearLayout ll_1 = (LinearLayout)findViewById(R.id.linearlayout1);
LinearLayout ll_2 = (LinearLayout)findViewById(R.id.linearlayout2);
LinearLayout ll_3 = (LinearLayout)findViewById(R.id.linearlayout3);
...
ll_1.addView(new TextView(this));
ll_1.addView(new Spinner(this));
ll_1.addView(new EditText(this));
...
In detail, say for example, if i want to add spinner to LinearLayout, programatically then,
You need to get layout
LinearLayout linearLayout = findViewById(R.id.layoutID);
Create spinner as below:
Spinner spinner = new Spinner(this);
spinner .setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.WRAP_CONTENT));
ArrayAdapter spinnerArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item, spinnerList);
spinner.setAdapter(spinnerArrayAdapter);
Then add spinner to view
linearLayout.addView(spinner);
I am trying to add empty header or footer on ListView to create a space between top and bottom ListView. I could create another empty layout and add into it. But I'm try to find alternative in coding, create for example linear/frame layout with width (fill_parent) and height (say, 50dp) and add header into ListView.
you may do it in this way:
LinearLayout viewHeader = new LinearLayout(context);
viewHeader.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams lp = new LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, 50);
viewHeader.setLayoutParams(lp);
yourListView.addHeaderView(viewHeader, null, false);