Multiple textviews are not shown in Linear layout - android

i try to develop a small Android app and i use fragments for tab feature. Here is my tab3's layout:
<?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="fill_parent"
android:background="#FF0000"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/European_Central_Bank" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Türk Lirası: " />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dolar:" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Japon Yeni:" />
</LinearLayout>
And this is the Java part:
public class Tab3Fragment extends Fragment {
private String turkishLira;
private String dollar;
private String japaneseMoney;
URL url;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myCreateView=(LinearLayout)inflater.inflate(R.layout.tab_frag3_layout, container, false);
getCurrencyInfo();
TextView textView1 = (TextView) myCreateView.findViewById(R.id.textView1);
TextView textView2 = (TextView) myCreateView.findViewById(R.id.textView2);
TextView textView3 = (TextView) myCreateView.findViewById(R.id.textView3);
textView1.setText(turkishLira);
textView2.setText(dollar);
textView3.setText(japaneseMoney);
return myCreateView;
}
getCurrencyInfo() {...}
}
The question is only the first textview is shown on the phone. If i swap the 1st and 2nd textview, this time 2nd textview which has a static content is shown.
What am i missing? I don't have any problem in getCurrencyInfo method, i printed it's results.
Thanks, in advance.

My guess is that turkishLira, dollar, or japaneseMoney might be null or empty.
Have you debugged getCurrencyInfo() method?
You should maybe try
textView1.setText("turkishLira");
textView2.setText("dollar");
textView3.setText("japaneseMoney");
This will let you check wether this is a layout issue or a getCurrencyInfo() issue.

Make your XML file like this:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/European_Central_Bank" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Türk Lirası: " />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Dolar:" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Japon Yeni:" />

Related

Adding Views dynamically in Linear Layout in Fragment

I am trying to add TextView to the nested linearLayout in fragmentin onCreateView, I don't know if this is the correct approach or not, new to android!
Thanks in advance
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_screen_slide_page, container, false);
List<LifeBerrysXmlParser.Item> items = (ArrayList<LifeBerrysXmlParser.Item>) getArguments().getSerializable("List");
TextView mainHeading = (TextView) rootView.findViewById(R.id.mainHeading);
ImageView mainImage = (ImageView) rootView.findViewById(R.id.articleMainImage);
LinearLayout articleLayout = (LinearLayout) rootView.findViewById(R.id.articleLayout);
TextView tev = new TextView(getActivity());
tev.setText("Hello..............");
articleLayout.addView(tev);
LifeBerrysXmlParser.Item item = items.get(getArguments().getInt("position"));
String articleMainHeading = item.mainHeading;
String articlemainImage = item.mainImage;
mainHeading.setText(articleMainHeading);
if (!articlemainImage.isEmpty()) {
Picasso.with(getContext()).load(articlemainImage).into(mainImage);
}
return rootView;
}
can anyone please help me what I am doing wrong?
here is my xml for the fragment
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:id="#+id/content"
android:padding="1dp"
android:layout_weight="1"
android:background="#000000"
android:layout_gravity="center">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
android:scrollIndicators="right">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/articleMainImage"
android:maxHeight="300dp"/>
<TextView
style="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="?android:attr/listPreferredItemHeightLarge"
android:layout_weight="1"
android:lineSpacingMultiplier="1.2"
android:id="#+id/mainHeading"
android:textColor="#ff6435"
android:clickable="false"
android:layout_gravity="center"
android:layout_centerInParent="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/articleLayout"
android:orientation="horizontal">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read More"
android:textSize="15dp"
android:textStyle="bold"
android:layout_gravity="center"
android:clickable="true"
android:focusable="false"
android:gravity="center"
android:onClick="readMore"
android:padding="1dp"
android:textColor="#android:color/holo_green_light" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
You have mistake in your article layout. Your article layout has horizontal orientation, and it's child TextView has match_parent width, so when you add new child, it'll be outside the screen. Change orientation to vertical:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/articleLayout"
android:orientation="vertical"> <---- Change this line
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read More"
android:textSize="15dp"
android:textStyle="bold"
android:layout_gravity="center"
android:clickable="true"
android:focusable="false"
android:gravity="center"
android:onClick="readMore"
android:padding="1dp"
android:textColor="#android:color/holo_green_light" />
</LinearLayout>

Cannot setText to TextView in Android

I am having real problems trying to set a text to TextView on my android application. The string that I am trying to display was passed to another activity via an intent. I seem to have successfully retrieved this extra from the intent but i am failing to attach it to my TextView. Please can someone help? i am using fragments throughout.
All I am getting is my layout display without the Text that i am trying to assign.
Fragment:2
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.article_fragment, parent, false);
journalTitle = (String) getArguments().getString(EXTRA_JOURNALTITLE); // extra from intent
Log.d("onCreateView", "a.getJournalTitle: " + journalTitle); // this works. displays in logCat
journalTitleView = (TextView) v
.findViewById(R.id.journalTitle_articleFragment_textView);
journalTitleView.setText(journalTitle);
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/journalTitle_articleFragment_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:text="journal title"
android:textSize="12sp" />
<TextView
android:id="#+id/pubYear_articleFragment_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/journalTitle_articleFragment_textView"
android:text="pubYear"
android:textSize="12sp" />
<TextView
android:id="#+id/volume_articleFragment_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/pubYear_articleFragment_textView"
android:text="volume"
android:textSize="12sp" />
<TextView
android:id="#+id/issue_articleFragment_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/volume_articleFragment_textView"
android:text="issue"
android:textSize="12sp" />
<TextView
android:id="#+id/pageInfo_articleFragment_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:layout_toRightOf="#id/issue_articleFragment_textView"
android:text="pageInfo"
android:textSize="12sp" />
<TextView
android:id="#+id/title_articleFragment_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/journalTitle_articleFragment_textView"
android:gravity="center"
android:layout_marginTop="25dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="article title"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/authorString_articleFragment_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/title_articleFragment_textView"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="authors"
android:textSize="12sp" />
<TextView
android:id="#+id/abstractText_articleFragment_textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/authorString_articleFragment_textView"
android:layout_marginBottom="5dp"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:text="abstractText"
android:textSize="15sp" />
In onCreateView you should only inflate your layout and return it:
View v = inflater.inflate(R.layout.article_fragment, parent, false);
return v;
Your textView modifications should be moved to onViewCreated
Try :
TextView journalTitleView = (TextView) v.findViewById(R.id.journalTitle_articleFragment_textView);
journalTitleView.setText(journalTitle);
I think you should try this code :
public static void changeFragmentTextView(String s) {
Fragment frag = getFragmentManager().findFragmentById(R.id.yourFragment);
((TextView) frag.getView().findViewById(R.id.ournalTitle_articleFragment_textView)).setText(s);
}`

Dialog using DialogFragment not showing up properly

I am using DialogFragment to display a dialog.But the dialog was displayed with the title.When i used no title my dialog was not being displayed properly.
Dialog with title
Dialog without title
Xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/share_background"
>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Share"
android:textSize="25sp"
android:textColor="#fff"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button3" />
</LinearLayout>
</LinearLayout>
DialogFragment Code
public class ShareDialogFragment extends DialogFragment {
private View view;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.share_dialog, null, false);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return view;
}
}
Why this is happening????
Try this:
<TextView
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Share"
android:textSize="25sp"
android:textColor="#fff"
android:id="#+id/textView"
android:layout_gravity="center_horizontal" />
Hope this helps.
Fix layout height and width of your parent linear layout to a fixed value.
Example:
android:layout_width="200dp"
android:layout_height="wrap_content"
Try this:
inflater.inflate(R.layout.share_dialog, container, false);

How to get id or values of object in layout using Layout inflater?

Im try Layout Inflater, it's work. Then a try to define the id of object in the view of the layout was inflated. but how to get the values or id of object in these layout? here xml code..
XML hidden.xml
<?xml version="1.0" encoding="utf-8"?>
android:id="#+id/hiddenLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Saya teks 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox1" />
<CheckBox
android:id="#+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox2" />
<CheckBox
android:id="#+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox3" />
<RadioGroup
android:id="#+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Satu" />
<RadioButton
android:id="#+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Dua" />
<RadioButton
android:id="#+id/radio2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tiga" />
</RadioGroup>
XML Main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/add"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Add View" />
<Button
android:id="#+id/remove"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:text="Remove View" />
</LinearLayout>
<ScrollView
android:id="#+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearhost"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="value" />
Java
private int jumlahteks = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View buttonAdd = findViewById(R.id.add);
buttonAdd.setOnClickListener(this);
View buttonRemove = findViewById(R.id.remove);
buttonRemove.setOnClickListener(this);
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId())
{
case R.id.add:
jumlahteks = jumlahteks + 1;
//Check if the Layout already exists
//LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.hiddenLayout);
//Inflate the Hidden Layout Information View
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myLayout = (LinearLayout)findViewById(R.id.linearhost);
View hiddenInfo = inflater.inflate(R.layout.hidden, myLayout, false);
TextView myTextView = (TextView) hiddenInfo.findViewById(R.id.textView1);
// Update the TextView Text
myTextView.setId(jumlahteks);
myTextView.setText(jumlahteks);
myLayout.addView(hiddenInfo);
//Get References to the TextView
break;
case R.id.remove:
View myView = findViewById(R.id.hiddenLayout);
ViewGroup parent = (ViewGroup) myView.getParent();
parent.removeView(myView);
break;
// More buttons go here (if any) ...
}
How to get id or values of object in hidden.xml?? and set the values, im work with this layout inflater to create dinamycaly views and grab the values of each object in that views was inflated. Thanks for help and advice..
I'm not entirely sure what you are trying to do here, but it sounds like you want to have hiddenLayout in your main layout, and want it to be hidden sometimes. Try including hiddenLayout in the appropriate place in main.xml with the include tag, set a name for it using android:id="", and then you can just use findViewById like on the other elements. To hide or show it, call setVisibility(VISIBLE or INVISIBLE).
EDIT: Your hidden.xml doesn't have a root type. It looks like you are missing a "<" as well, so maybe you just didn't include it when you copy/pasted. I would make a Hidden.java class that is a subclass of a type of layout, say a LinearLayout and has accessors to the fields that you want. Then you can use the subclass as follows in your xml . You can then call
Hidden hiddenLayout = inflater.inflate(R.layout.hidden, null)
to get your hidden layout, and then use your accessors like hidden.getTextView1() to get access to the things inside.
You could also just make the root type a LinearLayout and use
LinearLayout hiddenLayout = inflater.inflate(R.layout.hidden, null)
TextView text = hiddenLayout.findViewById(R.id.TextView1)
but IMO subclassing is better because it allows you to encapsulate behavior for your hidden layout beyond what is possible in the xml.

android -> relative-layout changes button text alignment on click

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

Categories

Resources