Hey, I have a ListView that I want to be displayed into an AlertDialog, I have 2 problems:
1. The text is displayed in the left side, I want it to be in the center
2. To choose an item, you have to select exactly the text of the row.. How to make it possible to select the item even if selecting outside the text in the same row?
You can see in the picture that the orange highlight is on the text itself only rather than the whole row..:
Code:
LinearLayout layout = new LinearLayout(mContext);
ListView lv = new ListView(mContext);
lv.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1, BooksOptions));
Builder alert;
alert = new AlertDialog.Builder(this);
layout.addView(lv);
alert.setView(layout);
You're utilizing the standard Android list item layout, android.R.layout.simple_list_item_1. Instead, create your own item layout based on that layout and set the properties appropriately.
<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/textAppearanceLarge"
android:gravity="center"
android:paddingLeft="6dip"
android:minHeight="?android:attr/listPreferredItemHeight"
/>
Note that you're also not setting layout properties on the ListView itself. Ensure that you specify it to have it match_parent for width as well in your layout.
Related
I placed a spinner on a dialog. When I want to select an item and the item list is shown below the spinner, everything is fine and I can scroll through the items. But if the item list is shown above the spinner, the item list is cut off and I cannot scroll:
It looks like the system does not identify, that the item list above the dialog is not visible and therefore does not show the scrollbars.
My spinner definition looks like this:
<Spinner
android:id="#+id/spnnrValue"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_marginTop="2dp"
android:layout_marginLeft="-20dp"
android:layout_weight="1" />
This is how I define the dialog:
dialogColorPickerExtended = new Dialog(Base.getMainActivity(), R.style.full_screen_dialog);
dialogColorPickerExtended.setCancelable(true);
dialogColorPickerExtended.setContentView(R.layout.template_color_picker_extended);
if (dialogColorPickerExtended.getWindow() != null)
dialogColorPickerExtended.getWindow().getAttributes().width = (int) (GUIManager.getScreenWidth() * 0.9f);
Do you have any idea, how I can show the complete item list?
You might not be able to show it all at once, but adding a scrollbar to the spinner itself may be a solution. Visit this page, you might be able to solve this problem with an overall functionality improvement by adding a scrollbar to your spinner.
Spinner's scrollbar style
how do i create a multiple row app with the same data for each row. This is how all the rows are to look like. it contains three text views and two spinners. this app is to help calculate the gpa of a student for one semester.
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.mbedobe.android.samplegpacalculator.app.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="C1"
android:id="#+id/course_textView"/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/course_textView"
android:layout_toEndOf="#+id/course_textView"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp" />
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner2"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/spinner"
android:layout_toEndOf="#+id/spinner"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/spinner2"
android:layout_toEndOf="#+id/spinner2"
android:id="#+id/gradePoints"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/gradePoints"
android:layout_toEndOf="#+id/gradePoints"
android:id="#+id/gradeValue"
android:layout_marginLeft="23dp"
android:layout_marginStart="23dp"/>
</RelativeLayout>
You should think about using a ListView with BaseAdapter.
The best solution is to use RecycleView
Use a RecyclerView with a custom adapter. The layout above will be the row layout for each row in this case.
Use the links above to understand how you can create your own Custom RecyclerView Adapters with Custom Layouts.
EDIT:
Ok, I'll give you the basics here. There are three major things you need for a custom RecyclerView.
They are:
List of Objects (your data)
A custom layout for each row
An Adapter that will transform your data onto the above layout.
Now, let's take an example and try and explain this.
You must have seen popular apps like Gmail, WhatsApp, etc.
When you open these apps, you see a list of Emails/Chats etc.
One thing to notice is that the layout for each row is same, even though the content inside them is different.
This tells us 2 things: first that the data must be a list of the same kind of objects, and secondly that the layout of each row is the same. So, to create such a listView or RecyclerView, we shouldn't need to create as many layouts as there are rows. One layout can be reused for each row.
So, two of our three needs are understood. The final item required is the Adapter. The adapter is what takes your List and converts each item from the list into a row on your RecyclerView. These rows are automatically created by the Adapter as the user scrolls through the list, and removed when the user can no longer see those rows.
If you want code for this, let me know. I'll upload code explaining this. But I would recommend you try it out on your own. It's not that hard.
Either use ListView, recyclerView or dynamically add the items in scrollview.
Use of ListView or RecyclerView will be the best approach for you.
I'll give you the last option that should be worked from java only if you are with good knowledged in java. ( only the steps not complete code)
1) your xml file should contain scroll view.
2) for each item to be added (3 textview 2 spinner), add a layout and provide layout params for it i.e width/height/orientation/background etc.
3)add this layout to the scrollview you created in xml by
findViewById(R.id.scrollviewId).add(layout)
4)create your textviews/spinners in and give them layout params.{ this process is much logical in a pattern of your design so must be very careful}
5)add all these textviews/spinners in the layout you created in step 3.
Note: according to your design requirements you might need to add other layouts in between the layout created in step 2/3 and other items created in step 4.
Here is the code you liked:
LinearLayout myLayout = (LinearLayout)findViewById(R.id.layout_in_xml);
for(int i = 0; i < required_fields.length; i++){
LinearLayout lr = new RelativeLayout(DisplaySadip.this);
LayoutParams layoutParams = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lr.serLayoutParams(layoutParams);
lr.setOrientation(LinearLayout.HORIZONTAL);
//add linear layout to your layout in xml
myLayout.add(lr);
//add textview, spinner to this layout now
TextView tv1 = new TextView(YourActivity.this)
tv1.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tv1.setText("......"+i);
lr1.add(tv1)
Spinner spinner1 = new Spinner(YourActivity.this)
spinner.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
Spinner spinner2 = new Spinner(YourActivity.this)
spinner.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
lr.add(spinner1);
lr.add(spinner2);
TextView tv2 = new TextView(YourActivity.this)
tv2.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tv2.setText("......"+i);
lr.add(tv2)
TextView tv3 = new TextView(YourActivity.this)
tv3.setLayoutParams(new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
tv3.setText("......"+i);
lr1.add(tv3)
}
it shall add add many no of views you want in your layout.
I need to implement two single choice list on an alert dialog. The two single choice items can be seen in the first picture. Once you clicked on the button , it will show a list of item(second picture). My question is how do I implement two single choice list item on an alert dialog? Thanks!
picture is adopted and a screenshots from VirtualGuitar+
You can create a layout with two spinners in horizontal linear layout.
Then make alert dialog with your layout xml like this
AlertDialog.Builder builderVal=new AlertDialog.Builder(mContext);
builderVal.setTitle("Title here") ;
View holder=View.inflate(mContext, R.layout.spinners, null);
builderVal.setView(holder);
Spinner spinner1 = (Spinner) holder.findViewById(R.id.spinner1);
//implement spinner logic
builderVal.setPositiveButton("Okay",null);
builderVal.show();
spinners.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner1"></Spinner>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner2"></Spinner>
</LinearLayout>
I want to add a title bar in the list view.
I have added a text view inside the listview which can act as title:
Layout File :
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/layout_rounded"
android:layout_marginRight="10px"
android:layout_marginLeft="10px"
android:layout_marginTop="30px"
android:layout_below="#id/linlaypay"
android:orientation="vertical"
android:padding="10dip" >
<ListView
android:id="#+id/rectran"
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
</ListView>
</LinearLayout >
Here is the code used to add the title listview dynamically:
listView = (ListView) findViewById(R.id.rectran);
TextView textView = new TextView(getApplicationContext());
textView.setText("header view");
listView.addHeaderView(textView);
The problem is ,an emty text view isadded to the listview without the text "header view".
Why?Am I missing anything?
I don't believe you can just add a view like this. The view being passed into listView needs to be inflated.
create a header.xml layout file with the text view in it with the text set.. and then run the code below.
LayoutInflater inflater = getLayoutInflater();
ViewGroup header = (ViewGroup)inflater.inflate(R.layout.header, listView, false);
listView.addHeaderView(header, null, false);
If I were you, I would add a title to the ActionBar, assuming that your ListView is the whole Activity. If it isn't the whole Activity or it doesn't take up the whole screen, you could add the title to the layout rather than dynamically adding it.
http://developer.android.com/reference/android/app/ActionBar.html
Check out the Android Developers documentation on the ActionBar
My activity holds 3 small listviews and I wanted to give title to each of the view by adding textview.
However the text in the Textview was not visible because the size of the text was too small.
setting the text size of the textview resolved the issue.
Thanks !!
I finished the English version of my application and now I am working on the Arabic localization. Arabic is a right-to-left language, so I need to adjust a lot of things in my layout, including my spinner display.
When I choose an item from the spinner selection menu, I see the following,
As you can see "Allergy" is aligned to the left and on top of the spinner arrow. I need it to be aligned to the right.
NOTE The Arabic webservices aren't finished yet, that's why the data in the image is still in English.
EDIT
Spinner mSpecialtySpinner = (Spinner) findViewById(R.id.specialty_appointment_spinner);
ArrayAdapter<Specialty> adapter = new ArrayAdapter<Specialty>(AppointmentReservationActivity.this,android.R.layout.simple_spinner_item, specialities);
adapter.setDropDownViewResource(R.layout.spinner_item);
mSpecialtySpinner.setAdapter(adapter);
Well you can have another layout for Spinner and pass it to ArrayAdapter's constructor.
make an xml file naming spinner_layout.xml in layout like this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_height="?android:attr/listPreferredItemHeight"
android:layout_width="fill_parent"
android:textSize="20sp"
android:paddingLeft="6dip"
android:paddingRight="6dip"
android:gravity="right"
/>
and now pass it in ArrayAdapter's constructor like this:
new ArrayAdapter<String>(this,R.layout.spinner_layout,conversionsadd);
Please remember:
what we have provided as R.layout.spinner_layout in ArrayAdapter constructor is layout of Spinner. and what we will provide as setDropdownView() is Spinner item (drop down item).
You can have a look at my another relative answer here
All you need to do is create a new spinner dropdown layout and set android:gravity="right" on the appropriate widget.
Call it with:
yourSpinner.setDropDownViewResource(R.layout.right_aligned_spinner_dropdown_item);