0

I'm trying to create a simple Activity in Android where user can easily sign up to my sqlite database. I want to give the chance to select some kind of avatar stored in my drawable folder and set that image in an ImageView.

The problem is that when i click on mi Set Avatar button i can only see a blank fragment without the content i want.

Here's my code :

Row Model

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_margin="16dp"
>
<ImageView
    android:id="@+id/avatar_img"
    android:layout_width="0dp"
    android:layout_weight="0.9"
    android:layout_height="50dp"

    android:src="@drawable/user" />
<TextView
    android:id="@+id/avatar_title"
    android:text="Avatar"
    android:layout_weight="2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textSize="20sp"
    android:layout_marginLeft="5dp"/>

RegisterActivity layout file:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:label="Sign In"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="it.docdev.openlibrary.activities.RegisterActivity">


<!-- Login progress -->
<ImageView
    android:id="@+id/imageView2"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    app:srcCompat="@drawable/add_user" />

<ProgressBar
    android:id="@+id/login_progress"
    style="?android:attr/progressBarStyleLarge"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginBottom="8dp"
    android:visibility="gone" />

<ScrollView
    android:id="@+id/login_form"
    android:layout_marginTop="40dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <LinearLayout
        android:id="@+id/email_login_form"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <android.support.design.widget.TextInputLayout
            android:id="@+id/user_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

        <AutoCompleteTextView
            android:id="@+id/username"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:hint="Username"
            android:layout_marginBottom="16dp"
            android:inputType="text"
            android:maxLines="1" />

        <android.support.design.widget.TextInputLayout
            android:id="@+id/pass_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

            <AutoCompleteTextView
                android:id="@+id/password"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Password"
                android:layout_marginBottom="16dp"
                android:inputType="textPassword"
                android:maxLines="1"
                />

        <android.support.design.widget.TextInputLayout
            android:id="@+id/repeat_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"/>

            <AutoCompleteTextView
                android:id="@+id/password2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:hint="Repeat Password"
                android:inputType="textPassword"
                android:maxLines="1"
                />

        <RelativeLayout
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">

            <ImageView
                android:id="@+id/uk"
                android:layout_width="75dp"
                android:layout_height="83dp"
                android:layout_marginTop="16dp"
                android:layout_marginBottom="8dp"
                app:srcCompat="@drawable/uk" />
            <TextView
                android:id="@+id/name"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Selected Avatar"
                android:layout_toRightOf="@id/uk"
                android:layout_marginTop="48dp"
                android:layout_marginLeft="16dp"
                android:layout_marginRight="32dp"
                />
            <Button
                android:id="@+id/setavatar"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_toRightOf="@id/name"
                android:layout_marginTop="32dp"
                android:layout_marginLeft="16dp"
                android:text="Set Avatar"
                />


        </RelativeLayout>



        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">

            <Button
                android:id="@+id/register"
                style="?android:textAppearanceSmall"
                android:layout_width="0dp"
                android:layout_height="wrap_content"
                android:layout_marginStart="8dp"
                android:layout_marginTop="16dp"
                android:layout_weight="1"
                android:background="@color/colorAccent"
                android:text="Register"
                android:layout_marginBottom="8dp"
                android:textStyle="bold" />
        </LinearLayout>
    </LinearLayout>
</ScrollView>

list layout

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android = "http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="411dp"
 android:orientation="vertical"
>
    <ListView xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/listview1"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
    >


    </ListView>

</RelativeLayout>

My Adapter Class

package it.docdev.openlibrary.layout;

import android.content.Context;
import android.view.LayoutInflater; 
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import it.docdev.openlibrary.R;


public class Adapter extends ArrayAdapter<String> {

private Context c;
private String[] avatars;
private int[] images;
private LayoutInflater inflater;

public Adapter(Context c, String[] avatars, int[] images) {
    super(c, R.layout.avatarmodel, avatars);

    this.c = c;
    this.avatars = avatars;
    this.images = images;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
    if (convertView == null) {
        inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        convertView = inflater.inflate(R.layout.avatarmodel, null);
    }

    TextView name = (TextView) convertView.findViewById(R.id.avatar_title);
    ImageView img = (ImageView) convertView.findViewById(R.id.avatar_img);

    name.setText(avatars[position]);
    img.setImageResource(images[position]);
    return convertView;
}
}

My Fragment Class

package it.docdev.openlibrary.layout;

import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;

import it.docdev.openlibrary.R;



public class AvatarFragment extends DialogFragment {

private ListView lv;
private String[] avatars = {"Avatar 1", "Avatar 2", "Avatar 3", "Avatar 4", "Avatar 5", "Avatar 6", "Avatar 7", "Avatar 8", "Avatar 9", "Avatar 10", "Avatar 11", "Avatar 12", "Avatar 13", "Avatar 14", "Avatar 15"};
private int[] images = {R.drawable.avatar1, R.drawable.avatar2, R.drawable.avatar3, R.drawable.avatar4, R.drawable.avatar5, R.drawable.avatar6, R.drawable.avatar7, R.drawable.avatar8, R.drawable.avatar9, R.drawable.avatar10, R.drawable.avatar11, R.drawable.avatar12, R.drawable.avatar13, R.drawable.avatar14, R.drawable.avatar15};


public View onCreateView(LayoutInflater l, ViewGroup container, Bundle savedInstaceState) {
    View root = l.inflate(R.layout.list, container, false);

    lv = (ListView) root.findViewById(R.id.listview1);

    getDialog().setTitle("Choose Your Avatar");

    Adapter a = new Adapter(getActivity(), avatars, images);
    lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
            Toast.makeText(getActivity(), avatars[pos], Toast.LENGTH_SHORT).show();
        }
    });

    return root;
}


}

Please, help me. Thank you

0 Answers0