I'm adding a Spinner programmatically, using a Custom Array Adapter class (which extends ArrayAdapter) and a Custom Layout for my Spinner (which consists of an ImageView and a TextView per row).
Everything works fine, except for Android Kit Kat: If I tap on my Spinner, it doesn't show dropdown items, even if it contains right items. I'm debugging on Android 6.x and 7.x: it works without any problems.
If I use Custom Adapter and Layout using an inflated layout (inside XML of my activity), I don't have any problems, but if I add my Spinner programmatically (using an external XML layout), it doesn't work.
Do you know if there are known compatibility issues about Spinner/Custom Adapter in Android 4.4.x? (I can add code if it can be useful)
EDIT
Partial code inside my Activity:
TableLayout container = (TableLayout)findViewById(R.id.table);
LayoutInflater inflator = this.getLayoutInflater();
//Single row I wish to add programmatically
TableLayout row = new TableLayout(getApplicationContext());
inflator.inflate(R.layout.internal_layout_to_clone, row);
container.addView(row);
//Acquire Spinner
Spinner spinner = (Spinner)row.findViewById(R.id.spinner);
//[here I use Custom Adapter to populate my Select: values are shown properly]
R.layout.internal_layout_to_clone is an XML file which contains a TableLayout with several TableRow(s) and one of this rows contains my Spinner.
I don't know if the problem is that I'm nesting TableLayout inside another TableLayout, maybe this is not well-managed in Android 4.4
I belive the problem is somewhere in the code, if you could upload it i'll have a look.
By the way I have been using an external library for spinner which made the use of spinners easier and it looks better then the regular one.
https://github.com/ybq/Android-SpinKit
I just solved my issue. I changed the way I use to inflate my external layout from this:
TableLayout table = (TableLayout)findViewById(R.id.table);
LayoutInflater inflator = this.getLayoutInflater();
TableLayout row = new TableLayout(getApplicationContext());
inflator.inflate(R.layout.internal_layout_to_clone, row);
table.addView(row);
to this:
TableLayout table = (TableLayout)findViewById(R.id.table);
TableLayout row = (TableLayout)LayoutInflater.from(this).inflate(R.layout.internal_layout_to_clone, null);
table.addView(row);
I don't know why, maybe some involved methods are not fully compatible with old Android version, but now it works properly on every version tested.
Thanks
Related
I am looking for solution where could dispay large data with horizontal and vetical scroll. Something like this https://www.syncfusion.com/blogs/post/new-datagrid-for-xamarinios-and-xamarinandroid.aspx. I would like to change number of columns and change the cell values.
Do you have please something like this? When it be a free i will be really happy. Thank you
You can create it by custom TableLayout. And the good news is you can add both side scrolling mechanism in TableLayout. You have to first set a TableLayout xml. For both side scrolling you have to add first ScrollView then HorizontalScrlollView then your TableLayout.
Now you you can create row and column. You can create it dynamically because you (may be) dont know how many column has required. So you have to create custom TableRow and cell and you can easily add your tableLayout .
TableRow row= new TableRow(this);
TableRow.LayoutParams lp = new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT);
row.setLayoutParams(lp);
// then create your text view and add it row. and last add row in your table layout
This is just example. You can create your own data grid library
And you can some ready mate library to show data grid in table view like your reference. :
https://github.com/evrencoskun/TableView
https://github.com/ISchwarz23/SortableTableView
I'm a Xamarin Android beginner. In an app that I'm writing, I created an Activity with a RadioGroup, and a button below it.
All was fine, until my radio buttons contained so much text that the radio group ran off the phone's screen, and the button was hidden.
So, I searched on the internet, and discovered that I could make the RadioGroup scrollable, by making it a ListView, and setting the ListView to be above the button.
Furthermore, I discovered that Xamarin Android offers the BuiltInView SimpleListItemSingleChoice which is a ready-made radio group as a ListView.
So, I implemented this, and all was fine, except that the text fields in each Item of the BuiltInView get cut short (i.e my radio button options to the user).
I want to apply the property
android:layout_height="wrap_content"
so that my long text labels for the radio buttons won't get cut short.
My question is, how do I apply this to each item of the BuiltInView?
I've tried to define my own custom view, but have run into problems trying to make it checkable, so I wondered if there is a simpler way to solve the problem by using the already provided BuiltInView.
In MyListAdapter GetView, I have
view = (context.LayoutInflater.Inflate(Android.Resource.Layout.SimpleListItemSingleChoice, parent, false));
and in my Activity, I have
myListAdapter = new Adapters.MyListAdapter(this, myStrings, false);
myListView.Adapter = myListAdapter;
As the BuiltInView does not offer an xml file (or I don't know where to access it in Xamarin), I added the following code to the custom Adapter's GetView method:
var textLabel = view.FindViewById<TextView>(Android.Resource.Id.Text1);
//I added these 2 lines to set the WrapContent property on each element of the BuiltInView
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(AbsListView.LayoutParams.MatchParent,
AbsListView.LayoutParams.WrapContent);
textLabel.LayoutParameters = layoutParams;
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);
Right, this is a strange problem I have been toying with for a while now, hopefully maybe I am missing something you guys can draw my attention to!
LinearLayouts seem to be disappearing once I add any spacing using views and defining the weight (a method which works elsewhere in the project).
I have a custom Dialog (extends Dialog). In the onCreate() I use the method setContentView(generateDialog()) which returns a vertical LinearLayout.
The LinearLayout has three elements, one row of four custom category buttons (LinearLayouts), one row of sorting buttons (also LinearLayouts) and one ListView which populates the rest of the dialog and refreshes based on which button is pressed.
All is functional and working fine. Except when I attempt to space the buttons out evenly using my spacer method:
Dialog.java:
LinearLayout catBtns = new LinearLayout(context);
catBtns.setOrientation(LinearLayout.HORIZONTAL);
catBtns.setBackgroundResource(R.drawable.btn_cat_gradient_bg);
catBtns.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
catBtns.addView(space(1));
cat1Btn = new CatButton(context,this,act,"CAT1");
catBtns.addView(cat1Btn);
catBtns.addView(space(1));
cat2Btn = new CatButton(context,this,act,"CAT2");
catBtns.addView(cat2Btn);
catBtns.addView(space(1));
cat3Btn= new CatButton(context,this,act,"CAT3");
catBtns.addView(cat3Btn);
catBtns.addView(space(1));
cat4Btn = new CatButton(context,this,act,"CAT4");
catBtns.addView(cat4Btn);
catBtns.addView(space(1));
The space() method:
private View space(int space) {
View view = new View(context);
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(0,0);
p.weight = space;
view.setLayoutParams(p);
return view;
}
What confuses me is that I have been using this method throughout the project and can't find as to why the category LinearLayout DISAPPEARS COMPLETELY when I add the spacers in between each button.
I use the same technique for the sorting buttons and it works perfectly! I use the same technique in another part of the project using slightly different versions of the same buttons (they are different class files though, because the onClickListener and some other stuff is slightly different)
Anyone have any clue?
I tried to build this in XML and it works fine. A possible difference is, that you do not set any LayoutParams for your buttons. Try something like that for every button:
cat1Btn = new CatButton(context,this,act,"CAT1");
catBtns.addView(cat1Btn, new LinearLayout.LayoutParams(0,0));
I have an android app which asks a question followed by x number of options.
Each option contains a textview, ImageView and a radio button.
The value of x (i.e. the number of options) is not constant. I want to dynamically add UI content to satisfy this requirement.
At the moment I have written the code in the layout's xml to display a maximum of 4 options. If number of options is 2 I hide the options 3 and 4 using something like
tvoption1.setVisibility(View.GONE);
tvoption2.setVisibility(View.GONE);
However this is not very scalable. Can anyone tell me how to add options for java dynamically. Or is there a better approach?
A View can be added at runtime by using the inflater like this:
LinearLayout linearLayout = (LinearLayout)inflater.inflate(R.layout.news_categories_item, null);
TextView categoryValueTextView = (TextView)linearLayout.findViewById(R.id.news_category_item_value);
mMainLinearLayout.addView(categoryValueTextView);
In this example, a LinearLayout containing a TextView is inflated. A reference to the constituent TextView is then obtained, and the TextView is dynamically added (at runtime) to the main linear layout (mMainLinearLayout).
The inflater object may be obtained in an Activity by using getLayoutInflater().
create your row layout separately, from the main xml
Get LayoutInflater service from context:
LayoutInflater inflater=(LayoutInflater)getSystemService(LAYOUT_INFLATE_SERVICE);
use following method to addview to main xml, I assume you have parent layout llParent in xml and you want to add items in this llPaent, from list list.
for(int i=0;i<list.size();i++)
{
LinearLayout llView=(LinearLayout)inflater.inflate(R.layout.row);
//get view id and set values
TextView txt=(TextView)llView.findViewById(R.id.text);
}
A ListView is a good view for displaying several similar items. Here is a tutorial (Other views with adapters are good too, such as GridView or Gallery).
You will probably want to create your own adapter for the list, so you can display all three views (checkbox, image and text) as one item, but there are lots of examples on that available on the net as well as here on SO.