Random NullPointerException on nested inflated view - android

I've a layout with a menu that can be reused more times in more Activities...
The menu layout is called menu_up_row.xml and every button of the layout is called menu_up_cell.xml.
So I inflate programmatically the menu_up_row.xml for every Activity and, in the meanwhile, also menu_up_cell.xml many times as how many buttons the menu has.
This exception occurs moreabout once every 70 users...
Here is the call to the static method of a class called MenuUp that raise the exception:
public static void *etMenuUpButtonEnabled(Context ctx, LinearLayout menuUpLayout, int buttonID, boolean enabled) {
LinearLayout btnLayout = (LinearLayout) menuUpLayout.findViewById(getResourceId(buttonID));
//SOMETIMES NULL POINTER EXCEPTION HERE:
ImageView menuCellImageView =(ImageView) btnLayout.findViewById(R.id.menuCellImageView); //<-- NULL POINTER EXCEPTION HERE
...
where :
private static int getResourceId(int buttonID) {
int retVal = 0;
switch (buttonID){
case BUTTON_MENU_UP_BTN1:
retVal = R.id.menuCell1;
break;
case BUTTON_MENU_UP_BTN2:
retVal = R.id.menuCell2;
break;
case BUTTON_MENU_UP_BTN3:
retVal = R.id.menuCell3;
}
return retVal;
}
The above method is called from the Main Activity:
...
LinearLayout menuUpLayout = (LinearLayout) findViewById(R.id.main_menu_up_row);
MenuUp.setMenuUpButtonEnabled(ctx, menuUpLayout, MenuUp.BUTTON_MENU_UP_BTN1,false);
...
In the xml of the Main Activity there is this for including the menu:
<LinearLayout android:id="#+id/main_menu_up_row"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:visibility = "gone"
>
</LinearLayout>
To inflate the menu, in the Main Activity I have this code:
MenuUp.inflateMenuUpCustom(ctx, menuUpLayout);
where:
public static void inflateMenuUpCustom(Context ctx, LinearLayout menuUpLayout) {
LayoutInflater layoutInflater = (LayoutInflater)
ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
menuUpLayout.addView(layoutInflater.inflate(R.layout.menu_up_row, menuUpLayout, false) );
//Aggiungo i bottoni del menu
addMenuUpButton(ctx, menuUpLayout, R.id.menuCell1, R.drawable.menu_grid_order_by, R.string.menu_grid_order_by_str);
addMenuUpButton(ctx, menuUpLayout, R.id.menuCell2, R.drawable.menu_grid_add, R.string.menu_grid_add_str);
addMenuUpButton(ctx, menuUpLayout, R.id.menuCell3, R.drawable.menu_grid_filter_by, R.string.menu_grid_filter_by_str);
}
and:
private static void addMenuUpButton(final Context ctx
, LinearLayout menuUpLayout
, int cellResourceId
, int imageDrawableId
, Integer stringResourceId) {
LayoutInflater layoutInflater = (LayoutInflater)
ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout btnLayout = (LinearLayout) menuUpLayout.findViewById(cellResourceId);
btnLayout.addView(layoutInflater.inflate(R.layout.menu_up_cell, btnLayout, false) );
ImageView menuCellImageView =(ImageView) btnLayout.findViewById(R.id.menuCellImageView);
LinearLayout menuCellLinearLayout = (LinearLayout) btnLayout.findViewById(R.id.menuCellLinearLayout);
menuCellImageView.setImageDrawable(ctx.getResources().getDrawable(imageDrawableId));
...
Here is menu_up_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/menu_row"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout android:id="#+id/radRowToBeSeen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="#+id/linearLayoutDets4"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#e7e7e7"
>
<LinearLayout android:id="#+id/menuCellCont1"
android:layout_weight="0.2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout android:id="#+id/menuCell1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</LinearLayout>
<LinearLayout android:id="#+id/menuCellCont2"
android:layout_weight="0.2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout android:id="#+id/menuCell2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</LinearLayout>
<LinearLayout android:id="#+id/menuCellCont3"
android:layout_weight="0.2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout android:id="#+id/menuCell3"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Here is menu_up_cell.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menuCellLinearLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/menuCellImageView"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="2dp"
android:src="#drawable/menu_dummy"
android:visibility="gone"
>
</ImageView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TextView
android:id="#+id/menuCellTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:maxLines="2"
android:text="ciao"
android:textColor="#ffffff"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
What am I wrong?

Related

ScrollView inside fragment not scrolling

In my Activity I have a FrameLayout, that suppose to use as container for fragments.
In my fragment I got a ScrollView that doesn't respond. ScrollView contains TextView set android:layout_height="wrap_content", with text from my java code that clearly longer than the size of the ScrollView.
My fragment 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="#dimen/bookview.imageWidth"
android:layout_height="250dp"
android:id="#+id/bookView.image"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:id="#+id/bookView.name"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:layout_below="#id/bookView.name"
android:id="#+id/bookView.author"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:layout_below="#id/bookView.author"
android:id="#+id/bookView.pages"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:layout_below="#id/bookView.pages"
android:id="#+id/bookView.category"/>
<TextView
android:textSize="#dimen/custom_book_text"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_toEndOf="#id/bookView.image"
android:layout_below="#id/bookView.category"
android:id="#+id/bookView.publishingDate"/>
</RelativeLayout>
<ScrollView
android:layout_height="200dp"
android:layout_width="match_parent"
android:layout_gravity="end">
<TextView
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:textSize="#dimen/custom_book_text"
android:id="#+id/bookView.description"/>
</ScrollView>
</LinearLayout>
the xml of the activity:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/use.drawer">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="#dimen/user_custom_book_height"
android:id="#+id/user.customBookContainer"/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:id="#+id/user.container"/>
</LinearLayout>
<include
layout="#layout/navigation_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"/>
</android.support.v4.widget.DrawerLayout>
i use FrameLayout because i want to change the fragment in that area,in runtime.better idea? i like to hear.
i have tried to decrese the heigth of the ScrollView.
my fragment java code:
public class BookView extends Fragment {
private Bitmap bitmap;
private String name;
public static BookView newInstance(String book,Bitmap bitmap) {
Bundle args = new Bundle();
BookView fragment = new BookView();
fragment.bitmap = bitmap;
fragment.name = book;
fragment.setArguments(args);
return fragment;
}
public BookView() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_book_view, container, true);
Backend backend = BackendFactory.getInstance();
try {
Book b = backend.readBook(name);
TextView textView = (TextView)v.findViewById(R.id.bookView_name);
textView.setText(b.getBookName());
textView = (TextView)v.findViewById(R.id.bookView_author);
textView.setText(b.getAuthor());
textView = (TextView)v.findViewById(R.id.bookView_category);
textView.setText(b.getCategory().name());
textView = (TextView)v.findViewById(R.id.bookView_description);
textView.setText(b.getDescription());
textView = (TextView)v.findViewById(R.id.bookView_pages);
textView.setText(Integer.toString(b.getPages()));
textView = (TextView)v.findViewById(R.id.bookView_publishingDate);
textView.setText(b.getPublishingDate());
final ImageView imageView = (ImageView)v.findViewById(R.id.bookView_image);
if(bitmap != null){
imageView.setImageBitmap(bitmap);
} else{
ImageLoader imageLoader = new ImageLoader();
imageLoader.setListener(new ImageLoader.ImageTaskListener() {
#Override
public void onActionEnd() {}
#Override
public void onImageDownload(Bitmap bitmap) {
imageView.setImageBitmap(bitmap);
}
});
imageLoader.execute(b.getImage());
}
}
catch (Exception e) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(e.getMessage());
builder.create().show();
}
return inflater.inflate(R.layout.fragment_book_view, container, false);
}
}
imageLoader is a class i wrote,it extened AsyncTask for download image from a url and update the UI when it ready.
I think other parts (TextViews and ImageViews) of this fragment full all area of screen and ScrollView render out of screen.
I suggest you put all layout inside ScrollView.
like this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/bookView.image"
android:layout_width="#dimen/bookview.imageWidth"
android:layout_height="250dp"/>
<TextView
android:id="#+id/bookView.name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="#id/bookView.image"
android:textSize="#dimen/custom_book_text"/>
<TextView
android:id="#+id/bookView.author"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/bookView.name"
android:layout_toEndOf="#id/bookView.image"
android:textSize="#dimen/custom_book_text"/>
<TextView
android:id="#+id/bookView.pages"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/bookView.author"
android:layout_toEndOf="#id/bookView.image"
android:textSize="#dimen/custom_book_text"/>
<TextView
android:id="#+id/bookView.category"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/bookView.pages"
android:layout_toEndOf="#id/bookView.image"
android:textSize="#dimen/custom_book_text"/>
<TextView
android:id="#+id/bookView.publishingDate"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/bookView.category"
android:layout_toEndOf="#id/bookView.image"
android:textSize="#dimen/custom_book_text"/>
</RelativeLayout>
<TextView
android:id="#+id/bookView.description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="#dimen/custom_book_text"/>
</LinearLayout>
I assume you are able to see the fragment's view.
If so a few things to try:
Set the scroll view to fillViewPort="true"
and then set the text view to match parent since this is the only view in the scroll view. As a note you should really avoid using hard coded widths & heights (even dp heights).
Instead of returning
return inflater.inflate(R.layout.fragment_book_view, container, false);
just return the original view: v
Nother' bit of readability: instead of reassigning textview each time.
do:
((TextView)v.findViewById(R.id.blabla)).setText("BLA");
Edit:
I have tested this & it works.
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:fillViewport="true">
<TextView
android:id="#+id/faq_output_answer"
android:layout_width="match_parent"
android:layout_margin="10dp"
android:layout_height="match_parent" />
</ScrollView>
Try putting an empty view as the last item in your scrollview.
<View
android:layout_width = "match_parent"
android:layout_height = "50dp" />
This worked for me.

Android: How to inflate different layout from different buttons?

I trying to do making something tabs but using button.
but each time I click on button it add me a row.
I want it to change the all layout to another..
MainActivity
public class MainActivity extends Activity {
Button btn1;
Button btn2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.button1);
btn1.setOnClickListener(switchListener);
btn2 = (Button) findViewById(R.id.button2);
btn2.setOnClickListener(switchListener);
}
private OnClickListener switchListener = new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
//ScrollView sv = (ScrollView) findViewById(R.id.MyScrollView);
LinearLayout ll = (LinearLayout) findViewById(R.id.MyLinearLayout);
LayoutInflater inflater = LayoutInflater
.from(getApplicationContext());
if (btn1 == v) {
View v1 = inflater.inflate(R.layout.firstview, null);
ll.addView(v1);
} else {
View v2 = getLayoutInflater().inflate(R.layout.secondview, null);
ll.addView(v2);
}
}
};
}
and here my Main XML file
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Layout 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Layout 2" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ScrollView
android:id="#+id/MyScrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<LinearLayout
android:id="#+id/MyLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</TableRow>
and here my other two layouts that I want to inflate
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="First View"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:background="#android:color/holo_blue_light"/>
and the second
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Second View"
android:textAppearance="?android:attr/textAppearanceLarge"
android:gravity="center"
android:background="#android:color/holo_red_light"/>
First make match parent your tableview.
and now do below on your code:-
if(ll.getChildCount() > 0) {
ll.removeAllViews();
}
if (btn1 == v) {
View v1 = inflater.inflate(R.layout.firstview, null);
ll.addView(v1);
} else {
View v2 = getLayoutInflater().inflate(R.layout.secondview, null);
ll.addView(v2);
}
the problem is you are just adding child on layout one by one so you need to remove first then add into layout so try above code.
before adding views to your ll linear layout check, as
if(ll.getChildCount() > 0) {
ll.removeAllViews();
}
then perform:
ll.addView(view) v1 or v2 as required.

Android : Listview of text and Image and the weird scrolling behaviour

In my application I need to create two lists each with different data . I have designed the UI like adding two listviews one after another in a TableLayout within a LinearLayout. I have stored the data in two String arrays and used arrayadapter to populate into the listview.My listview contain a textview and an image.The problem is
1.There is no error shown in the code but the list is getting populated in weird manner. 1st list item is shown, 2nd,3rd,4th list item not shown and 5th is shown 6th not shown, from 7th its fine. When i scroll up, the list is showing all the list items.
2.each list should have its own scrolling control. like if I scroll List "A" ,it should only be scrolled not the List"B" and vice versa. I have attached the code here . Thanks in advance.
mainscreen.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main_LinearLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/request_Table"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#color/black"
android:orientation="vertical"
android:padding="20dp" >
<TableRow
android:id="#+id/request_Row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/blue"
android:orientation="vertical" >
<TextView
android:id="#+id/request_TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="REQUESTS"
android:textColor="#color/white"
android:textStyle="bold" />
<ImageView
android:id="#+id/widget163"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</TableRow>
<TableRow
android:id="#+id/request_Row2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/gray"
android:orientation="horizontal" >
<ListView
android:id="#+id/request_ListView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:scrollbarAlwaysDrawVerticalTrack="true" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/offered_Table"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="#color/black"
android:orientation="vertical"
android:padding="20dp" >
<TableRow
android:id="#+id/offered_Row1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/blue"
android:orientation="horizontal" >
<TextView
android:id="#+id/offered_TextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center"
android:text="OFFERED"
android:textColor="#color/white"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/offered_Row2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ListView
android:id="#+id/offered_ListView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#color/gray"
android:padding="5dp"
android:scrollbars="vertical"
android:smoothScrollbar="true" />
</TableRow>
</TableLayout>
</LinearLayout>
mainscreen.java
public class MainMenu extends Activity {
RequestAdapter mRequestAdapter;
ListView mListView_req ;
public class RequestData {
int icon;
String list_text;
public RequestData( String mText) {
super();
//this.icon=image;
this.list_text=mText;
}
}
private RequestData[] getListItems(){
Resources mRes = getResources();
String[] sListItems= mRes.getStringArray(R.array.request_list);
int listsize = sListItems.length;
RequestData[] obj = new RequestData[listsize];
int size = obj.length;
for(int i=0; i<size; i++){
obj[i] = new RequestData(sListItems[i]);
}
return obj;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.mainmenu_screen);
mListView_req= (ListView)findViewById(R.id.request_ListView) ;
mRequestAdapter = new RequestAdapter(this,R.layout.listitem, getListItems());
mListView_req.setAdapter(mRequestAdapter);
}
class RequestAdapter extends ArrayAdapter{
private RequestData rList[]= null;
Context context;
public RequestAdapter(Context context, int textViewResourceId,
RequestData[] data) {
super(context, textViewResourceId, data);
// TODO Auto-generated constructor stub
this.rList= data;
}
#Override
public RequestData getItem(int position){
return rList[position];
}
#Override
public View getView(int pos, View convertView, ViewGroup parent) {
View row= convertView;
if (row== null){
row = getLayoyutinflater().inflate(R.layout.listitem,null);
}
else {
RequestData items = getItem(pos);
TextView text= (TextView) row.findViewById(R.id.List_Text);
if(text!= null){
text.setText(items.list_text);
}
}
return row;
}
listitem.xml :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="150dp">
<TextView
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:id="#+id/text"/>
<ImageView>
android:layout_width="40dp"
android:layout_height="wrap_content"
android:src="#drawable/arrow"
android:layout_marginRight="10dp"/>
</LinearLayout>

android: display data in xml

I want to create a game list wherein the game details like opponent name,score,etc will be displayed.I am having problem fetching the data into xml and then displaying it.please help.
I have created two xml.I have to set data into the 2nd xml and then include that 2nd xml into 1st xml.
my 1st xml is
<FrameLayout
android:id="#+id/gameListFrame"
android:layout_width="fill_parent"
android:layout_height="380dp"
android:orientation="vertical" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dip" >
<LinearLayout
android:id="#+id/GameListHolder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/scrollBackG"
android:orientation="horizontal" >
</LinearLayout>
</ScrollView>
</FrameLayout>
2nd xml is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gamelist_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="50px"
android:gravity="left"
android:orientation="horizontal"
android:background="#FFF" >
<ImageView
android:id="#+id/gl_avatar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<LinearLayout android:orientation="horizontal"
android:layout_width="225px"
android:layout_height="fill_parent"
android:gravity="left"
android:background="#000">
<LinearLayout android:orientation="vertical"
android:layout_width="175px"
android:layout_height="wrap_content"
android:gravity="top">
<TextView android:id="#+id/oppName"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<LinearLayout android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top" >
<TextView android:id="#+id/lastWord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView android:id="#+id/diffTime"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
My code is
gameListItem=(LinearLayout) findViewById(R.id.gamelist_items);
for (i=0;i<gamelistVector.size();i++)
{
rowView=View.inflate(context, R.layout.gamelist_details,null );
TextView oppName=(TextView) rowView.findViewById(R.id.oppName);
TextView lastWord=(TextView) rowView.findViewById(R.id.lastWord);
TextView diffTime=(TextView) rowView.findViewById(R.id.diffTime);
oppName.setText(gList.getOppName());
lastWord.setText(gList.getOppLastWord());
diffTime.setText(gList.getDiffTime()+"hr");
gameListItem.addView(rowView); //show the total games}
You need to extend ListAdapter and override the getView() method that sets the data for each row.
See this example
You need inflater reference to inflate views.
To GetInflater reference use:
LayoutInflater inflater = (LayoutInflater)context.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
gameListItem=(LinearLayout) findViewById(R.id.gamelist_items);
for (i=0;i<gamelistVector.size();i++)
{
rowView=inflater.inflate(R.layout.gamelist_details, null);
TextView oppName=(TextView) rowView.findViewById(R.id.oppName);
TextView lastWord=(TextView) rowView.findViewById(R.id.lastWord);
TextView diffTime=(TextView) rowView.findViewById(R.id.diffTime);
oppName.setText(gList.getOppName());
lastWord.setText(gList.getOppLastWord());
diffTime.setText(gList.getDiffTime()+"hr");
gameListItem.addView(rowView); //show the total games
}
thanks guys 4 answering to my query...i saw ur suggestions and figured a way out.this is wat i did nd its working...
private void doDisplay(){
this.runOnUiThread(new Thread() {
#Override
public void run() {
TextView gameCount=(TextView) findViewById(R.id.game_count);
gameCount.setText(" found "+maxGames+" games ");
for (i=0;i<gamelistVector.size();i++)
{
rowView=View.inflate(context, R.layout.gamelist_details, null);
TextView oppName=(TextView) rowView.findViewById(R.id.oppName);
TextView lastWord=(TextView) rowView.findViewById(R.id.lastWord);
TextView diffTime=(TextView) rowView.findViewById(R.id.diffTime);
TextView score=(TextView) rowView.findViewById(R.id.opplastMoveScore);
oppName.setText(" "+gamelistVector.get(i).getOppName());
lastWord.setText(" "+gamelistVector.get(i).getOppLastWord());
diffTime.setText(gamelistVector.get(i).getDiffTime()+"");
score.setText("("+gamelistVector.get(i).getLastMoveScore()+")");
ImageView oppImage = (ImageView) rowView.findViewById(R.id.gl_avatar);
oppImage.setImageBitmap(gamelistVector.get(i).getOppImage());
ImageView status = (ImageView) rowView.findViewById(R.id.gl_status);
status.setImageResource(gamelistVector.get(i).getTurnUserID());
gameListItem.addView(rowView);
}
}
});
}

problem in TextView in android

hi all i m making a sample app. for update List.
i have a list class and its xml is like.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/ListView01" android:layout_height="wrap_content"
android:layout_width="fill_parent"></ListView>
<TextView android:id="#+id/more"
android:layout_height="wrap_content"
android:text="Click to view more..."
android:textStyle="normal|bold" android:textColor="#FF8000" android:textSize="10dip"
android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent">
</TextView>
</LinearLayout>
now i have set TouchListener of text View and added the following code in it
#Override
public boolean onTouch(View v, MotionEvent event) {
switch(v.getId())
{
case R.id.more:
//update List Method Call..
more.setText("Click to view More..");
adapter.notifyDataSetChanged();
}
// TODO Auto-generated method stub
return false;
}
You see on the 3rd line of switch statement i have added
more.setText("Click to view More..");
line but when my list is updated . The text View is no longer shows in the bottom .
Please Guide my why this is happening to me and whats the solution??
you can use list footer in that case.
add this code in ur java file. and add footer dynamically in your list.
TextView more = new TextView(this);
more.setText("Click to view more...");
more.setClickable(true);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(width, Height);
more.setLayoutParams(params);
more.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//update List Method Call..
more.setText("Click to view More..");
adapter.notifyDataSetChanged();
}
});
ListView listView = (ListView) findViewById(R.id.ListView01);
listView.addFooterView(more);
this can help you.
try this ...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView android:id="#+id/ListView01" android:layout_width="fill_parent" android:layout_alignParentTop="true" android:layout_height="fill_parent" android:layout_above="#+id/more"></ListView>
<TextView android:id="#+id/more"
android:layout_height="wrap_content"
android:text="Click to view more..."
android:textStyle="normal|bold" android:textColor="#FF8000" android:textSize="10dip"
android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent" android:layout_alignParentBottom="true">
</TextView>
</RelativeLayout>
try android:layout_height="fill_parent" android:layout_above="#+id/more" in the ListView. Now even if ur ListView grows it wont hide the TextView.
UPDATE
<RelativeLayout android:id="#+id/relativeList"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/LinearBottom3">
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/chathlist"
/>
</RelativeLayout>
<RelativeLayout android:id="#+id/LinearBottom3"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="wrap_content"><!--Make it as center-->
<TextView android:id="#+id/more" android:layout_height="wrap_content"
android:text="Click to view more..." android:textStyle="normal|bold" android:textSize="10dip"
android:gravity="center_vertical|center_horizontal" android:layout_width="fill_parent"/>
</ReletiveLayout>
try this..
footer.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content">
<TextView android:layout_height="wrap_content"
android:textSize="30dip" android:gravity="center_horizontal"
android:text="Click to view More.." android:layout_width="fill_parent"
android:id="#+id/more"></TextView>
</LinearLayout>
in class file
LayoutInflater inflater = activity.getLayoutInflater();
LinearLayout footer = (LinearLayout)inflater.inflate(
R.layout.footer, null);
TextView more= (TextView)footer.findViewById(R.id.more);
more.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
//List Update Code
}
});
use the following code..
footer_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<LinearLayout
android:id="#+id/footer_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
android:layout_gravity="center">
<TextView
android:text="footer_text_1"
android:id="#+id/footer_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold"
android:layout_marginRight="5dip">
</TextView>
</LinearLayout>
</LinearLayout>
and use the following in code:
public class listactivty extends ListActivity{
private Context context = null;
private ListView list = null;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
list = (ListView)findViewById(android.R.id.list);
//code to set adapter to populate list
View footerView =
((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
list.addFooterView(footerView);
}
use seperate XML file for ListView..
Thanks
Shahjahan Ahmad

Categories

Resources