Android: Change Spinner Dropdown view - android

Im My application I want the below type of Spinner Dropdown view .
For this type of spinner view. I wrote this code.
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.spinner, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner_obj.setAdapter(adapter);
I got this from http://developer.android.com/guide/topics/ui/controls/spinner.html
But what I got is,
Please provide me the best way to do this....

Kind of reviving an old post here but the accepted answer is far from ideal. The correct way to do this is to set the Spinner to dropdown mode in your layout xml:
<Spinner
android:id="#+id/my_spinner"
...
android:spinnerMode="dropdown"/>
The available options are "dialog" and "dropdown".

Your application is running on old theme.
If you are using android 4.2 set android application theme (in the manifest file) to
android:theme="#android:style/Theme.Holo.Light"
OR
android:theme="#android:style/Theme.Holo.Light.DarkActionBar"

may be you are running in below than 4.0 , 4.0 will show you dropdown as your image

You can use popup like below:
spinner=(EditText)findViewById(R.id.txt_Spinner);
spinner.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
p = new Point();
p.x = location[0]+(v.getHeight());
p.y = location[1]+v.getHeight();
if (p != null)
showPopup(statusActivity.this, p);
System.out.println("show popup");
}
});
// The method that displays the popup.
private void showPopup(final Activity context, Point p) {
int popupWidth = 300;
int popupHeight = 500;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.popup_layout, viewGroup);
// Creating the PopupWindow
popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupWidth);
popup.setHeight(popupHeight);
popup.setFocusable(true);
// Some offset to align the popup a bit to the right, and a bit down, relative to button's position.
int OFFSET_X = 00;
int OFFSET_Y = 00;
// Clear the default translucent background
popup.setBackgroundDrawable(new BitmapDrawable());
// Displaying the popup at the specified location, + offsets.
popup.showAtLocation(layout, Gravity.NO_GRAVITY, p.x + OFFSET_X, p.y + OFFSET_Y);
((TextView)layout.findViewById(R.id.textView2)).setClickable(true);
((TextView)layout.findViewById(R.id.textView3)).setClickable(true);
((TextView)layout.findViewById(R.id.textView4)).setClickable(true);
((TextView)layout.findViewById(R.id.textView5)).setClickable(true);
((TextView)layout.findViewById(R.id.textView6)).setClickable(true);
((TextView)layout.findViewById(R.id.textView7)).setClickable(true);
((TextView)layout.findViewById(R.id.textView8)).setClickable(true);
((TextView)layout.findViewById(R.id.textView9)).setClickable(true);
}
and popup.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/popup_bg"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
style="#style/text_orange_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select Status"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="Sleeping"
android:text="Sleeping" />
<TextView
android:id="#+id/textView3"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="Available"
android:text="Available" />
<TextView
android:id="#+id/textView4"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="Busy"
android:text="Busy" />
<TextView
android:id="#+id/textView5"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="At work"
android:text="At work" />
<TextView
android:id="#+id/textView6"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="Battery charge low"
android:text="Battery charge low" />
<TextView
android:id="#+id/textView7"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="In meeting"
android:text="In meeting" />
<TextView
android:id="#+id/textView8"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="TMS me later"
android:text="TMS me later" />
<TextView
android:id="#+id/textView9"
style="#style/text_blue_contains"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:onClick="onClick"
android:clickable="true"
android:drawableBottom="#drawable/line_white"
android:tag="At the toilet"
android:text="At the toilet" />
<EditText
android:id="#+id/textCustomize"
style="#style/text_blue_contains"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:tag="Customize"
android:text="Customize" />
</LinearLayout>

For the GUI Use HoloEverywhere. https://github.com/Prototik/HoloEverywhere
HoloEverywhere is the best way to go if you want Holo theme on older Android then 4.0 .
And for the dropdown use android:spinnerMode="dropdown" in the layout as Stephen Kidson mentioned.

Its a AutocompleteTextView widget that you are suppose to use but you have tried with Spinner widget.
If my guess is right, Please refer the link.
AutocompleteTextView
Cheers.

Related

how to fix wrong alertDialog on a Click?

I have 2 TextViews showing on the main UI. I associate each TextView to a unique clickListener that launches a unique AlertDialog for the user to make a selection. The first TextView is launching the second layout for the second AlertDialog rather than the expected first layout for the first AlertDialog. What am I missing here?
activity_main.xml file
...
<TextView
android:id="#+id/fList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:text="filter"
android:textStyle="bold|italic"
android:textColor="#color/text_primary"
/>
<TextView
android:id="#+id/qList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="quickList"
android:textStyle="bold"
android:textColor="#color/text_primary"
android:gravity="center"
/>
MainActivity.java file
public class MainActivity extends AppCompatActivity
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTextViewFilter = (TextView)findViewById(R.id.fList);
TextView mTextViewQuickList = (TextView)findViewById(R.id.qList);
mTextViewFilter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
final AlertDialog.Builder alertDialogFilter = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflaterFilter = getLayoutInflater();
final ViewGroup nullParent = null;
// the AlertDialog layout for the first TextView click.
final View dialogLayoutFilter = inflaterFilter.inflate(R.layout.filter_main, nullParent);
alertDialogFilter.setView(dialogLayoutFilter);
final AlertDialog dialogFilter = alertDialogFilter.show();
...
}
});
mTextViewQuickList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = getLayoutInflater();
final ViewGroup nullParent = null;
// the AlertDialog layout for the second TextView click.
final View dialogLayout = inflater.inflate(R.layout.entire_layout, nullParent);
alertDialog.setView(dialogLayout);
final AlertDialog dialog = alertDialog.show();
...
}
});
entire_layout.xml
...
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="380dp"
android:minHeight="160dp" >
<ImageView
android:id="#+id/imageViewX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingRight="20dp"
android:paddingEnd="20dp"
android:contentDescription="x"
android:src="#drawable/ic_close_white_24dp" />
<TextView
android:id="#+id/FullList"
android:layout_below="#+id/imageViewX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.Large"
android:text="Show entire list" />
<TextView
android:id="#+id/fullNewest"
android:layout_below="#+id/FullList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginBottom="10dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="Created ... />
filter_main.xml
...
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="380dp"
android:minHeight="280dp" >
<ImageView
android:id="#+id/imageViewX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingRight="20dp"
android:paddingEnd="20dp"
android:contentDescription="x"
android:src="#drawable/ic_close_white_24dp" />
<TextView
android:id="#+id/Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewX"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.Large"
android:text="Filter..." />
<TextView
android:id="#+id/AllDos"
android:layout_below="#+id/Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="All..." />
<TextView
android:id="#+id/AllBuys"
android:layout_below="#+id/AllDos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="All..." />
<TextView
android:id="#+id/AllWork"
android:layout_below="#+id/AllBuys"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="All..." />
<TextView
android:id="#+id/AllHome"
android:layout_below="#+id/AllWork"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="All..." />
<TextView
android:id="#+id/AllWaitingfor"
android:layout_below="#+id/AllHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginBottom="5dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="All..." />
Not sure what your textviews' parent is but since you are using attributes like:
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
I guess it is a RelativeLayout.
Since you aren't using neither an orientation nor a relative position, I suppose your TextViews are both on the same line, with just a different margin top, but the quick list TextView has width match_parent and gravity center
android:layout_width="match_parent"
android:gravity="center"
So what's happening is something like this:
And your click always triggers the second TextView that covers entirely the first one.
To fix it change your quick list TextView this way:
<TextView
android:id="#+id/qList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="quickList"
android:textStyle="bold"
android:textColor="#color/text_primary"
android:layout_centerHorizontal="true"/>
With the attributes:
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
It will be centered but not overlapping the first TextView.
I hope this could help, if instead I am wrong and your activity_main.xml file differs, please update your code so that it can be easier figure out any alternative problem.

How to create a custom PopupMenu with fonts in menu item in Android

I want to change the default font of Popup Menu items and use from my custom font for them.Hows is it possible ?
This is the code that I used for creating Popup Menu:
<item
android:id="#+id/Setting"
android:title="Setting"/>
<item
android:id="#+id/About"
android:title="About"/>
<item
android:id="#+id/Help"
android:title="Help"/>
I want to change the typeface of my items using custom typeface (ttf) files.
you can you PopupWindow for this, check the below code:
pop_layout.xml
<LinearLayout 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"
tools:context=".MainActivity"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="22dp"
android:text="Item 1"
android:id="#+id/one"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:id="#+id/two"
android:layout_marginTop="22dp"
android:text="Item 2"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="22dp"
android:text="Item 3"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/four"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="22dp"
android:text="Item 4"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
PopupWindow:
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popupView = layoutInflater.inflate(R.layout.pop_layout, null);
TextView one = (TextView)popupView.findViewById(R.id.one);
TextView two = (TextView)popupView.findViewById(R.id.two);
TextView three = (TextView)popupView.findViewById(R.id.three);
TextView four = (TextView)popupView.findViewById(R.id.four);
// Do your customised stuff
PopupWindow popupWindow = new PopupWindow(
popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
popupWindow.setBackgroundDrawable(new BitmapDrawable());
popupWindow.setOutsideTouchable(true);
popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
#Override
public void onDismiss() {
//TODO do sth here on dismiss
}
});
popupWindow.showAsDropDown(view);// your view instance in which click you want to show menu

best way to implement pop up window in android application

I have the main activity in which I have few menu buttons. When the user presses specific button I want to open a new activity which also have a couple of buttons which I need to handle their click. In other words, I need to pop up window functionality as normal activity.
I looked online and found several ways to implement this such as: just customising the size of the activity, use diaglog theme in the manifest, use it as a fragment or use Popup Window Class. But as I am new to android I want the best way to implement it for my project.
Can someone help me achieve this?
EDIT:
this is the xml file i want to use in the pop up window (for better explanation of what i want to achieve):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:background="#0091cb"
android:padding="16dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="#dimen/activity_horizontal_margin"
android:weightSum="3"
android:id="#+id/check">
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/computer"
android:paddingTop="12dp"
android:layout_marginLeft="10dp"
android:text="button1"
android:textSize="10dp"
android:textColor="#fff"
android:layout_weight="1"
android:onClick="button1_OnClick"/>
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/electrical"
android:paddingTop="12dp"
android:layout_marginLeft="10dp"
android:text="button2"
android:textSize="10dp"
android:textColor="#fff"
android:layout_weight="1"
android:onClick="button2_OnClick"/>
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/hdtv"
android:paddingTop="12dp"
android:layout_marginLeft="10dp"
android:text="button3"
android:textSize="10dp"
android:textColor="#fff"
android:layout_weight="1"
android:onClick="button3_OnClick"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:orientation="horizontal"
android:layout_below="#id/check"
android:paddingTop="10dp">
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/bill"
android:paddingTop="12dp"
android:layout_marginLeft="10dp"
android:text="button4"
android:textSize="10dp"
android:textColor="#fff"
android:onClick="button4_OnClick"/>
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginLeft="10dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/water"
android:paddingTop="12dp"
android:text="button5"
android:textSize="10dp"
android:textColor="#fff"
android:onClick="button5_OnClick" />
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginLeft="10dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/electrical"
android:paddingTop="12dp"
android:text="button6"
android:textSize="10dp"
android:textColor="#fff"
android:onClick="button6_OnClick" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:orientation="horizontal">
<Button
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginLeft="10dp"
android:background="#drawable/circle"
android:drawableTop="#drawable/notepad"
android:paddingTop="7dp"
android:text="button7"
android:textSize="10dp"
android:textColor="#fff"
android:onClick="button7_OnClick" />
</LinearLayout>
</LinearLayout>
Depends on your needs.
If you need just to show 'classic' popup window (error while typing in input field or small color picker) - use PopupWindow class. You can use my gist.
If your message is more general (e.g. 'There is no internet connection') or user should choose yes/no - use dialog. There is cool library.
Activity with custom size is used rarely. Maybe in some dial applications.
So choose implementation depends on your needs.
create method where u want to open popup windiw in your activity
Like this,here p is a specific point(location) where you want exactly open your window.
final Point p = new Point();
show_popup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
showpopupwindows(Activity, p);
}
});
then,
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
int location[] = new int[2];
show_popup.getLocationOnScreen(location);
p.x = location[0];
p.y = location[1];
}
private void showpopupwindows(final Activity context, Point p) {
LinearLayout viewGroup = (LinearLayout) context
.findViewById(R.id.popup_menu);
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Display display = context.getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int popupwidth = (size.x / 2);
int popupheight =(size.y / 2);
View layout = layoutInflater.inflate(R.layout.detail_pop, viewGroup);
final PopupWindow popup = new PopupWindow(context);
popup.setContentView(layout);
popup.setWidth(popupwidth);
popup.setHeight(popupheight);
popup.setFocusable(true);
popup.setAnimationStyle(R.style.WindowAnimation);
popup.setBackgroundDrawable(new ColorDrawable(
android.graphics.Color.TRANSPARENT));
popup.showAtLocation(layout, Gravity.NO_GRAVITY, popupwidth,popupheight);
detail_pop1 = (TextView) layout.findViewById(R.id.detail_pop1);
detail_pop1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(getContext(), "pop window is opened, Toast.LENGTH_SHORT).show();
}
});
}
detail_pop.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/popup_menu"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:background="#color/skyblue"
android:gravity="center"
android:orientation="vertical"
>
<TextView
android:id="#+id/detail_pop1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="1"
android:drawableTop="#drawable/ic_launcher"
android:gravity="center"
android:text="P"
android:textSize="#dimen/font_24" />
</LinearLayout>

Android - Buttons to appear as if absolute, ignore scrolling content?

I have a listview populated by an adapter which, as expected, is scrollable when the content exceeds the boundaries of the page.
I also have two buttons below the list. What I want to do is have these two buttons appear at the bottom of the page right away, and remain there even if the content is scrolled, exactly like the "position: absolute" property for a website would work.
Here is the code I have... I've tried several methods but unfortunately our client wants as many pages to be created dynamically as possible, so I can't use any XML to do this, only the java class.... Hopefully one of you genius' people can help!
/**
* MaterialsActivity
*
* Activity to display list of materials used for a fault.
*/
public class MaterialsActivity extends ListActivity implements DialogCloseListener {
private String faultId = null;
private ProgressDialog progressDialog;
private MaterialListAdapter adapter = null;
/* (non-Javadoc)
* #see android.app.Activity#onCreate(android.os.Bundle)
*/
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Get data passed in (faultId)
Bundle extras = getIntent().getExtras();
if (extras != null) {
faultId = extras.getString("faultId");
}
// Check device id valid
if (faultId == null || faultId.length() == 0) {
ErrorDialog ed = new ErrorDialog(this, "No fault specified");
ed.show();
setResult(RESULT_CANCELED);
finish();
}
// Set up add button and handler
Button btnAdd = new Button(this);
btnAdd.setText("Add New Material Entry");
btnAdd.set
btnAdd.setOnClickListener(new Button.OnClickListener() {
//On click open add activity, passing next id to use
//(ID system used not very robust but sticking with way existing FMS does things)
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(MaterialsActivity.this, MaterialsAddActivity.class.getName());
intent.putExtra("faultId", faultId);
if (adapter != null) {
intent.putExtra("newMaterialUsedId", adapter.getNextId());
}
startActivityForResult(intent, 0);
}
});
this.getListView().addFooterView(btnAdd);
// Set up remove button and handler
Button btnRemove = new Button(this);
btnRemove.setText("Remove Material Entry");
btnRemove.setOnClickListener(new Button.OnClickListener() {
//On click open add activity, passing next id to use
//(ID system used not very robust but sticking with way existing FMS does things)
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setClassName(MaterialsActivity.this, MaterialsRemoveActivity.class.getName());
intent.putExtra("faultId", faultId);
if (adapter != null) {
intent.putExtra("newMaterialUsedId", adapter.getNextId());
}
startActivityForResult(intent, 1);
}
});
// Add the button to the footer (needs to be done after setting it up)
this.getListView().addFooterView(btnRemove);
// Set list adapter
adapter = new MaterialListAdapter(MaterialsActivity.this, R.layout.materials_list_item2, new ArrayList<MaterialsUsed>());
setListAdapter(adapter);
//Start new asynctask to retrieve materials list and show wait indicator
new LoadMaterialsUsed().execute(faultId);
progressDialog = ProgressDialog.show(MaterialsActivity.this, "", "Loading. Please wait...", true);
}
The ListAdapter populates the following XML with each row from my DB table, so it runs X amount of times until it has retrieved each row.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
>
<TextView
android:id="#+id/materialTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingTop="20dp"
android:text="Material : "
android:textSize="15sp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="#+id/matMaterial"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_toRightOf="#id/materialTitle"
android:gravity="right"
android:paddingTop="20dp"
android:paddingRight="15dp"
android:textColor="#acacac"
android:textSize="15sp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="#+id/assetNoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/materialTitle"
android:gravity="left"
android:text="Asset No : "
android:paddingTop="5dp"
android:textSize="15sp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="#+id/matAssetNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/assetNoTitle"
android:layout_below="#id/matMaterial"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingTop="5dp"
android:paddingRight="15dp"
android:textColor="#acacac"
android:textSize="15sp" >
</TextView>
<!-- <TextView
android:id="#+id/equipmentTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/assetNoTitle"
android:gravity="left"
android:text="Equipment : "
android:textSize="15sp"
android:paddingTop="5dp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="#+id/matEquipment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/equipmentTitle"
android:layout_below="#id/matAssetNo"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingTop="5dp"
android:paddingRight="15dp"
android:textColor="#acacac"
android:textSize="15sp" >
</TextView> -->
<TextView
android:id="#+id/manufacturerTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/assetNoTitle"
android:gravity="left"
android:text="Manufacturer : "
android:textSize="15sp"
android:paddingTop="5dp"
android:textStyle="bold" >
</TextView>
<TextView
android:id="#+id/matManufacturer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/manufacturerTitle"
android:layout_below="#id/matAssetNo"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingTop="5dp"
android:paddingRight="15dp"
android:textColor="#acacac"
android:textSize="15sp" >
</TextView>
<TextView
android:id="#+id/modelTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/manufacturerTitle"
android:gravity="left"
android:text="Model : "
android:textSize="15sp"
android:paddingTop="5dp"
android:textStyle="bold">
</TextView>
<TextView
android:id="#+id/matModel"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/matManufacturer"
android:layout_toRightOf="#id/modelTitle"
android:gravity="right"
android:paddingTop="5dp"
android:paddingRight="15dp"
android:textColor="#acacac"
android:textSize="15sp" >
</TextView>
<!-- <TextView
android:id="#+id/descriptionTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/modelTitle"
android:gravity="left"
android:text="Description : "
android:textSize="15sp"
android:paddingTop="5dp"
android:textStyle="bold">
</TextView>
<TextView
android:id="#+id/matDescription"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:gravity="right"
android:layout_alignParentRight="true"
android:layout_below="#id/matModel"
android:layout_toRightOf="#id/descriptionTitle"
android:textColor="#acacac"
android:paddingTop="5dp"
android:paddingRight="15dp"
android:textSize="15sp" >
</TextView> -->
<TextView
android:id="#+id/serialNoTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/modelTitle"
android:gravity="left"
android:text="Serial No : "
android:textSize="15sp"
android:paddingTop="5dp"
android:paddingBottom="20dp"
android:textStyle="bold">
</TextView>
<TextView
android:id="#+id/matSerialNo"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/serialNoTitle"
android:layout_below="#id/matModel"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingTop="5dp"
android:paddingRight="15dp"
android:paddingBottom="20dp"
android:textColor="#acacac"
android:textSize="15sp">
</TextView>
<!-- <TextView
android:id="#+id/modifiedByTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/serialNoTitle"
android:gravity="left"
android:text="Modified By : "
android:textSize="15sp"
android:paddingTop="5dp"
android:textStyle="bold">
</TextView>
<TextView
android:id="#+id/matModifiedBy"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/modifiedByTitle"
android:layout_below="#id/matSerialNo"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingTop="5dp"
android:paddingRight="15dp"
android:textColor="#acacac"
android:textSize="15sp" >
</TextView>
<TextView
android:id="#+id/modifiedTimeTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/modifiedByTitle"
android:gravity="left"
android:text="Modified Time : "
android:textSize="15sp"
android:paddingTop="5dp"
android:paddingBottom="20dp"
android:textStyle="bold">
</TextView>
<TextView
android:id="#+id/matModifiedTime"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/modifiedTimeTitle"
android:layout_below="#id/matModifiedBy"
android:layout_alignParentRight="true"
android:gravity="right"
android:paddingTop="5dp"
android:paddingRight="15dp"
android:paddingBottom="20dp"
android:textColor="#acacac"
android:textSize="15sp" >
</TextView> -->
<View
android:background="#drawable/white"
android:layout_height="2dp"
android:layout_width="fill_parent"/>
</RelativeLayout>
If you could change the XML where ListView resides in, it would be dead easy.
Since you can't, you'll need to use a few hacks. Here's a step by step plan:
Add the button to the ListView's parent layout.
Measure the button's height
Then wait for the ListView to be drawn so you can measure it's height.
Finally set the ListView's height so the button is visible (not overlayed by the list)
Here's a sample I've tried:
ViewGroup viewGroup = (ViewGroup) listView.getParent();
Button button = new Button(this);
button.setText("Button");
button.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
viewGroup.addView(button);
// Button height
button.measure(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
final int buttonHeight = button.getHeight();
Log.e("TAG", "Button height: " + buttonHeight);
// ListView height
listView.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v, int left, int top, int right, int bottom,
int oldLeft, int oldTop, int oldRight, int oldBottom) {
listView.removeOnLayoutChangeListener(this);
int listHeight = listView.getMeasuredHeight();
Log.e("TAG", "List height: " + listHeight);
// ListView's height = listHeight - buttonHeight
listView.setLayoutParams(new LinearLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, listHeight - buttonHeight));
}
});
Notes:
This sample assumes that the ListView's parent is a LinearLayout.
It will add the button after every other in the parent layout.
If, however, the parent is a RelativeLayout, you'll have to play around with layoutParams.addRule(RelativeLayout.BOTTOM_OF, listView.getId());
I guess your xml looks somthing like this
<LinearLayout android:orientation="vertical">
<ListView>
...
</ListView>
<LinearLayout android:orientation="horizontal">
<Button />
<Button />
</LinearLayout>
</LinearLayout>
I would not use a ListView but a LinearLayout inside of a ScrollView and dynamically add TextView item to the Layout
<LinearLayout android:orientation="vertical">
<ScrollView>
<LinearLayout android:orientation="vertical">
<TextView />
<TextView />
...
</LinearLayout>
</ScrollView>
<LinearLayout android:orientation="horizontal">
<Button />
<Button />
</LinearLayout>
</LinearLayout>
you just need to add all missing attributes like weights, width,height etc
edit:
The idea is the same if would generate all views with java code instead of an XML defenition
in your xml you have to do something like this...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" >
<ListView
android:layout_above="#+id/ll_buttons">
</ListView>
<LinearLayout
android:id="#+id/ll_buttons"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true" >
</LinearLayout>
</RelativeLayout>
this will set your buttons on bottom of screen always and the list view will always be above the LinearLayout containing the list items(i.e ofcourse scrollable).
For Dynamic buttons.. add in layout
Button button = new Button(this) ///this is a context object
button.setWidth(100);
button.setText("Save");
layout.addView(button);

to add a last row in listview

i have a custom listview i want to add one lat row to it which will calculate total of all the list when i scroll the last row gets added again and again
if(position==searchResult.size() && searchResult.size()!=1)
{
holder.checkImg.setVisibility(ImageView.GONE);
holder.fvtImg.setVisibility(ImageView.GONE);
holder.type.setVisibility(TextView.GONE);
holder.name.setVisibility(TextView.GONE);
holder.offer_price.setVisibility(TextView.GONE);
holder.real_price.setVisibility(TextView.GONE);
holder.total.setVisibility(TextView.VISIBLE);
holder.total_price.setVisibility(TextView.VISIBLE);
//DecimalFormat df = new DecimalFormat("0.00");
//holder.txt_distance.setText(df.format(mData.get(position).get("distance")).toString()+"...");
DecimalFormat twoDForm = new DecimalFormat("0.00");
String totalPrice = (twoDForm.format(mTotalPrice)+"").replace('.',',');
holder.total.setText("Total");
holder.total_price.setText("Kr. "+totalPrice);
}
else
{
holder.total.setVisibility(TextView.GONE);
holder.total_price.setVisibility(TextView.GONE);
if(isEdit){
/*LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
lp.leftMargin=30;
holder.fvtImg.setLayoutParams(lp);*/
/* TranslateAnimation anim = new TranslateAnimation(0,35,0,0);
holder.fvtImg.setAnimation(anim);
anim.setFillAfter(true);*/
holder.fvtImg.setVisibility(ImageView.GONE);
holder.name.setPadding(10,0, 0, 0);
holder.type.setPadding(10,0, 0, 0);
holder.checkImg.setVisibility(ImageView.VISIBLE);
Layout file :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/list_background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/price_list_background"
/>
<CheckBox android:id="#+id/checkBox"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:visibility="gone">
</CheckBox>
<ImageView
android:id="#+id/img_pol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/pol_icon_tag"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
/>
<TextView
android:id="#+id/txt_name"
android:layout_width="wrap_content"
android:layout_height="15dp"
android:textSize="16dp"
android:textColor="#000000"
android:layout_marginTop="5dp"
android:layout_marginLeft="35dp"
/>
<TextView
android:id="#+id/txt_type"
android:layout_width="200dp"
android:layout_height="13dp"
android:textSize="12dp"
android:textColor="#464647"
android:layout_marginTop="35dp"
android:layout_marginLeft="35dp"
/>
<TextView
android:id="#+id/txt_real_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="12dp"
android:textColor="#464647"
android:layout_marginTop="35dp"
android:layout_marginLeft="90dp"
/>
<TextView
android:id="#+id/txt_offer_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#464647"
android:layout_marginTop="31dp"
android:layout_marginLeft="190dp"
/>
<TextView
android:id="#+id/txt_total"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:textColor="#464647"
android:layout_centerVertical="true"
android:layout_marginLeft="100dp"
android:text="Total"
android:visibility="gone"
/>
<TextView
android:id="#+id/txt_total_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="16dp"
android:textColor="#464647"
android:layout_centerVertical="true"
android:layout_marginLeft="230dp"
android:text="kr. 222.22"
android:visibility="gone"
/>
</RelativeLayout>
Have you considered using a footer instead of placing the last item inside the ListView?
What you do is, before you set the adapter of the ListView, you inflate a footer layout (or perhaps just a TextView, if that's all you need). Store a reference to it, and then add it to the ListView:
TextView myFooter = new TextView( context );
myListView.addFooterView( myFooter );
Then when you add items to the list or when the calculation needs to occur, you just calculate and set the text of myFooter.
myFooter.setText( myCalculatedValue );
Setting the text of the footer must ofcourse happen on the UI-thread so if you use a background thread or a AsyncTask to add to your listview, be sure you update stuff the right place.

Categories

Resources