0

I have created a customized listView using the following tutorial http://www.ezzylearning.com/tutorial.aspx?tid=1763429.

My list view includes two row (each with an image and a TextView). The first row is user, and the second is password. I am looking for a way to make the password row to masked, something like ****, and to add another row that will enable the user to set it to visible/ mask.

I found the following examples, How to show hidden password in textview? How to switch between hide and view password

but I have no idea how to implement this on a specific row.

my rows 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="fill_parent"
    android:orientation="horizontal" >

     <ImageView android:id="@+id/imgUserAccountIcon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_marginRight="10dp"
        android:layout_marginTop="5dp"
        android:gravity="center_vertical"
        android:scaleType="fitStart" />

     <TextView android:id="@+id/txtTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp" 
                    android:layout_marginTop="5dp"
            />


</LinearLayout>

My user class

public class UserAccountData {
    public int icon;
    public String title;

    public UserAccountData(){
        super();
    }


public UserAccountData(int icon, String title) {
    super();
    this.icon = icon;
        this.title = title;
    }
}

My adapter class

public class UserAccountAdapter extends ArrayAdapter<UserAccountData> {
    Context context;
    int layoutResourceId;
    UserAccountData data[] = null;
public UserAccountAdapter(Context context, int layoutResourceId,
        UserAccountData[] data) {
    super(context, layoutResourceId, data);
    this.layoutResourceId = layoutResourceId;
    this.context = context;
    this.data = data;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    View row = convertView;
    UserAccountDataHolder holder = null;

    if (row == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        row = inflater.inflate(layoutResourceId, parent, false);

        holder = new UserAccountDataHolder();
        holder.imgIcon = (ImageView) row.findViewById(R.id.imgUserAccountIcon);
        holder.txtTitle = (TextView) row.findViewById(R.id.txtTitle);

        row.setTag(holder);
    } else {
        holder = (UserAccountDataHolder) row.getTag();
    }

    UserAccountData userAccountData = data[position];
    holder.txtTitle.setText(userAccountData.title);     
    holder.imgIcon.setImageResource(userAccountData.icon);


    return row;
}

static class UserAccountDataHolder {
    ImageView imgIcon;
    TextView txtTitle;
}

}

and the appropriate list view snippet of the activity method

List<UserAccountData> user_data = new ArrayList<UserAccountData>();
user_data.add(new UserAccountData(R.drawable.username_icon,"userName");
user_data.add(new UserAccountData(R.drawable.password_icon,"password");
usersArray = new UserAccountData[user_data.size()];
user_data.toArray(usersArray);

UserAccountAdapter adapter = new UserAccountAdapter(this,                       R.layout.user_accounts_row, usersArray);

userAccountsListView = (ListView) findViewById(R.id.userAccounts);

   userAccountsListView.setAdapter(adapter);

Attaching a picture of what I would like to accomplish

Before click:

enter image description here

after click

enter image description here

thanks

Community
  • 1
  • 1
Quantico
  • 2,398
  • 7
  • 35
  • 59

3 Answers3

0

You could fill up a list of TextViews in your adapter while you inflated. After inflation, you'd have access to all the TextViews, and then in your onItemClickListener, when someone clicked the item in in Position 2 (in this case at least), you simply reference your list of TextViews, get the appropriate one, and change the parameters on the fly.

Caerulius
  • 425
  • 4
  • 21
0

If you have a set, small number of rows like this a TableLayout is a better fit than a ListView. Then you could easily customize the xml of the two TextViews. Another option is to call setRawInputType to set the password on just the second TextView. You'd need to do that in the adapter's getView function based on the view's position.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127
  • The issue is that my list might include more data. I will always have password and user name, and password will always be the last row but in between I might have phone number, email, etc. So I don't think table will work. Can you explain a little bit on how to change the adapter's getView? – Quantico Jan 25 '13 at 21:48
  • Ok, then it makes sense to use a listView. THe change to getView- basically, when getView is called to inflate the views you need to know what type of data is at each position. You'd need to know this anyway to display your data. This is the spot where you should configure all of the settings that aren't in xml- the text of the view, sources of images, etc. Here since the type of the TextView changes based on settings and row, you should call setRawInputType() with the right mode on all rows to set it to the correct type. – Gabe Sechan Jan 25 '13 at 22:05
  • I am sorry, for asking again, but I am new to android development , and am a little confuse about where I should call setRawInputType. The thing is that I don't know if the password will be in the 2nd position or not, it might be in different positions based on the previous data that I got. For example I might have a list with 4 rows before the password, and that list will continue to have few more rows and another password row there. – Quantico Jan 25 '13 at 22:26
0

Well I got around this problem by displaying the password as plain-text in an AlertDialog when the user clicks the ListView entry.

Adeel Shahzad
  • 188
  • 1
  • 16