Multiple instances of the same android compound component - android

I have a custom EditText component (compound component, based on LinearLayout) and I am using multiple instances in the same Activity.
The custom component works as expected, however, when I rotate the ui, the text that was entered in the second component is suddenly copied to the first component. Otherwise, both custom components function exactly as expected.
Both components have unique id in the activity layout.
Here is the code for my customer component:
public class MyEditText extends LinearLayout{
private EditText mEditText;
TextView mTextCounter;
private int errorIconRes;
private String textHint;
private boolean showLength;
private int maxCount;
private int lines;
private boolean phoneField;
private String errorMessage;
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZipEditText, 0, 0);
phoneField = a.getBoolean(R.styleable.ZipEditText_phone, false);
errorIconRes = a.getInt(R.styleable.ZipEditText_icon_err, R.drawable.form_error_icon);
textHint = a.getString(R.styleable.ZipEditText_hint);
maxCount = a.getInt(R.styleable.ZipEditText_maxLength, 100);
lines = a.getInt(R.styleable.ZipEditText_lines, 1);
showLength = a.getBoolean(R.styleable.ZipEditText_showLength, false);
a.recycle();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.comp_zip_edittext, this, true);
mEditText = (EditText) v.findViewById(R.id.text_field);
mEditText.addTextChangedListener(getTextWatcher());
if (textHint!= null)mEditText.setHint(textHint);
mImageViewError = (ImageView) v.findViewById(R.id.icon_error);
mTextCounter = (TextView) v.findViewById((R.id.text_counter));
if (lines>1 && !phoneField) {
mEditText.setLines(lines);
mEditText.setSingleLine(false);
}
if (showLength) {
mTextCounter.setVisibility(View.VISIBLE);
mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxCount)});
mEditText.addTextChangedListener(getTextWatcherLength());
} else {
mTextCounter.setVisibility(View.GONE);
}
Here is the layout file of my custom component:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal">
<EditText
android:id="#+id/text_field"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="left"
android:paddingLeft="10dp"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:layout_alignParentTop="true"
/>
<ImageView
android:id="#+id/icon_error"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/form_error_icon"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:layout_alignRight="#id/text_field"
android:layout_alignTop="#id/text_field"
android:visibility="gone"/>
</RelativeLayout>
<TextView
android:id="#+id/text_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:layout_gravity="right"
android:visibility="gone"/>
</LinearLayout>
And here is my layout file for the activity:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:zip="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:background="#color/white" >
<TextView
android:id="#+id/error_msg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/home_image_red"
android:background="#color/zip_error_gray"
android:text="Something went wrong"
android:visibility="gone"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:background="#color/white" >
<TextView
android:id="#+id/upsell"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textSize="18sp"
android:text="Sign in to use all our features."
/>
<com.myproject.android.components.MyEditText
android:id="#+id/request_phone"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="5dp"
android:gravity="left"
android:singleLine="true"
zip:phone="true"
zip:hint="#string/hint_phone"/>
<com.myproject.android.components.MyEditText
android:id="#+id/request_comments"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="5dp"
android:gravity="left"
zip:lines="3"
zip:maxLength="120"
zip:showLength="true"
zip:hint="#string/hint_comments"/>
<CheckBox
android:id="#+id/checkbox_request_visit"
style="#style/contact_label"
android:layout_centerInParent="true"
android:text="#string/check_home_visit"
android:checked="false" />
<Button
android:id="#+id/request_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="Request a Showing"
android:textColor="#color/pbz_button_primary_foreground_color"
android:textStyle="bold"
android:background="#drawable/button_registration"
android:gravity="center"/>
<TextView
android:id="#+id/terms_link"
style="#style/FinePrint"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center_horizontal"
android:paddingBottom="5dp"
android:text="#string/request_showing_terms"
/>
</LinearLayout>
</LinearLayout>

I figured out how to do this: Instead of loading the component via id, parse the tree and load it by the position in the tree:
Instead of
mEditText = (EditText) v.findViewById(R.id.text_field);
Do this:
LinearLayout linearLayout = (LinearLayout) getChildAt(0);
mEditText = (EditText) relativeLayout.getChildAt(0);
This way, you avoid loading the wrong component when the activity / fragment is re-created after the rotation

The problem is in the same Ids of your EditText components. Try to remove it id,and create you custom components separately by inflating each view from xml, and adding it to complex layout.

Related

how to fix wrong alertDialog on a Click?

I have 2 TextViews showing on the main UI. I associate each TextView to a unique clickListener that launches a unique AlertDialog for the user to make a selection. The first TextView is launching the second layout for the second AlertDialog rather than the expected first layout for the first AlertDialog. What am I missing here?
activity_main.xml file
...
<TextView
android:id="#+id/fList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="20dp"
android:layout_marginStart="20dp"
android:text="filter"
android:textStyle="bold|italic"
android:textColor="#color/text_primary"
/>
<TextView
android:id="#+id/qList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="quickList"
android:textStyle="bold"
android:textColor="#color/text_primary"
android:gravity="center"
/>
MainActivity.java file
public class MainActivity extends AppCompatActivity
...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView mTextViewFilter = (TextView)findViewById(R.id.fList);
TextView mTextViewQuickList = (TextView)findViewById(R.id.qList);
mTextViewFilter.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view){
final AlertDialog.Builder alertDialogFilter = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflaterFilter = getLayoutInflater();
final ViewGroup nullParent = null;
// the AlertDialog layout for the first TextView click.
final View dialogLayoutFilter = inflaterFilter.inflate(R.layout.filter_main, nullParent);
alertDialogFilter.setView(dialogLayoutFilter);
final AlertDialog dialogFilter = alertDialogFilter.show();
...
}
});
mTextViewQuickList.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(MainActivity.this);
LayoutInflater inflater = getLayoutInflater();
final ViewGroup nullParent = null;
// the AlertDialog layout for the second TextView click.
final View dialogLayout = inflater.inflate(R.layout.entire_layout, nullParent);
alertDialog.setView(dialogLayout);
final AlertDialog dialog = alertDialog.show();
...
}
});
entire_layout.xml
...
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="380dp"
android:minHeight="160dp" >
<ImageView
android:id="#+id/imageViewX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingRight="20dp"
android:paddingEnd="20dp"
android:contentDescription="x"
android:src="#drawable/ic_close_white_24dp" />
<TextView
android:id="#+id/FullList"
android:layout_below="#+id/imageViewX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.Large"
android:text="Show entire list" />
<TextView
android:id="#+id/fullNewest"
android:layout_below="#+id/FullList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginBottom="10dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="Created ... />
filter_main.xml
...
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minWidth="380dp"
android:minHeight="280dp" >
<ImageView
android:id="#+id/imageViewX"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:paddingRight="20dp"
android:paddingEnd="20dp"
android:contentDescription="x"
android:src="#drawable/ic_close_white_24dp" />
<TextView
android:id="#+id/Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageViewX"
android:layout_marginStart="20dp"
android:layout_marginLeft="20dp"
android:textStyle="bold"
android:textAppearance="#android:style/TextAppearance.Large"
android:text="Filter..." />
<TextView
android:id="#+id/AllDos"
android:layout_below="#+id/Filter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="All..." />
<TextView
android:id="#+id/AllBuys"
android:layout_below="#+id/AllDos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="All..." />
<TextView
android:id="#+id/AllWork"
android:layout_below="#+id/AllBuys"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="All..." />
<TextView
android:id="#+id/AllHome"
android:layout_below="#+id/AllWork"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="All..." />
<TextView
android:id="#+id/AllWaitingfor"
android:layout_below="#+id/AllHome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:layout_marginStart="40dp"
android:layout_marginLeft="40dp"
android:layout_marginBottom="5dp"
android:textAppearance="#android:style/TextAppearance.Medium"
android:text="All..." />
Not sure what your textviews' parent is but since you are using attributes like:
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
I guess it is a RelativeLayout.
Since you aren't using neither an orientation nor a relative position, I suppose your TextViews are both on the same line, with just a different margin top, but the quick list TextView has width match_parent and gravity center
android:layout_width="match_parent"
android:gravity="center"
So what's happening is something like this:
And your click always triggers the second TextView that covers entirely the first one.
To fix it change your quick list TextView this way:
<TextView
android:id="#+id/qList"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:text="quickList"
android:textStyle="bold"
android:textColor="#color/text_primary"
android:layout_centerHorizontal="true"/>
With the attributes:
android:layout_width="wrap_content"
android:layout_centerHorizontal="true"
It will be centered but not overlapping the first TextView.
I hope this could help, if instead I am wrong and your activity_main.xml file differs, please update your code so that it can be easier figure out any alternative problem.

How can i overlay one Button over another button

i want 0 to 9 buttons in two row.Each button have overlay of another small button in right bottom corner.when i click the button,count will be displayed on overlay button.sorry for my bad english thanks in advance
<?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">
<LinearLayout
android:id="#+id/expandable2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignBottom="#id/btn0"
android:layout_alignRight="#id/btn0"
android:background="#FFFFFF"
android:text="0" />
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
My output
any other suggestion will be appreciated
Try setting it like this
android:layout_marginLeft="-20dp"
android:layout_marginTop="10dp"
Use a textview for showing the count because a button over a button is meaningless.
Anyways how to show it.
<RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "60dp">
<Button
android:layout_width = "match_parent"
android:layout_height="match_parent"
android:id="#+id/btn"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:id="#+id/countbtn"
android:layout_alignParentBottom="true"
android:layout_alignParentRight= "true"/>
</RelativeLayout>
use this view in your Grid or RecyclerView Adapter to make it 10 times as you wish to show.
incase you want to use TextView to show Count remove the countBtn and make it a textView with last two attributes same.
Use a framelayout
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/icon_image"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/black"
android:text="0" />
<Button
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="bottom|right"
android:background="#color/white"
android:layout_marginLeft="6dp"
android:src="#drawable/ic_goal_check" />
</FrameLayout>
After that you can reuse this layout for all your buttons. To reuse this in your xml, create a custom view as follows :
public class YourButton extends FrameLayout {
private Button buttonOne, buttonTwo;
public YourButton(Context context) {
super(context);
this.context = context;
initializeViews();
}
public YourButton(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GoalButton);
typedArray.recycle();
initializeViews();
}
private void initializeViews() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.your_button_layout, this);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
buttonOne = (TextView) this.findViewById(R.id.button_one);
buttonTwo = (ImageView) this.findViewById(R.id.button_two);
}
public void setCount(String count){
buttonTwo.setText(count);
}
}
now you can use in your layout as
<yourPackageName.YourButton
android:id="#+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This will keep your main layout simple and tidy.

Android custom array adapter getView is not called

this is my adapter, the getView method is not called even if the array list having elements
public class CarrierSammuryAdapter extends ArrayAdapter<CarrierSummary> {
private final Activity context;
private final ArrayList<CarrierSummary> ite;
private final int lay;
public CarrierSammuryAdapter(Activity context, ArrayList<CarrierSummary> menuLinkList,int layout) {
super(context,layout );
this.context = context;
this.ite = menuLinkList;
this.lay=layout;
}
// static to save the reference to the outer class and to avoid access to
// any members of the containing class
static class ViewHolder {
public TextView customer;
public TextView attempts;
public TextView successful;
public TextView minutes;
public TextView ASR;
public TextView ACD;
public TextView NER;
public TextView PDD;
}
#Override
public int getCount () {
return ite.size();
}
#Override
public long getItemId (int position) {
return position;
}
#Override
public CarrierSummary getItem (int position) {
return ite.get(position);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// ViewHolder will buffer the assess to the individual fields of the row layout
final ViewHolder holder;
// Recycle existing view if passed as parameter
// This will save memory and time on Android
// This only works if the base layout for all classes are the same
View rowView = convertView;
if (rowView == null) {
LayoutInflater inflater = context.getLayoutInflater();//this gives error !!
rowView=inflater.inflate(R.layout.carriersumamry_item,parent,false);
holder = new ViewHolder();
holder.customer=(TextView)rowView.findViewById(R.id.customer);
holder.attempts=(TextView)rowView.findViewById(R.id.attempts);
holder.successful=(TextView)rowView.findViewById(R.id.successful);
holder.minutes=(TextView)rowView.findViewById(R.id.minutes);
holder.ASR=(TextView)rowView.findViewById(R.id.asr);
holder.ACD=(TextView)rowView.findViewById(R.id.acd);
holder.NER=(TextView)rowView.findViewById(R.id.ner);
holder.PDD=(TextView)rowView.findViewById(R.id.pdd);
// ViewResizing.setListRowTextResizing(rowView, context);
rowView.setTag(holder);
} else {
holder = (ViewHolder) rowView.getTag();
}
holder.customer.setText(ite.get(position).getCustomer());
holder.attempts.setText(ite.get(position).getAttempts());
holder.successful.setText(ite.get(position).getSuccessful());
holder.minutes.setText(ite.get(position).getMinutes());
holder.ASR.setText(ite.get(position).getASR());
holder.ACD.setText(ite.get(position).getACD());
holder.NER.setText(ite.get(position).getNER());
holder.PDD.setText(ite.get(position).getPDD());
return rowView;
}
}
and this is how i called it
carrierSummaryList =(ListView)findViewById(R.id.carrierSummary_listview);
CarrierSammuryAdapter adapter = new CarrierSammuryAdapter(CarrierSummaryActivity.this, carriersam,R.layout.carriersumamry_item);
carrierSummaryList.setAdapter(adapter);
I search a lot to solve this issue but no solution, getView method is never called.
this is my XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pdd"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ner"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/acd"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/asr"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/minutes"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/successful"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/attempts"
android:textColor="#color/grey"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/customer"
android:textColor="#color/grey"
/>
</LinearLayout>
I had this exact same problem, to solve it, I simply changed the ArrayAdapter to BaseAdapter, implemented the overriden methods and it worked.
And as khuskal said, you should always use Context instead of Activity.
there are two mistakes..
first is change Activity to Contex in constructor
secondly change
rowView=inflater.inflate(R.layout.single_row,parent,false);
Hope this will help you
/**
* Constructor
*
* #param context The current context.
* #param resource The resource ID for a layout file containing a TextView to use when
* instantiating views.
*/
public ArrayAdapter(Context context, int resource) {
init(context, resource, 0, new ArrayList<T>());
}
while you are doing super(context,layout) , this is the constructor that gets called. So adapter data is set to empty list.
Like derek said, Instead you should use super(context,layout,menuLinkList);
/**
* Constructor
*
* #param context The current context.
* #param resource The resource ID for a layout file containing a TextView to use when
* instantiating views.
* #param objects The objects to represent in the ListView.
*/
public ArrayAdapter(Context context, int resource, List<T> objects) {
init(context, resource, 0, objects);
}
The problem was in my drawerLayout, i put the listView into RelativeLayout but when i change to LinearLayout its working, this is the code
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white"
android:orientation="vertical">
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/mainbar"
layout="#layout/second_topmain"/>
<ListView
android:id="#+id/carrierSummary_listview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/black"
></ListView>
<include
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="0dp"
android:id="#+id/mainbar"
layout="#layout/bottom_bar"/>
</LinearLayout>
<LinearLayout
android:id="#+id/linearSlider"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="right"
android:background="#7e7e7e"
android:orientation ="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Carrier Type"
android:textColor="#android:color/white"
android:padding="5dp"
android:background="#color/darkgrey"
/>
<LinearLayout
android:padding="3dp"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:background="#color/darkgrey"
android:layout_height="wrap_content">
<Button
android:id="#+id/customerButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/grey"
android:onClick="toggleClicked"
android:background="#drawable/toggleon"
android:text="Customer"
/>
<Button
android:id="#+id/SupplierButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="toggleClicked"
android:textColor="#color/grey"
android:background="#drawable/toggleoff"
android:text="Supplier"
/>
</LinearLayout>
<RelativeLayout
android:padding="3dp"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Carrier"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnerCarrier"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:padding="3dp"
android:background="#color/darkgrey"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Top"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
/>
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/SpinnerTop"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:padding="3dp"
android:layout_width="wrap_content"
android:onClick="showDateTimePickerFrom"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="From Date"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/fromDate"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:padding="3dp"
android:background="#color/darkgrey"
android:layout_width="wrap_content"
android:onClick="showDateTimePickeTo"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="To Date"
android:layout_alignParentLeft="true"
android:layout_centerHorizontal="true"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/toDate"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
<RelativeLayout
android:padding="3dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Group By Profile"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/byprofilecheck"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>

Dynamic View not being created in proper place. Overlapping previous view. Android

I am new to Android so I've been trying to tweak around for the past couple days but am stuck at this part. For my app, I'm trying to input a Button "Add More" which inflates a view to help incorporate user input but I'm running into issues. I looked around and I used his source code to help with my issue but I am running into the problem that my inflated xml layout is overlapping my previous set-up layout.
This is the layout that I have setup for my new_class_container file
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parentView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:background="#drawable/gradient_gray">
<!-- Class Name -->
<TextView
android:id="#+id/register_class_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/write_class_name"
android:layout_marginTop="20dp"
android:layout_marginLeft="5dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"/>
<EditText
android:id="#+id/register_class_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/register_class_textview"
android:layout_alignBaseline="#+id/register_class_textview"
android:inputType="text"
android:hint="#string/example_name"
android:singleLine="true" >
</EditText>
<!-- Assignment Type and Grade Weight -->
<TextView
android:id="#+id/register_assign_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/register_class_textview"
android:layout_alignLeft="#+id/register_class_textview"
android:text="#string/write_assignment_type"
android:layout_marginTop="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<EditText
android:id="#+id/register_assign_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/register_assign_textview"
android:layout_alignLeft="#+id/register_assign_textview"
android:gravity="center_horizontal"
android:hint="#string/example_assign"
android:singleLine="true">
</EditText>
<!-- Save Button -->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:gravity="bottom" >
<Button
android:id="#+id/button_save"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/save_class" />
</RelativeLayout>
<Button
android:id="#+id/btnAddNewItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/register_assign_edit"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp"
android:gravity="center_horizontal"
android:onClick="onAddNewClicked"
android:paddingLeft="5dp"
android:text="#string/more_assign"
android:textColor="#FFFFFF" />
<EditText
android:id="#+id/register_weight_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btnAddNewItem"
android:layout_alignLeft="#+id/register_weight_textview"
android:ems="10"
android:hint="#string/example_weight"
android:inputType="number"
android:singleLine="true" >
</EditText>
<TextView
android:id="#+id/register_weight_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/register_assign_textview"
android:layout_alignBottom="#+id/register_assign_textview"
android:layout_alignParentRight="true"
android:text="#string/write_assign_percent"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
This is how the layout for the code above looks like and I want to be able to press the "Add More" Button and it inflates to "create a new row", per say, of the TextViews of Assignment Type and Grade Weight and EditTexts of the respective TextViews. But when I run the app and press add more, the new set of TextView + EditText pop up right on top of what I have rather than underneath. The following is my other xml file new_class
<?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="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/register_assign_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="#string/write_assignment_type"
android:textAppearance="?android:attr/textAppearanceMedium"/>
<TextView
android:id="#+id/register_weight_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="#string/write_assign_percent"
android:textAppearance="?android:attr/textAppearanceMedium"
android:layout_marginLeft="30dp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/register_assign_edit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:hint="#string/example_assign"
android:singleLine="true">
</EditText>
<EditText
android:id="#+id/register_weight_edit"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/example_weight"
android:layout_marginLeft="30dp"
android:gravity="center_horizontal"
android:inputType="number"
android:singleLine="true">
</EditText>
</LinearLayout>
</LinearLayout>
And this is the Java Code that I have
public class NewClass extends Activity{
private RelativeLayout mContainerView;
private Button mAddButton;
private View mExclusiveEmptyView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_class_container);
mContainerView = (RelativeLayout) findViewById(R.id.parentView);
mAddButton = (Button) findViewById(R.id.btnAddNewItem);
}
protected void onSaveInstanceState(Bundle outState){
super.onSaveInstanceState(outState);
//TODO: Handle Screen Rotation
}
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// TODO: Handle screen rotation:
// restore the saved items and inflate each one with inflateEditRow;
}
// onClick handler for the "Add new" button;
public void onAddNewClicked(View v) {
// Inflate a new row and hide the button self.
inflateEditRow(null);
v.setVisibility(View.GONE);
}
// onClick handler for the "X" button of each row
public void onDeleteClicked(View v) {
// remove the row by calling the getParent on button
mContainerView.removeView((View) v.getParent());
}
private void inflateEditRow(String name) {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View rowView = inflater.inflate(R.layout.new_class, null);
//final ImageButton deleteButton = (ImageButton) rowView
// .findViewById(R.id.buttonDelete);
final EditText editText = (EditText) rowView
.findViewById(R.id.register_assign_edit);
if (name != null && !name.isEmpty()) {
editText.setText(name);
} else {
mExclusiveEmptyView = rowView;
//deleteButton.setVisibility(View.INVISIBLE);
}
// A TextWatcher to control the visibility of the "Add new" button and
// handle the exclusive empty view.
editText.addTextChangedListener(new TextWatcher() {
#Override
public void afterTextChanged(Editable s) {
if (s.toString().isEmpty()) {
mAddButton.setVisibility(View.GONE);
//deleteButton.setVisibility(View.INVISIBLE);
if (mExclusiveEmptyView != null
&& mExclusiveEmptyView != rowView) {
mContainerView.removeView(mExclusiveEmptyView);
}
mExclusiveEmptyView = rowView;
} else {
if (mExclusiveEmptyView == rowView) {
mExclusiveEmptyView = null;
}
mAddButton.setVisibility(View.VISIBLE);
// deleteButton.setVisibility(View.VISIBLE);
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
}
});
// Inflate at the end of all rows but before the "Add new" button
mContainerView.addView(rowView, mContainerView.getChildCount() - 1);
}
}
What can I do to prevent it from overlapping and showing up underneath? And how will I be able to move the button each time it's 'updated'? Because if I move the button to the new_class xml file then it won't show up originally for me to add more views.
I tried implementing the following code and what it did was enabled me to properly get a dynamic view added the first time but each time after it was overlapping the added view.
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
p.addRule(RelativeLayout.BELOW, R.id.register_assign_edit);
rowView.setLayoutParams(p);
This will work with having two layout files: main_layout and layout_to_include.
main_layout.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="400dp"
android:id="#+id/sv1" >
<LinearLayout
android:id="#+id/llAddingContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<include layout="#layout/layout_to_include"/>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/llButtonHolder"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_below="#+id/sv1"
android:gravity="center_horizontal"
android:layout_above="#+id/button2" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAlignment="center"
android:text="Add More" />
</LinearLayout>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:textAlignment="center"
android:text="Save Class" />
</RelativeLayout>
layout_to_include.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Class Name:"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="Ex: CS 301" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_below="#+id/linearLayout1" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="1" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Assignment Type"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ex: Homework" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginRight="10dp"
android:layout_weight="1" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Grade Weight(%)"
android:textAppearance="?android:attr/textAppearanceMedium" />
<EditText
android:id="#+id/editText3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Ex: 10" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
To use these layouts in your code:
....
setContentView(R.layout.main_layout);
....
llAddingContainer = (LinearLayout) findViewById(R.id.llAddingContainer);
// Clicking on this button will add another view from 'layout_to_include' to the ScrollView
button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
View view = getLayoutInflater().inflate(R.layout.layout_to_include, null);
// Find the elements in this view and set them up
EditText et1 = (EditText) view.findViewById(R.id.editText1)
....
....
....
// Add the view to the LinearLayout inside ScrollView
llAddingContainer.addView(view);
});
I'm not sure, but you can try follow code to move any view. I recommend to use RelativeLayout like your main layout.
RelativeLayout.LayoutParams bParams1 = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
bParams1.leftMargin = (int) leftMrgn;
bParams1.topMargin = (int) topMgrn;
bParams1.height = (int) bHeight;
bParams1.width = (int) bWidth;
anyView.setLayoutParams(bParams1);
RelativeLayouts act like a FrameLayout if you don't give them any LayoutParams (constraints like below, above, center, etc.).
You can either construct the appropriate RelativeLayout.LayoutParams, or better yet, create an empty vertical LinearLayout in your first layout that will contain the rows. Then, just add the rows to the LinearLayout instead of the RelativeLayout.

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