android: display data in xml - android

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);
}
}
});
}

Related

View width for dynamically added view

I want to show a notification counter like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:gravity="center"
android:padding="#dimen/_5sdp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:alpha=".5"
android:id="#+id/gvIcon"
android:src="#drawable/ic_person_reading"
android:scaleType="centerCrop"
android:layout_width="#dimen/_70sdp"
android:layout_height="#dimen/_70sdp" />
<LinearLayout
android:id="#+id/llTexts"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
style="#style/gridItemsText"
android:id="#+id/gvText"
android:text="Guten Morgen"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/partNoticeTooltip"
android:orientation="vertical"
android:background="#drawable/bg_tooltip_red"
android:layout_width="#dimen/tooltipWH"
android:layout_height="#dimen/tooltipWH">
<TextView
android:id="#+id/tvCounter"
android:text="4"
android:textColor="#color/white"
android:textSize="#dimen/h6"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Which has an output like this:
This is my expectation, but with dynamic way. So I wrote this in the setOnItemClickListener of my GridView:
View tooltip = context.getLayoutInflater().inflate(R.layout.part_tooltip, null);
TextView tvCounter = (TextView) tooltip.findViewById(R.id.tvCounter);
tvCounter.setText("" + counter);
LinearLayout llText = (LinearLayout) view.findViewById(R.id.llTexts);
llText.addView(tooltip);
the part_tooltip has exactly the same code but with another layout. And here is the output:
The red layout does not being displayed with full width. What am I missing?
You need to use a global layout listener and also the view tree observer:
ViewTreeObserver mVTO = parentLayoutOfTheViewAddedDynamically.getViewTreeObserver();
mVTO.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if(viewAddedDynamically != null){
int viewWidth = viewAddedDynamically.getWidth();
// any operation based on viewWidth is to be done here
}
}
});

addView() doesn't show a view

private void showText(){
textView.setText(Html.fromHtml(text));
Log.d("MyLog","tags.size="+tags.size());
for (int i=0;i<tags.size();i++){
Button button = new Button(context);
button.setText(tags.get(i));
ll.addView(button, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT));
tagButtons.add(button);
}
}
This should be simple but this buttons doesn't show. I checked with logs- tags.size=5 so the problem is not in this. ll is a LinearLayout
Here's layout file :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:layout_margin="5dp"
android:id="#+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<LinearLayout
android:orientation="horizontal"
android:id="#+id/ll2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="TextView" />
<LinearLayout
android:id="#+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
So, it's a very simple code and I don't understand why are theese views are not being added to a layout with id "ll"
You should call invalidate() on the parent layout which forces children to be drawn on the screen
Remove the linearlayout reference which you have not posted in the question and use like as i have done, Try this
Call showText() in OnCreate- method If you are using Activities
private void showText(){
textView.setText(Html.fromHtml(text));
for (int i=0;i<tags.size();i++){
Button button = new Button(context);
button.setText(tags.get(i));
LinearLayout ll=(LinearLayout) findViewById(R.id.ll);
ll.addView(button);
}
}

Add view to a vertical LinearLayout at bottom (programmatically)

I need to add views to a vertical Linearlayout at bottom programmatically. (Yes we know that, it's in the title).
Ok : i can add views to the linearlayout, it's not a problem. But it's for a chat, so i need to add views at the bottom for each message which is displayed. I don't find any attributes to the LinearLayout to do that.
Adding messages :
mHandler.post(new Runnable() {
#Override
public void run() {
TextView message = new TextView(ChatActivity.this);
String[] splitMessage = text.split(":");
message.setText(splitMessage[0]+"\n"+splitMessage[1]);
message.setGravity(Gravity.LEFT);
message.setMaxWidth(200);
message.setBackgroundColor(getResources().getColor(android.R.color.tertiary_text_light));
message.setTextColor(getResources().getColor(android.R.color.black));
message.setSingleLine(false);
mLayout.addView(message);
}
});
chat_layout.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/live_obs_mainLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/empty"
android:orientation="vertical" >
<ScrollView
android:id="#+id/chat_layout"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="10" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:isScrollContainer="tue"
>
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:orientation="horizontal" >
<EditText
android:id="#+id/edit_chat"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_gravity="left|center_vertical"
android:layout_weight="5" />
<Button
android:id="#+id/chat_submit"
android:layout_width="wrap_content"
android:layout_height="45dp"
android:layout_gravity="right|center_vertical"
android:onClick="onSubmit"
android:text="OK" />
</LinearLayout>
</LinearLayout>
You can do it pragmatically like this, this is just an example. You may want to change your code based on that.
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout);
TextView txt1 = new TextView(MyClass.this);
LinearLayout.LayoutParams layoutParams =
(RelativeLayout.LayoutParams) txt1.getLayoutParams();
layoutParams.addRule(LinearLayout.BOTTOM, 1);
txt1.setLayoutParams(layoutParams);
linearLayout.addView(txt1);

How to have a ListView with icons beside the text?

I'm trying to have a ListView with icon and text but there is a little problem as you can see in the screen shot (only a very small part of text is visible!):
Here is the xml files creating this layout:
//The overall layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" >
<ListView
android:id="#+id/ListView01"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</LinearLayout>
// this layout is for single rows
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/lIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/lText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Edit: This is the getView method of my custom adapter.
public View getView(int position, View convertView, ViewGroup parent) {
View myView = convertView;
cu.moveToPosition(position);
if (convertView == null) {
LayoutInflater li = getLayoutInflater();
myView = li.inflate(R.layout.listicon, null);
ImageView imageView = (ImageView) myView.findViewById(R.id.lIcon);
myView.setLayoutParams(new GridView.LayoutParams(60, 90));
myView.setPadding(8, 8, 8, 8);
byte[] b = Base64.decode(cu.getString(2),Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(b, 0, b.length));
TextView textView = (TextView) myView.findViewById(R.id.lText);
textView.setText(cu.getString(1));
}
//cu.moveToNext();
int catID = cu.getInt(0);
myView.setTag((Object) catID);
return myView;
}
Try setting layout_width of the ListView and TextView to fill_parent
EDIT: you're resetting list item width in the getView. Either remove or correct your code
Use a relative layout for your rows and place the text right of the image as below :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/lIcon"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/lText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/lIcon"/>
</RelativeLayout>
This is related to the way single row is inflated in the adapter
You should try using
LayoutInflater#inflate(layoutId, parent, false)
as suggested in this answer:
https://stackoverflow.com/a/1662611
Try this layout for the row layout:
Adjust the IDs to yours.. also notice the android:layout_toRightOf id.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<ImageView
android:id="#+id/bimage"
android:layout_height="20dip"
android:layout_width="20dip"
android:layout_gravity="left|center"
android:layout_marginTop="10dip"
android:layout_marginLeft="5dip"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
/>
<TextView
android:id="#+id/btitle"
android:layout_toRightOf="#id/bimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginTop="8dip"
android:paddingLeft="10dip"
android:paddingRight="10dip"
android:textSize="18dip"
android:textColor="#ffffff"
android:gravity="center_vertical"
/>
</RelativeLayout>

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