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);
}
Related
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
In my project, im going to make a multiple choice type question. I can select the questions and answer from mysql, however, i cant do the "matching" of the answer between input answer and the data from mysql. I tried some solutions but they doesnt work as well. I need a big help on it. Below are my codes.
the java class that make multiple choice
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.normalmode);
new DataAsyncTask().execute();
}
public void onClick(View v){
inputtext = ((Button) v).getText().toString();
tv3 = (TextView)findViewById(R.id.textView3);
tv3.setText(inputtext);
if(inputtext == tv2.getText().toString()){
playerscore +=5;
} else {
playerscore +=1;
}
score.setText("Scores : " + playerscore);
new DataAsyncTask().execute();
}
class DataAsyncTask extends AsyncTask<String, String, String>{
#Override
protected String doInBackground(String... param) {
String result = DBConnector.executeQuery("SELECT * FROM questions ORDER BY RAND( ) LIMIT 1");
return result;
}
#Override
protected void onPostExecute(String result) {
question = (TextView)findViewById(R.id.textView_question);
fchoice = (Button)findViewById(R.id.Btn_choice1);
schoice = (Button)findViewById(R.id.Btn_choice2);
tchoice = (Button)findViewById(R.id.Btn_choice3);
tv2 = (TextView)findViewById(R.id.textView2);
score = (TextView)findViewById(R.id.textView_score);
try {
JSONArray jsonArray = new JSONArray(result);
JSONObject jsonData = jsonArray.getJSONObject(0);
question.setText(jsonData.getString("q_desc"));
fchoice.setText(jsonData.getString("fchoice"));
schoice.setText(jsonData.getString("schoice"));
tchoice.setText(jsonData.getString("tchoice"));
ans = jsonData.getString("q_ans");
tv2.setText(ans);
} catch(Exception e) {
Log.e("log_tag", e.toString());
}
}
}
}
three buttons are sharing same onClick in xml file by using
android:onClick="onClick"
the current problem i faced is that it always return "false" no matter i pressed which button. also, is there any way to store/pass "previous" async task data?
I have tried using internal storage but it doesnt work as well
EDIT:
my xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.95" >
<TextView
android:id="#+id/textView_score"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/player_score" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.95" >
<TextView
android:id="#+id/textView_question"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/string_question"
android:textAppearance="?android:attr/textAppearanceMedium" />
<Button
android:id="#+id/Btn_choice3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/third_choice"
android:onClick="onClick" />
<Button
android:id="#+id/Btn_choice2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Btn_submit"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/second_choice"
android:onClick="onClick" />
<Button
android:id="#+id/Btn_choice1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/Btn_choice2"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="#string/first_choice"
android:onClick="onClick" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView1"
android:text="TextView" />
</RelativeLayout>
</LinearLayout
textview2 and textview3 are used to test my output and display them as text
try this if(inputtext.equals(tv2.getText().toString())) instead of if(inputtext == tv2.getText().toString())
becasue the == operator checks to see if the two strings are exactly the same object.
The .equals() method will check if the two strings have the same value.
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 want to put a context menu on a listview. This seems very simple to do but I am getting some interesting behavior. The listview has two fields, a textview and an editText. The long press opens up the menu on the EditText but nowhere else on the listview.
Here are the pieces of code.
public class ManageClass extends ListActivity {
super.onCreate(savedInstanceState);
setContentView(R.layout.manageclass);
//Tons of not relevant stuff that I would be happy to provide.
pfdata = new PortfolioData(this);
try {
Cursor cursor = getClasses();
showClasses(cursor);
} finally {
pfdata.close();
}
}
private Cursor getClasses() {
String sql = "select c._id, c.NAME as NAME , c.percentage as PERCENTAGE "
+ "from asset_classes c;";
SQLiteDatabase db = pfdata.getReadableDatabase();
Cursor cursor = db.rawQuery(sql, null);
startManagingCursor(cursor);
return cursor;
}
private void showClasses(Cursor cursor) {
adapter = new MySimpleCursorAdapter(this, R.layout.classrow, cursor,
FROM, TO, t);
// Log.d("TEST", Integer.toString(adapter.getCount()));
setListAdapter(adapter);
adapter.notifyDataSetChanged();
//I've tried moving this line around but it hasn't made a difference.
registerForContextMenu(getListView());
}
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
menu.add(Menu.NONE, 0, 0, "A");
menu.add(Menu.NONE, 1, 1, "B");
menu.add(Menu.NONE, 2, 2, "C");
}
Lastly, the layout itself which I admit is overly complicated
<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:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/editText1"
android:layout_width="300px"
android:layout_height="wrap_content"
android:text="#string/TCLabel" />
<TextView
android:id="#+id/classtotalpercentage"
android:layout_width="109dp"
android:layout_height="wrap_content"
android:textSize="50px" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/AddClassLabel"
android:layout_width="200px"
android:layout_height="wrap_content"
android:layout_weight="0.38"
android:text="#string/AddClassLabel" />
<Button
android:id="#+id/addclass_button"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:text="#string/addclassbutton_label" />
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/grey"
android:orientation="horizontal"
android:padding="10sp" >
<TextView
android:id="#+id/assetclassHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/assetclassHeader"
android:width="200dp" />
<TextView
android:id="#+id/percentageHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/assetclassHeader"
android:text="#string/percentageHeader"
android:width="100dp" />
</LinearLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:fadeScrollbars="false"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical" />
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_list_data" />
</RelativeLayout>
</LinearLayout>
I appreciate any advice. I would like the menu on the textview and not the edittext, if possible.
An update. I have found that it has something to do with the EditText. If I change it to a textview or remove it alltogether, then everything works as it should.
I had a similiar problem with checkboxes and found the issue was caused by the checkbox getting the focus. I changed the attribue to:
android:focusable="false"
and it now works correctly.
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....