Start activity using intent with extra does not show all the extras - android

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

Related

Custom Layout of Listitem not Updating after notifyDataSetChanged

In my application I have two activity, one which contain Recycleview and another one which is responsible for updating database value,in OnBind method of adpater I am updating thoes values(font size and color).I was able to getting change in fontsize and color according to update but Layout which contain thoes item not changing it's hight.
I am posting link of output thoes are in sequence , first one is without edit(https://ibb.co/931P0Q4), Second is with edit(https://ibb.co/KzY7VCL), Theird is after restart app(https://ibb.co/C7cqf7Z)
Code of my custom item:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/mainLainerHOlder"
android:layout_height="wrap_content"
tools:context=".MainActivity"
android:orientation="vertical">
<com.chauthai.swipereveallayout.SwipeRevealLayout
android:id="#+id/swipe_layout_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
app:mode="normal"
app:dragEdge="right">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="90dp"
android:layout_height="match_parent"
android:gravity="center"
android:id="#+id/cEditText"
android:background="#color/initColor"
android:textColor="#android:color/white"
android:text="Edit"
/>
<TextView
android:layout_width="90dp"
android:layout_height="match_parent"
android:gravity="center"
android:id="#+id/cDeleteText"
android:background="#android:color/holo_red_dark"
android:textColor="#android:color/white"
android:text="Delete"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/MianLinearLayoutContainer"
android:background="#drawable/border_solid_white">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginBottom="5dp"
android:layout_marginLeft="20dp"
android:layout_marginRight="5dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name will here"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:id="#+id/nametxtl"
android:textSize="20dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Number here"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="center"
android:id="#+id/nutxtl"
android:textSize="20dp"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</com.chauthai.swipereveallayout.SwipeRevealLayout>
</LinearLayout>
Code inside onResume by that I was able to get updated changes:
#Override
protected void onResume() {
super.onResume();
Log.e("onResume","yes");
TDataRead();
customRecycleAdapter.notifyDataSetChanged();
}
here TdataRead is just method by which I m geting updating data for notify.
code of TdataRead:
public void TDataRead() {
customDataListModelList.clear();
Cursor cursor = tempSqliteDatabaseHelper.getAllData();
if (cursor.getCount() == 0) {
Log.e("No_temp", "No temp Contact Selected");
return;
}
StringBuffer stringBuffer = new StringBuffer();
while (cursor.moveToNext()) {
customDataListModelList.add(new CustomDataListModel((cursor.getString(0)), cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getInt(4), cursor.getInt(5)));
stringBuffer.append("Id :" + cursor.getString(0) + "\n");
stringBuffer.append("Name :" + cursor.getString(1) + "\n");
stringBuffer.append("Number :" + cursor.getString(2) + "\n");
stringBuffer.append("color :" + cursor.getString(3) + "\n");
stringBuffer.append("Font Size :" + cursor.getInt(4) + "\n");
stringBuffer.append("Index:" + cursor.getInt(5) + "\n");
Log.e("CustomDataRecord", String.valueOf(customDataListModelList.get(i).Name));
}
}
I thing solution is reside with calling onBind method again or somerhing by list reload itself. and reload means calling contrustor of adapter again.I don't think thats proper way. if u have any other suggestion help me.
I solved this problem by calling xview.setAdapter(xAdapter) in onResume method means that for updated layout changes RecyclerView need updated instance of Adapter.
#Override
protected void onResume() {
super.onResume();
TDataRead();
customRecycleAdapter.notifyDataSetChanged();
recyleview.setAdapter(customRecycleAdapter);
}

How to design my chat app

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

More than 3 items per sub items GridView Android

I want to display the data from content provider in gridview more than 3 sub items or line
here is my java source code
#Override
public void onLoadFinished(Loader<Cursor> arg0, Cursor cursor) {
players.clear();
// First Check if cursor is not null
if(cursor != null ) {
StringBuilder result = new StringBuilder();
// Now loop to all items and append it to string builder
while (cursor.moveToNext()) {
String id = cursor.getString(cursor.getColumnIndex("ID"));
String name = cursor.getString(cursor.getColumnIndex("ITEM_NAME"));
String qty = cursor.getString(cursor.getColumnIndex("QUANTITY"));
String inst = cursor.getString(cursor.getColumnIndex("INSTRUCTION"));
String pid = cursor.getString(cursor.getColumnIndex("POS_ID"));
players.add("Items :" + name + "\n" + "Qty :" + qty + "\n" +
"Details :" + inst + "\n" + " Pos ID :" + pid);
gv.setAdapter(adapter);
resultView.setText(result);
}
} else {
// If cursor is null then display toast amd set empty data.
resultView.setText("Empty data");
Toast.makeText(MainActivity.this, "May be there is no app corresponding to your provider or there is null data.",
Toast.LENGTH_LONG).show();
}
}
the gv.setAdapter(adapter) is where i display the data coming from the content provider
and here is my xml source code
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:fillViewport="true"
tools:context="com.condorpos.kitchen_bumpbar.MainActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp">
<Button
android:id="#+id/btnRetrieve"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:padding="5dp"
android:text="#string/show_data" />
<TextView
android:id="#+id/result"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:clickable="false"
android:padding="5dp"
android:textColor="#000000"
android:textSize="15sp" />
<GridView
android:layout_width="match_parent"
android:layout_height="420dp"
android:numColumns="5"
android:verticalSpacing="140dp"
android:id="#+id/gridView1"/>
<android.support.v7.widget.RecyclerView
android:id="#+id/expListView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:visibility="invisible" />
<TextView
android:id="#+id/empty_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:visibility="gone"
android:text="#string/empty_list"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"/>
</LinearLayout>
</LinearLayout>
the gridView1 is equals to gv on the java source code i declare it like
Griview gv;
gv=(GridView) findViewById(R.id.gridView1);
from the image below it shows that the data is not fully display looked per column i want to display more than 3 lines but it gives me this result
Any idea or help will do
Thanks a lot guys
You need make height of GridView is match_parent/wrap_content (based on your layout), don't fix size 420dp

Adapter dynamically created textview is not aligning properly

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.

Android: Dynamically Populate a SlidingDrawer (or a small list) within the Main Activity

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....

Categories

Resources