I'm developing a Chat App, so I create a list of friends.
Take a look at my ListView declaration at layout.xml
<ListView
android:listSelector="#android:color/transparent"
android:id="#+id/usersList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#android:color/transparent"
android:cacheColorHint="#android:color/transparent"
android:divider="#android:color/transparent"
android:dividerPadding="2dp"
android:overScrollFooter="#null"
android:scrollingCache="false"
android:transcriptMode="normal"
/>
And the behavior of this ListView is annoying.
When I run my app in a phone with Android 2.3 the TextView itens of my ListView becomes transparent just like the image below:
Sometimes when a press the itens the color of TextView back to normality.
When I run my app in a phone with Android 4.1 this problem not happens.
My user item xml:
<?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:background="#android:color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:adjustViewBounds="true"
android:src="#drawable/quick_blox_user" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:orientation="vertical" >
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray"
android:textSize="25sp" />
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray_light"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/layout_pending_messages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button_message_pending_number"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="#color/color_new_message_title"
android:padding="2dp"
android:text="12"
android:textColor="#android:color/white"
android:textSize="24sp" />
<TextView
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:text="#string/pending_messages"
android:textColor="#color/color_new_message"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My Adapter:
public class UserAdapter extends BaseCustomAdapter<UserItem> {
public UserAdapter(Activity a, List<UserItem> d) {
super(a, d);
}
#Override
public View getView(int position, View view, ViewGroup parent) {
if(view == null) {
view = LayoutInflater.from(activity).inflate( R.layout.user_quickblox_item, null);
}
UserItem user = data.get(position);
String userLogin = user.getUser().getLogin();
int index = userLogin.indexOf("#");
if(index != -1) {
userLogin = userLogin.substring(0, index);
}
int count = ChatMessageDAO.getInstance(activity).countMessagePendentBySenderId(user.getUser().getId());
View viewPendingMessages = view.findViewById(R.id.layout_pending_messages);
if(count == 0) {
viewPendingMessages.setVisibility(View.GONE);
} else {
viewPendingMessages.setVisibility(View.VISIBLE);
Button buttonMessageNumberIndicator = (Button)view.findViewById(R.id.button_message_pending_number);
buttonMessageNumberIndicator.setText(String.valueOf(count));
}
TextView textViewUser = (TextView)view.findViewById(R.id.text_view_user);
textViewUser.setText(userLogin);
TextView textViewUserLastActivity = (TextView)view.findViewById(R.id.text_view_user_last_activity);
long diff = new Date().getTime() - user.getUser().getLastRequestAt().getTime();
long diffInDays = TimeUnit.DAYS.convert(diff, TimeUnit.MILLISECONDS);
if(diffInDays == 0) {
long diffInHours = TimeUnit.HOURS.convert(diff, TimeUnit.MILLISECONDS);
if(diffInHours == 0) {
long diffInMinutes = TimeUnit.MINUTES.convert(diff, TimeUnit.MILLISECONDS);
if(diffInMinutes < 0) {
textViewUserLastActivity.setText("Esta online agora");
} else {
textViewUserLastActivity.setText("Entrou hoje " + diffInMinutes+ " minuto(s) atrás");
}
} else {
textViewUserLastActivity.setText("Entrou hoje " + diffInHours+ " horas atrás");
}
} else if(diffInDays == 1) {
Calendar calendar = Calendar.getInstance();
calendar.setTime(user.getUser().getLastRequestAt());
int hour = calendar.get(Calendar.HOUR_OF_DAY);
textViewUserLastActivity.setText("Entrou ontem as " + hour + " horas");
} else {
textViewUserLastActivity.setText("Entrou " + diffInDays + " dia(s) atras");
}
return view;
}
}
How to avoid this problem?
There is a chance that the TextViews are not showing at all. not the colour is changing. you can check that by setting a background for the textviews.
A few mistakes in the item xml I would like to point out:
You have two items in the horizontal layout. the linear layout that contains the image and the linear layout that contains textview. In this case you can't have them both match_parent. the total size would be screen size * 2.
when setting weight for a linear layout in a horizontal container you set the width to 0dp.
your weight sum is 1 + 0.5 = 1.5. means that the image will reserve 2/3 of screen size and textview 1/3. looks un-logical.
These un logical values could cause different layout display for different os version.
Solution:
<?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:background="#android:color/white"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:padding="5dp" >
<LinearLayout
**android:layout_width="0dp"**
android:layout_height="match_parent"
**android:layout_weight="1"**
android:orientation="vertical" >
<ImageView
android:layout_width="65dp"
android:layout_height="65dp"
android:layout_gravity="center"
android:layout_margin="5dp"
android:adjustViewBounds="true"
android:src="#drawable/quick_blox_user" />
</LinearLayout>
<LinearLayout
**android:layout_width="0dp"**
android:layout_height="match_parent"
**android:layout_weight="2"**
android:orientation="vertical" >
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray"
android:textSize="25sp" />
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray_light"
android:textSize="12sp" />
<LinearLayout
android:id="#+id/layout_pending_messages"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/button_message_pending_number"
android:focusable="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:background="#color/color_new_message_title"
android:padding="2dp"
android:text="12"
android:textColor="#android:color/white"
android:textSize="24sp" />
<TextView
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:text="#string/pending_messages"
android:textColor="#color/color_new_message"
android:textSize="14sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
What solved my problem was android:background:
<LinearLayout
android:background="#android:color/white
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:orientation="vertical" >
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray"
android:textSize="25sp" />
<TextView
android:cacheColorHint="#android:color/transparent"
android:id="#+id/text_view_user_last_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="asdasdad"
android:textColor="#color/text_gray_light"
android:textSize="12sp" />
At my LinearLayout which has the TextViews at my user_item.xml(The layout of itens of the List). I put background color as White. Its strange because the main ViewGroup of this layout is already set with background color White. Now My itens appears normally.
An Annoying solution for an Annoying problem.
Related
how to create this kind of layout in android
i have tried using table layout. but getting error in creating this.
please check my code i am able to display images. 1 image in 1 row. but i want 2 small images in 1 row. and continues.
here is my code
private void getBannerList() {
final Call<BannerList_Model> itemMainResponse = APIHandler.getApiService().bannerList(getFiledMapForItemList());
itemMainResponse.enqueue(new Callback<BannerList_Model>() {
#Override
public void onResponse(Response<BannerList_Model> response, Retrofit retrofit) {
BannerList_Model itemResponse = null;
itemResponse = response.body();
if (itemResponse == null) {
UIUtils.showToast(getContext(), "Server Error !");
} else {
String msg = itemResponse.getMsg().toString();
if (msg.equals("success")) {
userList = itemResponse.getBannerList();
if(userList != null) {
TableLayout table = (TableLayout) view.findViewById(R.id.tblBanner_list);
table.setColumnStretchable(0,true);
table.setColumnStretchable(1,true);
int tsize = userList.size();
TextView[] t1 = new TextView[tsize];
ImageView[] img = new ImageView[tsize];
for(int i=0; i<userList.size();i++)
{
TableRow row = new TableRow(getContext());
String banner_size = userList.get(i).getBannerSize().toString();
img[i] = new ImageView(getContext());
Picasso.with(getContext())
.load(Constants.VIDEO_URL + userList.get(i).getBannerUrl())
.into(img[i]);
row.addView(img[i]);
table.addView(row);
}
}
}
}
}
}
});
}
here is the video how i want layout
http://shrivivah.com/layout.mp4
if there is 2 small images than it will display in 1 row.
if there is 2 medium or full size image than it will display in 1 row each.
if 1 small and 2 medum than 1 image per row to display.
layout image
Your mistake is that you are adding TextView to TableRow(row) but you didn't add row to TableLayout. Here is an example for what you need. You can set some margins and add button.
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical">
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="TextView" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
</TableRow>
</TableLayout>
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<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:orientation="vertical"
tools:context="com.example.tops.stackoverflow.MainActivity">
<LinearLayout
android:id="#+id/llShortcutParent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:elevation="20dp"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:duplicateParentState="true"
android:elevation="20dp"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:duplicateParentState="true"
android:elevation="20dp"
android:padding="10dp"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Button1"
android:layout_weight="1"/>
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="Button1"
android:layout_weight="1"/>
</LinearLayout>
</LinearLayout>
I have two views in my fragment. In the first view, I have a linear layout, which holds some controls and in the second view, I have a listview.
I want the linear layout in the first view to be collapsed / expand when I scroll up / down listview.
I tried to handle scroll up / down events of listview in the OnScrollListener and collapse / expand the listview.
But there is a problem with the onScroll method,i.e, when I scroll the listview, it fires many times when I scroll the listview one time. So expanding and collapsing process of linear layout is going crazy.
Here is - what is happening on the screen.
https://www.youtube.com/watch?v=U7KNwS6JlUk
What am I doing wrong?
What is the best way to handle scrol up / down events of listview and collapse / expand linear layout?
My Layout
<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="com.training.mehmetyilmaz.mywallet.TransactionsActivity.TransactionsFragment"
android:orientation="vertical">
<LinearLayout
android:id="#+id/transactions_filter_linear_layout"
android:layout_width="match_parent"
android:layout_height="160dp"
android:orientation="vertical"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#color/blue">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/account_type_label"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
/>
<RadioGroup
android:id="#+id/transactions_account_type_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/transactions_rd_cash"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/cash"
android:checked="true"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
android:buttonTint="#color/white" />
<RadioButton
android:id="#+id/transactions_rd_bank"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/bank"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
android:buttonTint="#color/white"
/>
</RadioGroup>
</LinearLayout><!-- Money Add Type-->
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/currency_label"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
/>
<Spinner
android:id="#+id/transactions_spinner_currency"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/entry_currency"
android:layout_gravity="right"
android:gravity="right|end"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
style="#style/DropDownColor"
android:background="#drawable/abc_spinner_mtrl_am_alpha"
>
</Spinner>
</LinearLayout><!-- Currency -->
</LinearLayout><!-- First Line-->
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/cyan_ligth"
android:layout_marginTop="1dp"
android:layout_marginBottom="1dp"
>
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
android:layout_marginTop="5dp"
>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/money_type_label"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
/>
<RadioGroup
android:id="#+id/transactions_money_type_radio_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton
android:id="#+id/transactions_rd_add"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/add"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
android:buttonTint="#color/white" />
<RadioButton
android:id="#+id/transactions_rd_sub"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/subtract"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
android:buttonTint="#color/white" />
</RadioGroup>
</LinearLayout><!-- Money Type-->
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/entry_date_label_all_dates"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="#dimen/transactions_filter_text"
android:textColor="#color/white"
/>
<Spinner
android:id="#+id/transactions_spinner_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/entry_date"
android:layout_gravity="right"
android:gravity="right|end"
android:textSize="#dimen/transactions_filter_text"
style="#style/DropDownColor"
android:background="#drawable/abc_spinner_mtrl_am_alpha">
</Spinner>
</LinearLayout><!-- Date -->
</LinearLayout><!-- Second Line -->
</LinearLayout><!-- Filter -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.training.mehmetyilmaz.mywallet.ScrollDetectingListView
android:layout_width="match_parent"
android:layout_height="320dp"
android:id="#+id/transactions_list_view"
></com.training.mehmetyilmaz.mywallet.ScrollDetectingListView>
<TextView
android:layout_width="match_parent"
android:layout_height="50dp"
android:id="#+id/transactions_total_textView"
android:text="#string/total"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:paddingBottom="5dp"
android:paddingTop="5dp"
android:textSize="#dimen/abc_text_size_medium_material"
android:background="#color/green"
android:textColor="#color/white"
android:gravity="center"
android:layout_alignParentBottom="true"
/>
</RelativeLayout>
</LinearLayout>
OnScrollListener
mTransactionListView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
//Log.d("Scroll State", "State : " + scrollState);
}
private int mInitialScroll = 0;
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
Log.d("Scroll", "Called");
int scrolledOffset = mTransactionListView.getVerticalScrollOffset();
if (scrolledOffset != mInitialScroll) {
boolean scrollUp = (scrolledOffset - mInitialScroll) < 0;
if (scrollUp) {
Log.d("Scroll", "Up");
expand(mTransactionsFilterLinearLayout);
} else {
Log.d("Scroll", "Down");
collapse(mTransactionsFilterLinearLayout);
}
mInitialScroll = scrolledOffset;
}
}
});
I think the solution is not possible with the classic ListView of Android SDK.
I found a custom ListView called Android-ObservableScrollView and implemented it in my project and the result is success.
You can find it from here
https://github.com/ksoichiro/Android-ObservableScrollView/
try setting a booolean value like this as global.
boolean flag = true;
then
if (scrollUp) {
if(flag == false){
Log.d("Scroll", "Up");
expand(mTransactionsFilterLinearLayout);
}
flag = true;
} else {
if(flag == true){
Log.d("Scroll", "Down");
collapse(mTransactionsFilterLinearLayout);
}
flag = false;
}
I am trying to implement a Listview with alphabet indexing . I succeed in displaying it but the problem is the text in Listview goes inside the alphabets rather than displaying horizontally. Please help . Song name went inside the alphabet indexer. Pasting below the xml file and code:
List item xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:onClick="songPicked"
android:orientation="vertical"
>
<LinearLayout
android:id="#+id/sort_key_layout"
android:layout_width="fill_parent"
android:layout_height="18dip"
android:background="#EAEAEA" >
<TextView
android:id="#+id/sort_key"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dip"
android:textColor="#000000"
android:textSize="13sp" />
</LinearLayout>
<TextView
android:id="#+id/tv_song_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
</LinearLayout>
Listview 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" >
<FrameLayout
android:id="#+id/title_layout"
android:layout_width="fill_parent"
android:layout_height="18dip"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:background="#EAEAEA" >
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dip"
android:textColor="#000000"
android:textSize="13sp" />
</FrameLayout>
<LinearLayout
android:id="#+id/indexer_layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentRight="true"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="vertical" >
<ListView
android:id="#+id/lv_song_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:cacheColorHint="#null"
android:paddingLeft="5dp"
android:paddingRight="5dp"
>
</ListView>
</LinearLayout>
</RelativeLayout>
Adapterclass:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
Log.d(this.getClass().getName(), "inside getview");
if(convertView == null) {
Log.d(this.getClass().getName(), "inside if getview & position is"+position);
convertView = songInf.inflate(R.layout.song, null);
holder = new ViewHolder();
holder.sortKeyLayout = (LinearLayout) convertView.findViewById(R.id.sort_key_layout);
holder.sortKey = (TextView) convertView.findViewById(R.id.sort_key);
holder.songName = (TextView) convertView.findViewById(R.id.tv_song_title);
holder.songArtist = (TextView)convertView.findViewById(R.id.tv_song_artist);
convertView.setTag(holder);
} else {
Log.d(this.getClass().getName(), "inside else getview & position is"+position);
holder = (ViewHolder) convertView.getTag();
}
Song currSong = songs.get(position);
holder.songName.setText(currSong.getTitle());
holder.songArtist.setText(currSong.getArtist());
int section = mIndexer.getSectionForPosition(position);
if (position == mIndexer.getPositionForSection(section)) {
holder.sortKey.setText(currSong.getSortKey());
holder.sortKeyLayout.setVisibility(View.VISIBLE);
} else {
holder.sortKeyLayout.setVisibility(View.GONE);
}
return convertView;}![enter image description here][1]
I guess the problem is with your layout Listview xml you are using relative layout they will obviously overlap each other until or unless you place them relative to each other.
You have to take out your lv_song_list from indexer_layout
You can refer
<FrameLayout
android:id="#+id/title_layout"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:background="#a86bf0"
android:visibility="gone" >
<TextView
android:id="#+id/title_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dip"
android:textColor="#000000"
android:textSize="13sp" />
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false"
android:orientation="horizontal"
android:weightSum="10" >
<ListView
android:id="#+id/indexed_listView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="9"
android:cacheColorHint="#null"
android:paddingLeft="5dp"
android:paddingRight="5dp" />
<LinearLayout
android:id="#+id/indexer_layout"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" />
</LinearLayout>
<RelativeLayout
android:id="#+id/section_toast_layout"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_centerInParent="true"
android:background="#drawable/black_base_rounded_transparent_50_rad2"
android:visibility="gone" >
<TextView
android:id="#+id/section_toast_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
style="#style/text_white_medium_franklin_bkcd"
android:textSize="30sp" />
</RelativeLayout>
I have created a custom expandable list view and a custom adapter. The list view consists in 3 XML layouts (please ignore the dumb use of table layout!):
One that defines the ExpandableListView
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ExpandableListView
android:id="#+id/expandable_list"
android:groupIndicator="#null"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000" >
<!-- android:listSelector="#android:color/transparent" -->
</ExpandableListView>
</LinearLayout>
Another that defines the parent (group):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:paddingTop="0dip"
android:layout_gravity="center_vertical"
android:gravity="center_vertical">
<TableRow android:layout_gravity="center_vertical">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dip"
android:layout_gravity="center_vertical"
android:gravity="center_vertical"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="100dip"
android:stretchColumns="1"
android:paddingTop="0dip"
android:layout_gravity="center_vertical"
android:gravity="center_vertical">
<TableRow android:layout_gravity="center_vertical">
<TextView
android:id="#+id/list_item_text_view"
android:textColor="#color/tab_sel"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:gravity="center_vertical"
android:fillViewport="true"
android:textSize="17sp"
android:layout_marginLeft="10dip"
android:textStyle="bold"/>
</TableRow>
</TableLayout>
<ImageView
android:layout_width="25dip"
android:id="#+id/rightcheck"
android:layout_height="25dip"
android:layout_gravity="left|center_vertical"
android:scaleType="centerCrop"
android:background="#drawable/sectionuncheck"
android:layout_marginRight="8sp"
android:layout_marginTop="0dip"/>
</TableRow>
</TableLayout>
And the last one defines the children:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:stretchColumns="1"
android:paddingTop="0dip"
android:layout_gravity="top">
<!-- android:background="#drawable/bar_bg" -->
<TableRow>
<ImageView
android:id="#+id/image"
android:layout_width="30dip"
android:layout_height="30dip"
android:layout_marginLeft="20dip"
android:src="#drawable/arrow"
android:layout_gravity="center_vertical"
android:scaleType="centerCrop"/>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="65dp"
android:stretchColumns="1"
android:paddingTop="0dip"
android:layout_gravity="top"
android:gravity="center_vertical" >
<TableRow>
<TextView
android:id="#+id/list_item_text_child"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="15sp"
android:layout_marginLeft="20dip"
android:layout_marginRight="35dip"
android:text=""
android:textColor="#003479"/>
</TableRow>
<TableRow >
<TextView
android:id="#+id/text"
android:layout_height="wrap_content"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_gravity="left|center_vertical"
android:textSize="15sp"
android:layout_marginLeft="20dip"
android:layout_marginRight="35dip"
android:text=""
android:textColor="#44B5E7"/>
</TableRow>
</TableLayout>
<ImageView
android:id="#+id/ivProdCheck"
android:layout_width="25dip"
android:layout_height="25dip"
android:layout_gravity="left|center_vertical"
android:scaleType="centerCrop"
android:layout_marginRight="8dp"
android:background="#drawable/check"
android:visibility="gone"/>
</TableRow>
</TableLayout>
Every time I click on a child, it gets selected or unselected, meaning that the ivProdCheck gets visible or not
if(is_preffered == 0)
{
Glob.getPreferred().add(prod_curent);
image.setVisibility(View.VISIBLE);
}
else
{
image.setVisibility(View.GONE);
}
Same thing happens in the adapter, to display the checked/uncheck items on activity start:
#Override
//in this method you must set the text to see the children on the list
public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup)
{
/*........*/
if(Glob.getPreferred() != null)
{
for (Product to_compare : Glob.getPreferred())
{
if(to_compare.id_produs == id_produs_curent)
{
image.setVisibility(View.VISIBLE);
break;
}
}
}
view.setTag(holder);
//return the entire view
return view;
}
When I click on the group/parent, I select/deselect all children and change the image according to that
if(counter != prod_in_cat)
{
for(Product p : cat_curent.getProd())
{
Glob.getPreferred().add(p);
}
imageParent.setImageResource(R.drawable.sectioncheck);
}
else
{
imageParent.setImageResource(R.drawable.sectionuncheck);
}
Problem statement
Every time a group is selected (meaning all children are selected), if I then deselect one child, I also want to change the parent image. Also, if I manually select/deselect all children in one group, I want to change the group image too. Same thing goes for onCreate().
I tried this on child click (the logs show what it's supposed to, so no error on that side):
if(counter != cat_curent.getProduse().size())
{
// Log.e("!= -> ","counter " + counter);
// Log.e("!= -> ","cat_curent" + cat_curent.getProduse().size());
imageParent.setImageResource(R.drawable.sectioncheck);
}
else
{
// Log.e("!= -> ","counter " + counter);
// Log.e("!= -> ","cat_curent" + cat_curent.getProduse().size());
imageParent.setImageResource(R.drawable.sectionuncheck);
}
and this on onCreate()
if((prod_pref > 0) && (prod_pref < prod_cat_glob))
{
Log.e("Log 4", "Categoria " + i + " Produse " + prod_pref);
imageParent.setImageResource(R.drawable.sectionuncheck); //NullPointerException here
}
else if(prod_pref == prod_cat_glob)
{
imageParent.setImageResource(R.drawable.sectioncheck); //NullPointerException here
}
and every time I get NullPointerException when I use setImageResource()
What's the catch here?
Nevermind, I got it working. The data source for my list view was always updated when I clicked a parent or child. The problem is the list wasn't refreshing. I had something similar to fix a few days ago and trying .notifyDataSetChanged() didn't work so I didn't use it for this. It turns out however, that this works. Thanks anyway #m0skit0. Still not sure however, why the NPE. I'll try to figure it out.
I am creating a ListView which has some some static headers as shown in the following picture:
I want that the header columns (Date, Status, Latitude, and Longitude) adapts their width according to the width of their according column.
What i am trying is to get the Layout of the list item associated with the adapter so that i can find the width of its childs (but it seems to be impossible) in my ListActivity.
then i have tried in my custom ArrayAdapter's getView method to retrieve the width of each TextView associated with the list item as shown in the following code: (But it returns null, I think it can not return width before redering the textView.)
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View row = convertView;
DispatchHolder holder = null;
if (row == null) {
LayoutInflater inflater = ((Activity) context).getLayoutInflater();
row = inflater.inflate(layoutResourceId, parent, false);
holder = new DispatchHolder();
holder.tvcurrentDateTime = (TextView) row
.findViewById(R.id.tvCurrentDateTimeLog);
holder.tvAction = (TextView) row.findViewById(R.id.tvActionLog);
holder.tvLatitude = (TextView) row.findViewById(R.id.tvlatitudeLog);
holder.tvLongitude = (TextView) row
.findViewById(R.id.tvlogitudeLog);
row.setTag(holder);
} else {
holder = (DispatchHolder) row.getTag();
}
holder.tvcurrentDateTime.setText(data.get(position).getTime());
int i = holder.tvcurrentDateTime.getWidth() <------- Here it is return 0
holder.tvAction.setText(data.get(position).getAction());
holder.tvLatitude.setText(data.get(position).getLatitude());
holder.tvLongitude.setText(data.get(position).getLogitude());
return row;
}
I have searched on it but i could not find a way to do so.
Here is the related code:
My ListActivity:
public class Activity_Log extends ListActivity {
PickupStatusDao puDao;
LogAdapter adapter;
List<LogModel> listOfDispatch;
private Object mItem;
private View childView;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_log_xml);
puDao = new PickupStatusDao(this);
List<LogModel> lstResult = puDao.readAllLogData();
adapter = new LogAdapter(this, R.layout.log_item_row, lstResult);
setListAdapter(adapter);
}
}
My list_item (R.layout.log_item_row):
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="#+id/BodyTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow2"
style="#style/BodyRow"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tvCurrentDateTimeLog"
style="#style/BodyText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="this time"
android:textSize="20sp" />
<TextView
android:id="#+id/tvActionLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="this is action"
android:textSize="20sp" />
<TextView
android:id="#+id/tvlogitudeLog"
style="#style/BodyText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="logitude"
android:textSize="20sp" />
<TextView
android:id="#+id/tvlatitudeLog"
style="#style/BodyText"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="latuide"
android:textSize="20sp" />
</TableRow>
</TableLayout>
List Activity Layout (R.layout.activity_log_xml)
<?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:orientation="vertical" >
<TableLayout
android:id="#+id/HeaderTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow style="#style/HeaderRow" >
<TextView
style="#style/HeaderText"
android:layout_width="239dp"
android:text="Date" />
<TextView
style="#style/HeaderText"
android:layout_width="156dp"
android:text="Status" />
<TextView
style="#style/HeaderText"
android:layout_width="121dp"
android:text="Latitude" />
<TextView
style="#style/HeaderText"
android:layout_width="121dp"
android:text="Longitude" />
</TableRow>
</TableLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
</LinearLayout>
Unfortunately, this just isn't something you can do efficiently with an AdapterView. Since they tie together an arbitrarily long data source, but only display a small subset of the data at any one time, there is no efficient way to determine what the maximum width of the data will be without measuring a view for each piece of data.
I would suggest maybe doing some work when initializing the data source to find the average length of the text that will be displayed, and setting each column to some value that makes sense for the data. Or, just use the same values for each column (be that some combination of layout_weights or some hardcoded values).
I have achieved this by XML android:layout_weight property, it works on all android devices with different densities, Here is the updated UI picture:
the updated XML of ListActivity (activity_log_xml.xml) and for list_item (log_item_row.xml) are as follows respectively:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="#+id/HeaderTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
style="#style/HeaderRow"
android:weightSum="10" >
<TextView
android:id="#+id/dateHeaderTV"
style="#style/HeaderText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Date" />
<TextView
android:id="#+id/statusHeaderTV"
style="#style/HeaderText"
android:layout_width="0dp"
android:layout_weight="4"
android:text="Status" />
<TextView
android:id="#+id/latitudeHeaderTV"
style="#style/HeaderText"
android:layout_width="0dp"
android:layout_weight="1.5"
android:text="Latitude" />
<TextView
android:id="#+id/longitudeHeaderTV"
style="#style/HeaderText"
android:layout_width="0dp"
android:layout_weight="1.5"
android:text="Longitude" />
</TableRow>
</TableLayout>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
</ListView>
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
android:id="#+id/BodyTable"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow2"
style="#style/BodyRow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="10" >
<TextView
android:id="#+id/tvCurrentDateTimeLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:text="this time" />
<TextView
android:id="#+id/tvActionLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="4"
android:text="this is action" />
<TextView
android:id="#+id/tvlogitudeLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:text="logitude" />
<TextView
android:id="#+id/tvlatitudeLog"
style="#style/BodyText"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:text="latuide" />
</TableRow>
</TableLayout>