Displaying multiple lines of text on a listview - java

I have been reading up on simple_list_item_2 and other standardized ways of displaying multi line text on a listview but none have given me what I want. As of now I am using a custom layout for the listview, but I lack the knowledge (after reading multiple articles) on how to customize each row in the listview to display it's own line of large text, tooltip (small text), and picture.
Here is the code I am using to initalize the listview and set the adapter
//List View
final String[] itemname = {
"More Rockets",
"Better Lasers",
"Super Shield",
"Super Defense"
};
final String[] itemtip = {
"+1 attack",
"+5 attack ",
"+1 defense",
"+5 defense"
};
final ListView upgradeList = (ListView) findViewById(R.id.upgradeListView);
upgradeList.setAdapter(new ArrayAdapter<String>(
this, R.layout.program_list,
R.id.Itemname, itemname));
program_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="80dp"
android:background="#drawable/buttontemplate">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Item Name"
android:id="#+id/Itemname"
android:textSize="30sp"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/imageView"
android:layout_marginLeft="15sp" />
<ImageView
android:layout_width="30sp"
android:layout_height="30sp"
android:id="#+id/imageView"
android:src="#drawable/heart"
android:layout_marginLeft="20dp"
android:layout_alignBottom="#+id/Itemname"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Tooltip:"
android:id="#+id/toolTip"
android:layout_centerVertical="true"
android:layout_toEndOf="#+id/Itemname"
android:layout_marginLeft="20dp" />
</RelativeLayout>
Here was my idea for puting another line of text in the listview but it is not the right amount of arguments
upgradeList.setAdapter(new ArrayAdapter<String>(
this, R.layout.program_list,
R.id.Itemname, itemname, R.id.toolTip, itemtip));
Any help is very much appreciated!
-Kelton

First make a POGO class with field String itemname,String itemtip then generate the getter and setter method , then from the activity class make an arraylist of this pogo class i.e
ArrayList<MyPOGO> arraylist =new ArrayList();
MyPOGO myPogo1=new MyPogo("More Rockets","+1 attack");
MyPOGO myPogo2=new MyPogo("Better Lasers","+5 attack ");
MyPOGO myPogo3=new MyPogo("Super Shield","+1 defense");
MyPOGO myPogo4=new MyPogo("Super Defense", "+5 defense");
arraylist.add(myPogo1);
arraylist.add(myPogo2);
arraylist.add(myPogo3);
arraylist.add(myPogo);
Here your arraylist size is 4 .
Now make a baseadapter or recyler view pass this arraylist

Related

setVisibility(View.GONE) shrinking display size

Android version 11.0.4
I am trying to use a ListView to display a series of elements with different types of information to display in the same list using a custom ListAdapter.
To make the same ListAdapter able to display different types of information I'm creating a custom XML template with all possible elements that I want to display and hiding the ones I don't want to show programmatically.
However, when I hide the extra elements I don't want to show the size of the view displaying them shrinks.
When the extra elements are shown
When the extra elements are gone
The code that hides the elements:
TextView catagory = (TextView) convertView.findViewById(R.id.title);
TextView time1 = (TextView) convertView.findViewById(R.id.time1);
TextView time2 = (TextView) convertView.findViewById(R.id.time2);
TextView location = (TextView) convertView.findViewById(R.id.location);
TextView description = (TextView) convertView.findViewById(R.id.description);
View separator1 = (View) convertView.findViewById(R.id.separator_vertical1);
View separator2 = (View) convertView.findViewById(R.id.separator_horizontal1);
View separator3 = (View) convertView.findViewById(R.id.separator_vertical2);
View separator4 = (View) convertView.findViewById(R.id.separator_horizontal2);
catagory.setText(item.name);
Class type = item.getType();
if (EventItem.class.equals(type)) {
time1.setText(String.valueOf(((EventItem)item).timeStart));
time2.setText(String.valueOf(((EventItem)item).timeEnd));
location.setText(((EventItem)item).location);
description.setText(((EventItem)item).description);
} else if (TaskItem.class.equals(type)) {
time1.setText(String.valueOf(((TaskItem)item).timeDue));
description.setText(((TaskItem)item).description);
//hide unused elements
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
} else if (ReminderItem.class.equals(type)) {
time1.setText(String.valueOf(((ReminderItem)item).time));
//hide unused elements
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
separator4.setVisibility(View.GONE);
description.setVisibility(View.GONE);
} else if (SubjectItem.class.equals(type)) {
description.setText(((SubjectItem)item).description);
} else if (NoteItem.class.equals(type)) {
description.setText(((TaskItem)item).description);
//hide unused elements
separator1.setVisibility(View.GONE);
time1.setVisibility(View.GONE);
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
} else if (ContactItem.class.equals(type)) {
//hide unused elements
separator1.setVisibility(View.GONE);
time1.setVisibility(View.GONE);
separator2.setVisibility(View.GONE);
location.setVisibility(View.GONE);
separator3.setVisibility(View.GONE);
time2.setVisibility(View.GONE);
}
The XML template:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rectangle">
<TextView
android:id="#+id/title"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:textAlignment="textStart"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_vertical1"
android:layout_width="1dp"
android:layout_height="23dp"
android:layout_toStartOf="#id/time1"
android:background="#android:color/white"/>
<TextView
android:id="#+id/time1"
android:layout_width="94dp"
android:layout_height="wrap_content"
android:layout_toStartOf="#id/title"
android:layout_alignParentEnd="true"
android:textAlignment="center"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_horizontal1"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/title"
android:background="#android:color/white"/>
<TextView
android:id="#+id/location"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#id/separator_horizontal1"
android:textAlignment="textStart"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_vertical2"
android:layout_width="1dp"
android:layout_height="23dp"
android:layout_toStartOf="#id/location"
android:layout_below="#id/separator_horizontal1"
android:background="#android:color/white"/>
<TextView
android:id="#+id/time2"
android:layout_width="94dp"
android:layout_height="wrap_content"
android:layout_toStartOf="#id/location"
android:layout_below="#id/separator_horizontal1"
android:layout_alignParentEnd="true"
android:textAlignment="center"
android:textSize="16sp"
android:textColor="#ffffffff"/>
<View
android:id="#+id/separator_horizontal2"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_below="#id/location"
android:background="#android:color/white"/>
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/separator_horizontal2"
android:textAlignment="textStart"
android:textSize="20sp"
android:textColor="#ffffffff"/>
The ListView the elements are displayed in:
<ListView
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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context=".MainActivity"
android:id="#+id/text_overview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:textAlignment="center"
android:textSize="20sp"
android:textColor="#ffffffff"
android:divider="#android:color/transparent"
android:dividerHeight="8dp"
android:headerDividersEnabled="true"
android:footerDividersEnabled="true"
android:scrollbars="none"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
The answer to your question is to use View.INVISIBLE
However, when I hide the extra elements I don't want to show the size of the view displaying them shrinks.
View.GONE: This view is invisible, and it doesn't take any space for layout purposes.
View.INVISIBLE: This view is invisible, but it still takes up space for layout purposes.
Here's the link of Documentation.
Use View.INVISIBLE which takes up space and invisible the view.
while you are using View.GONE which doesn't takes up space for the view and invisible the view.
For more description, you can go with:https://developer.android.com/reference/android/transition/Visibility
I eventually got it working, not sure how but setting each element as visible before going into the if statements stoped the shrinking of the view window. It also solved the issue of some list items missing elements that shouldn't have been hidden. I can only assume some resources were being reused somewhere and arriving pre-hidden.

Listview items not displaying the retrieved columns from database vertically

Using simple cursor adapter I retrieve 5 columns from a table using cursor query and display them using a listview. Everything goes well but the columns retrieved are displayed horizontally in the listview. I want them to be displayed one below the other like:
TX_UID
TX_NAME
TX_AMOUNT
TX_PARTICULARS
AND NOT LIKE (Displayed as of now):
TX_UID TX_NAME TX_AMOUNT TX_PARTICULARS
String[] from = new String[]{ vivzHelper.TX_UID,
vivzHelper.TX_NAME,vivzHelper.TX_AMOUNT,vivzHelper.TX_PARTICULARS};
int[] to = new int[]
{R.id.textView,R.id.textView2,R.id.textView3,R.id.textView5 };
SimpleCursorAdapter cursorAdapter;
cursorAdapter = new SimpleCursorAdapter(getBaseContext(),
R.layout.customgetalltxrowforeditordelete , c , from , to , 0);
thislist.setAdapter(cursorAdapter);
thislist.setAdapter(cursorAdapter);
XML Layout:
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="35dp"
android:fastScrollEnabled="false"
android:divider="#FFCC00"
android:dividerHeight="2px"
android:smoothScrollbar="true"
android:background="#drawable/my_selecter"
/>
columns retrieved are displayed horizontally in the listview. I want
them to be displayed one below the other
Problem is related to customgetalltxrowforeditordelete layout instead of ListView.
To show TextView's in ListView row Vertically. use LinearLayout with orientation attribute to vertical
In your customgetalltxrowforeditordelete xml file you need to do something like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/badge13"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/textView2"
android:layout_below = "#id/textView"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/textView3"
android:layout_below = "#id/textView2"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#id/textView4"
android:layout_below = "#id/textView3"
/>
you need to use layout_below attribute in your TextView providing top view's id in attribute.

Two Spinners and extracting strings from each. Android

My app offers guitar players a page they can define a song in terms of Name, chords used, and relevant scales.
My app has two spinners that have parts of a fully formed guitar chord, and another spinner for 'split' chords, so three spinners in all. They are working nicely.
In the Activity, one spinner holds a list of guitar chord primitives (i.e. 'C#'), and the second spinner holds a list of chord modifiers (i.e. 'maj7').
Then, from each of the spinners, I need to have the two strings combined into a textView or list which will hold something like "Gm, A#, C#maj7..." which are the user's chosen chords for performing the song, and they need to be saved for later use in a performance page.
I need to be able to store the combined strings from spinner1 and spinner2 to get 'C#maj7'. This algorithm is necessary since the list of chords would number in about 40,000 list items if you did it in just one spinner, so the 2-spinner method makes sense, I believe.
What I want know is:
How to get the strings out of the spinners and into a textView or whatever 'box' you might suggest.
How to combine the strings, which I can probably figure out on my own...
I've looked over hundreds of articles here in stackoverflow, and most of them are outdated or irrelevant, and often don't work with current code. Some are several years old, and usually codes are not quite the same it seems.
Here is my activity_new_song.xml layout.
<?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:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
tools:context="com.edsets.gigmaster.NewSong">
<!-- This is the title "Song Name" -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/new_song_name"
android:id="#+id/textView3"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25sp"
android:textColor="#0000FF"
android:textStyle="bold"/>
<!-- The user types in the song name -->
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignTop="#+id/textView3"
android:layout_toRightOf="#+id/textView3"
android:layout_toEndOf="#+id/textView3"
android:layout_marginLeft="29dp"
android:layout_marginStart="29dp"
android:width="400dp"
style="#style/Base.Widget.AppCompat.EditText"
android:textStyle="bold"
android:capitalize="words"
android:editable="false"
android:textSize="25sp"/>
<!-- This is the grid which holds the layout elements -->
<!-- This is the Title "Your Chords" -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Your Chords:"
android:id="#+id/textView4"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="39dp"
android:textColor="#0000FF"
android:textStyle="bold"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Primary"
android:id="#+id/textView5"
android:layout_alignTop="#+id/textView4"
android:layout_alignLeft="#+id/spinner"
android:layout_alignStart="#+id/spinner"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Modifiers"
android:id="#+id/textView6"
android:layout_alignTop="#+id/textView7"
android:layout_toLeftOf="#+id/textView7"
android:layout_toStartOf="#+id/textView7"
android:layout_marginRight="50dp"
android:layout_marginEnd="50dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Split Chords"
android:id="#+id/textView7"
android:layout_above="#+id/spinner3"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"/>
<Spinner
android:layout_width="80dp"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:spinnerMode="dropdown"
android:layout_below="#+id/textView5"
android:layout_alignLeft="#+id/editText"
android:layout_alignStart="#+id/editText"
android:entries="#array/PrimaryChords"/>
<Spinner
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="#+id/spinner2"
android:layout_alignBottom="#+id/spinner"
android:layout_alignLeft="#+id/textView6"
android:layout_alignStart="#+id/textView6"
android:spinnerMode="dropdown"
android:entries="#array/Modifiers"/>
<Spinner
android:layout_width="90dp"
android:layout_height="wrap_content"
android:id="#+id/spinner3"
android:layout_alignBottom="#+id/spinner2"
android:layout_alignLeft="#+id/textView7"
android:layout_alignStart="#+id/textView7"
android:entries="#array/SplitChords"/>
</RelativeLayout>
Here is my class "NewSong.java"
package com.edsets.gigmaster;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class NewSong extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_new_song);
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.PrimaryChords, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_new_song, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Fast answers, hopefully enough:
How to get the strings out of the spinners and into a textView or whatever 'box' you might suggest.
You can get the selected item by:
Object selectedItem = youtSpinner.getSelectedItem();
And cast it to whatever class they are. For instance, if your items are Strings, this should be fine to get the items:
String selectedItem = (String) yourSpinner.getSelectedItem();
How to combine the strings, which I can probably figure out on my own...
Just cat them:
String result = string1 + string2;
EDIT:
Try with spinner adapter:
yourSpinner.getAdapter().getItem(position)

Selected text alignment for Android spinner

I am trying to align selected text of a Spinner in Android. It should align to the matching indentation of the other TextView and EditText in the layout.
What I have in my layout:
http://i.imgur.com/Y5sgbsN.png
What i want to do:
http://i.imgur.com/viVHpiC.png
My Layout code:
<Spinner
android:id="#+id/spinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/TextView01"
android:layout_below="#+id/TextView01"
android:entries="#array/bank_arrays"
android:prompt="#string/bank_prompt"
android:fontFamily="arial,helvetica"
android:hint="#string/bank_prompt"
android:spinnerMode="dialog"
android:ellipsize = "none"/>
Please help me out with this UI change.
Thanks
Update 1:
Parent Layout code:
<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:layout_marginBottom="14.5sp"
android:layout_marginLeft="14.5sp"
android:layout_marginRight="14.5sp"
android:layout_marginTop="14.5sp"
tools:context=".RegistrationActivity" >
This is the spinnerinflate.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/spinnertext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#00f5ff"
android:textSize="#dimen/text_size"
android:text="Text Here" >
</TextView>
In your code you can set the custom Layout for spinner as
MyAdapter myAdapter = new MyAdapter(this, R.layout.spinnerinflate,
arraylist);
spinner.setAdapter(myAdapter);
You can use SimpleAdapter as well if you don't need to have your own adapter as below
ArrayAdapter<String> simpleadapter = new ArrayAdapter<String>(YourActivityName.this, R.layout.spinnerinflate, getResources().getStringArray(R.array.yourarray));
spinner.setAdapter(simpleadapter);
By this inflate you can do whatever formatting you want. Please let me know if it helps.
you can use this lines
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="0dp"
android:textDirection="ltr" />
and you can increase padding from left to more space

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