How to change only one property of a BuiltInView? - android

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;

Related

Creating Rows in ListView with ArrayAdapter in Kotlin

I have an app that's getting information from an API request and then displaying a list of devices. After several hours of combing through documentation, I cannot figure out how to format the View that is created from the ArrayAdapter. Essentially, if the device has an error, I want to display a red circle to the right of the button and display a green button if there is no error.
deviceList is the name of a ListView that I am trying to display my list of buttons inside of. deviceNames is an array of strings that contains the names of the devices.
The TextViews that are created are also clickable, which is what the onItemClickListener is handling. This section works, but I wanted to leave it in because I do need the buttons to start an activity that displays device-specific information.
Ideally I would like to essentially create a template that I can just change the values of the text and the color of the indicator for
Below is my code:
// List of device names
val listView: ListView = findViewById(R.id.deviceList)
val arrayAdapter1: ArrayAdapter<*>
arrayAdapter1 = ArrayAdapter(
this#Homepage,
R.layout.device_button,
deviceNames
)
listView.setAdapter(arrayAdapter1)
listView.onItemClickListener =
AdapterView.OnItemClickListener { parent, view, position, id ->
val pos = position
println(pos)
val device = jsonArray.getJSONObject(pos)
val ID = device.get("id") as String
println(ID)
goToDeviceDetail(ID)
}
Below is the XML file for device_button. I tried to add formatting here and essentially create a template for a button that would allow me to change the text and the color of the indicator, but it got mad that it wasn't just a TextView.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:gravity="center_vertical"
android:textColor="#25383C"
/>
Below is the button that I would like for it to look like. I'm likely going to just make the background a solid color rather than the image that is in the below picture:
I would say the biggest problem is your using a simple API for a more complex problem. It is entirely possible do it with a ListView and ArrayAdapter. But I would highly recommend looking into RecyclerView/RecyclerView.Adapter
The way it works out is...
RecyclerView.Adapter binds your list of data ie Devices to the individual RecyclerView.ViewHolder
The ViewHolder would inflate your xml layout that contains the button. You then have access to all View contained in that layout easily.
You then can put listeners on the button.
The Adapter then can be setup to receive new data, when received it can rebind the data that has changed.
Say the user clicks one of the device buttons it does a task. When it gets back it will say hey Adapter I have a new List for you.(The list now contains the "fixed" device).
ViewModel(contains observable data)->Fragment/Activity(Observers the data)->Adapter(Receives the data)->ViewHolder(Displays the data)->Activity("Fixes the data")->ViewModel->...loops
Here is a very good example.
https://medium.com/#atifmukhtar/recycler-view-with-mvvm-livedata-a1fd062d2280
If you really want to keep using the ListView and ArrayAdapter you are receiving the clicked view here.
OnItemClickListener {
/*Parent of the view*/ parent,
/*The view clicked*/ view,
/*position of data*/position,
/*id of the view clicked*/ id
->{
view.findById(R.id.text_view);
//onClick
}
}
With that you know what has been clicked so you know what has to be changed later when you get back from your other Activity.

Android: Spinner not working properly on 4.4.x

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

How to add blocks of linearLayouts and is there a better way?

This is the main idea of what I am trying to do.
I would like to ask you what is the best way to make such a design. the problem is that these gray/black blocks might not show up (user will choose which should show up). So I would like to find out do I need to make these 3 text views inside linearLayout programatically? Is there anyway to create some sort of template which I would only have to edit by setting new texts for textViews and add them to some sort of layout?
Another option is to just have a LinearLayout and an xml for the row entry. Then you can do:
LinearLayout layout = (LinearLayout) this.findViewById(R.id.your_layout_id);
List<Blocks> userBlocks = getMyUserBlocks();
for(Block b : userBlocks) {
View blockView = LayoutInflater.from(getBaseContext())
.inflate(R.layout.your_row_layout, layout, false);
TextView someData = (TextView) blockView.findViewById(R.id.your_text_view_id);
someData.setText(b.someAttribute.toString());
layout.addView(blockView);
}
You will need a separate xml layout for the block row.
You can use a ListView with large dividers. Your dark gray blocks are the row views, and you can just append them to a dataset and update the adapter for the ListView. For the dividers, see setDivider() and setDividerHeight().

Android: LinearLayout disappears due to spacing using weight?

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));

Dynamically add UI content in android

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.

Categories

Resources