How to dynamically add a button to a popup window? - android

I'm trying to write a function that opens a popup window, and populates it with button links to all of the previous pictures or recorded audio files the app has recorded. I tried to write the function so that it will work for audio files or images. The goal is to have a pop-up window appear on the screen which has a button for each of the previous files and will open the designated file on click. The app will successfully open the popup window, but it will only be populated by the auto-defined button (back) which will close the popup window. However, white space appears underneath the back button, which increases the height of the scroll view and allows for scrolling within the pop-up window but no buttons actually appear.
Initially, my problems came from not including pw.getContentView() (pw is my popupwindow) for each of my layout elements, but I'm not sure what it does exactly so that may be a part of the problem.
This is the function that is called to open the popup window.
private void display_media(int check, String header_text){
//The check variable is a 1 or 0 depending on if it's an image or an audio.
// header_text is a simply text to replace the default header.
try{
LayoutInflater inflater = (LayoutInflater) NewPlan_Two.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.files_layout, (ViewGroup) findViewById(R.id.show_media));
pw = new PopupWindow(layout,width/4,height/3,true);//width and height are predefined values
Button close_button = (Button) layout.findViewById(R.id.back_scroll);
close_button.setOnClickListener(close_view);
TextView header =(TextView) pw.getContentView().findViewById(R.id.previous_files);
header.setText(header_text);
LinearLayout l_l = (LinearLayout) pw.getContentView().findViewById(R.id.scroll_linear);
for(String media_file: files){ // this is defined and explained below
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
Button new_button = new Button(this);
if(check==0){
new_button.setId(image_id_count);
image_id_count++; // this is initialized to 3000
button_id = new_button.getId();
}
else{
new_button.setId(audio_id_count);
audio_id_count++; // this is initialized to 2000
button_id = new_button.getId();
}
new_button.setText(media_file);
l_l.addView(new_button,params);
if(check==0)
button_two= (Button) pw.getContentView().findViewById(button_id);
else
button_two = (Button) pw.getContentView().findViewById(button_id);
button_two.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
pw.dismiss();
// this will be replaced with code to open the image or audio, but I haven't written it yet.
}
});
}
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
}catch(Exception e){
e.printStackTrace();
}
}
To parse the audio or image files, I use this function. It is successfully getting the names of the files. It is called with either "mp4" or "png" as the argument.
private void parse_Files(String fileType){
files = new ArrayList<String>();
File folder = new File(abs_path); // abs_path is the path to the folder with the files
File[] list_of_files = folder.listFiles();
for(int count = 0; count < list_of_files.length;count++){
String file_name = list_of_files[count].getName();
String[] parsed_list = file_name.split("\\.");
if(parsed_list[1].equals(fileType))
files.add(parsed_list[0]);
}
}
Finally here is the xml file that opens as the popup window.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/show_media"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding = "15dp"
android:background="#DCDDF7">
<TextView
android:id="#+id/previous_files"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/previous_files"
android:textSize="20sp"/>
<ScrollView android:layout_height="wrap_content" android:layout_width="match_parent" android:background="#FFFFFF" >
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/scroll_linear">
<Button
android:id="#+id/back_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/back"
android:textSize="18sp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Thanks!
Edit:
I tried to change it to a list view, but I am getting the same issue. It opens a pop-up and there are x buttons corresponding to x associated files, but none of the buttons have text or do anything on click. What am I missing?
Here is the revised code:
private void display_media(int check, String header_text){
try{
display_media_array=new String[files.size()];
display_media_array=files.toArray(display_media_array);
LayoutInflater inflater = (LayoutInflater) NewPlan_Two.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.files_layout, null);
pw = new PopupWindow(layout,LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT,true);
ListView listView = (ListView) layout.findViewById(R.id.list_view);
listView.setAdapter(new ArrayAdapter<String>(getApplicationContext(),
android.R.layout.simple_list_item_1, display_media_array));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
pw.dismiss();
}
});
Button close_button = (Button) layout.findViewById(R.id.back_scroll);
close_button.setOnClickListener(close_view);
TextView header =(TextView) pw.getContentView().findViewById(R.id.previous_files);
header.setText(header_text);
pw.showAtLocation(layout, Gravity.CENTER, 0, 0);
}catch(Exception e){
e.printStackTrace();
}
}
And here is the new xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/show_media"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#DCDDF7"
android:orientation="vertical"
android:padding="15dp" >
<TextView
android:id="#+id/previous_files"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="#string/previous_files"
android:textSize="20sp" />
<ListView
android:id="#+id/list_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
</ListView>
<Button
android:id="#+id/back_scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/back"
android:textSize="18sp" />
</LinearLayout>
Any help is appreciated, thanks!

Related

Trouble with Spinner: Attempted to finish an input event but the input event receiver has already been disposed

As the title says, I get this error while interacting with a Spinner. I've noticed that there are many post about this argument, but every one is different from each other (answers too). Unfortunately, I didn't find a solution, so I'm asking here.
Here is a screenshot of the Spinner:
As you can see, the first Spinner is ok, but the second has two problems:
First one, it doesn't show values
Second one, when I tap the spinner, nothing happen. If I tap again the spinner, I get the error "Attempted to finish an input event but the input event receiver has already been disposed."
Maybe the two things are connected somehow...
Here is the code:
public class Settings extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
Integer[] radiusArray = new Integer[]{500,700,1000,1500,2000,2500,3000,4000,5000};
Integer[] geofenceRadius = new Integer[]{100,200,300,400,500};
try {
final Spinner spinnerRA = (Spinner) findViewById(R.id.search_radius);
final Spinner spinnerGR = (Spinner) findViewById(R.id.geofence_radius);
ArrayAdapter<Integer> adapterRA = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, radiusArray);
spinnerRA.setAdapter(adapterRA);
ArrayAdapter<Integer> adapterGR = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, geofenceRadius);
spinnerRA.setAdapter(adapterGR);
//Getting from preference files, saved settings, if any
//1000 and 100 are default settings
final SharedPreferences sharedPref = getSharedPreferences("Settings",Context.MODE_PRIVATE);
String temp = getResources().getString(R.string.search_radius);
int savedRadius = sharedPref.getInt(temp, 1000);
temp = getResources().getString(R.string.geofence_radius);
int savedGeofence = sharedPref.getInt(temp, 100);
//Show selected value for spinner, or default value
int i;
for(i=0; i<radiusArray.length; i++){
if(radiusArray[i].equals(savedRadius)){
break;
}
}
spinnerRA.setSelection(i);
for(i=0; i<geofenceRadius.length; i++){
if(geofenceRadius[i].equals(savedGeofence)){
break;
}
}
spinnerGR.setSelection(i);
Button Save = (Button) findViewById(R.id.save_settings_button);
Save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Integer searchRadius = (Integer)spinnerRA.getSelectedItem();
Integer geofenceRadius = (Integer)spinnerGR.getSelectedItem();
//Saving new value of search_radius
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt(getString(R.string.search_radius),searchRadius);
editor.putInt(getString(R.string.geofence_radius),geofenceRadius);
editor.putBoolean(getString(R.string.initialized),true);
editor.commit();
CharSequence text = "Changes saved succesfully!";
int duration = Toast.LENGTH_LONG;
Toast toast = Toast.makeText(getApplicationContext(), text, duration);
toast.show();
}
});
}catch (Exception e){
e.printStackTrace();
}
}
}
And here is the xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings"
android:textSize="14pt"
android:layout_marginTop="8pt"
android:gravity="center"/>
<View android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="5pt"
android:id="#+id/second_gray_line"
android:background="#android:color/darker_gray"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10pt">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Initial search radius (in meters):"
android:textColor="#FF8500"
android:layout_marginLeft="10pt"
android:layout_marginTop="15pt"
android:textSize="11pt"/>
<Spinner
android:layout_width="60pt"
android:layout_height="wrap_content"
android:id="#+id/search_radius"
android:layout_marginTop="5pt"
android:layout_marginLeft="5pt">
</Spinner>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10pt">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Location radius (in meters):"
android:textColor="#FF8500"
android:layout_marginLeft="10pt"
android:layout_marginTop="15pt"
android:textSize="11pt"/>
<Spinner
android:layout_width="60pt"
android:layout_height="wrap_content"
android:id="#+id/geofence_radius"
android:layout_marginTop="5pt"
android:layout_marginLeft="5pt">
</Spinner>
</LinearLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/save_settings_button"
android:text="Save Settings"
android:textSize="10pt"
android:layout_gravity="center"
android:layout_marginTop="50pt"/>
</LinearLayout>
I think it's a really stupid error, but I can't figure out what causes it. Could you help me? Thank you!
I solved and, as I said, the error was really stupid... I made a mistake here, in the second adapter:
ArrayAdapter<Integer> adapterGR = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, geofenceRadius);
spinnerRA.setAdapter(adapterGR);
This must be changed in:
ArrayAdapter<Integer> adapterGR = new ArrayAdapter<Integer>(this,android.R.layout.simple_spinner_item, geofenceRadius);
spinnerGR.setAdapter(adapterGR);
It was just an oversight. I was setting the second adapter using spinnerRA instead of spinnerGR. Now it works!

Adding views programatically to a relative layout within a scrollview

Please bear with me as I am new to the use of Views and Layouts.
I am trying to create an application in which the user can enter a text field, press a button and have a new text field appear and they can continue adding text fields in this way.
My solution was to have the top level be a scrollview and then have a relative view as a child within that(this way I can then programatically insert more editviews in my code with the OnClick() listener.
I have seen and read a couple of other posts pertaining to relative views but it seems there is still something that I am missing. I have tried
Here is the xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_create_accounts"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.nic.mybudget.CreateAccountsActivity">
<RelativeLayout
android:id="#+id/activity_create_accounts_relativeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fillViewport="true"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/activity_name"
android:inputType="textAutoComplete"
android:layout_alignParentTop="true"
android:id="#+id/activity_createAccounts_relativeLayout_activityName"/>
<Button
android:text="+"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#+id/activity_createAccounts_relativeLayout_activityName"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_addButton" />
<Button
android:text="Save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/activity_create_accounts_relativeLayout_activityName_addButton"
android:layout_centerHorizontal="true"
android:id="#+id/activity_create_accounts_relativeLayout_activityName_saveButton" />
</RelativeLayout>
And here is the Code where I try to add new editviews.
public class CreateAccountsActivity extends Activity {
static private final String TAG = "MAIN-Activity";
int numAccounts = 0;
int lastAccountID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create_accounts);
final RelativeLayout Relative = (RelativeLayout) findViewById(R.id.activity_create_accounts_relativeLayout);
final TextView oldAccount = (TextView) findViewById(R.id.activity_createAccounts_relativeLayout_activityName);
final TextView newAccount = new TextView(this);
final Button addNewAccountButton = (Button) findViewById(R.id.activity_create_accounts_relativeLayout_activityName_addButton);
addNewAccountButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "addNewAccountOnClick");
numAccounts = numAccounts+1;
int newAccountID = oldAccount.getId() + numAccounts;
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
newAccount.setLayoutParams(rlp);
newAccount.setHint("Hint" );
newAccount.setId(newAccountID);
Relative.addView(newAccount);
RelativeLayout.LayoutParams blp = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,ViewGroup.LayoutParams.WRAP_CONTENT);
blp.addRule(RelativeLayout.BELOW, newAccountID-1);
addNewAccountButton.setLayoutParams(blp);
}
});
}
}
As you can see what I am trying (and failing) to do is add the new edit view at the top of the page and simply push everything else down the page. What am I getting wrong here with the relative layout?
Any help is appreciated.
First thing View with id activity_createAccounts_relativeLayout_activityName is EditText and you are casting it with TextView so that is wrong cast it to EditText.
And to your actual problem:
You are using same EditText instance with variable newAccount and adding it again in relative layout if you want to add one more EditText in relative layout you have to initialise EditText inside onclicklistener.
Just add one line newAccount= new EditText(context)in your onclicklistener code before line numAccounts = numAccounts+1;
Happy Coding !

Translate Animation is not working perfectly in dynamic view

In this project, I want to move TextView's clone from top TextView place to bottom TextView place when button is click.
I got a problem in using Translate Animation. I want to move a view to exact position(Like in Zapya).
In Zapya, when user select item and choose "send", the gridview item's clone move to user icon.
So, I use Translate Animation to move view. And I created a view dynamically to show item's clone.
Using Translate Animation works with view that is initially created in xml but not with view that is dynamically created.
The code is here
top = (TextView) findViewById(R.id.textviewtop);
bottom = (TextView) findViewById(R.id.textviewbottom);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int []from = new int[2];
int []to = new int[2];
from[0] = (int) top.getX();
from[1] = (int) top.getY();
to[0] = (int) bottom.getX();
to[1] = (int) bottom.getY();
ViewGroup vg = (ViewGroup) top.getParent();
view = new TextView(getApplicationContext());
view.setWidth(top.getWidth());
view.setHeight(top.getHeight());
view.setX(from[0]);
view.setY(from[1]);
view.setText(top.getText());
view.setVisibility(View.VISIBLE);
view.setTextColor(Color.BLACK);
view.setId(0x23454657);
vg.addView(view);
TranslateAnimation anim = new TranslateAnimation( 0, 0 , from[1], to[1] );
anim.setDuration(1000);
anim.setFillAfter( true );
view.startAnimation(anim);
}
});
and xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textviewtop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true"
android:text="#string/hello_world" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Move"/>
<TextView
android:id="#+id/textviewbottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:text="#string/hello_world" />
</RelativeLayout>
"view" show only starting and ending time. But not in between. How can I solve that.
Please tell if there's any other way. I will thanks a ton.
Increase the duration Time, may be that is a problem
anim.setDuration(4000);

listview row in custom dialog invisible

I have a custom dialog with the format of Header + listview + footer. The corresponding code is as follows: (I don't mind to share the code but it is over 800 lines) ...
/* Setup dialog layout */
View header = (View)getLayoutInflater().inflate(R.layout.stockcountheader, null);
View footer = (View)getLayoutInflater().inflate(R.layout.stockcountfooter, null);
final Dialog listDialog;
listDialog = new Dialog(this, android.R.style.Theme_Dialog);
listDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater li = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = li.inflate(R.layout.dialog, null, false);
listDialog.setContentView(v);
listDialog.setCancelable(true);
final AdditemAdapter adapter = new AdditemAdapter(this, R.layout.additemrow, updateitems);
final ListView filterrow = (ListView) listDialog.findViewById(R.id.dialoglist);
filterrow.addHeaderView(header);
filterrow.addFooterView(footer);
filterrow.setAdapter(adapter);
i = adapter.getCount();
listDialog.show();
In the header, I have a radiobutton and intend to control the visibility of the listview row. So, I have the following code: ...
radiostockresGroup = (RadioGroup)listDialog.findViewById(R.id.radiostockres);
radiostockresButton = (RadioButton) radiostockresGroup.findViewById(radiostockresGroup.getCheckedRadioButtonId());
radiostockresGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
public void onCheckedChanged(RadioGroup radiostockresGroup, int checkedId) {
radiostockresButton = (RadioButton) radiostockresGroup.findViewById(checkedId);
switch(checkedId) {
case R.id.radioresno:
listDialog.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);
break;
case R.id.radioresyes:
listDialog.findViewById(R.layout.tblayout2).setVisibility(View.VISIBLE);
break;
}
}
});
However, it give me the java null pointer error in the "listDialog.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE); " line. The xml for the listview and the dialog is as follows:
stocktakerow.xml
<?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="fill_parent">
<TableLayout android:id="#+id/tblayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:shrinkColumns="0"
android:stretchColumns="*">
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/txtfieldname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="3"
android:gravity="top" />
<EditText android:id="#+id/txtinput"
android:layout_toRightOf="#+id/txtfieldname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_span="3"
android:gravity="top" />
</TableRow>
</TableLayout>
</RelativeLayout>
I try to use " filterrow.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);" and still got the same null pointer.
Please kindly give me a hand, thanks a lot in advance!
Kelvin
If you look at your code, you will notice:
final ListView filterrow = (ListView) listDialog.findViewById(R.id.dialoglist);
listDialog.findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);
Essentially, you are saying that R.id.tblayout2 is a member of the same thing as R.id.dialoglist. I don't think they are in this case.
Not seeing what your class extends, I can only suppose that this should work. If it doesn't, can you please post the class definitions? Just the line like public class MainActivity extends Activity { will do for both classes.
findViewById(R.id.tblayout2).setVisibility(View.INVISIBLE);

Forward compatability issues in alert dialog for Android 2.3.3

I have an issue with an Alert dialog for Android 2.3.3 HTC Desire HD.
Create list view to display items
ArrayList<HashMap<String,String>> lst = new ArrayList<HashMap<String,String>>();
for (int i = 0; i < m_lstSettings.size(); i++)
lst.add( new HashMap<String,String>());
// While we are setting a list adapter, we are only using it to hold the
// structure of the list. We will actually override its data in getView()
// before displaying it.
setListAdapter(
new SimpleAdapter(
this,
lst,
R.layout.settings_row,
new String[] { "text1","text2" },
new int[] { android.R.id.text1, android.R.id.text2 }
) {
/**
* Override for the getView() method. This allows us to alter the display
* attributes on a line-by-line basis. We also use this opportunity to
* fill in the text fields with the right values.
*/
#Override
public View getView(int iPos, View oConvertView, ViewGroup oParent)
{
// Fetch list item based on position
ListItem oItem = m_lstSettings.get(iPos);
// create view for this row from xml layout file
View oView = m_oInflater.inflate(R.layout.settings_row, null);
// for some reason, the logic in this method call seems to be reversed,
// but it works this way
oView.setClickable(oItem.m_bLocked);
// set up the label text view
TextView tvLabel = (TextView) oView.findViewById(android.R.id.text1);
if (tvLabel != null)
{
tvLabel.setText(oItem.m_sLabel);
tvLabel.setEnabled(!oItem.m_bLocked);
}
// set up the value text view
TextView tvValue = (TextView) oView.findViewById(android.R.id.text2);
if (tvValue != null) {
tvValue.setText(oItem.m_sValue);
tvValue.setEnabled(!oItem.m_bLocked);
}
}
return oView;
}
}
);
/**
* Click handler for the list items. Will create the appropriate edit
* box for the clicked setting.
*/
#Override
public void onListItemClick(ListView oParent, View v, int iPos, long id)
{
// inflate the xml layout
final View oView = m_oInflater.inflate(R.layout.settings_edit, null);
final ListView oParentListView = oParent;
final SettingsListItem oItem = m_lstSettings.get(iPos);
// set label
TextView tv = (TextView) oView.findViewById(R.id.TextViewEditSettings);
if (tv != null)
tv.setText(oItem.m_sLabel);
// set value
tv = (TextView) oView.findViewById(R.id.EditTextEditSettings);
if (tv != null) {
tv.setText(oItem.m_sValue);
tv.setInputType(InputType.TYPE_CLASS_TEXT);
}
// special input validator for integers
if (oItem.m_bTypeInt)
tv.setInputType(InputType.TYPE_CLASS_NUMBER);
userName = m_lstSettings.get(3).m_sValue;
// show dialog
new AlertDialog.Builder(this)
.setTitle(getString(R.string.Label_Edit) + " " + oItem.m_sLabel)
.setView(oView)
.setPositiveButton(R.string.main_connection_button_ok_button,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
TextView tv = (TextView) oView.findViewById(R.id.EditTextEditSettings);
oItem.m_sValue = tv.getText().toString();
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tv.getWindowToken(), 0);
oParentListView.invalidate();
dialog.dismiss();
}
})
.setNegativeButton(android.R.string.cancel,
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
TextView tv = (TextView) oView.findViewById(R.id.EditTextEditSettings);
InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE);
imm.hideSoftInputFromWindow(tv.getWindowToken(), 0);
dialog.dismiss();
}
})
.show();
}
XML files:
settings_row.xml:
<?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="wrap_content"
android:orientation="vertical"
android:padding="5dip"
android:background="#color/message_list_item_background"
>
<TextView android:id="#android:id/text1"
android:textSize="24sp"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#android:id/text2"
android:textSize="16sp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
settings_edit.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dip"
>
<TextView
android:gravity="left"
android:text=""
android:id="#+id/TextViewEditSettings"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TextView>
<EditText
android:gravity="left"
android:text=""
android:id="#+id/EditTextEditSettings"
android:layout_width="fill_parent"
android:singleLine="true"
android:layout_height="wrap_content">
</EditText>
</LinearLayout>
Using the above code first we create the list view and upon clicking each item it displays an alert dialog to enter input.
We have 6 items in the list. Upon clicking the 4th item, the app crashes. It's reproducible only on the HTC Desire HD. App is working fine on other Android 2.3 devices, such as Google Nexus S and Sony Ericsson Arc.

Categories

Resources