How to align text in Spinner for android? - android

I want to align text in Spinner to the left but how can i achieve this require help
here is my code & screen shot
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/roundshape" >
<!-- Lable Area -->
<TableRow
android:id="#+id/tblRwspnLbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="1dip">
<TextView
android:id="#+id/lblCust"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="#string/lblCust"
android:textSize="14sp"
android:textStyle="bold"
android:gravity="left"/>
<TextView
android:id="#+id/lblPros"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:text="#string/lblPros"
android:textSize="14sp"
android:textStyle="bold" />
</TableRow>
<!-- Spinner Area -->
<TableRow
android:id="#+id/tblRwspn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="1dip"
android:gravity="center" >
<Spinner
android:id="#+id/spnAECust"
android:layout_width="75dp"
android:layout_height="35dp"
android:fontFamily="verdana,arial,helvetica"
android:hint="#string/SelectCust"
android:textSize="14sp"
android:layout_gravity="left"/>
<Spinner
android:id="#+id/spnAEProspect"
android:layout_width="75dp"
android:layout_height="35dp"
android:fontFamily="verdana,arial,helvetica"
android:hint="#string/SelectProspect"
android:textSize="14sp" />
</TableRow>
<!-- Text Area -->
Here is my Screen the red dot is the space which i want to remove so tht my all labels and spinners will come in same aligned line

Just remove android:padding="1dip" from Spinner TableRow and try i am not sure just try
Edit:
You have to use a custom view here to give alignment to the Spinner values. Create a CustomView like this answer has created and add android:gravity' to the TextView asright`.
And set the CustomView to your adapter using
adapter.setDropDownViewResource(android.R.layout.your_custom_created_view);

Thank you all for giving all possible answers here is solution i get
 android:layout_width="75dp"
android:layout_height="35dp"
  android:layout_marginLeft="5dp"
android:paddingLeft="5dp"
android:gravity="left"

I think you should use custom ArrayAdapter with TextView and play with textview property as per your requirement.
Example :
MyAdapter.java
public class MyAdapter extends ArrayAdapter {
Context context;
public MyAdapter(Context context, int resource) {
super(context, resource);
this.context=context;
// TODO Auto-generated constructor stub
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=inflater.inflate(R.layout.mylayout, parent,false);
TextView textView=(TextView)view.findViewById(R.id.text);
return view;
}
}
mylayout.xml
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text"
android:layout_width="match-parent"
android:layout_height="wrap_content"
android:text="#string/lblPros"
android:textSize="14sp"
android:gravity="right"
android:textStyle="bold" />
</LinearLayout>

Try this:-
use android:layout_span="1". if required then give left margin for the first row.
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#drawable/roundshape" >
<!-- Lable Area -->
<TableRow
android:id="#+id/tblRwspnLbl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" ">
<TextView
android:id="#+id/lblCust"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/lblCust"
android:layout_span="1"
android:textSize="14sp"
android:textStyle="bold"
android:gravity="left"/>
<TextView
android:id="#+id/lblPros"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="1"
android:text="#string/lblPros"
android:textSize="14sp"
android:textStyle="bold" />
</TableRow>
<!-- Spinner Area -->
<TableRow
android:id="#+id/tblRwspn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center" >
<Spinner
android:id="#+id/spnAECust"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="1"
android:fontFamily="verdana,arial,helvetica"
android:hint="#string/SelectCust"
android:textSize="14sp"
android:layout_gravity="left"/>
<Spinner
android:id="#+id/spnAEProspect"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_span="1"
android:fontFamily="verdana,arial,helvetica"
android:hint="#string/SelectProspect"
android:textSize="14sp" />
</TableRow>
<!-- Text Area -->

This is sample code it may useful
<Spinner
android:id="#+id/spinner1"
style="?android:attr/spinnerStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:background="#drawable/dropdown"
android:dropDownVerticalOffset="1dp"
android:focusable="false"
android:spinnerMode="dropdown" />
take one more layout for the text view following is the code for layout
spinner_text_layout.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text_for_spinner"
style="?android:attr/spinnerItemStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="10dp"
android:drawableRight="#drawable/arrow"
android:gravity="center"
android:textColor="#android:color/white" >
</TextView>
now in the custom adapter these are the two methods
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.spinner_text_layout, null);
}
TextView textView = (TextView) convertView
.findViewById(R.id.text_for_spinner);
textView.setText((String) getItem(position));
notifyDataSetChanged();
return convertView;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = LayoutInflater.from(context).inflate(
R.layout.spinner_text_layout, null);
}
TextView textView = (TextView) convertView
.findViewById(R.id.text_for_spinner);
textView.setCompoundDrawablesWithIntrinsicBounds(0, 0, 0, 0);
textView.setText((String) getItem(position));
textView.setTextColor(Color.BLACK);
textView.setBackgroundResource(R.drawable.drop_down_selector);
return convertView;
}

Related

Remove one layout from listview dynamically

I am working on an application in which I am using custom listview and for my listview I have define a row layout for customisation.
In my row layout I have two Relative layouts.
I want to dynamically remove that one Relative layout from my listview and then add it again.
Please help me how can I do that. I have added a screen shot of my layout you can see here
Below is my Row layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/r2_imageslayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_persontext"
android:text="Per Person"
android:textSize="15sp"
android:textColor="#color/toolbar_color"
android:layout_marginLeft="150sp"
android:layout_marginTop="#dimen/size_8"
/>
<TextView
android:layout_width="100sp"
android:layout_height="wrap_content"
android:id="#+id/result2_price"
android:text="$568"
android:textSize="20sp"
android:textStyle="bold"
android:background="#color/yellow"
android:layout_alignParentRight="true"
android:layout_marginRight="#dimen/size_10"
android:layout_marginTop="#dimen/size_5"
android:paddingLeft="#dimen/size_15"
/>
<ImageView
android:layout_width="30sp"
android:layout_height="20sp"
android:contentDescription="#null"
android:layout_marginLeft="#dimen/size_10"
android:layout_marginTop="#dimen/size_30"
android:id="#+id/r2_departairlineimage"
android:src="#drawable/dl"
/>
<TextView
android:layout_width="40sp"
android:layout_height="20sp"
android:id="#+id/result2_flightcode"
android:text="SG250"
android:textSize="12sp"
android:layout_marginLeft="10sp"
android:layout_below="#+id/r2_departairlineimage"
/>
<ImageView
android:layout_width="30sp"
android:layout_height="20sp"
android:id="#+id/r2_depart_image"
android:layout_toRightOf="#+id/r2_departairlineimage"
android:layout_marginLeft="#dimen/size_15"
android:layout_marginTop="35sp"
android:src="#drawable/depart"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_departtime"
android:text="11:30"
android:textColor="#color/toolbar_color"
android:textSize="#dimen/size_15"
android:layout_toRightOf="#+id/r2_depart_image"
android:layout_marginLeft="#dimen/size_5"
android:layout_marginTop="36sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_dep_citycode"
android:text="Bos"
android:textSize="12sp"
android:layout_toRightOf="#+id/r2_departtime"
android:layout_marginTop="38sp"
android:layout_marginLeft="#dimen/size_3"
/>
<ImageView
android:layout_width="80sp"
android:layout_height="wrap_content"
android:id="#+id/r2_image"
android:contentDescription="#null"
android:src="#drawable/onestop_line"
android:layout_toRightOf="#+id/r2_dep_citycode"
android:layout_toEndOf="#+id/r2_dep_citycode"
android:layout_marginTop="42sp"
android:layout_marginLeft="#dimen/size_10"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_1stoptext"
android:text="1 stop"
android:textSize="12sp"
android:layout_toRightOf="#+id/r2_dep_citycode"
android:layout_toEndOf="#+id/r2_dep_citycode"
android:layout_marginTop="32sp"
android:layout_marginLeft="#dimen/size_30"
/>
<ImageView
android:layout_width="10sp "
android:layout_height="10sp"
android:id="#+id/r2_departclock"
android:layout_toRightOf="#+id/r2_dep_citycode"
android:layout_toEndOf="#+id/r2_dep_citycode"
android:layout_below="#+id/r2_1stoptext"
android:layout_marginTop="5sp"
android:src="#drawable/time_image"
android:layout_marginLeft="#dimen/size_30"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="15sp"
android:id="#+id/r2_dep_flightduration"
android:text="5h 60m"
android:textSize="12sp"
android:layout_below="#+id/r2_1stoptext"
android:layout_marginTop="#dimen/size_1"
android:layout_toRightOf="#+id/r2_departclock"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_arrivetime"
android:text="11:30"
android:textColor="#color/toolbar_color"
android:textSize="#dimen/size_15"
android:layout_toRightOf="#+id/r2_image"
android:layout_marginLeft="#dimen/size_5"
android:layout_marginTop="36sp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_arrive_citycode"
android:hint="Was"
android:textSize="12sp"
android:layout_toRightOf="#+id/r2_arrivetime"
android:layout_marginTop="38sp"
android:layout_marginLeft="#dimen/size_3"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/result2_returnlayout" >
<ImageView
android:layout_width="30sp"
android:layout_height="20sp"
android:contentDescription="#null"
android:layout_marginLeft="#dimen/size_10"
android:layout_marginTop="#dimen/size_80"
android:id="#+id/r2_return_airlineimage"
android:src="#drawable/dl"
/>
<TextView
android:layout_width="40sp"
android:layout_height="20sp"
android:id="#+id/result2_returnflightcode"
android:text="SG250"
android:textSize="12sp"
android:layout_marginLeft="10sp"
android:layout_below="#+id/r2_return_airlineimage" />
<ImageView
android:layout_width="30sp"
android:layout_height="20sp"
android:id="#+id/r2_return_image"
android:layout_toRightOf="#+id/r2_return_airlineimage"
android:layout_marginLeft="#dimen/size_15"
android:layout_marginTop="80sp"
android:src="#drawable/return_image" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_return_deptime"
android:text="11:30"
android:textColor="#color/toolbar_color"
android:textSize="#dimen/size_15"
android:layout_toRightOf="#+id/r2_return_image"
android:layout_marginLeft="#dimen/size_5"
android:layout_marginTop="80sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_return_depcitycode"
android:text="Bos"
android:textSize="12sp"
android:layout_toRightOf="#+id/r2_return_deptime"
android:layout_marginTop="82sp"
android:layout_marginLeft="#dimen/size_3"/>
<ImageView
android:layout_width="80sp"
android:layout_height="10sp"
android:id="#+id/r2_ret_image"
android:contentDescription="#null"
android:src="#drawable/nonstop"
android:layout_toRightOf="#+id/r2_return_depcitycode"
android:layout_toEndOf="#+id/r2_return_depcitycode"
android:layout_marginTop="85sp"
android:layout_marginLeft="#dimen/size_10"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_return_nonstoptext"
android:text="Non stop"
android:textSize="12sp"
android:layout_toRightOf="#+id/r2_return_depcitycode"
android:layout_toEndOf="#+id/r2_return_depcitycode"
android:layout_marginTop="75sp"
android:layout_marginLeft="#dimen/size_25" />
<ImageView
android:layout_width="10sp "
android:layout_height="10sp"
android:id="#+id/r2_ret_clock"
android:layout_toRightOf="#+id/r2_return_depcitycode"
android:layout_toEndOf="#+id/r2_return_depcitycode"
android:layout_below="#+id/r2_return_nonstoptext"
android:layout_marginTop="5sp"
android:src="#drawable/time_image"
android:layout_marginLeft="#dimen/size_30"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="15sp"
android:id="#+id/r2_ret_flightduration"
android:text="5h 60m"
android:textSize="12sp"
android:layout_marginTop="#dimen/size_1"
android:layout_below="#+id/r2_return_nonstoptext"
android:layout_toRightOf="#+id/r2_ret_clock"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_ret_arrivetime"
android:text="11:30"
android:textColor="#color/toolbar_color"
android:textSize="#dimen/size_15"
android:layout_toRightOf="#+id/r2_ret_image"
android:layout_marginLeft="#dimen/size_5"
android:layout_marginTop="80sp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/r2_ret_arrivecitycode"
android:text="Was"
android:textSize="12sp"
android:layout_toRightOf="#+id/r2_ret_arrivetime"
android:layout_marginTop="82sp"
android:layout_marginLeft="#dimen/size_3"/>
</RelativeLayout>
</RelativeLayout>
That's my adapter code:
Result2Adapter(Context context, List list_row)
{
// super(context, resourceID, list_row);
this.context=context;
this.list_row=list_row;
}
#Override
public int getCount() {
return list_row.size();
}
#Override
public Custom_Result getItem(int position) {
return list_row.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
private class ViewHolder
{
ImageView DepAirline_logo, Ret_Airlinelogo;
TextView Dep_Airline_code,dep_time,dep_arr_time,dep_stops,dep_duration,Dep_dep_citycode,Dep_arrivecitycode,Ret_dep_citycode,Ret_arrivecitycode,Ret_Airline_code,Ret_time,Ret_arr_time,
Ret_stops,Ret_duration,price;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder=null;
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(convertView==null){
convertView=inflater.inflate(R.layout.result2_rowlayout,null);
holder=new ViewHolder();
holder. DepAirline_logo=(ImageView) convertView.findViewById(R.id.r2_departairlineimage);
holder.Dep_Airline_code=(TextView) convertView.findViewById(R.id.result2_flightcode);
holder.dep_time=(TextView) convertView.findViewById(R.id.r2_departtime);
holder.dep_arr_time=(TextView) convertView.findViewById(R.id.r2_arrivetime);
holder.Dep_arrivecitycode=(TextView) convertView.findViewById(R.id.r2_arrive_citycode);
holder.dep_stops=(TextView) convertView.findViewById(R.id.r2_1stoptext);
holder.dep_duration=(TextView) convertView.findViewById(R.id.r2_dep_flightduration);
holder.price=(TextView) convertView.findViewById(R.id.result2_price);
holder.Dep_dep_citycode=(TextView) convertView.findViewById(R.id.r2_dep_citycode);
holder.Ret_Airlinelogo = (ImageView) convertView.findViewById(R.id.r2_return_airlineimage);
holder.Ret_Airline_code = (TextView) convertView.findViewById(R.id.result2_returnflightcode);
holder.Ret_time = (TextView) convertView.findViewById(R.id.r2_return_deptime);
holder.Ret_arr_time = (TextView) convertView.findViewById(R.id.r2_ret_arrivetime);
holder.Ret_arrivecitycode = (TextView) convertView.findViewById(R.id.r2_ret_arrivecitycode);
holder.Ret_dep_citycode = (TextView) convertView.findViewById(R.id.r2_return_depcitycode);
holder.Ret_stops = (TextView) convertView.findViewById(R.id.r2_return_nonstoptext);
holder.Ret_duration = (TextView) convertView.findViewById(R.id.r2_ret_flightduration);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
String retimagename=list_row.get(position).getDep_Airline_code().toLowerCase();
String retpath="drawable/"+retimagename;
int retimageresource=context.getResources().getIdentifier(retpath, null, context.getPackageName());
String imagename=list_row.get(position).getRet_Airline_code().toLowerCase();
String path="drawable/"+imagename;
int imageresource=context.getResources().getIdentifier(path, null, context.getPackageName());
if(imageresource==0)
{
imageresource=R.drawable.flight_icon;
}
if(retimageresource==0)
{
imageresource=R.drawable.flight_icon;
}
Drawable image=context.getResources().getDrawable(imageresource);
Drawable retimage=context.getResources().getDrawable(imageresource);
holder.DepAirline_logo.setImageDrawable(image);
holder.Ret_Airlinelogo.setImageDrawable(retimage);
holder.Dep_Airline_code.setText(list_row.get(position).getDep_Airline_name());
holder.Ret_Airline_code.setText(list_row.get(position).getRet_Airline_name());
holder.dep_time.setText(list_row.get(position).getDepart_time());
holder.Ret_time.setText(list_row.get(position).getRet_dep_time());
holder.dep_arr_time.setText(list_row.get(position).getArrive_time());
holder.Ret_arr_time.setText(list_row.get(position).getRet_arr_time());
holder.dep_stops.setText(list_row.get(position).getDep_stops());
holder.Ret_stops.setText(list_row.get(position).getRet_stops());
holder.dep_duration.setText(list_row.get(position).getDep_duration());
holder.Ret_duration.setText(list_row.get(position).getRet_duration());
holder.Dep_arrivecitycode.setText(list_row.get(position).getDep_CityCode());
holder.Ret_arrivecitycode.setText(list_row.get(position).getArr_citycode());
holder.Ret_dep_citycode.setText(list_row.get(position).getRet_dep_citycode());
holder.Ret_arrivecitycode.setText(list_row.get(position).getRet_arr_citycoode());
holder.price.setText(list_row.get(position).getPrice());
return convertView;
}
You could do that in the adapter class's getView method.
For a particular row by inflating the view and getting the relative layout by using findViewById().
e.g.,
RelativeLayout relativeLayout1 = (RelativeLayout) view.findViewById(R.id.relativeLayoutId);
You can hide the layout by using
relativeLayout1.setVisibility(View.GONE);
and unhide using
relativeLayout1.setVisibility(View.VISIBLE);

How to make the listView row height change based on content?

I have the following layout which corresponds to a row in a list view. I wanted the TextView in the middle to expand based on the length of text.
With the below layout, my textview is not even visible. Can someone point out what mistake I am doing?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="3dip"
android:paddingLeft="6dip"
android:paddingRight="6dip" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_centerVertical="true"
android:clickable="true"
android:scaleType="fitXY"
android:src="#drawable/batman" />
<LinearLayout
android:id="#+id/user_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/imageView1"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingTop="5dp" >
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#color/display_name_color"
android:textSize="14dp" />
<TextView
android:id="#+id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="15dp"
android:text="Small Text"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textSize="12dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/actions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/imageView1"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<ImageView
android:id="#+id/favourite"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="40dp"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/bookmark"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="60dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<TextView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/actions"
android:layout_below="#+id/user_info"
android:layout_toRightOf="#+id/imageView1"
android:paddingBottom="5dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="5dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
and this is my adapter's getView function.
#Override
public View getView(int position, View convertView, ViewGroup container) {
ImageView imageView;
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.list_row, container, false);
imageView = (ImageView)convertView.findViewById(R.id.imageView1);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
} else {
imageView = (ImageView)convertView.findViewById(R.id.imageView1);
}
imageView.setTag(position);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int position = Integer.parseInt(v.getTag().toString());
mOnProfilePicClickCallback.onItemClick(position);
}
});
//Load TextFields here
Feed feed = (Feed) getItem(position);
User user = null;
if (feed != null) {
user = (User) CommonData.users.get(feed.owner);
TextView handle = (TextView) convertView.findViewById(R.id.handle);
TextView userName = (TextView) convertView.findViewById(R.id.username);
TextView feedContent = (TextView) convertView.findViewById(R.id.feedcontent);
handle.setText(feed.owner);
if(tweeter!=null && !TextUtils.isEmpty(user.displayname))
{
userName.setText(user.displayname);
}
else
{
userName.setText(feed.owner);
}
feedContent.setText(feed.feedcontent);
}
// Finally load the image asynchronously into the ImageView, this also takes care of
// setting a placeholder image while the background thread runs
if(user!=null)
{
mImageFetcher.loadImage(user.profileimageurl, imageView);
}
return convertView;
}
Try this...
Remove android:layout_above="#+id/actions"from your "content" TextView
Add android:layout_below="#+id/content" to your "actions" LinearLayout
Remove android:layout_alignParentBottom="true"from "actions"
It may work, if it still doesn't use LinearLayouts instead of RelativeLayout.
In my ListView, every row height changes automaticly according the data.

Android - onListItemClick

I have a list view with an image view and text view for each list element, as per the screenshot below. The image and text views together make the clickable element so if you tap to the right of any text nothing happens, you have to tap on the text or image. How do I make the link span the width of the whole list element, basically to the right hand edge of the screen?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:layout_gravity="center|right"
android:gravity="center|right"
android:src="#drawable/image5" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:layout_marginTop="4dp"
android:layout_gravity="center|right"
android:gravity="center|right" >
</TextView>
</LinearLayout>
Make your Parent Layout width and height as match_parent/fill_parent
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/icon"
android:layout_width="32dp"
android:layout_height="32dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="10dp"
android:layout_marginTop="4dp"
android:layout_gravity="center|right"
android:gravity="center|right"
android:src="#drawable/image5" >
</ImageView>
<TextView
android:id="#+id/label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="20sp"
android:layout_marginTop="4dp"
android:layout_gravity="center|right"
android:gravity="center|right" >
</TextView>
</LinearLayout>
In your listview item layout, set the width of textview to 0dp and add android:layout_weight="1" attribute to textview.
You can make something like this inside your Adapters getView method:
public View getView(int position, View convertView, ViewGroup parent) {
View vi = convertView;
if(convertView==null) { ... }
...
vi.setOnClickListener(new OnListItemClickListener(position));
return vi;
}
private class OnListItemClickListener implements OnClickListener {
private int mPosition;
OnListItemClickListener(int pos) {
this.mPosition = pos;
}
#Override
public void onClick(View arg0) {
//DO WHATEVER YOU WANT WITH YOUR "CLICK". THE POSITION CLICKED IS STORED INSIDE mPosition.
}
}

Problema with InfoWindowAdapter laytout

I implemented one layout to use on popup of map marker (Maps V2), but I cant configure my layout as I need.
In the image bellow, you can see that the popup with informations about the marker have a white border, I need remove it. I neet fill the popup background with yellow (as the center box).
What is wrong in my layout xml?
My XML (info_window_map)
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/pastel" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/blue_nightsky"
android:orientation="horizontal"
>
<TextView
android:id="#+id/linha_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Linha "
android:textColor="#color/white"
android:textSize="13sp"
android:textStyle="bold" />
<TextView
android:id="#+id/cod_linha"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:textColor="#color/white"
android:textSize="13sp"
android:layout_gravity="left"
android:layout_marginLeft="5dp"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/onibus_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ônibus....: "
android:textColor="#color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/cod_onibus"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="12sp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/previsao_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Previsão..: "
android:textColor="#color/black"
android:textSize="12sp"
android:textStyle="bold" />
<TextView
android:id="#+id/previsao"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
My Adapter (that use the xml)
class MyInfoWindowAdapter implements InfoWindowAdapter{
private final View myContentsView;
MyInfoWindowAdapter(){
myContentsView = getLayoutInflater().inflate(R.layout.info_window_map, null);
}
#Override
public View getInfoContents(Marker marker) {
//Seta a linha no infowindow
TextView txtTitle = ((TextView)myContentsView.findViewById(R.id.cod_linha));
txtTitle.setText(marker.getTitle());
//Pega a informação em snippet e separa linha, codOnibus do endereço
String snippet = marker.getSnippet();
Snippet snp = new Snippet();
snp.fillSnippet(snippet);
TextView txtCodLinha = ((TextView)myContentsView.findViewById(R.id.cod_linha));
txtCodLinha.setText(snp.getLinha());
TextView txtCodOnibus = ((TextView)myContentsView.findViewById(R.id.cod_onibus));
txtCodOnibus.setText(snp.getCodOnibus());
TextView txtPrevisao = ((TextView)myContentsView.findViewById(R.id.previsao));
txtPrevisao.setText(snp.getPrevisao());
return myContentsView;
}
#Override
public View getInfoWindow(Marker marker) {
// TODO Auto-generated method stub
return null;
}
}

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" />

Categories

Resources