Below is code of my girdview. i need to change font size of the data. i aslo need to change default selection colour which is orange. please help i am new to android development.
String[] tes={"AAA","BBB","CCC"};
ArrayAdapter<String> aa = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
tes );
gv.setAdapter(aa);
gv.setOnItemClickListener(this);
You have to change the font size in the baseadapter class for the gridview.
Like this:
public View getView(int position, View convertView, ViewGroup parent) {
TextView tv;
if (convertView == null) {
tv = new TextView(context);
tv.setLayoutParams(new GridView.LayoutParams(100, 80));
tv.setTextSize(20); //text size in gridview
}
The gridview just show whatever the
gridview.setAdapter(new MyAdapter (thi));" //MyAdapter class is where the getView method run
sets the setAdapter to contain.
You can try to achieve this by changing styles of your widgets. Take a look a this
post to learn to apply styles and themes. Also you'll find description of all android styles and themes there. Hope this helps.
You can change it in the XML file, if you write android:textColor="#000000" where 000000 is the code of your color hexcode what you want.
a little bit too late but for others who are looking for it will work :
first you create this a simple_textview.xml(put it a name)
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:shadowColor="#color/grisActionBar"
/>
then in yout activity or fragment you set the adapter with the created textview.xml
gvMain = (GridView)v.findViewById(R.id.gridView);
gvMain.setAdapter(new ArrayAdapter<String
(getActivity(),R.layout.simple_textview,data));
Related
I am trying to figure out how I can change the text color of android.R.layout.simple_list_item_multiple_choice
I know you can create your own listview with checkboxes but I really do not want to have to do that as the android.R.layout.simple_list_item_multiple_choice works perfectly, I would just like to know how to change the Text color.
I have come accross this question here but I do not understand how I can use the most voted up answer.
How can I override android.R.layout.simple_list_item_multiple_choice textview so I can change the color of it. Thank you.
You can change color easily.
You just have to override the getView() method.
And inflate the android.R.layout.simple_list_item_multiple_choice in particular view. now you can access the whole template.
So that you can get the TextView used in this template by finding using id android.R.id.text1. after that you can change the behavior of textview.
ArrayAdapter<String> list = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_multiple_choice, your_data_container) {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = LayoutInflater.from(InvitationFlowActivity.this).inflate(android.R.layout.simple_list_item_multiple_choice, null);
TextView tv = (TextView) v.findViewById(android.R.id.text1);
tv.setTextColor("Your_color");
}
};
And if you are using custome adapter then you can also override the same getView() method and you can customize it as per your requirement.
Just make custom layout - simple_list_item_multiple_choice.xml like this -
<CheckedTextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="fill_parent"
android:layout_height="?android:attr/listPreferredItemHeight"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center_vertical"
android:checkMark="?android:attr/listChoiceIndicatorMultiple"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:textColor="#FF0000"
/>
and use it in place of android.R.layout.simple_list_item_multiple_choice.xml
There are two ways to do this.
1. Override the getView method
2. Use custom layout instead of standard layout.
No. 2 is better as you have more control on the same and you can edit at your ease.
Alt+Click will clone the required layout to your res/layout folder.
Make changes to the new layout and use it in the adapter.
ArrayAdapter aa = new ArrayAdapter(getApplicationContext(),R.layout.text_change,listItems);
listView.setAdapter(aa);
Here listItems is the String array of list.
text_change.xml is the updated layout xml
listView is your listview in the main layout.
In order to dynamically update color of content use the following code
AdapterView.OnItemClickListener itemClickListener = new AdapterView.OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if(position==0)
{
//Intent intent = new Intent(CoffeeShop.this,DrinkCategoryActivity.class);
//startActivity(intent);
TextView txtV = (TextView)view;
txtV.setTextColor(Color.BLUE);
}
else
{
TextView txtV = (TextView)view;
txtV.setTextColor(Color.YELLOW);
Toast toast = Toast.makeText(getApplicationContext(),(CharSequence)"These Methods are yet to be made public", Toast.LENGTH_SHORT);
toast.show();
}
}
};
ListView listView = (ListView)findViewById(R.id.list_opt);
listView.setOnItemClickListener(itemClickListener);
Adding onclicklistner for the view will allow updating the color of the text upon clicking. You can add respective logic as to which color should the text be displayed.
I need to implement function of changing the application theme up on button click.
I have already changed successfully style of buttons, edittexts and textviews. But faced the problem of changing style for Spinner.
The only successful change is change of background color:
spinnerRoutes.setBackgroundResource(R.drawable.dark_theme_spinner_background);
BUT only background of title row changed. I need also to change textColor of rows and items, and background of spinner rows.
I have already tried alot of similar stackoverflow solutions but none of them worked.
Is it even possible to change style of spinner programatically?
If u want to give customize the drop down menu of the spinner u need to override the getView function in adapter class. And also if u want to control the text that appears on the Spinnner u need to this spinner.onItemSelectedListener() and modify the text view in the call back method
sample code for customizing spinner drop down menu:
#Override
public View getDropDownView (int position, View convertView, ViewGroup parent) {
/**The Adapter's view to be supplied for the spinner when constructing a spinner. */
View view = super.getView(position, convertView, parent);
TextView listView = (TextView) view.findViewById(android.R.id.text1);
listView .setBackgroundColor(Color.parseColor(fontBack_BgnColor));
listView .setHeight((int) heightPixels);
listView .setGravity(Gravity.CENTER);
listView .setTextSize(TypedValue.COMPLEX_UNIT_PX, 20);
listView .setTextColor(Color.parseColor(fontColor));
listView .setText(super.getItem(position));
return view;
}
Customizing spinner text(text that appears on top of the spinner):
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
((TextView) view).setTextSize(TypedValue.COMPLEX_UNIT_PX, 25);
((TextView) view).setTextColor(Color.parseColor(spinnerTextColor));
}
The other guys are right when it comes to fully customizing the items in the spinner.
But if the only thing you need to change is the style of the text in the items (meaning you still want a text view but with say different font, color, gravity) then you only need to pass the adapter a custom item layout:
This is how your adapter should look like:
spinner.setAdapter(new ArrayAdapter<String>(getActivity(), R.layout.custom_item, data)
where custom_item is an xml layout file that you create as follow:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:gravity="center"
android:textColor="#android:color/red"
android:paddingStart="?android:attr/listPreferredItemPaddingStart"
android:paddingEnd="?android:attr/listPreferredItemPaddingEnd"
android:minHeight="?android:attr/listPreferredItemHeightSmall" />
Just make sure that the text view is the one and only view in that xml, and that its id is as specified above, so the default adapter can recognize it.
Hope that helped.
How to set style on spinner ? I have style with font size and family for custom edit text class and I need same for spinner. I tried to add style tag in spinner xml but it is ignored, I tried in adapter but it doesn't work.
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, tempList) {
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
Typeface externalFont = Typeface.createFromAsset(getAssets(), "fonts/CANDARA.TTF");
((TextView) v).setTypeface(externalFont);
return v;
}
}
Instead of using the simple spinner layout android.R.layout.simple_spinner_item, create your own layout file containing a TextView and style it the way you'd like. Then, pass a reference to that layout when you instantiate the ArrayAdapter.
You are passing the default android layout for the spinner items, instead create your own layout like:
<RelativeLayout
android:id="#+id/rel"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/txtSpinnerItem"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
stlye="#style/txtViewStyleHere"/>
</RelativeLayout>
Pass this layout to the adapter, in that case, you will have to create your own adapter class to access the textView.
you want to add customize spinner
for that see these two link it might be help you .
Link 1 normal as you want just changing text and all that,
and for hover effect on your spinner look this link . Link2
The text on my spinners is white, and I have no idea why.
This is my xml, nothing special
<Spinner
android:id="#+id/spinner_date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
And my code
dateSpinner = (Spinner) findViewById(R.id.spinner_date);
selectedDate = calendar.getTime();
List<String> list = new ArrayList<String>();
list.add(formatter.format(selectedDate));
dateAdapter = new ArrayAdapter<String>(mContext,
android.R.layout.simple_spinner_item, list);
dateSpinner.setAdapter(dateAdapter);
What could be the reason that my text is displayed in white?
EDIT:
I've found the reason, I replaced the mContext parameter which was set in my onCreate.
mContext = getApplicationContext();
Now I use d
ateAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, list);
and it works.
I have same problem and have found the answer. You dont use application context, instead, just use getActivity() (if you are in fragment) or this (if you are in activity), it will work
dateAdapter = new ArrayAdapter<String>(**this**,
android.R.layout.simple_spinner_item, list);
I solved this problem using
getBaseContext()
instead of
getApplicationContext()
i change it from
new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, some_list);
to
new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, list);
it's fixed, although i don't want to use "this"
I've changed the text color of that spinner's textview without creating a new layout. I know it's been a long time but it's what worked for me, just thought of sharing it. Best part is you can use it on any default adapter.
Here's the code : (this for Activity & requireActivity for Fragments)
1) Java
arrayAdapter = new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,groups){
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View view = super.getView(position, convertView, parent);
TextView listItem = view.findViewById(android.R.id.text1);
listItem.setTextColor(Color.WHITE);
listItem.setTextSize(20);
listItem.setElevation(18);
return view;
}
};
2) Kotlin
arrayAdapter = object : ArrayAdapter<String>(requireActivity(), android.R.layout.simple_spinner_item, spinnerCategoriesList) {
override fun getView(position: Int, #Nullable convertView: View?, parent: ViewGroup): View {
val view = super.getView(position, convertView, parent)
val listItem = view.findViewById<TextView>(android.R.id.text1)
listItem.setTextColor(Color.BLACK)
listItem.textSize = 16f
return view
}
}
I also had same problem it was because of my application theme. Which I solved by replacing the
android.R.layout.simple_spinner_item
with
android.R.layout.simple_list_item_1
in my ArrayAdapter. I hope this may solve your problem
Maybe you have a white android:textColor="#android:color/white" attribute in your simple_spinner_item.xml in the layout folder of your project.
Better use a custom spinner item layout with a good android:textColor="#android:color/COLOR_YOU_WANT_TO_USE" attribute.
I assume you have created your own TextView for your Spinner, e.g.
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#color/white"
android:padding="5dip"
/>
and glued it to your Adapter via a call like that
String[] spinnerItems = getResources().getStringArray(R.array.my_array);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spinner_text, spinnerItems);
This should result in having the selection-text of your spinner including its items within the dropdown view painted in white. Now the background of your dropdown view will be influenced by your app theme, which in most cases, will lead your white text to be rendered on a white background. To avoid this android allows you to set a resource for your spinner dropdown view. You can set your own view or simply use the default drop-down view, which will overwrite your custom textview within the dropdown menu through the call
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
The complete code could look like this
this.spinner = findViewById(R.id.spinnerView);
String[] spinnerItems = getResources().getStringArray(R.array.my_array);
ArrayAdapter<String> adapter = new ArrayAdapter<>(this, R.layout.spinner_text, spinnerItems);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
this.spinner.setAdapter(adapter);
You can easily style the Spinner. Use this in style.xml :
<style name="SpinnerThemeLight" >
<item name="android:colorBackground">#color/black</item>
<item name="android:textColorPrimary">#color/black</item>
<item name="android:textColorSecondary">#color/black</item>
<item name="android:textColorTertiary">#color/black</item>
<item name="android:textColorPrimaryDisableOnly">#color/black</item>
</style>
In the above xml file, I have simply given black color for time sake. Just play with it and sort out your preferred color.
Define the Spinner in the activity.xml as follows :
<android.support.v7.widget.AppCompatSpinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:entries="#array/countrys"
android:spinnerMode="dropdown"
android:theme="#style/SpinnerThemeLight"/>
I have a android spinner. I want to change the size of the spinner text (selected text from the dropdown) run time. I have a button on whose onCLick event I want to do the size change of few things. All other texts are changing by setTextSize() but there are issues with spinner. I have taken the reference of the textview used in getView() of my custom spinner adapter. I apply setTextSize() on it. The first Time it changes the size of he spinner but by again pressing the button ever thing (other textViews on the screen) change their size but not the spinner. Please help..
Regards,
Krishna
try this code
Spinner sp_state = new Spinner(this);
RelativeLayout.LayoutParams lp = new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
sp_state.setLayoutParams(lp);
ArrayAdapter<String> state_Adapter= new ArrayAdapter<String>(getParent(),android.R.layout.simple_spinner_item,ARRLIST_STATE){
public View getView(int position, View convertView,ViewGroup parent) {
View v = super.getView(position, convertView, parent);
((TextView) v).setTextSize(12);
((TextView) v).setTextColor(Color.WHITE);
return v;
}
};
state_Adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sp_state.setAdapter(driver_state_Adapter);
If you are using customize Adapter.Then you have to use one Base Adapter class like in List View.The xml file you inflate, can be used as you want.You can increase size there
Edited
Refer to this link for more details