Customized spinner - java

I have added a spinner in an application which gives the package information in only a word or couple of word. it seems like regular dropdown menu.
I tried to make changes in simple_spinner_dropdown_item.xml file but it does not help and also it is having a radio button which i dont require.
I am done with handling functionality and only needed to change design of that dropdown menu of spinner.
for that i am using checkedTextView like:
service_provider_spinner.setPopupBackgroundResource(R.drawable.ser_menu);
now i don't want that layout rather i like to add a background for each item and also want to set text padding and margin and all that. How can i do this in java file specificly and also in XML.

You can customize the spinner_layout and spinner_dropdown_layout by simply creating an xml layout for both containing only TextView like this as shown.
<?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="wrap_content"
android:gravity="center"
/>
And you can apply this xml to your adapter as shown below:
ArrayAdapter<String> adapter= new ArrayAdapter<String>(
getActivity(), R.layout.your_spinnerLayout, yourArray);
adapter.setDropDownViewResource(R.layout.your_spinner_dropdown_layout);
spinner.setAdapter(adapter);

Related

there is some way to make my own item in xml?

i work on android-studio project, and my question is about if
there is some way i can define in xml type of some item that contain
few edit text and buttons, and open listview that contain the item i create?
somthing like :
<item
<Editext(some setting)/>
<Edittext 1(some setting)/>
<Button(some setting)/>
/>
and then some adapter that adjust or something like that, that i can add to ListView.
i saw in youtube some videos that try to explain that but i get stock.. i dno't really get this.
This took my a while to figure out, but it works.
Create your Layout file just however you want. Save it as res/layout/something.txt. Example:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:text="hello"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:id="#+id/text1"/>
<TextView
android:text="world"
android:layout_height="wrap_content"
android:layout_width="150dp"
android:id="#+id/text2"/>
</LinearLayout>
Use it in your code as follows:
LinearLayout mainLinearLayout = new LinearLayout(getApplicationContext());
// inflate your container
View view = inflater.inflate(R.layout.something, null);
// you can make changes to the elements like this:
TextView txt1 = (TextView) view.findViewById(R.id.text1);
TextView txt2 = (TextView) view.findViewById(R.id.text2);
view.setTag(tag);
// add to main layout
mainLinearLayout.addView(view);
Where tag is an unique integer to identify the View. You don't need to set the tag if you're using the container only once.
I didn't use an Adapter when I made this. I just used a ScrollView to make a continous list of entries. Change this to your needs.

Android: Default layout for listitem containing title and subtitle

I have made a listitem, containing a title and a subtitle. Looking to the result, I think it doesn't look really professional. So that's why I'm asking.
The listitem I'm looking for is pretty common (it's used in a lot of apps i.e. Android's default settings menu and it's also shown when you add a listview in the graphical layout editor tab in eclipse).
So my question is: Where can I find a default layout for a listitem with a title and a subtitle?
Resource id is android.R.layout.simple_list_item_2
Upper text line has id android.R.id.text1 and lower one - android.R.id.text2
Layout is located in the <ANDROID_SDK_ROOT>/platforms/<any_api_level>/data/res/layout folder
OR
You can use TwoLineListItem from the default Android controls list(it is located under "Advanced" tab in Eclipse Layout Editor)
OR
You can build your own layout with anything you like(for example LinearLayout with orientation="vertical" and two TextEdits added
So The Best Way Is:- I took out the simple list item 2 and made an layout in my project and with little editing it took the same layout as you may give in android.R.simple_list_item_2
So the code is:-
<?xml version="1.0" encoding="utf-8"?>
<TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/listPreferredItemHeight"
android:mode="twoLine"
android:paddingStart="?attr/listPreferredItemPaddingLeft"
android:paddingLeft="?attr/listPreferredItemPaddingLeft"
android:paddingEnd="?attr/listPreferredItemPaddingRight"
android:paddingRight="?attr/listPreferredItemPaddingRight">
<TextView android:id="#+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:textAppearance="?attr/textAppearanceListItem" />
<TextView android:id="#id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/text1"
android:layout_alignStart="#+id/text1"
android:layout_alignLeft="#+id/text1"
android:textAppearance="?attr/textAppearanceListItemSmall" />

AutoCompleteTextView background/foreground color

I am working on AutoCompleteTextView.Its working fine but the dropdown text is always white text on white background.
this picture explain my problem
picture explain my problem
XML:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000"
>
<!-- <AutoCompleteTextView -->
<AutoCompleteTextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Tapez votre 1texte"
android:textColor="#000"
/>
</LinearLayout>
Java:
view = (AutoCompleteTextView)findViewById(R.id.text);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple_dropdown_item_1line,data);
adapter.setDropDownViewResource(R.drawable.ic_action_search);//dd
view.setAdapter(adapter);
If you want to change the look of the dropdown items change the XML layout you pass to the ArrayAdapter, (in your case this is android.R.layout.simple_dropdown_item_1line).
Lets make a new layout named my_list_item.xml in res/layout:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/dropDownItemStyle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="5dp"
android:textAppearance="?android:attr/textAppearanceMediumInverse"
android:textColor="#00f" />
This text is smaller, blue, and centered. I don't recommend using this layout, it's more to demonstrate that you can customize it.
Now use this instead:
view = (AutoCompleteTextView)findViewById(R.id.text);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.my_list_item, data);
view.setAdapter(adapter);
When you set attributes here (like the text color):
<AutoCompleteTextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Tapez votre 1texte"
android:textColor="#000"
/>
You are only changing the box above the dropdown list (the box that you see before you interact with the AutoCompleteTextView). Hope that helps!
This question might be old but I believe this is very important for me to share.
I had been experiencing the same problem as OP but it is because I was using 'getApplicationContext()' in place of 'this'
Wrong
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getApplicationContext(), android.R.layout.simple_list_item_1, PLACES);
Correct
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(this, android.R.layout.simple_list_item_1, PLACES);
A tricky solution for this is:
Add this line
setTheme(android.R.style.Theme); before setContentView() in your activity file in which your autocompletetextview is present. Let me know if this works.
I tried setting up the theme before setcontext, tried different resources parameter in arrayAdapter and tried different theme ,but nothing helped.
Then I changed the context from 'this' to 'getApplicationContext' but the problem was persistent.
Finally I changed the context parameter to "getBaseContext()" and the problem was solved.
i got my solution in slecting layout of "select_dialog_singlechoice
ArrayAdapter<String > s1= new ArrayAdapter<String>
(this,android.R.layout.select_dialog_singlechoice,state);enter image description here
instead of get application context write "this" in ArrayAdapter<>;
try using different layouts u will definately solve your queries
I solved this issue by doing a simple tricky Way,
Just change the Theme of your activity declared in your AndroidManifest.xml as,
<activity
android:name=".YourActivity"
android:configChanges="keyboardHidden|orientation|screenSize"
android:theme="#style/Theme.AppCompat.Light.NoActionBar"
android:windowSoftInputMode="stateHidden"></activity>
and in the Activity as follows,
ArrayAdapter<String> adapter = new ArrayAdapter<String>
(getActivity(), android.R.layout.select_dialog_item, arraylist);
Answer by didldum https://code.google.com/p/android/issues/detail?id=5237
workaround by extending the theme and overriding the 2 styles for the typed text and the suggest text:
use an extended theme in your manifest:
...
...
create the new theme (res/values/themes.xml) which uses fixed styles:
...
#style/AutoCompleteTextViewLight
#style/Widget.DropDownItemLight
...
create the styles (res/values/styles.xml) which fix the color:
...
#android:color/primary_text_light
#android:color/primary_text_light

How to make 2 list views in a tab widget

I made a list view by the android.com's tutorial, and its in a tab widget, but the problem is that its spreading all over the tab's area and I want to create 2 lists in one tab, one beside the other... how can I do that?
here is a link to the tutorial:
http://developer.android.com/resources/tutorials/views/hello-listview.html
EDIT: ok that what I have in the onCreate method, there are 2 static final String arrays in this class called CONTACTS and FAVORITES, I copied MH's layout to an xml file called list_who and changed the IDs to lv_cons and lv_vav, and another layout called tv (TextView) that only have a text view because that what the adapter requires
super.onCreate(savedInstanceState);
ListView lv_cons = (ListView) findViewById(R.id.lv_cons);
ListView lv_fav = (ListView) findViewById(R.id.lv_fav);
lv_cons.setAdapter(new ArrayAdapter<String>(this, R.layout.tv, CONTACTS));
lv_fav.setAdapter(new ArrayAdapter<String>(this, R.layout.tv, FAVORITES));
setContentView(findViewById(R.layout.list_who));
If you want two display two lists side-by-side, there are a few things you'll need to change.
First the (minimal) layout for the tab's content:
<?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" >
<ListView
android:id="#+id/left_listview"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
<ListView
android:id="#+id/right_listview"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="1" />
</LinearLayout>
If you followed the Hello ListView sample code you linked, you'll have to change your ListActivity back to Activity. The former basically offers a number of convenience methods for working with a single list, but you can do exactly the same with a regular activity, which you'll need to work with multiple lists.
You can inflate your ListViews like any other view by using their ids (there is no more getListView() convenience method, since we're not extending ListActivity anymore)`:
ListView leftList = (ListView) findViewById(R.id.left_listview);
ListView rightList = (ListView) findViewById(R.id.right_listview);
From that point on you can add all stuff to populate/manipulate the lists' contents.
//Edit: If you insist on creating the layout programmatically, simple convert above xml code to it's Java equivalent:
// Create ViewGroup as container for both lists
LayoutParams rootParams = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);
LinearLayout root = new LinearLayout(this);
root.setLayoutParams(rootParams);
// Create LayoutParams for the lists - both identical in this example
LinearLayout.LayoutParams listParams = new LinearLayout.LayoutParams(0, LayoutParams.FILL_PARENT);
listParams.weight = 1;
ListView leftList = new ListView(this);
ListView rightList = new ListView(this);
leftList.setLayoutParams(listParams);
rightList.setLayoutParams(listParams);
// Add lists to container
root.addView(leftList);
root.addView(rightList);
// Set container as content view
setContentView(contentView);
If you're looking to handle two separate ListViews in separate tabs (i.e. a ListView in each tab), then follow this tutorial: Android TabActivity with two list views.
You simply need to make one composite layout and extract your tabHost from the xml programmatically and then inflate the tabs on the fly. I further suggest using this method if you want two lists to communicate with each other via an Intent. Both tabs would be hosted in one activity. If I misunderstood your question and you want to display two separate, adjacent lists within one activity..then disregard my answer.
I think this is what you're looking for. Good luck.

set a background for a listview

I have four tabs that hold four listviews, I want to set a background for each list view but whenever I try to add the background it puts the image in each cell of the listview instead of behind the list.
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/pre"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="21sp">
</TextView>
I've realised that this is beacuse I've tried to add the background in a textview so it adds the image in each cell in the listview, so I have tried to add a linearlayout, listview and a imageview and put the background there but it force closes. I think this is becuse the tabhost uses main.xml to draw the main page and it conflicts, so I even tried to add the listview there still it nforce closes, it will only work if I have a textview only, howe can I add a background to each listview, below is the listview code;
public class prem extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
// Create an array of Strings, that will be put to our ListActivity
String[] names = new String[] { "Pre"};
ListView lv = getListView();
lv.setCacheColorHint(00000000);
lv.setAdapter(new ArrayAdapter<String>(this,
R.layout.list_item, names));
}
Okay, your XML layout files are going to be used in the setContentView() method. You haven't posted the code for your activity which includes the TabHost, but I'll assume you're using the default setContentView(R.layout.main);. If you're NOT using setContentView() (in the case of your ListActivity), adding a ListView to an XML file isn't going to change anything, because it's never being used.
You are correct in that you're having the issue because you're setting the background of the TextView. Since you're using a ListActivity, you'll need to set the ListView's background using code. ListView is a subclass of View, so you can use the methods from the View class to set your background resource for the ListView.
For example:
ListView listView = getListView();
//set background to color
listView.setBackgroundColor(#FF888888);
//set background to Drawable
listView.setBackgroundDrawable(myDrawable);
//set background to Resource
listView.setBackgroundResouce(R.id.my_res_id);
As per JonniBravo, adding....
If you have an XML layout file that you are using to set the view by using setContentView() in your activity, you can have a ListView defined inside that.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lists_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/paper_frame" />
</RelativeLayout>
The activity can "find it" using
lv = getListView();
As shown in the sample above, you can set the background for the ListView with the "background" attribute. As usual, this can be a valid drawable (e.g. a color, or an image). In my example it happens to be a 9.patch image that is stretched by Android to enclose the list, providing a frame.
Good luck.

Categories

Resources