Setting background while using Custom ListView - java

I am working on a android application project in which I am using a customer simple adapter to repeat a layout called row.xml. Everything is going good except for the fact that I have a background color/image that I am using for my app and when I use this simple adapter method I get white space for the area that is not filled by the list.
Here is the code from my Java class
public class CustomerActivity extends ListActivity {
final static ArrayList<HashMap<String, ?>> arrayListData = new ArrayList<HashMap<String, ?>>();
ListView mCustomerList;
static {
HashMap<String, Object> CurrentRow = new HashMap<String, Object>();
CurrentRow.put("Icon", R.drawable.has_360_inspection_icon);
CurrentRow.put("Icon2", R.drawable.has_solid_fuel_inspection_icon);
CurrentRow.put("Name", "Xue Test");
arrayListData.add(CurrentRow);
CurrentRow = new HashMap<String, Object>();
CurrentRow.put("Icon", R.drawable.has_360_inspection_icon);
CurrentRow.put("Icon2", R.drawable.no_solid_fuel_inspection_icon);
CurrentRow.put("Name", "Ryan Test");
arrayListData.add(CurrentRow);
CurrentRow = new HashMap<String, Object>();
CurrentRow.put("Icon", R.drawable.no_360_inspection_icon);
CurrentRow.put("Icon2", R.drawable.has_solid_fuel_inspection_icon);
CurrentRow.put("Name", "Becca Test");
arrayListData.add(CurrentRow);
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SimpleAdapter adapter = new SimpleAdapter(this, arrayListData, R.layout.row, new String[] { "Icon", "Icon2", "Name" },
new int[] { R.id.imageView1, R.id.imageView2, R.id.customer_name_textView });
setListAdapter(adapter);
}
and here is the code from row.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lightBlue"
android:weightSum="6" >
<TextView
android:id="#+id/customer_name_textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="TextView" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/has_360_inspection_icon" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/has_solid_fuel_inspection_icon" />
<View
android:id="#+id/view1"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="2" />
</LinearLayout>
Thank you for the help and if you need any more information just let me know.

If you just want the entire list to have a light blue background, set it on the listview itself instead of on each row. That way if there's empty space in the list it will still be blue.

Have you use wrap_content or match_parent for Listview? If match_parent then replace to wrap_content

Related

How to add a line or some separation between username and article

In the activity I've created there is a article written by the user and below it directly is the username. there is no line or any kind of seperation between them.
i wish to have some sort of differenciation between the two.
here is the code:
final ListView listView = findViewById(R.id.listView);
final List<Map<String, String>> articleData = new ArrayList<>();
ParseQuery<ParseObject> query = ParseQuery.getQuery("ARTICLE");
query.whereContainedIn("username", ParseUser.getCurrentUser().getList("FOLLOWING"));
query.orderByDescending("createdAt");
query.setLimit(1000);
query.findInBackground(new FindCallback<ParseObject>() {
#Override
public void done(List<ParseObject> objects, ParseException e) {
if (e == null) {
for (ParseObject article : objects) {
Map<String, String> articleInfo = new HashMap<>();
articleInfo.put("content", article.getString("ARTICLE"));
articleInfo.put("username", article.getString("username"));
articleData.add(articleInfo);
}
SimpleAdapter simpleAdapter = new SimpleAdapter(FeedActivity.this, articleData, android.R.layout.simple_list_item_2, new String[]{"content", "username"}, new int[]{android.R.id.text1, android.R.id.text2});
listView.setAdapter(simpleAdapter);
}
}
});
Here is the XMl:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FeedActivity">
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginBottom="1dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I'm new to android studio, so please elaborate your answer if possible.
thanks....
The easiest thing would be to and a new line to the article name.
articleInfo.put("content", article.getString("ARTICLE") + "\n ");
You add a new line or (the enter key) with \n. I have also added a space character because sometimes the line does not get shown if they don't have any characters.
I hope this works I haven't used HashMaps very often so message me if not and I will try to help further.
I think you are looking for divider height property:
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:divider="#drawable/list_divider" <= //also this line
android:dividerHeight="2dp" > <= //this line here
and you have to create custom divider in Drawable folder named list_divider:
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke
android:width="1dp"
android:color="#8F8F8F"
android:dashWidth="1dp"
android:dashGap="1dp" />
</shape>
or you can use this code under your username to add line under it:
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#android:color/black"/>

Listview adapter not displaying list columns after closing of Dialog

Setup
In my app I have a section that creates rows of data to be saved in a database. Each row is built from a textbox, number box then "set" into a listview for display. Later on the user can open a "plan" created before and review the data.
I created a listview, a custom layout for the display line and figured out how to add/display each new row. However, when I perform the open, I create a dialog to display the list for plans, user selects on and the rows should display. They do not. I trace through the code and I see the arraylist for the adapter being populated and I call the notifydatasetchanged, but the main listview remains empty.
I am not using a customer adapter class, but just the Simple adapter.
This is the beginning of the Activity. I am still learning android coding so I tend to use generic names as I learn.
public class TrackItEqMainActivity extends AppCompatActivity {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
public int rowCount = 1;
private static final String eTAG = "Exception";
final Context context = this;
// global variables needed for processing the add gaits display
**private ListView lstGaits;
private ListAdapter adapter;
private ArrayList<HashMap<String, String>> mylist;**
// *****
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i(eTAG, "onCreate Build");
setTitle(R.string.activityManagePlans);
setContentView(R.layout.activity_track_it_eq_build);
Toolbar mySecondaryToolbar = (Toolbar) findViewById(R.id.my_secondaryToolbar);
setSupportActionBar(mySecondaryToolbar);
setActivityBuildListeners();
// set the objects needed to handle the dynamic gaits list
ListView lstGaits = (ListView) findViewById(R.id.lstMyGaits);
mylist = new ArrayList<HashMap<String, String>>();
try {
adapter = new SimpleAdapter(this, mylist, R.layout.gaits_detail,
new String[] { "keyGait", "keyTime" }, new int[] {
R.id.txtdisplayGait, R.id.txtdisplayTime });
lstGaits.setAdapter(adapter);
} catch (Exception e) {
Log.i(eTAG, e.getMessage());
}
// ATTENTION: This was auto-generated to implement the App Indexing API.
// See https://g.co/AppIndexing/AndroidStudio for more information.
client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}
SO when a user taps a new button a set of controls appear allowing to select a gait and enter a time. They then tap on the set button and in the set onClick, I add the data to the internal array and display the custom layout on the screen.
private Button.OnClickListener onClick_btnSet = new OnClickListener() {
#Override
public void onClick(View v) {
Spinner spinner = (Spinner) findViewById(R.id.spinner_gaits);
TextView textGait = (TextView) spinner.getSelectedView();
String gtResult = textGait.getText().toString();
TextView txtTime = (TextView) findViewById(R.id.txtSelected);
String tmeResult = txtTime.getText().toString();
buildDisplayLine(true,gtResult,tmeResult);
txtTime.setText("");
spinner.requestFocus();
}
};
The function that builds the line and adds to the array is this:
private void buildDisplayLine(boolean isNew, String gait, String time) {
HashMap<String, String> map2 = new HashMap<String, String>();
map2.put("keyGait",gait); // put the col data in
map2.put("keyTime", time); // put the col data in
mylist.add(map2);
if (isNew) {
((BaseAdapter) (adapter)).notifyDataSetChanged();
}
}
All that works although a strange side effect is that the list will not display if the number keypad is visible. When I clear it off the list appears.
Ifa use wants to look at or change a plan they tap the open button. IT display a dialog box that shows a list of plans. Tap on a plan and all the elements should display:
private final ThreadLocal<OnClickListener> onClick_btnOpen = new ThreadLocal<OnClickListener>() {
#Override
protected OnClickListener initialValue() {
return new OnClickListener() {
#Override
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_track_it_eq_open_plan);
dialog.setTitle("Open Exercise Plan...");
Button diaCancelButton = (Button) dialog.findViewById(R.id.btnCancelOpen);
diaCancelButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
// get a list of files from the local app plans
final eqDatabaseService eqDB = new eqDatabaseService(context, 2);
ArrayList<HashMap<String,String>> allPlans = eqDB.getPlanList();
if (allPlans.isEmpty()) {
Toast toast = new Toast(context);
toast.setGravity(Gravity.TOP, 0, 0);
Toast.makeText(context, "No Files to open, Please create one!", Toast.LENGTH_LONG).show();
return;
}
ListView lvPlan = (ListView) dialog.findViewById(R.id.lvPlans);
try {
adapter = new SimpleAdapter(context, allPlans, R.layout.plans_columns,
new String[] { "keyName", "keyENum" }, new int[] {
R.id.txtPlanName, R.id.txtNumElements });
lvPlan.setAdapter(adapter);
} catch (Exception e) {
}
//********************************************************
lvPlan.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
TextView txtplan = (TextView) v.findViewById(R.id.txtPlanName);
String planName = txtplan.getText().toString();
List<eqSessions_dt> myPlan = eqDB.getCurrentPlan(planName);
clearGrid();
for (eqSessions_dt row : myPlan) {
buildDisplayLine(false, row.get_gait(),Integer.toString(row.get_time()));
}
((BaseAdapter)(adapter)).notifyDataSetChanged();
dialog.dismiss();
}
});
dialog.show();
}
};
}
};
I have played around with this, did look at other similar questions and answers, but didn't see anything that would explain why the notify would not work. I avoided doing a custom adapter, trying to keep this simple at first. What am I missing that stops the list from displaying. I see the arraylist being filled and all related objects are global to the activity. I'll keep playing around, but I hope otehr eyes see what I miss.
This is the activity xml`
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
tools:context="com.newproject.jhull3341.trackiteq.TrackItEqMainActivity">
<android.support.v7.widget.Toolbar
android:id="#+id/my_secondaryToolbar"
android:layout_width="fill_parent"
android:layout_height="?attr/actionBarSize"
android:background="#236dc1"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/lyFileAction">
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="New"
android:id="#+id/btnNew"
android:layout_row="0"
android:layout_column="0"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Open"
android:id="#+id/btnOpen"
android:layout_row="0"
android:layout_column="1"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Save"
android:id="#+id/btnSave"
android:layout_row="0"
android:layout_column="2"
android:layout_gravity="center_horizontal" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="match_parent"
android:text="Delete"
android:id="#+id/btnDelete"
android:layout_row="0"
android:layout_column="3"
android:layout_gravity="center_horizontal" />
</GridLayout>
<GridLayout
android:layout_width="367dp"
android:layout_height="wrap_content"
android:id="#+id/grdEntry">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/label_time"
android:id="#+id/lblTime"
android:layout_row="0"
android:layout_column="1"
android:layout_marginBottom="5dp" />
<Spinner
android:layout_width="147dp"
android:layout_height="wrap_content"
android:id="#+id/spinner_gaits"
android:layout_row="1"
android:layout_column="0"
android:focusable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/label_Gait"
android:id="#+id/lblGait"
android:layout_row="0"
android:layout_column="0"
android:layout_marginBottom="5dp" />
<EditText
android:layout_width="124dp"
android:layout_height="wrap_content"
android:id="#+id/txtSelected"
android:layout_row="1"
android:layout_column="1"
android:layout_gravity="fill_horizontal|center_vertical"
android:singleLine="true"
android:numeric="integer" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="92dp"
android:layout_height="fill_parent"
android:text="Set"
android:id="#+id/btnSet"
android:layout_row="1"
android:layout_column="2"
android:layout_gravity="center_vertical"
android:clickable="true" />
</GridLayout>
<ListView
android:id="#+id/lstMyGaits"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_below="#+id/grdEntry" />
</LinearLayout>
this is the custom layout for each display row.`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtdisplayGait"
android:layout_width="wrap_content"
android:layout_height="44dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="TextView"
android:textSize="18sp" />
<TextView
android:id="#+id/txtdisplayTime"
android:layout_width="80dp"
android:layout_height="44dp"
android:gravity="center_vertical"
android:text="TextView"
android:textSize="18sp" />
<ImageButton
android:id="#+id/btnRemoveLine"
android:layout_width="25dp"
android:layout_height="44dp"
android:layout_weight="0.39"
android:background="#color/wallet_bright_foreground_holo_dark"
android:elevation="1dp"
android:src="#android:drawable/btn_dialog" />
</LinearLayout>`

Android custom List SimpleAdapter - chose row icon

Layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#android:color/black">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="40dp"
android:layout_height="40dp"
android:id="#+id/icon"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/line_a"
android:textSize="14dp"
android:textColor="#android:color/white"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold"/>
<TextView android:id="#+id/line_b"
android:textSize="10dp"
android:textColor="#android:color/darker_gray"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</LinearLayout>
Java:
SimpleAdapter sa;
ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
HashMap<String,String> item;
for(int i = 0; i < j; i++){
item = new HashMap<String,String>();
item.put( "line1", ListData[i][0]);
item.put( "line2", ListData[i][1]);
item.put( "type", ListData[i][2]);
list.add( item );
}
sa = new SimpleAdapter(this, list,
R.layout.two_lines_list,
new String[] { "line1","line2" },
new int[] {R.id.line_a, R.id.line_b});
// can you add the setImageView here? If yes, how?
setListAdapter( sa );
}
How can I make the icon to get an image from res/drawable... depending on the type (ListData[i][2] inserted in the HashMap)? eg. if the type is "1" set icon1.png at the beginning of the 2-line-row
You should override SimpleAdapter.getView(int position, View convertView, ViewGroup parent) method, and get the list item with the current position(the first param in that method), then check the value set set the icon you want.
NOTE: you' d better use ViewHolder pattern in the getView method for performance.

Making the ListView Scrollable

I have two xml files. One of them contains the ListView and the one who populates the listView. I was
able to display the items but the thing is, it can't be scrolled. For example, I have list of items to display but it will not automatically enables scroll. Which part of the code should be enhanced so that it will allow scrolling. I know that listView enables scrolling by default. Please help me with this one. Thanks in advance.
public class ActivityViewCategory extends ListActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_products);
// Hashmap for ListView
productsList = new ArrayList<HashMap<String, String>>();
// Get listview
ListView lv = getListView();
.
.
.
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting all products
pDialog.dismiss();
// updating UI from Background Thread
runOnUiThread(new Runnable() {
public void run() {
/**
* Updating parsed JSON data into ListView
ArrayList<HashMap<String, String>> com.example.restotrial.ActivityViewCategory.productsList * */
ListAdapter adapter = new SimpleAdapter(
ActivityViewCategory.this, productsList,
R.layout.list_item, new String[] { TAG_PID,
TAG_NAME},
new int[] { R.id.pid, R.id.name });
// updating listview
setListAdapter(adapter);
}
});
}
}
activity_view_products.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(#android:id/list)
-->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="456dp" >
</ListView>
</LinearLayout>
list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- Product id (pid) - will be HIDDEN - used to pass to other activity -->
<TextView
android:id="#+id/pid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<!-- Name Label -->
<TextView
android:id="#+id/name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingTop="6dip"
android:paddingLeft="6dip"
android:textSize="17dip"
android:textStyle="bold" />
</LinearLayout>
I think it would work better if you used the constraints of a RelativeLayout to contain your ListView.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<!-- Main ListView
Always give id value as list(#android:id/list)
-->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content" >
</ListView>
</RelativeLayout>
The visual top of your ListView will be at the top of the parent view, the visual bottom will be at the bottom of the parent view, and your content will be as long as your list happens to be, and scroll where it needs to.

Android ListView Background repeats background after each entry

I figured it out I was using the same layout for my on create and to show the data so it was repeating everyting.
I have a list of data showing and for some reason the background I have set looks fine but after each two text views the background is squished into a 20px window overlaying the orginal background.
My xml for my view looks like so:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:background="#drawable/patriot_bg2"
android:cacheColorHint="#00000000"
android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:drawSelectorOnTop="true"
/>
<TextView
android:id="#+id/item_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:background="#00000000"
android:padding="2dp"
android:textSize="20dp" />
<TextView
android:id="#+id/item_subtitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#00000000"
android:cacheColorHint="#00000000"
android:padding="2dp"
android:textSize="13dp" />
</LinearLayout>
Below is the Java that places the text into the text fields and adds it to the screen:
for (int i = 0; i < nodes.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element)nodes.item(i);
map.put("main_content", XMLfunctions.getValue(e, "content"));
map.put("name", XMLfunctions.getValue(e, "name"));
mylist.add(map);
}
//
ListAdapter adapter = new SimpleAdapter(ShowXMLPAR.this, mylist , R.layout.listplaceholder,
new String[] {"main_content", "name" },
new int[] { R.id.item_title, R.id.item_subtitle });
setListAdapter(adapter);
its almost as if there is another listview after each set of textfields but I am not sure how to get rid of it. Please help.
I had to use a seperate layout for the ListView items so only that would repeat and not the main layout.

Categories

Resources