How to set item height in custom listView - android

I implemented a custom listView as seen here:
http://www.tutecentral.com/android-custom-navigation-drawer/ but I can't get a correct height for my items in the list. Both header and section layout are way too large (height).
Here is my xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:facebook="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/profileLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="0dp"
>
<com.facebook.widget.ProfilePictureView
android:id="#+id/drawer_profilePicture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="3"
/>
<TextView
android:id="#+id/drawer_profileName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="1"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="0dp">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<Button
android:id="#+id/button_logout"
android:text="Logout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/sectionLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
android:minHeight="?android:attr/listPreferredItemHeight"
>
<TextView
android:id="#+id/drawer_sectionName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
/>
</LinearLayout>
<LinearLayout
android:id="#+id/headerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical"
android:layout_marginTop="0dp"
>
<TextView
android:id="#+id/drawer_headerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceLarge"
/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:layout_marginBottom="1dp"
android:layout_marginTop="1dp"
android:background="#DADADC" ></View>
</LinearLayout>
Any suggestion ?
[EDIT] Here is my customDrawerAdapter getview code:
public class CustomDrawerAdapter extends ArrayAdapter<DrawerItem> {
private final JSONObject userProfile;
Context context;
List<DrawerItem> drawerItemList;
int layoutResID;
public CustomDrawerAdapter(Context context, int layoutResourceID,
List<DrawerItem> listItems, JSONObject userProfile) {
super(context, layoutResourceID, listItems);
this.context = context;
this.drawerItemList = listItems;
this.layoutResID = layoutResourceID;
this.userProfile = userProfile;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
DrawerItemHolder drawerHolder;
View view = convertView;
if (view == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
drawerHolder = new DrawerItemHolder();
view = inflater.inflate(layoutResID, parent, false);
drawerHolder.profileName = (TextView) view
.findViewById(R.id.drawer_profileName);
drawerHolder.profilePicture = (ProfilePictureView) view.findViewById(R.id.drawer_profilePicture);
drawerHolder.sectionName = (TextView) view.findViewById(R.id.drawer_sectionName);
drawerHolder.headerName = (TextView) view.findViewById(R.id.drawer_headerName);
drawerHolder.buttonLogout = (Button) view.findViewById(R.id.button_logout);
drawerHolder.buttonLogout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ParseUser.logOut();
Intent intent = new Intent(getContext(), LoginActivity.class);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
view.getContext().startActivity(intent);
}
});
drawerHolder.profileLayout = (LinearLayout) view.findViewById(R.id.profileLayout);
drawerHolder.sectionLayout = (LinearLayout) view.findViewById(R.id.sectionLayout);
drawerHolder.headerLayout = (LinearLayout) view.findViewById(R.id.headerLayout);
view.setTag(drawerHolder);
} else {
drawerHolder = (DrawerItemHolder) view.getTag();
}
DrawerItem dItem = this.drawerItemList.get(position);
if (dItem.isHeader()) {
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.sectionLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.headerLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.headerName.setText(dItem.getHeaderName());
} else if (dItem.getProfilePicture() == 0){
drawerHolder.profileLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.sectionLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.headerLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.sectionName.setText(dItem.getSectionName());
} else {
drawerHolder.profileLayout.setVisibility(LinearLayout.VISIBLE);
drawerHolder.sectionLayout.setVisibility(LinearLayout.INVISIBLE);
drawerHolder.headerLayout.setVisibility(LinearLayout.INVISIBLE);
try {
if (userProfile.getString("facebookId") != null) {
String facebookId = userProfile.get("facebookId")
.toString();
drawerHolder.profilePicture.setProfileId(facebookId);
} else {
// Show the default, blank user profile picture
drawerHolder.profilePicture.setProfileId(null);
}
if (userProfile.getString("name") != null) {
drawerHolder.profileName.setText(userProfile.getString("name"));
} else {
drawerHolder.profileName.setText("");
}
} catch (JSONException e) {
Log.d(CarpoolMeApp.TAG,
"Error parsing saved user data.");
}
}
return view;
}

You can just set the height manually for both the headerName and the sectionName Textviews
android:layout_height="2dp"
like you did with the View in your headerLayout

You can specify layouts gravities, it is most possible reason of issue (too much height).
Also you can use LinearLayout as root layout instead of RelativeLayout.
If the exact height needed, specify it in the layout_height parameter.

Related

Android - update ListView in onCreate does not work

I have the following code:
public class MainActivity extends Activity {
private ListView list;
private static final String path = Environment.getExternalStorageDirectory().getPath() + File.separator + "BOE";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list = (ListView) findViewById(R.id.listView1);
String books[] = {"Mercedes", "Skoda", "Volkswagen", "Audi", "Citroen", "plik"};
BookArrayAdapter bma = new BookArrayAdapter(this, books);
bma.setNotifyOnChange(true);
list.setAdapter(bma);
if (!new File(path).exists()) {
new File(path).mkdirs();
}
for (int i=0; i<books.length; i++) {
View v = list.getAdapter().getView(i, null, null);
String name = ((TextView)v.findViewById(R.id.row_text)).getText() + "";
for (File f:new File(path).listFiles()) {
if (f.isFile()) {
Button b = (Button) v.findViewById(R.id.row_button);
if (name.equals(f.getName().substring(0, f.getName().length()-4))) {
b.setText("Open");
} else {
b.setText("Download");
}
bma.notifyDataSetInvalidated();
bma.notifyDataSetChanged();
Log.d("AC", b.getText().toString());
}
}
}
}
public class BookArrayAdapter extends ArrayAdapter<String> {
private final Context context;
private final String[] values;
public BookArrayAdapter(Context context, String[] values) {
super(context, R.layout.row, values);
this.context = context;
this.values = values;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.row, parent, false);
final TextView textView = (TextView) rowView.findViewById(R.id.row_text);
textView.setText(values[position]);
rowView.findViewById(R.id.row_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (((Button)v).getText().toString().equals("Open")) {
//open
} else {
//Download
}
}
});
return rowView;
}
}
}
and I have problems in for loop in onCreate. this Log shows correct Download/Open texts depenging on Files in file system. But the graphical representation of this list view does not changing. Here's my layout.xml:
<RelativeLayout 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="${relativePackage}.${activityClass}" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
</ListView>
<RelativeLayout
android:id="#+id/main_linear_blackout"
android:layout_width="match_parent"
android:visibility="invisible"
android:layout_height="match_parent"
android:background="#CC000000" >
<ProgressBar
android:id="#+id/main_progress_startup"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
</RelativeLayout>
and my row.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/row_button"
android:orientation="vertical" >
<TextView
android:id="#+id/row_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ProgressBar
android:id="#+id/row_progress"
style="#android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:visibility="invisible" />
</LinearLayout>
<Button
android:id="#+id/row_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/row_percentage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/row_button"
android:layout_alignBottom="#+id/row_button"
android:layout_alignLeft="#+id/row_button"
android:layout_alignRight="#+id/row_button"
android:layout_alignTop="#+id/row_button"
android:gravity="center"
android:textAlignment="center"
android:textSize="20sp"
android:visibility="invisible" />
</RelativeLayout>
how to update it? What is wrong?
EDIT:
I moved editing single row to getView(...) overrided method on my adapter class. I don't really know why my first idea was wrong, but now it works.

Can't click on element in ListView Android

I am trying to create a custom list. My list is contained in a Fragment that correctly implements onScrollListener and populates the list using an adapter. The problem is that I cannot click on each item and I cannot figure out why. Here there is the code of my layout fragment
<?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"
android:background="#ffffff"
android:clickable="true">
<ListView
android:id="#+id/listNotification"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:footerDividersEnabled="false"
android:headerDividersEnabled="false"
android:paddingStart="15dp"
android:paddingEnd="15dp"
android:clickable="true"
/>
</LinearLayout>
and here there is the code of my custom list
<?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"
android:background="#ffffff"
android:clickable="true">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/imageNotification"
android:layout_width="60dp"
android:layout_height="60dp"
android:padding="5dp"
android:focusable="false"/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textNotification"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:padding="2dp"
android:textColor="#33CC33"
android:focusable="false"/>
<TextView
android:id="#+id/idQuestion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"
android:focusable="false"
/>
<TextView
android:id="#+id/typeNotification"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:visibility="gone"
android:focusable="false"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
here there is the code that creates my list using the adapter and setting the onclicklistener
adapter = new NotificationListAdapter(getActivity(), this.rows);
list = (ListView) firstAccessView.findViewById(R.id.listNotification);
list.setAdapter(adapter);
list.setOnScrollListener(this);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
if(adapter.getItem(position).getTypeNotification()==0) {
mNotificationInteface.readQuestion(adapter.getItem(position).getQuestionId());
}
if(adapter.getItem(position).getTypeNotification()==1){
mNotificationInteface.readAnswer(adapter.getItem(position).getQuestionId());
}
}
});
and here there is the code of my adapter
public class NotificationListAdapter extends ArrayAdapter<NotificationItem> {
private View view;
private final Activity context;
private List<NotificationItem> rows;
private int count = 1;
public NotificationListAdapter(Activity context, List<NotificationItem> firstRows ) {
super(context, R.layout.list_notifications, firstRows);
this.context = context;
this.rows = firstRows;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater inflater = context.getLayoutInflater();
view = inflater.inflate(R.layout.list_notifications, null);
view.setPadding(0,10,0,10);
holder = new ViewHolder();
holder.textNotification = (TextView) view.findViewById(R.id.textNotification);
holder.idQuestion = (TextView) view.findViewById(R.id.idQuestion);
holder.typeNotification = (TextView) view.findViewById(R.id.typeNotification);
holder.imageNotification = (ImageView) view.findViewById(R.id.imageNotification);
view.setTag(holder);
} else {
view=convertView;
holder = (ViewHolder) convertView.getTag();
}
int typeNotification = this.rows.get(position).getTypeNotification();
holder.textNotification.setTextColor(Color.BLACK);
holder.idQuestion.setText(String.valueOf(this.rows.get(position).getQuestionId()));
holder.typeNotification.setText(String.valueOf(this.rows.get(position).getTypeNotification()));
if(typeNotification==0){
holder.textNotification.setText(R.string.askQuestion);
holder.imageNotification.setImageResource(R.mipmap.iconuseranonymous);
}
if(typeNotification==1){
//nome da recuperare da con id notifica, quindi id utente quindi dome
holder.textNotification.setText(R.string.answerQuestion);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
if(typeNotification==2){
//nome e immagine da recuperare
holder.textNotification.setText(R.string.newFriend);
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").transform(new CircleTransform()).fit().centerCrop().into(holder.imageNotification);
}
return view;
}
#Override
public NotificationItem getItem(int position){
return this.rows.get(position);
}
#Override
public int getCount() {
return count;
}
public void setCount(int count) {
this.count = count;
}
static class ViewHolder {
ImageView imageNotification;
TextView textNotification;
TextView idQuestion;
TextView typeNotification;
int position;
}
Remove android:clickable="true" from the ListView and it's parent in your XML layout, and also from the root of your list item layout.

ListView with BaseAdapter and LinearLayout?

I'm trying create a BaseAdapter to my ListView. The problem is when I do create a LinearLayout inside other LinearLayout the listener OnItemClickListener doesn't works. If I put the components outside of LinearLayout works fine.
How could I do this works ?
ListView
<?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">
<ListView
android:id="#+id/lvEntregasPendente"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
></ListView>
</LinearLayout>
Adapter 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="wrap_content"
android:background="#ffe3b3"
android:layout_margin="5dp"
android:padding="2dp"
android:id="#+id/llEntregaPendenteVendas">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvVenda"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Venda"
android:textColor="#color/action_bar"
android:textStyle="bold"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrega em: "
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_marginLeft="10dp"
/>
<TextView
android:id="#+id/tvDataEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Data"
android:textColor="#color/action_bar"
android:textStyle="bold"
android:layout_weight="1"
/>
<CheckBox
android:id="#+id/cbEntregue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/action_bar"
android:text="Entregue"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/tvAtrasoEntrega"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Atraso de: 20 dias"
android:textColor="#FF0000"
android:padding="5dp"
android:visibility="visible"
android:layout_weight="1"
android:gravity="right"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Adapter
public class EntregaPendenteListAdapter extends BaseAdapter {
private Context context;
private List<Venda> lista;
private DateControl dateControl;
private EntregaPendenteFrag rpf;
private Venda venda;
public EntregaPendenteListAdapter(Context context, List<Venda> lista, EntregaPendenteFrag rpf) {
this.context = context;
this.lista = lista;
this.rpf = rpf;
dateControl = new DateControl();
}
/** limpa a lista */
public void clearList(){
lista.clear();
notifyDataSetChanged();
}
/** altera lista */
public void changeList(List<Venda> lista){
this.lista = lista;
notifyDataSetChanged();
}
#Override
public int getCount() {
return lista.size();
}
#Override
public Object getItem(int position) {
return lista.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final ViewHolder holder;
Venda venda = lista.get(position);
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.entregas_pendente_adapter, parent, false);
holder.llEntregaPendenteVendas = (LinearLayout) convertView.findViewById(R.id.llEntregaPendenteVendas);
holder.tvVenda = (TextView) convertView.findViewById(R.id.tvVenda);
holder.tvDataEntrega = (TextView) convertView.findViewById(R.id.tvDataEntrega);
holder.tvAtrasoEntrega = (TextView) convertView.findViewById(R.id.tvAtrasoEntrega);
holder.cbEntregue = (CheckBox)convertView.findViewById(R.id.cbEntregue);
convertView.setTag(holder);
}else{
holder = (ViewHolder)convertView.getTag();
}
holder.tvVenda.setText("Venda: " + FormataCodigo.getCodFormat(venda.getId()));
if(venda.getData_entrega() != null){
holder.tvDataEntrega.setText(new SimpleDateFormat("dd-MM-yyyy").format(venda.getData_entrega()));
if(dateControl.getDiasVencido(venda.getData_entrega()) > 0){
holder.tvAtrasoEntrega.setText("Atraso de: " + new DateControl().getDiasVencido(venda.getData_entrega()) + "dias");
holder.tvAtrasoEntrega.setVisibility(View.VISIBLE);
}
}
if((position % 2) == 0){
holder.llEntregaPendenteVendas.setBackgroundColor(Color.parseColor("#ffe3b3"));
}else{
holder.llEntregaPendenteVendas.setBackgroundColor(Color.WHITE);
}
return convertView;
}
private static class ViewHolder{
LinearLayout llEntregaPendenteVendas;
TextView tvVenda;
TextView tvDataEntrega;
TextView tvAtrasoEntrega;
CheckBox cbEntregue;
}
}
Activity
//listview
lvEntregasPendente = (ListView)view.findViewById(R.id.lvEntregasPendente);
lvEntregasPendente.setOnItemClickListener(this);
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.i("ITEM->", position + "");
}
It seems your CheckBox is stealing the focus.
Try setting these properties on it:
android:focusable="false"
android:focusableInTouchMode="false"
android:clickable="false"

Display Repeatation Item in ListView after three Item on Scrolling in android?

I want to fill Listview but But Repeat only imgLike,imgComments,txtLikeUnlike and txtComments from all Items. Because I using array for imgLike,imgComments,txtLikeUnlike and txtComments for get (fetch)selected Position. what is mistake in my code please guide me.
My code,
/** Adapter Class */
public class Adapter1 extends BaseAdapter {
ImageView imgImage = null,imgUnlike[] = null, imgLike[] = null,imgComments[] = null, imgShare[] = null;
TextView txtId = null,txtLikeUnlike[] = null, txtComments[] = null;
public ArrayList<HashMap<String, String>> arr = null;
Context context = null;
LayoutInflater layoutInflater = null;
HashMap<String, String> getData = new HashMap<String, String>();
public Adapter1(Context context,
ArrayList<HashMap<String, String>> arr) {
this.context = context;
this.arr = arr;
layoutInflater = LayoutInflater.from(context);
this.imgUnlike = new ImageView[arr.size()];
this.imgLike = new ImageView[arr.size()];
this.imgComments = new ImageView[arr.size()];
this.imgShare = new ImageView[arr.size()];
this.txtLikeUnlike = new TextView[arr.size()];
this.txtComments = new TextView[arr.size()];
}
#Override
public int getCount() {
return arr.size();
}
#Override
public Object getItem(int position) {
return arr.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("InflateParams")
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View row = null;
if (convertView == null) {
LayoutInflater inflater = ((Activity) context)
.getLayoutInflater();
row = inflater.inflate(R.layout.list_item, parent,
false);
} else {
row = convertView;
}
/** Initialize Widgets */
/** Imageview */
imgImage = (ImageView) row
.findViewById(R.id.imgImage);
imgUnlike[position] = (ImageView) row
.findViewById(R.id.imgUnlike);
imgLike[position] = (ImageView) row
.findViewById(R.id.imgLike);
imgComments[position] = (ImageView) row
.findViewById(R.id.imgComments);
imgShare[position] = (ImageView) row
.findViewById(R.id.imgShare);
/** TextView */
txttId = (TextView) row
.findViewById(R.id.txtId);
txtLikeUnlike[position] = (TextView) row
.findViewById(R.id.txtLikeUnlike);
txtComments[position] = (TextView) row
.findViewById(R.id.txtComments);
getData = arr.get(position);
txtId.setText(getData.get(Fragment1.TAG_ID));
imgUnlike[position]
.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imgUnlike[position]
.setVisibility(View.INVISIBLE);
imgLike[position]
.setVisibility(View.VISIBLE);
getCurrentPosition = position;
}
});
imgLike[position].setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
imgNewsFeedLike[position].setVisibility(View.INVISIBLE);
imgNewsFeedUnlike[position].setVisibility(View.VISIBLE);
}
});
row.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getActivity(),
Detail.class);
startActivity(intent);
}
});
return row;
}
xml file is,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/masterLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/five_dp" >
<ImageView
android:id="#+id/imgImage"
android:layout_width="fill_parent"
android:layout_height="150dp"
android:layout_below="#+id/imgBlueStrip"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:background="#null"
android:contentDescription="#string/content_description"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imgImage"
android:background="#drawable/strip"
android:baselineAligned="false"
android:gravity="center_vertical"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="#+id/imgUnlike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:contentDescription="#string/content_description"
android:src="#drawable/unlike" />
<ImageView
android:id="#+id/imgLike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:contentDescription="#string/content_description"
android:src="#drawable/like"
android:visibility="invisible" />
<TextView
android:id="#+id/txtLikeUnlike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="2dp"
android:layout_toRightOf="#+id/imgUnlike"
android:background="#drawable/get_notification_bg"
android:gravity="center"
android:padding="2dp"
android:textColor="#color/white"
android:textSize="10sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="#+id/imgComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:contentDescription="#string/content_description"
android:src="#drawable/comments" />
<TextView
android:id="#+id/txtComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/imgComments"
android:background="#drawable/get_"
android:gravity="center"
android:padding="2dp"
android:textColor="#android:color/white"
android:textSize="10sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:id="#+id/imgShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerInParent="true"
android:contentDescription="#string/content_description"
android:src="#drawable/share" />
</RelativeLayout>
</LinearLayout>
<TextView
android:id="#+id/txtId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="10dp"
android:text="id" />
<ImageView
android:id="#+id/imgBlueStrip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/txtId"
android:contentDescription="#string/content_description"
android:src="#drawable/blue_strip" />
</RelativeLayout>
My Layout Display Like this,

UI in Graphical Layout and in device are not the same [Android]

ListView in Graphical Layout is fit on appropriate height but when i imported into android device, ListView is reduced in a small field. i want ListView are the same in Graphical Layout in xml file and in real device screen
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_margin="10sp"
android:id="#+id/header"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/id"
android:layout_weight="1.25"
android:gravity="center"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/amount"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/alert"
android:layout_weight="1"
android:gravity="center"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/detail"
android:layout_weight="1"
android:gravity="center"/>
</LinearLayout>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_margin="10sp"></ListView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="2.5">
<Button
android:id="#+id/addQueue"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:text="#string/addQueue"/>
<Button
android:id="#+id/submit"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:text="#string/submit"/>
<Button
android:id="#+id/reset"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/addQueue"
android:layout_centerHorizontal="true"
android:layout_margin="10dp"
android:text="#string/resetQueue" />
</RelativeLayout>
I have tried to modify your layout. Try this may be it will be helpful for you.. just create a new xml layout file and check it is working for you or not.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/addQueue"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/submit"
android:text="addQueue" />
<Button
android:id="#+id/submit"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="submit" />
<Button
android:id="#+id/reset"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="resetQueue" />
<LinearLayout
android:id="#+id/header"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1.25"
android:gravity="center"
android:text="text1" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text2" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text3" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="text4" />
</LinearLayout>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/addQueue"
android:layout_alignParentLeft="true"
android:layout_below="#+id/header" >
</ListView>
</RelativeLayout>
My custom adapter class is here. :
public class my_custom_adapter extends ArrayAdapter<String> {
private Context context = null;
String[] elements = null;
private ArrayList<Boolean> itemChecked = null;
public my_custom_adapter(Context context, int type, String[] elements2)
{
super(context, type, elements2);
this.elements = elements2;
this.context = context;
itemChecked = new ArrayList<Boolean>();
BroadcastReceiver receiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals("master_check_change")) {
boolean check_value = intent.getBooleanExtra("check_value", false);
set_checked(check_value);
notifyDataSetChanged();
}
}
};
context.registerReceiver(receiver, new IntentFilter("master_check_change"));
set_checked(false);
}
// AS EVERY TIME LISTVIEW INFLATE YOUR VIEWS WHEN YOU MOVE THEM SO YOU NEED TO SAVE ALL OF YOUR CHECKBOX STATES IN SOME ARRAYLIST OTHERWISE IT WILL SET ANY DEFAULT VALUE.
private void set_checked(boolean is_checked)
{
for (int i=0; i < elements.length; i++) {
itemChecked.add(i, is_checked);
}
}
//THIS IS SIMPLY A CLASS VIEW WILL HOLD DIFFERENT VIEWS OF YOUR ROW.
static class ViewHolder
{
public TextView tv;
public CheckBox cb;
public ImageView iv;
}
#Override
public View getView (final int position, View convertView, ViewGroup parent)
{
View rowView = convertView;
ViewHolder holder = null;
if (rowView == null) {
LayoutInflater inflater = (LayoutInflater)context.getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
// HERE I AM INFLATING LISTVIEW LAYOUT.
rowView = inflater.inflate(R.layout.inflated_layout, null, false);
holder = new ViewHolder();
holder.cb = (CheckBox) rowView.findViewById(R.id.checkBox1);
holder.tv = (TextView) rowView.findViewById(R.id.textView1);
holder.iv = (ImageView) rowView.findViewById(R.id.imageView1);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
if (holder != null) {
holder.tv.setText(elements[position]);
holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
itemChecked.set(position, isChecked);
}
});
if(position < itemChecked.size()) {
holder.cb.setChecked(itemChecked.get(position));
}
}
return rowView;
}
}
My activity class is here where i am create custom adapter object,
public class Test_stflowActivity extends Activity {
public CheckBox master_cb = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.temp_layout);
String[] elements = new String[10];
for (int i = 0; i < 10; i++) {
elements[i] = "elements " + i;
}
CheckBox master_cb = new CheckBox(getApplicationContext());
master_cb.setText("Check All");
//HERE IS THE LIST VIEW WHICH I HAVE CREATED IN MY XML FILE.
ListView lv = (ListView) findViewById(R.id.listView1);
//HERE I AM CREATING CUSTOM ADAPTER OBJECT.
my_custom_adapter adapter = new my_custom_adapter(this, android.R.layout.simple_list_item_1, elements);
lv.addHeaderView(master_cb);
lv.setAdapter(adapter);
master_cb.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
Intent my_intent = new Intent("master_check_change");
my_intent.putExtra("check_value", isChecked);
sendBroadcast(my_intent);
}
});
}
}

Categories

Resources