I have this chat application but because it only has one textview that being appended I don't know how to make it more presentable via designs I learnt the code from the internet and it is the easiest way I can do it can you help me guys?
Here is the code.
private String chat_msg,chat_user_name;
private void append_chat_conversation(DataSnapshot dataSnapshot) {
Iterator i = dataSnapshot.getChildren().iterator();
while (i.hasNext()){
chat_msg = (String) ((DataSnapshot)i.next()).getValue();
chat_user_name = (String) ((DataSnapshot)i.next()).getValue();
chat_conversation.setGravity(Gravity.BOTTOM);
if(chat_user_name.contains(pref.getString("username","").toString())){
chat_conversation.append(chat_user_name + " : " + chat_msg + " \n");
}else {
chat_conversation.append(chat_user_name + " : " + chat_msg + " \n");
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/btn_send"
android:src="#drawable/send"/>
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/msg_input"
android:layout_toLeftOf="#+id/btn_send"
android:layout_toStartOf="#+id/btn_send" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:textSize="15sp"
android:textColor="#000000"
android:scrollbars = "vertical"
android:id="#+id/textView"
android:layout_above="#+id/msg_input" />
</RelativeLayout>
Thank you sirs!!!
Try to add the TextView dynamically for each message.
To add the TextView dynamically, check this sample code
//Get the parent layout
RealtiveLayout relativeLayout = (RealtiveLayout)findViewById(R.id.parent_layout);
//New TextView
TextView textView = new TextView(this);
//Layout Params
textView1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
textView.setText("Your text");
//Add to the parent
relativelayout.addView(textView1);
Related
Thanks in advance and here is my problem. I have a database and i have to show its content whenever the "SHOW DATA" button is clicked (on the same activity (layout)). so when the user clicks again that button i want to remove the views in the layout so that the content of the database is not shown twice. I've have used removeAllView() and removeAllViewsInLayout() but it seems it's not working (for sure i'm not using correctly this method).
Here is the java code :
public void onClickShowData(){
LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer);
ll.removeAllViews();
showAll.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Cursor cursor = myDB.getAllData();
if (cursor.getCount() == 0) {
//Toast.makeText(MainActivity.this, "There is no data", Toast.LENGTH_SHORT).show();
showData("Error", "Nothing found");
return;
}
StringBuffer tmp = new StringBuffer();
LinearLayout ll = (LinearLayout)findViewById(R.id.cardViewContainer);
Context context = getApplicationContext();
LinearLayout ll2 = new LinearLayout(context);
ll2.setOrientation(LinearLayout.VERTICAL);
ll2.setLayoutParams(new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
TextView tv = new TextView(context);
CardView cv = new CardView(context);
cv.setId(R.id.cardView);
while (cursor.moveToNext()){
tmp.append("name : " + cursor.getString(0) + "\n" +
"calory : " + cursor.getString(1) + "\n" +
"protein : " + cursor.getString(2) + "\n" +
"lipid : " + cursor.getString(3) + "\n" +
"clucid: " + cursor.getString(4));
ll.addView(cv);
cv.addView(ll2);
tv.setText(tmp.toString());
ll2.addView(tv);
}
}
}
);
}
and this is the layout :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_database"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="gafood.mobiledeviceproject.arianchitgar.foodmakerwithga.databaseActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/scrollViewLL">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:id="#+id/Name"
android:hint="Food name"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:layout_below="#+id/Name"
android:layout_centerHorizontal="true"
android:id="#+id/Calory"
android:hint="Calory"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:layout_below="#+id/Calory"
android:layout_alignRight="#+id/Calory"
android:layout_alignEnd="#+id/Calory"
android:id="#+id/protein"
android:hint="Protein" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberDecimal"
android:ems="10"
android:layout_below="#+id/protein"
android:layout_centerHorizontal="true"
android:id="#+id/lipid"
android:hint="Lipid" />
<Button
android:text="Add food"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lipid"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="22dp"
android:id="#+id/addFood" />
<Button
android:text="Update"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/addFood"
android:layout_centerHorizontal="true"
android:id="#+id/updateFood" />
<Button
android:text="Delete"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/updateFood"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/delete" />
<Button
android:text="Food DB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/addFood"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/showAll" />
<Button
android:text="search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/updateFood"
android:layout_alignLeft="#+id/updateFood"
android:layout_alignStart="#+id/updateFood"
android:id="#+id/search" />
<Button
android:text="Advanced"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/delete"
android:layout_alignLeft="#+id/delete"
android:layout_alignStart="#+id/delete"
android:id="#+id/advancedSearch" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cardViewContainer"></LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
enter code here
When the app starts
When i click 3 times on show DB
You have to remove
LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer);
This line from inside onClickShowData() method and, add
LinearLayout ll;
To the top of your activity class.
then in onCreate(Bundle savedInstanceState) method add,
ll = (LinearLayout) findViewById(R.id.cardViewContainer);
In your current code, you are adding views to an ll and calling ll.removeAllViews() in a different object.
the final code should be something like,
public class YourActivityName extends Activity {
LinearLayout ll;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ll = (LinearLayout) findViewById(R.id.cardViewContainer);
// .... other code //
}
public void onClickShowData(){
ll.removeAllViews();
showAll.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Cursor cursor = myDB.getAllData();
if (cursor.getCount() == 0) {
//Toast.makeText(MainActivity.this, "There is no data", Toast.LENGTH_SHORT).show();
showData("Error", "Nothing found");
return;
}
StringBuffer tmp = new StringBuffer();
Context context = getApplicationContext();
LinearLayout ll2 = new LinearLayout(context);
ll2.setOrientation(LinearLayout.VERTICAL);
ll2.setLayoutParams(new ActionBar.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
TextView tv = new TextView(context);
CardView cv = new CardView(context);
cv.setId(R.id.cardView);
while (cursor.moveToNext()){
tmp.append("name : " + cursor.getString(0) + "\n" +
"calory : " + cursor.getString(1) + "\n" +
"protein : " + cursor.getString(2) + "\n" +
"lipid : " + cursor.getString(3) + "\n" +
"clucid: " + cursor.getString(4));
ll.addView(cv);
cv.addView(ll2);
tv.setText(tmp.toString());
ll2.addView(tv);
}
}
}
);
}
}
You are setting your onClick() listener with showAll.setOnClickListener but the code that gets executed does not include ll.removeAllViews(). Change your click listener to something like the following to remove views when the button is clicked.
showAll.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
LinearLayout ll = (LinearLayout) findViewById(R.id.cardViewContainer);
ll.removeAllViews();
...etc.
In my android application i am creating textview dynamically and each textview have onclick with respect to the type which have been send from webservice.
last text must be aligned to next line, below i have added my layout details
My adapter class
ArrayList<GroupTitleVo> titlelist = activitylist.get(position)
.getTitlelist();
LinearLayout sample_layout = new LinearLayout(_context);
sample_layout.setLayoutParams(new LinearLayout.LayoutParams(
0, 0));
sample_layout.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < titlelist.size(); i++) {
if (titlelist.get(i).getType().equals("user")) {
TextView user_text = new TextView(_context);
user_text.setId(i);
user_text.setTextColor(Color.parseColor("#000000"));
user_text.setTextSize(12);
user_text.setTypeface(null, Typeface.BOLD);
user_text.setText(" "+titlelist.get(i).getName());
user_text.setTag(titlelist.get(i).getId()+"~"+titlelist.get(i).getName());
user_text.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String id = (String) v.getTag();
Toast.makeText(_context, "user id" + id,
Toast.LENGTH_SHORT).show();
String[] name = id.split("~");
listener.userProfileredirect(name[0],name[1]);
}
});
holder.horizontaltext.addView(user_text);
} else if (titlelist.get(i).getType().equals("verb")) {
TextView verb_text = new TextView(_context);
verb_text.setId(i);
verb_text.setText(" "+titlelist.get(i).getName());
verb_text.setTextSize(10);
verb_text.setTag(titlelist.get(i).getId());
holder.horizontaltext.addView(verb_text);
} else {
TextView group_text = new TextView(_context);
group_text.setId(i);
group_text.setTextColor(Color.parseColor("#000000"));
group_text.setTextSize(14);
group_text.setTypeface(null, Typeface.BOLD);
group_text.setTag(titlelist.get(i).getId() + "~"
+ titlelist.get(i).getType());
group_text.setTextSize(12);
group_text.setText(" "+titlelist.get(i).getName());
group_text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String id = (String) v.getTag();
Toast.makeText(_context, "Group ID" + id,
Toast.LENGTH_SHORT).show();
String idtype[] = id.split("~");
if (idtype.length > 1) {
listener.userGroupRedirect(idtype[1], idtype[0]);
}
}
});
holder.horizontaltext.addView(group_text);
}
}
And XML parent layout
<LinearLayout
android:id="#+id/textlinearlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/posted_person_img_view_id"
android:orientation="horizontal" >
</LinearLayout>
Kindly help me to align this textviews
You need to make nested layouts.
LinearLayout
You have to set horizontally LinearLayout, so you have all items in one horizontal line. You need to put into your existing LinearLayout your ImageView and create into it another LinearLayout set vertically to have two TextView items one below another.
Your view should look like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_gallery"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="First text"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Second text text"/>
</LinearLayout>
</LinearLayout>
In existing code create another LinearLayout, set orientation of it as vertical and copy into it your two TextViews.
RelativeLayout
You need to declare a position of every item, just like in this example
<?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"
android:orientation="horizontal">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_gallery"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"/>
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="First text"
android:layout_above="#+id/textView2"
android:layout_toRightOf="#+id/image"/>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Second text text"
android:layout_below="#+id/textView1"
android:layout_toRightOf="#+id/image"/>
</RelativeLayout>
I used here XML files, because I think it might be more clear to see what attributes and views you would need to use.
EDIT: To create RelativeLayout programmaticaly read:
How to lay out Views in RelativeLayout programmatically?
Hope it help.
i have implemented the onitemCLickListener() on my listview but it is not working. The thing is that while updating my listview through Custom Adapter i have added a view to it through my code.
i have tried Focusable and focusableintouch mode to false also and also tried to block the descendents, but i am not getting any solution. Please find below my code.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/top_greenbox"
android:orientation="horizontal" >
<ImageView
android:id="#+id/backArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="10dp"
android:layout_centerVertical="true"
android:src="#drawable/topleft_arrow" />
<TextView
android:id="#+id/showdate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Today is, 07 March 2014"
android:textColor="#android:color/white" />
<ImageView
android:id="#+id/nextArrow"
android:padding="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_gravity="right"
android:src="#drawable/topright_arrow" />
</RelativeLayout>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:descendantFocusability="blocksDescendants"
android:smoothScrollbar="true" />
</LinearLayout>
in my Custom Adapter i am adding view int his manner:
LinearLayout list = (LinearLayout) view.findViewById(R.id.show_time);
View line = list.inflate(mContext, R.layout.show_time_item, null);
list.addView(line);
My Layout show_time_item is:
<?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:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/separator" />
<TextView
android:id="#+id/time4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#55b237"
android:gravity="center"
android:padding="10dp"
android:text="9:45 AM"
android:textColor="#android:color/white"
android:textSize="14sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/separator" />
</LinearLayout>
Listener
my_list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.e("hello","hi");
Intent i = new Intent(ABCDListActivity.this,
ShowABCDProfile.class);
i.putExtra("Doc_name", name_main);
startActivity(i);
}
});
Custom Adapter getView():
View view = null;
view = inflator.inflate(R.layout.doctor_list_item, null);
ViewHolder holder = new ViewHolder();
holder.doc_image = (ImageView) view.findViewById(R.id.doc_img);
holder.doc_name = (TextView) view.findViewById(R.id.doc_name);
holder.doc_qualification = (TextView) view
.findViewById(R.id.doc_qualification);
holder.doc_speciality = (TextView) view.findViewById(R.id.Specialities);
holder.doc_experience = (TextView) view.findViewById(R.id.experience);
holder.doc_experience.setText(list.get(position)
.get(ResponseConst.TAG_STATUS_DOCTOR_EXP).toString());
holder.doc_name.setText((list.get(position).get(
ResponseConst.TAG_STATUS_DOCTOR_NAME).toString()));
JSONArray hours = avail_list.get(position);
Log.e("hours are", "size is " + hours.length());
LinearLayout list = (LinearLayout) view.findViewById(R.id.show_time);
for (byte a = 0; a < hours.length(); a++) {
try {
JSONObject object_avail = hours.getJSONObject(a);
String doctor_id = object_avail
.getString(ResponseConst.TAG_STATUS_DOCTOR_ID);
String hour = object_avail
.getString(ResponseConst.TAG_STATUS_DOCTOR_AVAILABILITY_HOUR);
String min = object_avail
.getString(ResponseConst.TAG_STATUS_DOCTOR_AVAILABILITY_MIN);
String ampm = object_avail
.getString(ResponseConst.TAG_STATUS_DOCTOR_AVAILABILITY_AMPM);
String status = object_avail
.getString(ResponseConst.TAG_STATUS_DOCTOR_AVAILABILITY_STATUS);
String time = hour + ":" + min + " " + ampm;
View line = list.inflate(mContext, R.layout.show_time_item, null);
holder.time = (TextView) line.findViewById(R.id.time4);
if(status.equals(0))
holder.time.setTextColor(Color.GRAY);
else holder.time.setTextColor(Color.WHITE);
holder.time.setText(time);
list.addView(line);
} catch (Exception e) {
e.printStackTrace();
}
}
String drawablename = "no_image.png";
int resID = mContext.getResources().getIdentifier(drawablename,
"drawable", mContext.getPackageName());
holder.doc_image.setBackgroundResource(resID);
return view;
Stuck since so long. Please help
Your ImageView takes focus when you click on List Item
Add this to LinearLayout in show_time_item.xml.
android:descendantFocusability="blocksDescendants"
I simply added the click listener in my getView() of Custom Adapter. I still don't know why the listener was not working in my activity.
You can always try using the onclick function.
XML
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp"
android:descendantFocusability="blocksDescendants"
android:smoothScrollbar="true"
android:onClick="Start" />
And in the activity you will just write a function Start.
Inside a setOnItemClickListener I use an intent to start a new activity. I put 7 strings as extras for this intent. When I click an item (of a ListView) I want all these extras to be displayed (on a new screen). But I see only the first 4 extras. Here's the layout file for the new activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/address_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/city_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
<TextView
android:id="#+id/zip_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
<TextView
android:id="#+id/state_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
</LinearLayout>
<TextView
android:id="#+id/name_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
<TextView
android:id="#+id/number_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
<TextView
android:id="#+id/store_id_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip"/>
</LinearLayout>
Then here's the listener code:
lv_custom.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
//Log.v("ITEM",lv_custom.getItemAtPosition(position).toString());
c.moveToPosition(position);
String adr = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_ADDRESS));
String city = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_CITY));
String name = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_NAME));
String store_id = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_STORE_ID));
String phone = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_PHONE));
String zip = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_ZIP));
String state = c.getString(c.getColumnIndex(MySQLiteHelper.COLUMN_STATE));
Intent intent=new Intent(DisplayActivity.this,DisplayInfoActivity.class);
intent.putExtra("Address: ",adr);
intent.putExtra("City: ", city);
intent.putExtra("Zip: ", " " + zip + ", ");
intent.putExtra("State: ", state);
intent.putExtra("Name: ", name);
intent.putExtra("Store ID: ", "Store ID " + store_id);
intent.putExtra("Phone: ", phone);
startActivity(intent);
}
});
Here's the new activity code:
public class DisplayInfoActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.list_entry);
TextView address = (TextView) findViewById(R.id.address_entry);
TextView city = (TextView) findViewById(R.id.city_entry);
TextView zip = (TextView) findViewById(R.id.zip_entry);
TextView state = (TextView) findViewById(R.id.state_entry);
TextView name = (TextView) findViewById(R.id.name_entry);
TextView store_id = (TextView) findViewById(R.id.store_id_entry);
TextView phone = (TextView) findViewById(R.id.number_entry);
Intent intent = getIntent();
address.setText(intent.getStringExtra("Address: "));
city.setText(intent.getStringExtra("City: "));
zip.setText(intent.getStringExtra("Zip: "));
state.setText(intent.getStringExtra("State: "));
name.setText(intent.getStringExtra("Name: "));
store_id.setText(intent.getStringExtra("Store ID: "));
phone.setText(intent.getStringExtra("Phone: "));
}
}
The problem is that it shows only the address, the city, the zip code, and the state. It does not show the name, store id and the phone number. What happens with them? Why they are not shown/seen on the new screen? Actually, before I added more extras, I could see all the extras in my intent. Is there a problem with the number of extras? Thanks for help, if you have an answer for me!
You can simplify adding extras - no need to use extra characters as key:
intent.putExtra("Address",adr);
intent.putExtra("City", city);
intent.putExtra("Zip", " " + zip + ", ");
intent.putExtra("State", state);
intent.putExtra("Name", name);
intent.putExtra("StoreID", "Store ID " + store_id);
intent.putExtra("Phone", phone);
and correspondingly, in your DisplayInfoActivity:
address.setText(intent.getStringExtra("Address"));
city.setText(intent.getStringExtra("City"));
zip.setText(intent.getStringExtra("Zip"));
state.setText(intent.getStringExtra("State"));
name.setText(intent.getStringExtra("Name"));
store_id.setText(intent.getStringExtra("StoreID"));
phone.setText(intent.getStringExtra("Phone"));
Next, have you tried printing the extras to logcat, in case your layout is incorrect? Add this below the lines where you retrieve extras:
Log.d("MYTAG", "Name: " + intent.getStringExtra("Name"));
and see if you can see it in logcat. If yes, maybe your layout is not showing all those TextViews.
Btw, try moving the last 3 TextViews (name, store_id and number) in your layout file INSIDE the second LinearLayout (together with all the other textviews), just to see if they are displayed.
Your inner horizontal LinearLayout's height is set to match_parent:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
Thus, it stretches to the bottom of the screen and leaves no place for your other three TextViews. Therefore it should be set to wrap_content:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
Your layout is wrong
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/address_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
...
You should have
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/address_entry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="28dip" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
...
Because your second LinearLayout is taking all the remaining screen
I am working on an Android mapping app that allows a user to type a search parameter into an EditText box, and then the closest destinations matching the query are plotted with pins on the MapView.
Clicking on a pin will give info about the location, but I would like to provide more information to the user up front. I thought a SlidingDrawer could populate and open up to show the user the names of the destinations/the distances away.
Having spent a lot of time on this, having done much reading, and having made little progress, I am now asking the community for assistance.
The problem I'm having is that I can't figure out how to populate the contents of the SlidingDrawer within the main activity. I am able to launch a new activity, but that takes my MapView off the screen. I need to be able to see the map as well as the SlidingDrawer. And I can't figure out how to populate the SlidingDrawer without it being set up as an Activity.
(And I should say also that it doesn't necessarily need to be a SlidingDrawer that contains the list of destinations. I could modify my layout to make a small (max 3 items) list and take away some of the map's real estate if that is easier, but I'm not sure how to do that, either.)
Here is the code that calls my SlidingDrawer:
//call SlidingDrawer
Intent drawerIntent = new Intent(this, SlidingDrawerFinal.class);
drawerIntent.putExtra("lat", lat);
drawerIntent.putExtra("lon", lon);
int numberOfTargetsForList = numberOfLoops;
drawerIntent.putExtra("numberOfTargetsForList", numberOfTargetsForList);
for(int i = 0; i < target.length; i++){
drawerIntent.putExtra("target" + Integer.toString(i), target[i]);
}
this.startActivity(drawerIntent);
This is the code in my SlidingDrawer class (extends Activity) that fills the list inside the drawer:
View inflatedDrawerLayout = getLayoutInflater().inflate(R.layout.drawer_main, null);
int width = getWindow().getAttributes().width;
int height = 300;
LayoutParams params = new LayoutParams(width, height);
getWindow().addContentView(inflatedDrawerLayout, params);
// add some data
ArrayList<MyData> myDataList = new ArrayList<MyData>();
myData = new MyData[numberOfTargets];
for(int i = 0; i < numberOfTargets; i++){
String lineTwo = "";
if(target[i].getRating().equals("Not Yet Rated")){
lineTwo = " " + target[i].getRating() + " " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
else{
lineTwo = " rating: " + target[i].getRating() + "/5 stars " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
String lineOne = (Integer.toString(i + 1) + ": " + target[i].getName() + ", " + target[i].getVicinity()) + "\n" + lineTwo;
myData[i] = new MyData(lineOne, i);
myDataList.add(myData[i]);
}
mListView = (ListView) findViewById(R.id.listview_);
mListView.setAdapter(new MyListAdapter(this, R.layout.drawer_row, myDataList));
drawer = (SlidingDrawer) findViewById(R.id.drawer);
handleButton = (Button) findViewById(R.id.handle);
drawer.animateOpen();
drawer.setOnDrawerOpenListener(new OnDrawerOpenListener() {
public void onDrawerOpened() {
handleButton.setBackgroundResource(R.drawable.handle_down_white);
}
});
My layout files:
main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip">
<EditText
android:id="#+id/geocode_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/placeName"
android:inputType="text"
android:lines="1" />
<Button
android:id="#+id/geocode_button"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/goLabel" />
</LinearLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<view android:id="#+id/mv"
class="com.google.android.maps.MapView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:clickable="true"
android:apiKey="my_API_Key"/>
</LinearLayout>
<include layout="#layout/drawer_main"/>
</FrameLayout>
</LinearLayout>
and, finally, drawer_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<SlidingDrawer xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/drawer"
android:handle="#+id/handle"
android:content="#+id/content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:background="#null"
android:layout_gravity="bottom">
<LinearLayout
android:id="#id/content"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="14dip"
android:textStyle="bold|italic"
android:background="#android:color/white"
android:textColor="#android:color/black"
android:text="#string/top_search_results" >
</TextView>
<View
android:background="#android:drawable/divider_horizontal_bright"
android:layout_width="fill_parent"
android:layout_height="2dp" >
</View>
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/listview_"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:divider="#android:color/transparent"
android:dividerHeight="2dp" >
</ListView>
</LinearLayout>
<Button
android:id="#id/handle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/handle_down_white">
</Button>
</SlidingDrawer>
</FrameLayout>
Sorry that this is so lengthy. Any assistance will be greatly appreciated.
You can use Popup Window.
View popupView = layoutInflater.inflate(R.layout.popup_window, null);
final PopupWindow popupWindow = new PopupWindow(popupView,
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
ImageButton btnCancel = (ImageButton) popupView
.findViewById(R.id.btnCancel);
popupWindow.showAtLocation(LayoutView, Gravity.BOTTOM
| Gravity.CENTER_HORIZONTAL, (LayoutView.getWidth() - popupView
.getWidth()) / 2, LayoutView.getHeight()
- popupWindow.getHeight() - 10);
popupWindow.setAnimationStyle(Animation.RELATIVE_TO_SELF);
popupWindow.setFocusable(true);
popupWindow.update();
The solution I opted for is programmatically a bit less than ideal, but the layout works well. Since I knew I wouldn't have more than 3 results, I just changed my layout to include (up to) 3 small TextViews under my search bar and (sadly) got rid of the SlidingDrawer altogether. When the results come back, I populate as many TextViews as necessary, each with an onClickListener. It won't scale well, and everything is hard-coded; but it works. I may try to improve upon this solution as I learn more about Android.
new Layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dip">
<EditText
android:id="#+id/geocode_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/placeName"
android:inputType="text"
android:lines="1" />
<Button
android:id="#+id/geocode_button"
android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/goLabel" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="13dip"
android:textStyle="bold|italic"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:id="#+id/search_results_header" >
</TextView>
<View android:id="#+id/bar_over_one"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DADADA" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11dip"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:id="#+id/line_one" >
</TextView>
<View android:id="#+id/bar_over_two"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DADADA" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11dip"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:id="#+id/line_two" >
</TextView>
<View android:id="#+id/bar_over_three"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#DADADA" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="11dip"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:id="#+id/line_three" >
</TextView>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<view android:id="#+id/mv"
class="com.google.android.maps.MapView"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:clickable="true"
android:apiKey="0fmGmyFrFDUrPGrwXR_scZZxVx6CkSdK-sys-Qw"/>
</LinearLayout>
</LinearLayout>
new code:
private void populateList(int numberOfTargetsForList){
TextView header = (TextView) findViewById(R.id.search_results_header);
header.setText("Top Search Results");
int i = 0;
//first line in list
barOverOne.setVisibility(View.VISIBLE);
String ratingAndDistance = "";
if(target[i].getRating().equals("Not Yet Rated")){
ratingAndDistance = " " + target[i].getRating() + " " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
else{
ratingAndDistance = " rating: " + target[i].getRating() + "/5 stars " +
Double.toString((Math.round(target[i].getDistance()*100.0))/100.0) + " miles";
}
String detailsForLineOne = (Integer.toString(i + 1) + ": " + target[i].getName() + ", " + target[i].getVicinity()) + "\n" + ratingAndDistance;
TextView lineOne = (TextView) findViewById(R.id.line_one);
lineOne.setText(detailsForLineOne);
lineOne.setOnClickListener(this);
i++;
...(similar blocks handle cases of i = 1 and i =2)
Finally, in my onClick() method:
case R.id.line_one:{
String locForURL = "origin=" + Double.toString(lat/1000000.0) +
"," + Double.toString(lon/1000000.0) + "&destination=" +
Double.toString(target[0].getLat()/1000000.0) + "," +
Double.toString(target[0].getLon()/1000000.0);
Intent intent = new Intent(this, ParsingXMLDirectionsWithListView.class);
intent.putExtra("dirTitle", target[0].getName()+ " (" +
Double.toString((Math.round(target[0].getDistance()*100.0))/100.0) + " miles)");
intent.putExtra("locForURL", locForURL);
this.startActivity(intent);
break;
}//line_one
...(similar blocks for lines two and three)
Not perfect, but oh well. At least looks pretty good from the user's end, and I was never going to meet my deadline by continuing down the SlidingDrawer route. Maybe when I have some more free time I'll see if I can figure that out....