Android bubble chat in relative layout - android

I am working on chat layout in android.
My problem is that bubble is not resizing according to message text.
I followed this example Bubble Chat layout
Please suggest the solution.
http://i62.tinypic.com/4i0mcn.png (not able to upload image due to low rep)
Overall layout code :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/main">
<LinearLayout
android:id="#+id/bottom_write_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#drawable/message_bar" >
<EditText
android:id="#+id/text"
android:layout_width="10dip"
android:layout_height="wrap_content"
android:layout_margin="5sp"
android:layout_weight="1.88"
android:background="#drawable/message_field"
android:ems="15"
android:padding="5sp"
android:textColor="#color/textFieldColor" >
<requestFocus />
</EditText>
<Button
android:layout_width="40dp"
android:layout_height="27dp"
android:layout_margin="5sp"
android:layout_weight="0.07"
android:background="#drawable/ic_action_send"
android:onClick="sendMessage"
android:textColor="#FFF" />
</LinearLayout>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_above="#id/bottom_write_bar"
android:layout_alignParentTop="true"
android:cacheColorHint="#FFFFFF"
android:divider="#FFFFFF"
android:dividerHeight="2dp"
android:listSelector="#FFFFFF" >
</ListView>
<TextView
android:id="#android:id/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_above="#id/bottom_write_bar"
android:layout_alignParentTop="true"
android:gravity="center_vertical|center_horizontal"
android:text="#string/main_empty_messages" />
</RelativeLayout>
Message layout codes are:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/background"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:paddingRight="4dip"
android:paddingBottom="4dip"
android:background="#drawable/speech_bubble_green">
<!--message -->
<TextView
android:id="#+id/tvmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:layout_marginLeft="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:text="#string/strstatus"
android:textColor="#B5ACAF"
android:textSize="15sp"
/>
<TextView
android:id="#+id/tvdate"
android:layout_marginTop="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="#string/strtime"
android:textColor="#FFFFFF"
android:textSize="12sp"
/>
<TextView
android:id="#+id/tvname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_alignLeft="#+id/tvmessage"
android:fontFamily="georgia"
android:paddingBottom="29dip"
android:text="#string/strname"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="serif" />
</RelativeLayout>
Java code :
Message message = (Message) this.getItem(position);
ViewHolder holder;
if(convertView == null)
{
holder = new ViewHolder();
convertView = LayoutInflater.from(mContext).inflate(R.layout.sms, parent, false);
holder.message = (TextView) convertView.findViewById(R.id.tvmessage);
holder.tdate = (TextView) convertView.findViewById(R.id.tvdate);
holder.tsender = (TextView) convertView.findViewById(R.id.tvname);
convertView.setTag(holder);
}
else
holder = (ViewHolder) convertView.getTag();
RelativeLayout bg = (RelativeLayout)convertView.findViewById(R.id.background);
/* Current Time formatting */
Time = "";
suff = "AM";
timeHR = new SimpleDateFormat("HH").format(Calendar.getInstance().getTime());
time = Integer.parseInt(timeHR);
if(time > 12){
time = time - 12;
suff = "PM";
}
String timeMIN = new SimpleDateFormat("mm").format(Calendar.getInstance().getTime());
// SimpleDateFormat("yyyyMMdd_HHmmss") for full Date!
if(!message.isStatusMessage()){
Time = String.valueOf(time)+ ":"+timeMIN+" "+suff;
}
if(message.isMine()) {
pre = "Me";
bg.setBackgroundResource(R.drawable.bubble_green);
bg.setGravity(Gravity.RIGHT);
} else {
bg.setBackgroundResource(R.drawable.bubble_yellow);
bg.setGravity(Gravity.LEFT);
pre = this.sender;
}
holder.message.setTextColor(R.color.textColor);
holder.message.setText(message.getMessage());
holder.tdate.setText(Time);
holder.tsender.setText(pre);
if(message.isStatusMessage()){
holder.tdate.setText("");
holder.tsender.setText("");
}
//check if it is a status message then remove background, and change text color.
if(message.isStatusMessage())
{
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
holder.message.setBackgroundDrawable(null);
} else {
holder.message.setBackground(null);
}
holder.message.setTextColor(R.color.textFieldColor);
}
return convertView;

The parent RelativeLayout has:
android:layout_width="fill_parent"
And, indeed, in your image, it does will the parent's width. At the least, you will need to change that to:
android:layout_width="wrap_content"
It would also be good practice to define the relative layout fully:
<TextView
android:id="#+id/tvname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:fontFamily="georgia"
android:paddingBottom="29dip"
android:text="#string/strname"
android:textColor="#000000"
android:textSize="14sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/tvdate"
android:layout_marginTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginRight="10dp"
android:text="#string/strtime"
android:textColor="#FFFFFF"
android:textSize="9sp"
/>
<TextView
android:id="#+id/tvmessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="28dp"
android:layout_marginLeft="5dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_below="#id/tvname"
android:text="#string/strstatus"
android:textColor="#B5ACAF"
android:textSize="15dip"
/>

I know is bit late but may help others with the problem.
bg.setBackgroundResource(R.drawable.bubble_green);
Isn't set in the viewHolder so isn't resizing.
Declare the bg layout in the viewHolder and call it:
holder.bg.setBackgroundResource(R.drawable.bubble_green);

Related

How to sort the views added dynamically

I have this master view where I have a TableLayout to which I add new views programmatically. The master view is given below (masterLayout.xml)
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tableLayoutActivity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:padding="5dp"
android:stretchColumns="*">
<TableRow
android:id="#+id/tableRow3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:weightSum="2">
<ScrollView
android:id="#+id/queryScrollView"
android:layout_width="match_parent"
android:layout_span="2"
android:padding="5dp">
<TableLayout
android:id="#+id/queryTableLayoutActivity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:stretchColumns="*"></TableLayout>
</ScrollView>
</TableRow>
In the above view I add new views to queryTableLayoutActivity Layout. The layout to be added is given below (childLayout.xml)
<RelativeLayout 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:background="#drawable/border"
android:orientation="vertical"
tools:context="com.teamtreehouse.oslist.ClassActivity">
<TextView
android:id="#+id/activeClassActivities"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="top|center_horizontal"
android:text="Active Class Activities"
android:textSize="18dp"
android:textStyle="bold"
android:visibility="gone" />
<TextView
android:id="#+id/classNameActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size"
android:layout_below="#+id/editClassActivity"
android:layout_alignRight="#+id/deleteActivity"
android:layout_alignEnd="#+id/deleteActivity" />
<TextView
android:id="#+id/courseNumberActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/classNameActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/typeActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/courseNumberActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/nameActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/typeActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/dueDateActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/nameActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/descriptionActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/dueDateActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/maxGradeActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/descriptionActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<TextView
android:id="#+id/enterGradeActivity"
android:layout_width="#dimen/abc_action_bar_stacked_tab_max_width"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/classNameActivity"
android:layout_below="#id/maxGradeActivity"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:background="#ccc"
android:textColor="#000"
android:textSize="#dimen/text_size" />
<Button
android:id="#+id/editClassActivity"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_height="25dp"
android:background="#drawable/icon_document_edit"
android:onClick="editListener"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/deleteActivity" />
<Button
android:id="#+id/deleteActivity"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:textStyle="bold"
android:layout_height="wrap_content"
android:background="#drawable/icon_document_delete"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="deleteActivities"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Class Name :"
android:id="#+id/classNameDisplay"
android:textStyle="bold"
android:layout_alignTop="#+id/classNameActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/courseNumberActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Course ID :"
android:id="#+id/courseIDDisplay"
android:textStyle="bold"
android:layout_alignTop="#+id/courseNumberActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/courseNumberActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Course Type :"
android:id="#+id/courseTypeDisplay"
android:textStyle="bold"
android:layout_alignTop="#+id/typeActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/nameActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Activity Name :"
android:textStyle="bold"
android:id="#+id/activityNameDisplay"
android:layout_alignTop="#+id/nameActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/dueDateActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Due Date :"
android:textStyle="bold"
android:id="#+id/dueDateDisplay"
android:layout_alignTop="#+id/dueDateActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/descriptionActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Description :"
android:textStyle="bold"
android:id="#+id/descDisplay"
android:layout_alignTop="#+id/descriptionActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/maxGradeActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Maximum Grade :"
android:id="#+id/maxGradeDisplay"
android:textStyle="bold"
android:layout_alignTop="#+id/maxGradeActivity"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/maxGradeActivity" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Grade :"
android:id="#+id/gradeDisplay"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/enterGradeActivity" />
</RelativeLayout>
I add the childLayout.xml to masterLayout.xml using the following code
TableLayout queryTableLayout = (TableLayout) findViewById(R.id.queryTableLayoutActivity);
LayoutInflater inflater = (LayoutInflater) getSystemService(
Context.LAYOUT_INFLATER_SERVICE);
String courseIDValue = c.getString(courseIdIndex);
View newTagView = inflater.inflate(R.layout.activity_class_activities, null);
queryTableLayout.addView(newTagView, activeClassActivityIndex++);
I want to sort childLayout based on dueDateActivity field. How can I achieve this?
First of all you dont want to sort layouts - you want to sort views that you inflated with certain layout. So there is no "childLayout" there is newTagView.
Your question is a form of more general question: how to reorder n relative child views on a parent layout in certain order. This is not about changing their sizes and positions(that's easy, you just individually because you must know all new params for each) this is about reordering them. This problem breaks into 4:
Note: You can't just copy-paste code, you need to make changes based on what you specifically need (like what Parameter you need and how to compare it)
1. Establish the current order and the order in which they need to be :
use
getChildCount()
to know number of TableLayout children, use
getChildAt(i)
to traverse and access them.
Now make the Map that will determine the transition to the new order.
Map<View, Parameter> map = new HashMap<Integer, Parameter>();
for(int i = 0; i < queryTableLayout.getChildCount(); i++){
map.put(queryTableLayout.getChildAt(i),
queryTableLayout.getChildAt(i).findViewById(R.id.dueDateActivity)
.getParameter());
//since newTagView should inherit all RelativeLayout methods after infation
//getParameter() - the method from TextView(dueDateActivity) you need
Now to make the order right you need to sort the map by parameter,
which in Java 8 looks like
LinkedList<View> ordering = map.entrySet()
.stream()
.sorted(Map.Entry.comparingByValue(new Comparator<Parameter>(){
/*implement your comparator*/
})/*now when stream is sorted collect it to list*/
.collect(Collectors.toList(Map.Entry::getKey);
Now you have your ordering
2. Method to Swap
easiest way to swap views is to swap their layout params
like in this answer
RelativeLayout.LayoutParams params1 = (RelativeLayout.LayoutParams) b1.getLayoutParams();
RelativeLayout.LayoutParams params2 = (RelativeLayout.LayoutParams) b2.getLayoutParams();
b1.setLayoutParams(params2);
b2.setLayoutParams(params1);
Make a method void swap(View A, View B){...} which will work for your RelativeLayout View with that code.
3. Do the thing.
for(int i = 0; i < queryTableLayout.getChildCount(); i++)
swap(queryTableLayout.getChildAt(i), ordering.get(i));
If you want them add dynamically in already sorted place that is much easier You just need to get the right pos, and params like in (1)
and use
public void addView (View child, int index, ViewGroup.LayoutParams params)
According to bedbad's 4 steps I followed first 2 and found solution
I used this answer to help my situation.
This is my sort method (on button click)
public void sortByWeek(View v){
queryTableLayout = (TableLayout) findViewById(R.id.queryTableLayoutActivity);
int childCount = queryTableLayout.getChildCount();
Map<View,String> map = new HashMap<View,String>();
for(int i = 0; i<childCount;i++){
map.put(queryTableLayout.getChildAt(i),((TextView)queryTableLayout.getChildAt(i).findViewById(R.id.dueDateActivity)).getText().toString());
}
Map<View,String> sortedMap = sortByComparator(map,sort);
ArrayList<View> viewSet = new ArrayList<>(sortedMap.keySet());
queryTableLayout.removeAllViews();
for(int i = 0;i<childCount;i++){
queryTableLayout.addView(viewSet.get(i));
}
if(sort){
sort = false;
}
}
public Map<View,String> sortByComparator(Map<View,String> map,final boolean order){
List<Map.Entry<View,String>> list = new LinkedList<Map.Entry<View,String>>(map.entrySet());
Collections.sort(list, new Comparator<Map.Entry<View, String>>() {
#Override
public int compare(Map.Entry<View, String> lhs, Map.Entry<View, String> rhs) {
if(order){
return lhs.getValue().compareTo(rhs.getValue());
}else{
return rhs.getValue().compareTo(lhs.getValue());
}
}
});
Map<View,String> sortedMap = new LinkedHashMap<View,String>();
for(Map.Entry<View,String> entry: list){
sortedMap.put(entry.getKey(),entry.getValue());
}
return sortedMap;
}

Error in Android 2.x with the infowindow background

This is my problem in all info window I have are too big, and I like to set up at 180dpheight and 300dp width, or warp_content instead:
I realized that the problem only occurs in version 2.2 and Android 2.3, on Android 4.1 dont have problems, and I do not try on version 3.x
Any idea how to fix it?
I read other post that said changed the getInfoContent for getInfoWindow, but I already have that way.
InfoWindow.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#243439"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imagen"
android:layout_width="114dp"
android:layout_weight="0.5"
android:layout_height="174dp"
android:contentDescription="#string/app_name"/>
<LinearLayout
android:layout_width="174dp"
android:layout_height="174dp"
android:orientation="vertical" >
<TextView
android:id="#+id/titulo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:textColor="#FFFFFF"
android:gravity="center_horizontal|center"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="#+id/direccion"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:textColor="#FFFFFF"
android:gravity="center_horizontal|center"
android:textSize="12sp" />
<TextView
android:id="#+id/vermas"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:textColor="#FFFFFF"
android:gravity="bottom|center_horizontal"
android:text="#string/vermas"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
The fragment.java:
googlemap.setInfoWindowAdapter(new InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
View v = getLayoutInflater(null).inflate(R.layout.infowindow, null);
TextView titulo = (TextView) v.findViewById(R.id.titulo);
TextView direccion = (TextView) v.findViewById(R.id.direccion);
ImageView imagen = ((ImageView)v.findViewById(R.id.imagen));
titulo.setText(marker.getTitle());
direccion.setText(marker.getSnippet());
if(hash.get(marker) != null)
imagen.setImageDrawable(getResources().getDrawable(hash.get(marker)));
return v;
}
//..
#Override
public View getInfoContents(Marker marker) {
// TODO Auto-generated method stub
return null;
}
});
}
try below coxmk
<?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:background="#243439"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imagen"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_weight="0.5"
android:contentDescription="#string/app_name" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/titulo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center_horizontal|center"
android:textColor="#FFFFFF"
android:textSize="17sp"
android:textStyle="bold" />
<TextView
android:id="#+id/direccion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="14dp"
android:gravity="center_horizontal|center"
android:textColor="#FFFFFF"
android:textSize="12sp" />
<TextView
android:id="#+id/vermas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom|center_horizontal"
android:text="test"
android:textColor="#FFFFFF"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>

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

Change The Textview Size Runtime in listview

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

half or quarter black screen in android

I have an android activity that when I launch sometimes (about 1 in 4 times) it only draws quarter or half the screen before showing it. When I change the orientation or press a button the screen draws properly.
I'm just using TextViews, Buttons, Fonts - no drawing or anything like that.
All of my code for initialising is in the onCreate(). In this method I'm loading a text file, of about 40 lines long, and also getting a shared preference. Could this cause a delay so that it can't draw the intent?
Thanks in advance if anyone has seen anything similar.
EDIT - I tried commenting out the loading of the word list, but it didn't fix the problem.
EDIT 2 - I didn't manage to resolve the problem, but managed to improve it by adding a timer right at the end of the onCreate. I set a low refresh rate and in the tick changed the text of one of the controls. This then seemed to redraw the screen and get rid of the black portions of the screen.
Here is the activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/blue_abstract_background" >
<RelativeLayout
android:id="#+id/relativeScore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/ScoreLabel"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:padding="10dip"
android:text="SCORE:"
android:textSize="18dp" />
/>
<TextView
android:id="#+id/ScoreText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ScoreLabel"
android:padding="5dip"
android:text="0"
android:textSize="18dp" />
<TextView
android:id="#+id/GameTimerText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:minWidth="25dp"
android:text="0"
android:textSize="18dp" />
<TextView
android:id="#+id/GameTimerLabel"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/GameTimerText"
android:padding="10dip"
android:text="TIMER:"
android:textSize="18dp" />
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeHighScore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/relativeScore" >
<TextView
android:id="#+id/HighScoreLabel"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="10dip"
android:text="HIGH SCORE:"
android:textSize="16dp" />
<TextView
android:id="#+id/HighScoreText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/HighScoreLabel"
android:padding="10dip"
android:text="0"
android:textSize="16dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeHighScore"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearMissing"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_gravity="center"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/yellow_text" />
<TextView
android:id="#+id/MissingWordText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:text="MSSNG WRD"
android:textSize="22dp"
android:typeface="normal" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/yellow_text" />
<TableLayout
android:id="#+id/buttonsTableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="5dp" >
<Button
android:id="#+id/aVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:background="#drawable/blue_vowel_button"
android:text="#string/a"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
<Button
android:id="#+id/eVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:background="#drawable/blue_vowel_button"
android:text="#string/e"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="#+id/iVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:background="#drawable/blue_vowel_buttoni"
android:text="#string/i"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
<Button
android:id="#+id/oVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:background="#drawable/blue_vowel_button"
android:text="#string/o"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
<Button
android:id="#+id/uVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:background="#drawable/blue_vowel_button"
android:text="#string/u"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
</TableRow>
</TableLayout>
<TextView
android:id="#+id/TimeToStartText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="24dp" />
<Button
android:id="#+id/startButton"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:background="#drawable/blue_button_menu"
android:gravity="center"
android:padding="10dip"
android:text="START!"
android:textSize="28dp" />
</LinearLayout>
</RelativeLayout>
And the OnCreate() and a few methods:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
isNewWord = true;
m_gameScore = 0;
m_category = getCategoryFromExtras();
// loadWordList(m_category);
initialiseTextViewField();
loadHighScoreAndDifficulty();
setFonts();
setButtons();
setStartButton();
enableDisableButtons(false);
}
private void loadHighScoreAndDifficulty() {
//setting preferences
SharedPreferences prefs = this.getSharedPreferences(m_category, Context.MODE_PRIVATE);
int score = prefs.getInt(m_category, 0); //0 is the default value
m_highScore = score;
m_highScoreText.setText(String.valueOf(m_highScore));
prefs = this.getSharedPreferences(DIFFICULTY, Context.MODE_PRIVATE);
m_difficulty = prefs.getString(DIFFICULTY, NRML_DIFFICULTY);
}
private void initialiseTextViewField() {
m_startButton = (Button) findViewById(R.id.startButton);
m_missingWordText = (TextView) findViewById(R.id.MissingWordText);
m_gameScoreText = (TextView) findViewById(R.id.ScoreText);
m_gameScoreLabel = (TextView) findViewById(R.id.ScoreLabel);
m_gameTimerText = (TextView) findViewById(R.id.GameTimerText);
m_gameTimerLabel = (TextView) findViewById(R.id.GameTimerLabel);
m_timeToStartText = (TextView) findViewById(R.id.TimeToStartText);
m_highScoreLabel = (TextView) findViewById(R.id.HighScoreLabel);
m_highScoreText = (TextView) findViewById(R.id.HighScoreText);
}
private String getCategoryFromExtras() {
String category = "";
Bundle extras = getIntent().getExtras();
if (extras != null) {
category = extras.getString("category");
}
return category;
}
private void enableDisableButtons(boolean enable) {
Button button = (Button) findViewById(R.id.aVowelButton);
button.setEnabled(enable);
button = (Button) findViewById(R.id.eVowelButton);
button.setEnabled(enable);
button = (Button) findViewById(R.id.iVowelButton);
button.setEnabled(enable);
button = (Button) findViewById(R.id.oVowelButton);
button.setEnabled(enable);
button = (Button) findViewById(R.id.uVowelButton);
button.setEnabled(enable);
}
private void setStartButton() {
m_startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
m_gameScore = 0;
updateScore();
m_startButton.setVisibility(View.GONE);
m_timeToStartText.setVisibility(View.VISIBLE);
startPreTimer();
}
});
}

Categories

Resources