Change The Textview Size Runtime in listview - android

i want to change the textview size runtime dynamically which is used in listview but is not take any effect on textview i divide after getting my screen width in 6 part after getting screen width runtime i am assign the textview size from screen width but this textview is in different layout so how to take this layout in my layout and change the size of textview runtime
here i put my layout
Home.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/textlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="12"
android:layout_weight="1"
android:gravity="center">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#drawable/flightlist"
android:text="Destination"
android:textSize="17dp"
android:textStyle="bold" />
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Flight"
android:textSize="17dp"
android:textStyle="bold" />
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#drawable/flightlist"
android:text="Airlines"
android:textSize="17dp"
android:textStyle="bold" />
<TextView
android:id="#+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Schedule"
android:textSize="17dp"
android:textStyle="bold" />
<TextView
android:id="#+id/text5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Term Gate"
android:textSize="17dp"
android:textStyle="bold"
android:background="#drawable/flightlist" />
<TextView
android:id="#+id/text6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Status"
android:textSize="17dp"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="#+id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="10"
>
</ListView>
</LinearLayout>
list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="6"
android:gravity="center">
<TextView
android:id="#+id/textdestination"
android:layout_width="183dp"
android:layout_height="fill_parent"
android:text="Destination"
android:textSize="15dp"
android:gravity="center"
/>
<TextView
android:id="#+id/textflight"
android:layout_width="105dp"
android:layout_height="fill_parent"
android:text="Flight"
android:textSize="15dp"
android:gravity="center"
android:background="#drawable/flightlist"
/>
<TextView
android:id="#+id/textairline"
android:layout_width="185dp"
android:layout_height="fill_parent"
android:text="Airlines"
android:textSize="15dp"
android:gravity="center"/>
<TextView
android:id="#+id/textschedule"
android:layout_width="73dp"
android:layout_height="fill_parent"
android:text="Schedule"
android:textSize="15dp"
android:gravity="center"
android:background="#drawable/flightlist"
/>
<TextView
android:id="#+id/texttermgate"
android:layout_width="105dp"
android:layout_height="fill_parent"
android:text="Term Gate"
android:textSize="15dp"
android:gravity="center"
/>
<TextView
android:id="#+id/textstatus"
android:layout_width="73dp"
android:layout_height="fill_parent"
android:text="Status"
android:textSize="15dp"
android:gravity="center"
android:background="#drawable/flightlist"/>
</LinearLayout>
HomeActivity.java
View inflater=getLayoutInflater().inflate(R.layout.list2, list, false);
TextView textdestination=(TextView)inflater.findViewById(R.id.textdestination);
textdestination.setHeight(50);
textdestination.setWidth((textlayoutwidth*25)/100);
textdestination.setTextSize(100);

You can do that in the getView() method of the Adapter you use for the ListView.
mInflater = (LayoutInflater) mActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.list, parent,
false);
holder.tv1 = (TextView) convertView
.findViewById(R.id.textdestination);
holder.tv2 = (TextView) convertView
.findViewById(R.id.textflight);
holder.tv3 = (TextView) convertView
.findViewById(R.id.textairline);
.......
.......
.......
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
if(someCondition){
holder.tv1.setLayoutParams(new LinearLayout.LayoutParams(50,50));
holder.tv2setLayoutParams(new LinearLayout.LayoutParams(50,50));
holder.tv3.setLayoutParams(new LinearLayout.LayoutParams(50,50));
.....
.....
.....
}
return convertView;
}

you can change your textview width and height on runtime with :
textdestination.setWidth(pixels);
textdestination.setHeight(pixels);

Related

Android Inside Custom ListView, TextView Weight property not working

I have a custom listView. The listview data are setting by some textview horizontally for each rows. In main layout I have same number of textView for setting the title of those columns. My problem is, the main layout working fine with the weight property. But the custom value setting layout for listview which contains the textview is not working with the weight property.
Main Layout : (Weight property working correctly)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:weightSum="8">
<TextView
android:layout_weight="0"
android:id="#+id/cid"
android:layout_width="0dp"
android:textSize="16dp"
android:text="B.Id" />
<TextView
android:layout_weight="1"
android:id="#+id/nm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Name" />
<TextView
android:layout_weight=".9"
android:id="#+id/mob"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Mobile" />
<TextView
android:layout_weight="1.2"
android:id="#+id/eml"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Email" />
<TextView
android:layout_weight=".8"
android:id="#+id/bk_no"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Bike No" />
<TextView
android:layout_weight=".4"
android:id="#+id/dy_rt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Rate" />
<TextView
android:layout_weight=".9"
android:id="#+id/frm"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="From Date" />
<TextView
android:layout_weight=".9"
android:id="#+id/to"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="To Date" />
<TextView
android:layout_weight=".25"
android:id="#+id/dy"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Dys" />
<TextView
android:layout_weight=".45"
android:id="#+id/cst"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="0"
android:id="#+id/vid"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="V.ID" />
<TextView
android:layout_weight=".9"
android:id="#+id/book_dt"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="1dp"
android:text="Bk Date" />
<TextView
android:layout_weight=".3"
android:id="#+id/rting"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Rating" />
</LinearLayout>
<ListView
android:id="#+id/list_past_report"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
</LinearLayout>
Custom ListView Value setting Layout:(report_custom_list)(Weight property not working)
<?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" >
<LinearLayout
android:id="#+id/cus_layout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center_horizontal"
android:weightSum="8" >
<TextView
android:layout_weight="0"
android:id="#+id/id"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="1"
android:id="#+id/nm"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_weight=".9"
android:id="#+id/mob"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="1.2"
android:id="#+id/eml"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_weight=".8"
android:id="#+id/bk_no"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_weight=".4"
android:id="#+id/dy_rt"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_weight=".9"
android:id="#+id/frm"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_weight=".9"
android:id="#+id/to"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_weight=".25"
android:id="#+id/dy"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_weight=".45"
android:id="#+id/cst"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_weight="0"
android:id="#+id/vid"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_weight=".9"
android:id="#+id/book_dt"
android:layout_width="0dp"
android:layout_height="wrap_content" />
<TextView
android:layout_weight=".3"
android:id="#+id/rting"
android:layout_width="0dp"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Custom Adapter Setting values:
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
final Object[] id_obj=_id.toArray();
Object[] name_obj=c_name1.toArray();
Object[] mb_obj=m_no1.toArray();
Object[] eml_obj=email1.toArray();
...
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.report_custom_list, null);
if(position%2==0)
rowView.setBackgroundColor(Color.YELLOW);
else
rowView.setBackgroundColor(Color.GRAY);
holder.tv=(TextView) rowView.findViewById(R.id.id);
holder.tv1=(TextView) rowView.findViewById(R.id.nm);
holder.tv2=(TextView) rowView.findViewById(R.id.mob);
...
holder.tv.setText((CharSequence) id_obj[position]);
holder.tv1.setText((CharSequence) name_obj[position]);
holder.tv2.setText((CharSequence) mb_obj[position]);
...
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
return rowView;
}
Before I hard coded the width for the textview. But if it goes to different screen the design collapse. How to fix this?
Probably the problem is:
​​
<ListView
android:id="#+id/list_past_report"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>`
Replace layout_width="wrap_content" with layout_width="match_parent"
change listviews layout_width to fill_parent
android:layout_width="fill_parent"
I think you have to update you main layout file with this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/list_past_report"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></ListView> </LinearLayout>
and make some changes in Adapter class also
public View getView(final int position, View convertView, ViewGroup parent) {
final Object[] id_obj=_id.toArray();
Object[] name_obj=c_name1.toArray();
Object[] mb_obj=m_no1.toArray();
Object[] eml_obj=email1.toArray();
...
Holder holder=new Holder();
View rowView;
rowView = inflater.inflate(R.layout.report_custom_list, null);
if(position%2==0)
rowView.setBackgroundColor(Color.YELLOW);
else
rowView.setBackgroundColor(Color.GRAY);
holder.tv=(TextView) rowView.findViewById(R.id.id);
holder.tv1=(TextView) rowView.findViewById(R.id.nm);
holder.tv2=(TextView) rowView.findViewById(R.id.mob);
...
if(position == 0){
holder.tv.setText("Title1");
holder.tv.setText("Title2");
holder.tv.setText("Title3");
....
}
else{
holder.tv.setText((CharSequence) id_obj[position]);
holder.tv1.setText((CharSequence) name_obj[position]);
holder.tv2.setText((CharSequence) mb_obj[position]);
...
}
rowView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
}
});
return rowView;}

How to remove list view item in list view android?

In my application i have one custom list view. whenever i click the close image list item, the list item layout must be removed with space. i used visibility:gone attribute, the list item layout deleted but the space is visible. I am using listview in scroll view ,,that was also not working....please help me out.
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
if (inflater == null)
inflater = (LayoutInflater) activity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
convertView = inflater.inflate(R.layout.recharge_listitem, null);
TextView accountnumber = (TextView) convertView
.findViewById(R.id.accountnumber);
TextView servicename = (TextView) convertView
.findViewById(R.id.servicetypname);
TextView planamount = (TextView) convertView
.findViewById(R.id.rechargeamount);
final ImageView close = (ImageView) convertView
.findViewById(R.id.crosse_close_img);
final LinearLayout layoutrecharge=(LinearLayout)convertView.findViewById(R.id.rechargelay);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
layoutrecharge.setVisibility(View.GONE);
}
});
accountnumber.setText("Rs."+bpData.get(position).getAccountNumber());
servicename.setText(bpData.get(position).getServicename());
planamount.setText(bpData.get(position).getRechargeamount());
return convertView;
}
}
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/background">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1e365a">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_margin="10dp"
android:text="PAYMENT"
android:textColor="#ffffff"
android:textSize="20dp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginTop="10dp"
android:text="Your Recharges"
android:textColor="#1e365a"
android:textSize="20dp" />
</RelativeLayout>
<View
android:layout_width="fill_parent"
android:layout_height="4dp"
android:background="#drawable/dottedline_view" />
<ListView
android:id="#+id/recharge_lv"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone">
</ListView>
<View
android:layout_width="fill_parent"
android:layout_height="0.8dp"
android:layout_marginTop="12dp"
android:background="#1e365a" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:text="Amount to be Paid"
android:textColor="#1e365a"
android:textSize="20dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_weight="1"
android:layout_margin="10dp"
android:text="Rs.130"
android:textColor="#1e365a"
android:textSize="20dp" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:gravity="center_vertical"
android:orientation="horizontal"
android:weightSum="100" >
<EditText
android:id="#+id/ev_b_dob"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_weight="50"
android:hint="Enter coupn code"
android:inputType="text" >
<requestFocus />
</EditText>
<Button
android:id="#+id/bt_apply"
style="#style/payment_button_style"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_marginLeft="-5dp"
android:layout_weight="20"
android:gravity="center"
android:text="Apply" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="10dp"
android:layout_marginLeft="28dp"
android:layout_marginRight="30dp">
<CheckBox
android:id="#+id/cb_reload_cash"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Pay using Reload Cash"
android:textColor="#1e365a"
android:textSize="20dp"
android:layout_marginLeft="12dp"/>
</LinearLayout>
<Button
android:id="#+id/atmdebit_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="28dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="#F93249"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:text="ATM/DEBIT/CREDIT"
android:textColor="#ffffff"
android:textSize="20dp"/>
<Button
android:id="#+id/netbankng_btn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="28dp"
android:layout_marginRight="30dp"
android:layout_marginTop="20dp"
android:background="#F93249"
android:gravity="center"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:text="NET BANKING"
android:textColor="#ffffff"
android:textSize="20dp"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
please help me.
custom listview item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rechargelay">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="3">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1">
<TextView
android:id="#+id/accountnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8008505106" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:text="(" />
<TextView
android:id="#+id/servicetypname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Vodafone" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=")" />
</LinearLayout>
<TextView
android:id="#+id/rechargeamount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="RS.30"
android:layout_weight="1" />
<ImageView
android:id="#+id/crosse_close_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/close"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
Set View.GONE for View is not right way to remove row from ListView. to remove row remove select row item from Adapter data-source and call notify data change method of adapter to populate changes:
close.setTag(position);
close.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int deletePos=Integer.parseInt(v.getTag().toString());
bpData.remove(deletePos);
notifyDataSetChanged();
}
});
Set visibility View.GONE of SCROLL VIEW or other parent of list view....

ANDROID :: custom ListView show only first LinearLayout Child

HERE is my xml for custom ListView
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.prototype.auinsight.schedual_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/wrapper">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="0dp"
android:layout_height="35dp"
android:id="#+id/imgDay" android:layout_weight=".1"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"
android:layout_marginLeft="12dp"/>
<com.prototype.auinsight.schedual_listview.CustomView_Schedule
android:layout_width="0dp"
android:layout_height="35dp"
android:id="#+id/view"
custom:labelColor="#android:color/white"
custom:timelineColor="#color/slate"
android:layout_weight=".8"
android:layout_marginTop="5dp" android:layout_marginBottom="5dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="35dp"
android:id="#+id/imgUpDown" android:scaleType="fitXY" android:src="#drawable/down"
android:layout_weight=".1" android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:layout_marginRight="12dp"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtTime1" android:text="09:00" android:paddingLeft="10dp" android:paddingRight="10dp"
android:paddingTop="5dp" android:paddingBottom="5dp" android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ENGLISH IV"
android:id="#+id/txtSubject1" android:paddingLeft="10dp" android:paddingRight="10dp"
android:paddingTop="5dp" android:paddingBottom="5dp" android:textSize="20sp"
android:background="#color/subject_alizarin" android:textColor="#ffffff"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtTime2" android:text="09:00" android:paddingLeft="10dp" android:paddingRight="10dp"
android:paddingTop="5dp" android:paddingBottom="5dp" android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ENGLISH IV"
android:id="#+id/txtSubject2" android:paddingLeft="10dp" android:paddingRight="10dp"
android:paddingTop="5dp" android:paddingBottom="5dp" android:textSize="20sp"
android:background="#color/subject_alizarin" android:textColor="#ffffff"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtTime3" android:text="09:00" android:paddingLeft="10dp" android:paddingRight="10dp"
android:paddingTop="5dp" android:paddingBottom="5dp" android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ENGLISH IV"
android:id="#+id/txtSubject3" android:paddingLeft="10dp" android:paddingRight="10dp"
android:paddingTop="5dp" android:paddingBottom="5dp" android:textSize="20sp"
android:background="#color/subject_alizarin" android:textColor="#ffffff"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtTime4" android:text="09:00" android:paddingLeft="10dp" android:paddingRight="10dp"
android:paddingTop="5dp" android:paddingBottom="5dp" android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ENGLISH IV"
android:id="#+id/txtSubject4" android:paddingLeft="10dp" android:paddingRight="10dp"
android:paddingTop="5dp" android:paddingBottom="5dp" android:textSize="20sp"
android:background="#color/subject_alizarin" android:textColor="#ffffff"/>
</LinearLayout>
<LinearLayout android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtTime5" android:text="09:00" android:paddingLeft="10dp" android:paddingRight="10dp"
android:paddingTop="5dp" android:paddingBottom="5dp" android:textSize="20sp"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="ENGLISH IV"
android:id="#+id/txtSubject5" android:paddingLeft="10dp" android:paddingRight="10dp"
android:paddingTop="5dp" android:paddingBottom="5dp" android:textSize="20sp"
android:background="#color/subject_alizarin" android:textColor="#ffffff"/>
</LinearLayout>
</LinearLayout>
HERE is my Adapter
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
viewHolder vh = null;
if(v == null){
vh = new viewHolder();
LayoutInflater vi = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.schedual_listview,null);
v.setBackgroundResource(R.drawable.custom_middle_listview);
vh.wrapper = (LinearLayout) v.findViewById(R.id.wrapper);
vh.imgDay = (ImageView) v.findViewById(R.id.imgDay);
vh.customView = (CustomView_Schedule) v.findViewById(R.id.view);
vh.txtTime1 = (TextView) v.findViewById(R.id.txtTime1);
vh.txtTime2 = (TextView) v.findViewById(R.id.txtTime2);
vh.txtTime3 = (TextView) v.findViewById(R.id.txtTime3);
vh.txtTime4 = (TextView) v.findViewById(R.id.txtTime4);
vh.txtTime5 = (TextView) v.findViewById(R.id.txtTime5);
vh.txtSubject1 = (TextView) v.findViewById(R.id.txtSubject1);
vh.txtSubject2 = (TextView) v.findViewById(R.id.txtSubject2);
vh.txtSubject3 = (TextView) v.findViewById(R.id.txtSubject3);
vh.txtSubject4 = (TextView) v.findViewById(R.id.txtSubject4);
vh.txtSubject5 = (TextView) v.findViewById(R.id.txtSubject5);
v.setTag(vh);
}else{
vh = (viewHolder)v.getTag();
}
Schedule_lvItem schedule_lvItem = scheduleLvItems.get(position);
vh.imgDay.setImageResource(schedule_lvItem.getDay());
vh.customView.setSubjectData(schedule_lvItem.getSubjectData());
vh.txtTime1.setText("09:00");
vh.txtTime2.setText("10:30");
vh.txtTime3.setText("12:00");
vh.txtTime4.setText("13:30");
vh.txtTime5.setText("15:00");
vh.txtSubject1.setText("ENGLISH IV");
vh.txtSubject2.setText("MACRO");
vh.txtSubject3.setText("STRATEGIC TECHNOLOGY MANAGEMENT");
vh.txtSubject4.setText("DATA COMMUNICATION");
vh.txtSubject5.setText("OBJECT ORIENTED PROGRAMMING");
AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
AbsListView.LayoutParams.FILL_PARENT,
schedule_lvItem.getCurrentHeight()
);
vh.wrapper.setLayoutParams(layoutParams);
schedule_lvItem.setWrapper(vh.wrapper);
v.setTag(vh);
return v;
}
public class viewHolder{
LinearLayout wrapper;
ImageView imgDay;
CustomView_Schedule customView;
TextView txtTime1;
TextView txtTime2;
TextView txtTime3;
TextView txtTime4;
TextView txtTime5;
TextView txtSubject1;
TextView txtSubject2;
TextView txtSubject3;
TextView txtSubject4;
TextView txtSubject5;
}
The first LinearLayout visible in the screen while others not
NOTE:: This is for a single row items of ListView
I'm currently create expandable ListView with slide animation.
This Layout will contains both before and after expand effect does.
what did I do wrong?? plz advice.
Your first linearlayout is taking the full space from its parent because of FILL_PARENT :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.prototype.auinsight.schedual_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/wrapper">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"> <=== here
replace it with : android:layout_height="wrap_content" should solve your issue.
Using a parent with heith : wrap_content, and a child with height fill_parent has unpredictable result since both rely on each other for their size.
By the way "FILL_PARENT" is deprecated, it has been renamed to "MATCH_PARENT".

Android RelativeLayout not center

I have a problem about TextView in RelativeLayout is not center, (only some objects)
First, I'm use ListAdapter for ListView.
public class ListAdapter extends ArrayAdapter<Menu> {
private LayoutInflater inflater;
public ListAdapter(Context c, ArrayList<Menu> o) {
super(c, 0, o);
inflater = (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getView(int position, View v, ViewGroup parent) {
View view = null;
if (v == null) {
view = inflater.inflate(R.layout.row, null);
} else {
view = v;
}
final Menu data = this.getItem(position);
TextView title = (TextView) view.findViewById(R.id.title);
ImageView background = (ImageView) view.findViewById(R.id.background);
ImageView icon = (ImageView) view.findViewById(R.id.icon);
TextView subtitle = (TextView) view.findViewById(R.id.subtitle);
subtitle.setText(data.getSubTitle());
title.setText(data.getTitle());
background.setImageDrawable(data.getBackground());
icon.setImageDrawable(data.getIcon());
return view;
}
}
and, My row.xml.
<?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" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="75dip"
android:layout_height="82dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
<ImageView
android:id="#+id/background"
android:layout_width="75dip"
android:layout_height="83dip"
android:src="#drawable/mediumorchid" />
<ImageView
android:id="#+id/icon"
android:layout_width="35dip"
android:layout_height="35dip"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/ic_discard" />
</RelativeLayout>
<FrameLayout
android:id="#+id/fl1"
android:layout_width="75dip"
android:layout_height="80dip"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</FrameLayout>
<RelativeLayout
android:id="#+id/width"
android:layout_width="fill_parent"
android:layout_height="80dip"
android:layout_alignBottom="#+id/fl1"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/fl1"
android:layout_toRightOf="#+id/fl1"
android:background="#ecf0f1" >
<RelativeLayout
android:id="#+id/item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginBottom="10dip"
android:text="Pure"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:layout_below="#+id/title"
android:layout_marginTop="5dip"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</RelativeLayout>
</RelativeLayout>
<FrameLayout
android:id="#+id/frameLayout1"
android:layout_width="75dip"
android:layout_height="2dip"
android:layout_alignLeft="#+id/width"
android:layout_alignParentRight="true"
android:layout_below="#+id/width"
android:background="#drawable/cdivider" >
</FrameLayout>
This xml's Graphic Design
But in Device(example grouper) , i discover this problem.
In some object, (example see Help ~) Was centered properly. but, other objects, wasn't centered properly.
and i discover this problem another device(different resolution, size, Android version, density).
What can i do solve this problem??
Try this..
Add android:layout_centerHorizontal="true" for subtitle TextView
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/title"
android:layout_marginTop="5dip"
android:gravity="center_vertical"
android:layout_centerHorizontal="true"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />
or this
<TextView
android:id="#+id/subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_below="#+id/title"
android:layout_marginTop="5dip"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall" />

How to center align a textview below imageview as a title in a List view

I am not able to align a text below imageview in a listview. below is my code.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center" >
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="50dp" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/icon"
android:layout_alignParentBottom="true"
android:gravity="center"
android:singleLine="true"
android:layout_marginLeft="20dp"
android:textColor="#color/white"/>
</RelativeLayout>
and ListView is
<ListView android:id="#+id/grid_view"
android:layout_width="wrap_content"
android:layout_height="280sp"
android:layout_alignParentLeft="true"
android:layout_marginTop="20dp"
android:layout_toLeftOf="#+id/imgView"
android:scrollbars="none"
android:divider="#null"
android:gravity="center" >
</ListView>
java code to dynamically add text and image from array
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
convertView = l_Inflater.inflate(R.layout.gridview_layout, null);
holder = new ViewHolder();
holder.imageName = (TextView) convertView.findViewById(R.id.name);
holder.image = (ImageView) convertView.findViewById(R.id.icon);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
holder.imageName.setText(name[position]);
holder.image.setImageResource(mThumbIds[position]);
return convertView;
}
not getting how to center text below imageview. please help me to solve this problem.
Simply use the layout_centerHorizontal attribute in your TextView:
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/icon"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:singleLine="true"
android:textColor="#color/white" />
In textview change
android:layout_width="wrap_content"
to
android:layout_width="match_parent"
You should use Linear layout to set orientation attribute. Set orientation to vertical for Linear layout and set imageview attribute android:layout_gravity="center"
Here is the complete code:
<?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:orientation="vertical"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:layout_marginBottom="#dimen/activity_horizontal_margin"
>
<ImageView
android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/home" >
</ImageView>
<TextView
android:id="#+id/grid_item_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cardiology"
android:layout_gravity="center"
android:layout_marginTop="5px"
android:textSize="20sp" >
</TextView>
</LinearLayout>
hope it will help

Categories

Resources