I have a problem with setting gravity of a list item layout. I have a list layout which has 2 textviews. The first one has right gravity and second one has left gravity. When I load list view I always get right gravity.
Here is my code:
<?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" >
<TextView
android:id="#+id/poem_verse_list_item_01"
style="#style/TextAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right" />
<TextView
android:id="#+id/poem_verse_list_item_02"
style="#style/TextAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left" />
</LinearLayout>
And here is my Cursor and its ViewBinder:
public class CustomViewBinder implements ViewBinder {
#Override
public boolean setViewValue(View view, Cursor cursor, int columnIndex) {
int getIndex = cursor.getColumnIndex(ClubCP.COLUMN_POSITION);
int position = cursor.getInt(getIndex);
// Log.i("ViewBinder position",position);
TextView right =(TextView)view;
int viewId = view.getId();
switch(position){
case 0 :
// Right = 0,
if(viewId==R.id.poem_verse_list_item_02){
view.setVisibility(view.GONE);
}
else if(viewId==R.id.poem_verse_list_item_01){
view.setVisibility(view.VISIBLE);
}
//left.setVisibility(left.GONE);
//right.setGravity(Gravity.RIGHT);
// text.setVisibility(text.GONE);
// return true;
case 1:
// Left = 1,
if(viewId==R.id.poem_verse_list_item_02){
view.setVisibility(view.GONE);
}
else if(viewId==R.id.poem_verse_list_item_01){
view.setVisibility(view.VISIBLE);
}
//left.setVisibility(left.GONE);
//right.setGravity(Gravity.LEFT);
// return true;
case 2:
case 3:
case 4:
case -1:
}
return false;
}
}
SimpleCursorAdapter adapter = new SimpleCursorAdapter(this, R.layout.poem_verse_list_item, c,
columns, new int[] {R.id.poem_verse_list_item_01,R.id.poem_verse_list_item_02}, 0);
I tested a lot but each time I get only right or left gravity for both of them... I hope some one can help me. Thanks for your time.
This is my activity including ListView layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/FrameLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ff64512f"
android:paddingBottom="0dp"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingTop="0dp"
tools:context=".Read_peom" >
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/list_top_repeat" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:scaleType="fitXY"
android:src="#drawable/list_bottom_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:scaleType="fitXY"
android:src="#drawable/list_left_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:scaleType="fitXY"
android:src="#drawable/list_right_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/list_top_left_no_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:src="#drawable/list_top_right_no_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:src="#drawable/list_bottom_left_no_repeat" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:src="#drawable/list_bottom_right_no_repeat" />
<TextView
android:id="#+id/txt_poem_container_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<ListView
android:id="#+id/list_poem_container"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal|center"
android:layout_margin="#dimen/list_top_margin"
android:choiceMode="none"
android:clickable="false"
android:longClickable="false"
android:padding="5dp" >
</ListView>
Remove android:layout_gravity from both the TextViews since android:layout_width="match_parent"
OR
make android:layout_width="wrap_content" and remove android:gravity from both the TextViews.
ELSE
You may also try a RelativeLayout with TextViews having android:layout_width="wrap_content" and android:layout_alignParentLeft or android:layout_alignParentRight set to true accordingly.
Edit:
To debug the issue do the following. Create a ArrayAdapter with static data and set it as the list adapter.
String[] arr = {"a","b","c"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.poem_verse_list_item, R.id.poem_verse_list_item_01, arr);
setListAdapter(adapter);
}
Then set some value using android:text to the TextViews like this.
<TextView
android:id="#+id/poem_verse_list_item_01"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:text="poem_verse_list_item_01" />
<TextView
android:id="#+id/poem_verse_list_item_02"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:gravity="left"
android:text="poem_verse_list_item_02"/>
So if the problem persists then the issue is not with the view binder. I would suggest you to redesign your main layout file containing the ListView.
Try change android:orientation="vertical"
to
android:orientation="horizontal"
Hope this helped you
Related
I make a project and ı stuck in there.
In my design papers like that
And I did't put the arrow as seem so I can do it just Text side.
Like that
How can I put the Arrows ?
My codes are
String[] maddeler = new String[] { "YÖNETİM KURULU BAŞKANI FİKRİ ÖZTÜRK'ÜN MESAJI", "GENEL MÜDÜR CÜNEYT AĞCA İLE RÖPORTAJ", "21. YILINDA OPET",
"MİSYONUMUZ VİZYONUMUZ DEĞERLERİMİZ"};
ArrayList<String> maddelerListe = new ArrayList<String>();
maddelerListe.addAll( Arrays.asList(maddeler) );
listAdapter = new ArrayAdapter<String>(this, R.layout.simplerow, maddelerListe);
mainListView.setAdapter( listAdapter );
mainListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
switch(position)
{
case 0:
Intent baskanlar =new Intent(getBaseContext(),BaskanlarSplash.class);
startActivity(baskanlar);
break;
case 1:
Intent baskanlar2=new Intent(getBaseContext(),BaskanlarSplash2.class);
startActivity(baskanlar2);
break;
case 2:
Toast.makeText(getBaseContext(),"içerik bekleniyor",Toast.LENGTH_LONG).show();
break;
case 3:
Toast.makeText(getBaseContext(),"içerik bekleniyor",Toast.LENGTH_LONG).show();
break;
}
}
});
**My simplerow.xml **
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#color/homeBac"
>
</TextView>
This is my Listview Normaly
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/mainListView">
</ListView>
If you can help my I'm be so greteful :)
change the row to
<?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="horizontal">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:textColor="#color/colorAccent"
android:textSize="16sp"
android:layout_weight="1"></TextView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_dialog_info" />
</LinearLayout>
and then change the adapter declaration to
ArrayAdapter<String> listAdapter =
new ArrayAdapter<String>(this, R.layout.simplerow,R.id.rowTextView, maddelerListe);
Make the base layout of your simplerow a RelativeLayout and set the values kind of like this. You can make the arrow a drawable ImageView if you'd prefer. May have to change settings but this should work.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rowTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#color/homeBac" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:padding="10dp"
android:textSize="16sp"
android:textColor="#color/homeBac"
android:text=">" />
</RelativeLayout>
Change your row layout like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_your_arrow"/>
</LinearLayout>
Hey guys I'm newbie at Android development, and I would like to build a sub item for each listview item.
Just like this:
ListView 1
sub item 1
ListView 1
sub item 1
I though that it would be esier to create a new relative layout for each listview item, so my XML file looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/wrap_layout"
android:layout_width="fill_parent"
android:layout_height="200dp"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/first_col"
android:layout_width="fill_parent"
android:layout_height="200dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<ImageView
android:id="#+id/bg_image"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/overlay_bg" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="9dp" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/current_event"
android:layout_alignParentLeft="true"
android:layout_marginBottom="6dp"
android:letterSpacing="1.2"
android:shadowColor="#color/dark_grey"
android:shadowDx="-2"
android:shadowDy="2"
android:shadowRadius="0.01"
android:textColor="#color/white"
android:textSize="22sp" />
<ImageView
android:layout_width="17dp"
android:layout_height="17dp"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/persons" />
<TextView
android:id="#+id/current_day"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/name"
android:layout_alignParentBottom="true"
android:text="TextView"
android:textColor="#color/grey"
android:textSize="13sp" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/second_col"
android:layout_width="fill_parent"
android:layout_marginTop="200dp"
android:layout_height="200dp" >
<TextView
android:id="#+id/test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="15dp"
android:layout_marginTop="24dp"
android:text="TextView" />
</RelativeLayout>
</RelativeLayout>
Each item is divided by two columns, the second column should be hidden column, thats why I changed the layout height to 200dp.
And to show the full layout, I'm doing that:
public View getView(final int position, View convertView, ViewGroup parent) {
// here you let SimpleAdapter built the view normally.
final View v = super.getView(position, convertView, parent);
final RelativeLayout lebg = (RelativeLayout) v.findViewById(R.id.wrap_layout);
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
}
});
return v;
}
I just need to change the height from 200 to 400 to show the full Listview item.
The issue:
Is not working that well because when I click at the listview it only changes the height when I scroll...
Is there any better way to do what I am trying?
Thanks.
When you change LayoutParams you have to call requestLayout to see changes:
lebg.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View b) {
lebg.getLayoutParams().height = 400;
requestLayout();
}
});
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.
I have researched a lot regarding this topic and I'm finally posting this question cause I did not find what I searched for.
I have a layout with Listview inside a ScrollView.
Listview is used to display the comments from various users.
The layout is as follows :
Layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/panel_content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ffffff" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:scaleType="centerCrop"
android:src="#drawable/image" />
<View
android:id="#+id/image_divider"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_below="#+id/image"
android:background="#c2c2c2" />
<TextView
android:id="#+id/prod_name_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/image_divider"
android:layout_marginLeft="5dp"
android:layout_marginTop="10dp"
android:gravity="left"
android:text="SUV car by Nissaan"
android:textAlignment="gravity"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/productCost_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/prod_name_text"
android:layout_below="#+id/prod_name_text"
android:text="$150"
android:textColor="#000000"
android:textStyle="bold" />
<TextView
android:id="#+id/likes_details_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/prod_name_text"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/productCost_text"
android:text="rob + 1,124"
android:textColor="#000000" />
<ImageButton
android:id="#+id/dreamitBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/likes_details_text"
android:layout_margin="10dp"
android:background="#null"
android:src="#drawable/dreamplus" />
<ImageButton
android:id="#+id/addtoListBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/likes_details_text"
android:layout_margin="10dp"
android:layout_toRightOf="#+id/dreamitBtn"
android:background="#null"
android:src="#drawable/add_to_list_bg" />
<View
android:id="#+id/_first_divider"
android:layout_width="fill_parent"
android:layout_height="8dp"
android:layout_below="#+id/addtoListBtn"
android:background="#838B8B" />
<TabHost
android:id="#+id/tabHost"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/_first_divider"
android:layout_marginTop="10dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
</TabHost>
<Button
android:id="#+id/cost"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_alignRight="#+id/image"
android:layout_alignTop="#+id/addtoListBtn"
android:layout_marginLeft="120dp"
android:layout_toRightOf="#+id/addtoListBtn"
android:background="#drawable/buy_button"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:text="60$"
android:textSize="12sp"
android:textStyle="bold" />
</RelativeLayout>
<View
android:id="#+id/second_divider"
android:layout_width="fill_parent"
android:layout_height="8dp"
android:layout_marginTop="10dp"
android:background="#838B8B" />
<RelativeLayout
android:id="#+id/relative2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF" >
<TextView
android:id="#+id/comments_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/upper_divider"
android:layout_alignParentLeft="true"
android:text="1"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/comments_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#+id/comments_count"
android:text="Comment(s)"
android:textSize="20sp"
android:textStyle="bold" />
<View
android:id="#+id/upper_divider"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_alignBottom="#id/comments_text"
android:background="#000000" />
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/upper_divider"
>
</ListView>
<View
android:id="#+id/lower_divider"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_alignBottom="#id/list"
android:background="#000000" />
<EditText
android:id="#+id/comments_edit"
android:layout_width="220dp"
android:layout_height="fill_parent"
android:layout_below="#+id/lower_divider"
android:background="#FFFFFF"
android:hint="Enter your comment"
android:paddingLeft="8dp"
android:paddingTop="10dp" />
<View
android:layout_width="1dp"
android:layout_height="fill_parent"
android:layout_marginBottom="0dp"
android:background="#000000" />
<Button
android:id="#+id/post_button"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="#+id/lower_divider"
android:layout_toRightOf="#+id/comments_edit"
android:background="#FFFFFF"
android:text="Post" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
And the layout looks like :
So the layout consists of three parts : (Thick Grey Line acts as a divider)
First Part :
Includes Webservice call for image and cost,product name and users etc.
Second Part :
Has 3 tabs where all the tabs content will be retrieved from single web service.
Third Part :
This is where the actual listview exists but it fails to show. The edit text at the bottom is actually below the listview which fails to show up. This also includes a web service call.
I have added a header to listview.
Solutions that I have tried so far :
I have used a helper class to set the height of Listview dynamically(atleast thats what I think it does) :
The class is as follows :
public class Helper {
public static void getListViewSize(ListView myListView) {
ListAdapter myListAdapter = myListView.getAdapter();
if (myListAdapter == null) {
// do nothing return null
return;
}
// set listAdapter in loop for getting final size
int totalHeight = myListView.getPaddingBottom() + myListView.getPaddingTop();
for (int size = 0; size < myListAdapter.getCount(); size++) {
View listItem = myListAdapter.getView(size, null, myListView);
if (listItem instanceof ViewGroup)
listItem.setLayoutParams(new LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
// setting listview item in adapter
ViewGroup.LayoutParams params = myListView.getLayoutParams();
System.out.println("List view id: "+myListView.getId());
params.height = totalHeight
+ (myListView.getDividerHeight() * (myListAdapter
.getCount() - 1));
System.out.println("The params height is " + params.height);
myListView.setLayoutParams(params);
// print height of adapter on log
Log.i("height of listItem:", String.valueOf(totalHeight));
}
}
And I have called this as
Helper.getListViewSize(MainActivity.listView);
after setting the adapter to list.
Second Solution:
listView.setOnTouchListener(new ListView.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// Disallow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(true);
break;
case MotionEvent.ACTION_UP:
// Allow ScrollView to intercept touch events.
v.getParent().requestDisallowInterceptTouchEvent(false);
break;
}
// Handle ListView touch events.
v.onTouchEvent(event);
return true;
}
});
From what I read,this syncs the scrollview according to listview.
Web Service Call :
I am calling the webservice for comments as soon as the above layout launches. I can see the web service being called as I can see the response printed in the logcat. But it does not set the listview.
Note :
I have implemented the webservice call of listview separately and it works and sets the comments to the listview. So, the real problem exists with the fact that I have used scrollview inside a listview.
Any help will be much appreciated. Thanks in advance.
ListView inside a ScrollView is not a good practice because listview has it own scrolling if it doesnt fit on the screen.
This is my main.xml:`
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:addStatesFromChildren="false"
android:layout_height="wrap_content">
<TableLayout
android:id="#+id/header"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_width="fill_parent"
android:layout_alignParentTop="true">
<TableRow>
<TextView
android:id="#+id/leftHeader"
android:textColor="#android:color/black"
android:layout_margin="1dp"
android:gravity="center_horizontal"
android:layout_weight="1"
android:background="#ffcccccc"
android:text="textview1"
android:layout_width="30dip" />
<TextView
android:id="#+id/rightHeader"
android:textColor="#android:color/black"
android:gravity="center_horizontal"
android:layout_weight="1"
android:layout_margin="1dp"
android:background="#android:color/white"
android:text="textview2"
android:layout_width="70dip" />
</TableRow>
</TableLayout>
<ScrollView
android:id="#+id/table_scroll"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="2dp"
android:layout_marginLeft="2dp"
android:layout_below="#+id/header"
android:layout_above="#+id/buttonTable">
<TableLayout
android:id="#+id/maintable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black">
<TableRow
android:id="#+id/maintableRow">
<TextView
android:id="#+id/maintableLeft"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginRight="1dp"
android:layout_marginLeft="1dp"
android:text="textview1"
android:layout_width="30dip" />
<TextView
android:id="#+id/maintableRight"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:text="textview2"
android:layout_width="70dip" />
</TableRow>
</TableLayout>
</ScrollView>
<TableLayout
android:id="#+id/buttonTable"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_alignParentBottom="true">
<TableRow>
<Button
android:id="#+id/button_ResetData"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Reset Data"
android:layout_gravity="center"
android:layout_weight="1" />
<Button
android:id="#+id/button_Refresh"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:text="Refresh"
android:gravity="center"
android:layout_weight="1" />
</TableRow>
</TableLayout>
`
After I press one of the buttons its text alignment goes to the left, how can I make it stay in center?
something similar happens with the first table (header) (textview1, textview2) but here it also reduses the textsize the amount of the text in the view...
EDIT: I managed to find the code that causes the change but I don't know how to fix this.
Maybe some one can explain me why does this happen and how to fix it?
private void fillTableView(List<BatteryInfo> list) {
TextView leftHeader = (TextView) findViewById(R.id.leftHeader);
TextView rightHeader = (TextView) findViewById(R.id.rightHeader);
LayoutParams leftHeaderLayoutParams = leftHeader.getLayoutParams();
LayoutParams rightHeaderLayoutParams = rightHeader.getLayoutParams();
TableLayout contentTable = (TableLayout) findViewById(R.id.maintable);
contentTable.removeAllViews();
for (int i = list.size() - 1; i >= 0; i--) {
TextView tv1 = new TextView(this);
tv1.setTextColor(Color.BLACK);
tv1.setGravity(Gravity.CENTER);
tv1.setBackgroundColor(Color.LTGRAY);
tv1.setLayoutParams(leftHeaderLayoutParams);
tv1.setText(String.valueOf(list.get(i).getLevel()));
TextView tv2 = new TextView(this);
tv2.setTextColor(Color.BLACK);
tv2.setGravity(Gravity.CENTER);
tv2.setBackgroundColor(Color.WHITE);
tv2.setLayoutParams(rightHeaderLayoutParams);
tv2.setText(list.get(i).getDurationTimeInString());
TableRow tblRow = new TableRow(this);
tblRow.addView(tv1);
tblRow.addView(tv2);
contentTable.addView(tblRow);
}
}
After I use removeAllViews or any addView function the layout of the top most table changes as I explained before. Why does this happen?
You can fix this by changing the android:layout_width="30dip" and android:layout_width="70dip" in leftHeader and rightHeader to android:layout_width="fill_parent". I have to admit that I'm not entirely certain why that is, though.
Instead of clearing and re-filling a TableLayout every time, though, you might want to consider using a ListAdapter. It's much more suited to what you're doing (and will probably perform better, too).
UPDATE NUMBER 2:
Notice the change in the main.xml and the change in the onCreate method. This will make your header static while letting your listview scroll, and changing from TableLayout to LinearLayout for your buttons will fix the alignment issue.
Here's how to do this with a custom adapter instead of TableLayout (which isn't really meant for dynamic data like this):
Change you main.xml to this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:addStatesFromChildren="false" android:layout_height="wrap_content">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentTop="true" android:id="#+id/header">
<TextView android:id="#+id/leftHeader" android:textColor="#android:color/black"
android:layout_margin="1dp" android:gravity="center_horizontal"
android:layout_weight="1" android:background="#ffcccccc" android:text="textview1"
android:layout_width="fill_parent" android:layout_height="wrap_content"/>
<TextView android:id="#+id/rightHeader" android:textColor="#android:color/black"
android:gravity="center_horizontal" android:layout_weight="1"
android:layout_margin="1dp" android:background="#android:color/white"
android:text="textview2" android:layout_width="fill_parent" android:layout_height="wrap_content"/>
</LinearLayout>
<LinearLayout android:id="#+id/buttonTable" android:orientation="horizontal"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#android:color/black"
android:layout_alignParentBottom="true" >
<Button android:id="#+id/button_ResetData"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Reset Data" android:layout_gravity="center"
android:layout_weight="1" />
<Button android:id="#+id/button_Refresh"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:text="Refresh" android:gravity="center"
android:layout_weight="1" />
</LinearLayout>
<ListView android:id="#+id/listView" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:layout_weight="1"
android:fadingEdge="none"
android:layout_below="#id/header" android:layout_above="#id/buttonTable">
</ListView>
Add XML layout files for your rows (row.xml):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:id="#+id/maintableLeft"
android:layout_height="wrap_content" android:layout_weight="1"
android:layout_marginRight="1dp" android:layout_marginLeft="1dp"
android:gravity="center_horizontal" android:background="#ffcccccc"
android:textColor="#android:color/black" android:layout_width="fill_parent" />
<TextView android:id="#+id/maintableRight"
android:layout_height="wrap_content" android:background="#ffcccccc"
android:textColor="#android:color/black" android:layout_weight="1"
android:layout_marginLeft="1dp" android:layout_marginRight="1dp"
android:gravity="center_horizontal" android:layout_width="fill_parent" />
</LinearLayout>
Now everything is in place to create a custom class from BaseAdapter to display your information (and make your layout work better):
class CustomAdapter extends BaseAdapter {
private List<BatteryInfo> info;
private LayoutInflater inflater;
public CustomAdapter(Context context, List<BatteryInfo> x) {
this.info = x;
inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public int getCount() {
return info.size();
}
public Object getItem(int position) {
return info.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v;
v = inflater.inflate(R.layout.row, parent, false);
BatteryInfo b = (BatteryInfo) getItem(position);
((TextView) v.findViewById(R.id.maintableLeft)).setText(b
.getDurationTimeInString());
((TextView) v.findViewById(R.id.maintableRight)).setText(b
.getLevel());
return v;
}
}
Modify your onCreate method to inflate the header, add it, and then bind your data to your ListView using the new CustomAdapter (I've created a fake BatteryInfo class for this example, you'll need to modify things slightly to get this working):
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final ListView lv = (ListView) this.findViewById(R.id.listView);
final Context context = this;
Button refresh = (Button) this.findViewById(R.id.button_Refresh);
refresh.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
List<BatteryInfo> x = new ArrayList<BatteryInfo>();
x.add(new BatteryInfo());
x.add(new BatteryInfo());
x.add(new BatteryInfo());
x.add(new BatteryInfo());
lv.setAdapter(new CustomAdapter(context, x));
}
});
}