Cant change listview font in listactivity - android

Im very new in android programming, and my task is to load up the items in my shared preference to a listview, and i just want to change the design of my listview, like changing its font color, font size and etc.. I have tried this solution but it doesn't work, please tell me where I did wrong. Thanks..
heres my code in loading the listview in a listactivity
Map<String,?> datakeys = datapref.getAll();
for(Map.Entry<String,?> entry : datakeys.entrySet()){
Log.d("values123",entry.getKey() + ": " + entry.getValue().toString());
lvlist.add(entry.getValue().toString());}
lvadapter = new ArrayAdapter<String>(this, R.layout.mytextview , R.id.text1, lvlist);
//lvadapter = new ArrayAdapter<String> (this,android.R.layout.simple_list_item_1,android.R.id.text1, lvlist);
setListAdapter(lvadapter);
heres the mytextview xml..
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10dip"
android:textColor="#666666"
android:textStyle="italic" />
and my listview xml
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView3"
android:background="#b6fcd5" >
</ListView>
here's the screenshots

You need other constructor of ArrayAdapter :
public ArrayAdapter (Context context, int resource, int textViewResourceId, T[] objects)
so Change this line to :
lvadapter = new ArrayAdapter<String>(this, R.layout.mytextview, lvlist);
To :
lvadapter = new ArrayAdapter<String>(this, R.layout.mytextview , R.id.text1, lvlist);
And mytextview xml :
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
android:paddingTop="2dip"
android:paddingBottom="3dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="10dip"
android:textColor="#666666"
android:textStyle="italic" />
If this doesn't work , then Go with Custom Adapter

one more approach: have a look
String [] yourStringarrayorlist= {"Lionssss","Cats", "yeaaahhh :)"};
ListView listView =new ListView(
getApplicationContext());
listView.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, yourStringarrayorlist) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
TextView textView = (TextView) super.getView(position, convertView, parent);
// if you want to change any specific items color
int textColor = textView.getText().toString().equals("Cats") ? R.color.any : android.R.color.black;
textView.setTextColor(VideoviewActivity.this.getResources().getColor(textColor));
//If you want to change the text color of all use below code
// textView.setTextColor(VideoviewActivity.this.getResources().getColor( R.color.any));
return textView;
}
});
layoutrela.addView(listView);
You can change the textcolor easily. However for just a check i've created ListView through code and added to the layout :) but it works like awesome..
Inspired by : How to change text color of simple list item
not a new answer so i would not expect any vote or so.

Related

Android Spinner arrow closer?

Can I move the arrow somewhat closer to the text, in a easy way? Don't really understand the purpose of this as default when its transparent.
<Spinner
android:id="#+id/spinner_months"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
I have answered a similar question here: https://stackoverflow.com/a/42596698/1260126
This can be achieved by creating a custom layout for the selected spinner item custom_spinner_item.xml. I have added a TextView which displays the currently selected spinner item. The arrow icon is added in an ImageView. You can use any icon. The arrow icon moves depending on the length of the text. In fact you can completely modify the look of your spinner in this layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:orientation="horizontal">
<TextView
android:id="#+id/spinner_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#mipmap/ic_arrow_down"/>
</LinearLayout>
Create a custom spinner adapter and inflate the above view. Also set the text of your selected spinner item from your list by overriding the default getView() method.
public class CustomSpinnerAdapter extends ArrayAdapter<String> {
LayoutInflater inflater;
List<String> spinnerItems;
public CustomSpinnerAdapter(Context applicationContext, int resource, List<String> spinnerItems) {
super(applicationContext, resource, spinnerItems);
this.spinnerItems = spinnerItems;
inflater = (LayoutInflater.from(applicationContext));
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflater.inflate(R.layout.custom_spinner_item, null);
TextView type = (TextView) view.findViewById(R.id.spinner_item_text);
type.setText(spinnerItems.get(i));
return view;
}
}
Then instantiate the CustomSpinnerAdapter class and set it as your spinner's adapter. spinnerList is the list of items to be shown in the spinner.
CustomSpinnerAdapter customSpinnerAdapter = new CustomSpinnerAdapter(getContext(), android.R.layout.simple_spinner_item, spinnerList);
spinner.setAdapter(customSpinnerAdapter);

Can't get view to set elements of LISTVIEW

have problem with 'listView'. I searched everywhere but the problem does not arise none. Let me explain:
I have classic 'listview' in my 'layout':
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/roza"
android:tileMode="repeat">
<ListView
android:id="#android:id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#80ffffff"
android:alpha="200">
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_todos" />
</LinearLayout>
And i create custom list_row xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/list_name"
android:layout_alignParentTop="true"
android:textSize="25dp"
android:layout_alignParentStart="true"
android:layout_alignEnd="#+id/list_rune" />
------------bla bla bla -----------------
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:layout_alignTop="#+id/list_date_update"
android:layout_toLeftOf="#+id/list_rune"
android:layout_toStartOf="#+id/list_rune"
android:id="#+id/textTester" />
</RelativeLayout>
My main class -
public class MainActivity extends ListActivity implements AdapterView.OnItemLongClickListener, SharedPreferences.OnSharedPreferenceChangeListener {
private NordBase dbHelper;
private Cursor cursor;
int rage = list_row; // my custom row
-------------------on create----------------
setContentView(R.layout.activity_main);
this.getListView().setDividerHeight(2);
dbHelper = new NordBase(this);
fillData();
------------------fillData-----------------
cursor = dbHelper.getAll();
startManagingCursor(cursor);
String[] from = new String[] { NordBase.NORD_NAME, NordBase.NORD_DESCRIPTION,
NordBase.NORD_PUNKTS, NordBase.NORD_PUNKTS_CROSS,
NordBase.NORD_DATE_CREATE, NordBase.NORD_DATE_UPDATE, NordBase.NORD_RUNE,
NordBase.NORD_IMPORTANT, NordBase.NORD_SUM };
int[] to = new int[] { R.id.list_name, R.id.list_description, R.id.list_punkts,
R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
R.id.list_rune, R.id.list_important, R.id.list_sum, };
notes = new SimpleCursorAdapter(this,
rage, cursor, from, to);
setListAdapter(notes);
getListView().setOnItemLongClickListener(this);
Alas, I can not get to the items inside list_row, to bind them for comparison operators. for exp: if price == 0, then textview.setvisible = false...
I try LayoutInflater, but he make view element difrent from those in listview.
I try adapter binder class - but i think its no good for that.
Mostly i have an error - can't do that for the "null element"... that case - looks like all i ever do - my program can't findViewById elements in the list_row.
There is one point of contact
int[] to = new int[] { R.id.list_name, R.id.list_description, R.id.list_punkts,
R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
R.id.list_rune, R.id.list_important, R.id.list_sum, };
notes = new SimpleCursorAdapter(this,
rage, cursor, from, to);
So help me Obi-Van Kenobi, and sorry for my dummy ENG, and i'm not sure that i past cod in right way)))
First of all your layout is not having the views which you have mention in the adapter like
R.id.list_description, R.id.list_punkts,
R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
R.id.list_rune, R.id.list_important, R.id.list_sum
put this thing in the layout list_row xml
if you mean by ------------bla bla bla -----------------
this views than ok
also simpleCursorAdapter will not solve your purpose as you want to hide and show the views based on the data in respective views
so my first suggestion Change your layout to linearLayout or tableLayout or other layout as you feel and then do this in Your CustomAdapter class
like
class MyAdapter extends BaseAdapter
{
#Override
public View getView(int position, View convertView, ViewGroup parent) {
//here you infalteYour layout and and do your logical thing
//you can take your data out from cursor and store in the arrayList<YourModelClass> type and have better control
}
}
otherwise in relative layout just fill the data don't hide it so it may work but if you do hiding of some views chances are high it will not solve your problem
and typo mistake is there i hope so like
int[] to = new int[] { R.id.list_name, R.id.list_description, R.id.list_punkts,
R.id.list_punkts_cross, R.id.list_date_create, R.id.list_date_update,
R.id.list_rune, R.id.list_important, R.id.list_sum, };
//here no item but still u have written "," after R.id.list_sum this like "R.id.list_sum, "
notes = new SimpleCursorAdapter(this,
rage, cursor, from, to);

Text color by programmatically created grid view content

I understand how to do this in design view, but I'm not sure how to create a style based on programmatically created elements. I followed a tutorial and here's where I'm at:
I have a grid view which is populated by a string array as in the code below:
...
gridView = (GridView) findViewById(R.id.gridView);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strData);
gridView.setAdapter(adapter);
Each element of the string array populates the grid just fine.
How would I set the text color of the items added to the gridView?
2 solutions: dynamically or with a custom layout.
Dynamically: you can set a text color by using setTextColor(...) when you Override the getView() method in your ArrayAdapter, something like this:
gridview.setAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, strData) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView text = (TextView) view.findViewById(android.R.id.text1);
text.setTextColor(getResources().getColor(R.color.my_color));
return view;
}
});
Custom Layout: this is the simplest way, build a custom layout as:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tv"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:textColor="#color/my_color"
android:background="#drawable/my_background"
android:padding="5sp"
android:singleLine="true"
android:gravity="center" />
Then, set it to your adapter:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_layout_above, strData);
Even if, you wanted this dynamically, I'm not sure but I think it's better to do with a custom layout.
Hope this helps.
By creating a custom layout and supplying it to your ArrayAdapter. Currently you are supplying the default layout android.R.layout.simple_list_item_1. Create a new layout xml like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flFragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
And style it however you want.
You can use it like this:
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.custom_layout, R.id.tvText, strData);

How to put an image inside a listAdapter

I have an Activity and a xml, and I want to put an image inside the listAdapter that I have.
My xml:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical|center_horizontal"
android:padding="15dp"
android:textSize="18sp"
android:textStyle="italic" >
</TextView>
My listAdapter in the Activity:
String adapter[] = new String[1];
adapter[0] = "Test";
setListAdapter(new ArrayAdapter<String>(this, R.layout.activity_main, adapter));
This is just part of the code, actually I have an for() making many lines in the list, my ideia is put in which line a image with diferent colors, like labels according the value inside in which line.
Is that possible?
Thank You.
You need to override the method getView in the ArrayAdapter you created
public View getView(int position, View convertView, ViewGroup parent) {
// Customize the view
}
Here is a good tutorial about this topic:
http://www.vogella.com/articles/AndroidListView/

How to change the Spinner font color?

I'm having an issue with the Droid X phones where users say that the font color turns out to be white in the spinner, making it invisible unless the users highlight the items. No other phones seem to have this problem. I was going to try to force the font to be black to see if that helps. How can I do that?
Here's how I'm currently populating the spinner. It seems like the simple_spinner_item is broken on Droid X's.
String spin_arry[] = new String[str_vec.size()];
str_vec.copyInto(spin_arry);
ArrayAdapter adapter =
new ArrayAdapter(this,android.R.layout.simple_spinner_item, spin_arry);
I'm going to use Spinner project sample from Android SDK for next code examples.
Code:
First, you need to create you custom adapter which will intercept the creation of views in drop down list:
static class CustomArrayAdapter<T> extends ArrayAdapter<T>
{
public CustomArrayAdapter(Context ctx, T [] objects)
{
super(ctx, android.R.layout.simple_spinner_item, objects);
}
//other constructors
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent)
{
View view = super.getView(position, convertView, parent);
//we know that simple_spinner_item has android.R.id.text1 TextView:
/* if(isDroidX) {*/
TextView text = (TextView)view.findViewById(android.R.id.text1);
text.setTextColor(Color.RED);//choose your color :)
/*}*/
return view;
}
}
Then you create adapter in your code like this:
String [] spin_arry = getResources().getStringArray(R.array.Planets);
this.mAdapter = new CustomArrayAdapter<CharSequence>(this, spin_arry);
Explanation:
Because CustomArrayAdapter knows that we use android's built-in layout resource, it also knows that text will be placed in TextView with id android.R.id.text1. That's why it can intercept the creation of views in drop down list and change text color to whatever color is needed.
Screenshot:
Simple and Crisp ...
private OnItemSelectedListener OnCatSpinnerCL = new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int pos,
long id) {
((TextView) parent.getChildAt(0)).setTextColor(Color.BLUE);
((TextView) parent.getChildAt(0)).setTextSize(5);
}
public void onNothingSelected(AdapterView<?> parent) {
}
};
write a R.layout.simplespinneritem:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:singleLine="true"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
The ID is android:id="#android:id/text1", set the color of font and background.
ArrayAdapter adapter =
new ArrayAdapter(this,packagename.R.layout.simple_spinner_item, spin_arry);
public class ee extends Activity{
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.ww);
addListenerOnSpinnerItemSelection();
}
public void addListenerOnSpinnerItemSelection(){
ArrayList<String> array = new ArrayList<String>();
array.add("item0");
Spinner spinner1;
ArrayAdapter<String> mAdapter;
spinner1= (Spinner) findViewById(R.id.spinner2);
spinner1= new ArrayAdapter<String>(this, R.layout.spinner_item, array);
spinner1.setAdapter(mAdapter);
}
}
and in xml res/layout add new xml file: type layout, spinner
(in spinner_item.xml)
<?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="fill_parent"
android:gravity="top"
android:singleLine="true"
android:textColor="#00f0ff" />
To add to sasad's reply, make a copy of that file, which you can find in your Android folder, in your project, change the text color of the TextView in that file, and use that layout while initializing the Adapter instead of android's.
You could try this approach too wherein you add 2 new Layout Resource Files
Custom_spinner_list_item
Custom_spinner_dropdown_item
and use them in the code .
String spin_arry[] = new String[str_vec.size()];
str_vec.copyInto(spin_arry);
ArrayAdapter adapter =
new ArrayAdapter(this,R.layout.custom_simple_spinner_item, spin_arry);
adapter.setDropDownViewResource(R.layout.custom_spinner_dropdown_item);
custom_spinner_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:fontFamily="#font/roman"
android:singleLine="true"
android:textAlignment="inherit"
android:textColor="#color/black"
android:textSize="14sp">
</TextView>
custom_spinner_dropdown_item.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text1"
style="?attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="?attr/dropdownListPreferredItemHeight"
android:ellipsize="marquee"
android:fontFamily="#font/roman"
android:singleLine="true"
android:textAlignment="textStart"
android:textColor="#color/black"
android:textSize="14sp">
</TextView>
Happy Coding !! :)
make your own layout xml file, and give a android:textColor="#000" for black text
Here is more appropriate way guys,
First find the "simple_spinner_item.xml" file in your system,
Follow the below path,
C:\Users[username]\AppData\Local\Android\sdk\platforms[android-23]\data\res\layout
Now copy the content of "simple_spinner_item.xml" file
Second create the custom_spinner.xml file in your project res\layout folder
and paste the copied content in recently created file
Here is the sample:
res\layout\custom_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView android:textAlignment="inherit"
android:ellipsize="marquee"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:singleLine="true"
android:textColor="#color/dark_gray"
style="?android:attr/spinnerItemStyle"
android:id="#android:id/text1" xmlns:android="http://schemas.android.com/apk/res/android"/>
Here is the set adapter code:
Spinner ddlArea = (Spinner) findViewById(R.id.ddlArea);
ddlArea.setAdapter(new ArrayAdapter<String>(this, R.layout.custom_spinner, areaList));
Where areaList is the List
Thanks,
Ejaz Waquif

Categories

Resources