Delayed ContextMenu on ListView - android-listview

I have an Activity with a ListView. You long-press each row to get a ContextMenu. At the bottom of the ListView is an EditText box. It looks like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"
android:layout_marginTop="3dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/TVReviewItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginLeft="10dp"
android:layout_weight="90"
android:text="Item Name"
android:textSize="20sp" />
<Spinner
android:id="#+id/spinnerSort"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="5dp"
android:gravity="left" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#bbb" >
</LinearLayout>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:longClickable="true"
android:scrollbars="none" >
</ListView>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#bbb" >
</LinearLayout>
<EditText
android:id="#+id/etTweetReview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:ems="10"
android:hint="Write a Review"
android:imeOptions="actionDone"
android:inputType="textCapSentences|textMultiLine"
android:maxLength="140"
android:maxLines="4"
android:minLines="1"
android:paddingBottom="9dp"
android:paddingTop="9dp"
android:singleLine="false" >
<requestFocus />
</EditText>
The problem: The ContextMenu will not show up until you physically press the cursor inside the EditText box (even though I am already requesting focus) and then LongPress the List row. If you long-press the list row FIRST, nothing happens. Here is the OnCreateContextMenu:
#Override
public void onCreateContextMenu(ContextMenu menu, View v,
ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
android.view.MenuInflater inflater = getActivity()
.getMenuInflater();
inflater.inflate(R.menu.reviews_context, menu);
menu.setHeaderTitle("Available Actions");
android.view.MenuItem Edit = menu.findItem(R.id.edit);
android.view.MenuItem Delete = menu.findItem(R.id.delete);
AdapterContextMenuInfo info = (AdapterContextMenuInfo) menuInfo;
int position = info.position;
ListView lv = (ListView) v;
int firstVisible = lv.getFirstVisiblePosition();
View rowView = lv.getChildAt(position - firstVisible);
ReviewUser = ((TextView) rowView.findViewById(R.id.labelUser))
.getText().toString();
ReviewWords = ((TextView) rowView.findViewById(R.id.labelComment))
.getText().toString();
if (Rateit.isUserLoggedIn == true) {
if (Rateit.username.equals(ReviewUser) {
Edit.setVisible(true);
Delete.setVisible(true);
} else {
Edit.setVisible(false);
Delete.setVisible(false);
}
} else {
Edit.setVisible(false);
Delete.setVisible(false);
}
}
I've tried this with the Super above and below the other content. Can anyone see what's wrong?
UPDATE: New info. I found if the ListView scrolls, you have to scroll to the bottom THEN it will allow you to access ContextMenu if you try. In this case, it has nothing to do with EditText box.

I figured it out. In my row layout file (not shown), I have three buttons. I had to set each to:
android:focusable="false"
Simple fix apparently.

Related

list view on relative layout didn't show

activity_sms.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ListView
android:id="#+id/SMSList"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_margin="10dp"
/>
</RelativeLayout>
code on SMSActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sms);
smsListView = (ListView) findViewById(R.id.SMSList);
arrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, smsMessagesList);
smsListView.setAdapter(arrayAdapter);
smsListView.setOnItemClickListener(this);
code on fragment_1.java
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_sms, container, false);
return rootView;
I tried to make a program to read SMS from inbox and show it on a ListView. It works when SMSActivity is the only activity there
Now, I use MainActivity.java instead of SMSActivity.java as Main Activity.
My goal is to invoke SMSActivity.java using fragment.
If anyone can help me it would be nice :)
What exactly you want to show in your layout?For example you have some items like this
<LinearLayout
android:layout_weight="1"
android:orientation="vertical"
android:paddingLeft="30dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/list_item_title"
android:text="#string/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/list_item_details"
android:text="#string/description"
android:paddingTop="10dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
In ths case you can set the value for this items in your adapter
#Override
public View getView (int position,View convertView,ViewGroup parent){
View view = LayoutInflater.from(context).inflate(R.layout.linear_big_list_item, parent, false);
TextView nameOfItem = (TextView)convertView.findViewById(R.id.list_item_title);
TextView descriptionOfItem = (TextView)convertView.findViewById(R.id.list_item_details);
return view;
}

A part of an activity displaying listview in android

I want to display the list view in half of the screen and the other half contains some static content. When i tried to put the listview, the whole screen is repeated in the loop. I want only that particular part the screen in listview. Can anyone help?
This is my MainActivity.java
List<HashMap<String,String>> aList = new ArrayList<HashMap<String,String>>();
for(int i=0;i<6;i++){
HashMap<String, String> hm = new HashMap<String,String>();
hm.put("text", menu[i]);
hm.put("icon", Integer.toString(image[i]) );
aList.add(hm);
}
String[] from = { "icon","text" };
int[] to = { R.id.icon,R.id.text};
SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList, R.layout.activity_test_main, from, to);
ListView listView = ( ListView ) findViewById(R.id.list);
listView.setAdapter(adapter);
This my activity_test_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/new_background"
>
<ImageView
android:layout_width="100dip"
android:layout_height="100dip"
android:layout_marginTop="30dip"
android:layout_gravity="center_horizontal"
android:src="#drawable/katrina" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Katrina"
android:textColor="#FFFFFF"
android:textStyle="italic"
android:layout_gravity="center_horizontal"
android:layout_marginTop="20dip"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="katrina#gmail.com"
android:textColor="#FFFFFF"
android:textStyle="italic"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dip"/>
<RelativeLayout
android:id="#+id/single_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dip">
<ImageView
android:id="#+id/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="30dip"
android:textColor="#FFFFFF"/>
<ImageView
android:id="#+id/arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"/>
</RelativeLayout>
If I'm getting this right you want to display the ListView below the arrow ImageView?
You can simply put the ListView below the ImageView inside the RelativeLayout, then set the android:layout_below param to the id of the ImageView...arrow and finally make sure your ListView's id is set to list

How to load menu inside a list view when a list item is long pressed

I'm trying to mimic an action in the Twitter android app. When you long press/swipe a tweet (list item) it looks like this
This makes it easy for the user to take actions on the list item without leaving the screen. How can I do something similar in my app?
For now I've added a context menu which looks like this (notice that it fades out rest of the app).
I achieved this by registerForContextMenu(listView)
Currently, my list is like this:
ListAdapter adapter = new SimpleAdapter(
MainActivity.this, myList,
R.layout.list_items, new String[] {LI_ID,
LI_NAME, LI_COUNT}, new int[] {
R.id.li_id, R.id.li_name, R.id.li_count});
setListAdapter(adapter);
and my layout is simply like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/li_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="gone" />
<TextView
android:id="#+id/li_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="16dip"
android:textColor="#000000"
android:paddingTop="15dip"
android:paddingBottom="15dip"
android:paddingLeft="10dip"
android:textStyle="bold"/>
<TextView android:id="#+id/li_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:textColor="#ffffff"
android:textStyle="bold"
android:background="#9ed321"
android:paddingRight="3dip"
android:paddingLeft="3dip"/>
</RelativeLayout>
Have you looked tried an expandable list view ?
http://developer.android.com/reference/android/widget/ExpandableListView.html
http://www.youtube.com/watch?v=mwE61B56pVQ
You could just nest another layout in each individual ListView item that's initially invisible, then just toggle the visibility of the view on a long click event.
For example:
<?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"
android:animateLayoutChanges="true" >
<TextView
android:id="#+id/main_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:padding="16dp"
android:layout_gravity="center" />
<LinearLayout
android:id="#+id/hidden_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Delete" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Edit" />
</LinearLayout>
</LinearLayout>
Then in your activity, just set an onItemLongClickListener on your ListView, and toggle the
visibility of the hidden_view:
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id){
LinearLayout mView = view.findViewById(R.id.hidden_view);
switch(mView.getVisibility()){
case View.VISIBLE:
mView.setVisibility(View.GONE);
break;
case View.GONE:
mView.setVisibility(View.VISIBLE);
break;
}
}
}

Get value from custom dialog edittext in android

I my project i have a text view and button on my screen. On button click event i show custom dialog box (Dialog box content Edit text and a button). I put some data in dialog box edit text and click dialog box save button.
Now i want to show value of dialog edit text on screen text view on button click. How can i do this....
holder.list1.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
final TextView tv=(TextView)v.findViewById(R.id.textView3);
final Dialog dialog = new Dialog(activity);
dialog.setContentView(R.layout.customdailogfroaddmilk);
dialog.setTitle("Account");
// set the custom dialog components - text, image and button
final EditText text = (EditText) dialog.findViewById(R.id.editText1);
text.setText(tv.getText().toString());
Button save = (Button) dialog.findViewById(R.id.button1);
Button cancle = (Button) dialog.findViewById(R.id.button2);
save.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
tv.setText(text.getText().toString());
}
});
cancle.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
on button(save) click event edittext(save) value not set in textview(tv)
?????.....
Create a layout in your layout folder and provide it as the View to a Dialog Object.
And for the example shown below, you need to have a TextView, a Button and EditText in that layout.
final Dialog dialog = new Dialog(context,
android.R.style.Theme_Translucent_NoTitleBar);
Window window = dialog.getWindow();
window.setLayout(ViewGroup.LayoutParams.FILL_PARENT,
ViewGroup.LayoutParams.FILL_PARENT);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setContentView(R.layout.xmlfile);
Button ok = (Button) dialog.findViewById(R.id.alert_ok_button);
TextView alert_title = (TextView ) dialog.findViewById(R.id.alert_title);
final EditText shelf_name_edit=(EditText)dialog.findViewById(R.id.shelf_name_edit_area);
alert_title.setText(title);
alert_title.setTextSize(20);
ok.setText("OK");
ok.setTextSize(20);
ok.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.cancel();
Log.i("EditText Value",shelf_name_edit.getEditableText().toString());
}
});
dialog.show();
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:id="#+id/ClkBtn"
android:text="Click Me"></Button>
</RelativeLayout>
mydialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#ffffff" android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView android:id="#+id/Tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15px"
android:textColor="#6aa4cc"
android:text="Friendcaster for Facebook"
android:padding="10dip"
/>
<TextView android:id="#+id/Tv2"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textSize="14px"
android:textColor="#android:color/black"
android:layout_below="#+id/Tv1"
android:textStyle="bold"
android:text="A , B , C , D and or 5 friend like your status "
android:padding="10dip"
/>
<View android:layout_height="15dip" android:layout_width="fill_parent"/>
<LinearLayout android:layout_height="wrap_content" android:layout_width="fill_parent"
android:orientation="horizontal" android:background="#d6d6d6"
android:layout_weight="3"
>
<LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="left"
android:id="#+id/ll"
android:layout_weight="1" android:padding="5dip"
>
<ImageView android:src="#drawable/video"
android:layout_height="30dip" android:layout_width="30dip" android:layout_gravity="center_vertical|center_horizontal"
/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="view" android:layout_gravity="center_vertical|center_horizontal"
android:textColor="#0388e5"
/>
</LinearLayout>
<View android:layout_height="match_parent" android:layout_width="1dip" android:background="#android:color/darker_gray"/>
<LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical"
android:gravity="center"
android:layout_weight="1" android:padding="5dip"
>
<ImageView android:src="#drawable/chat"
android:layout_height="30dip" android:layout_width="30dip" android:layout_gravity="center_vertical|center_horizontal"
/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Reply" android:layout_gravity="center_vertical|center_horizontal"
android:textColor="#0388e5"
/>
</LinearLayout>
<View android:layout_height="match_parent" android:layout_width="1dip" android:background="#android:color/darker_gray"/>
<LinearLayout android:layout_height="wrap_content" android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_weight="1" android:padding="5dip"
android:gravity="right"
>
<ImageView android:src="#drawable/cancel"
android:layout_height="30dip" android:layout_width="30dip" android:layout_gravity="center_vertical|center_horizontal"
/>
<TextView android:layout_height="wrap_content" android:layout_width="wrap_content"
android:text="Zip" android:layout_gravity="center_vertical|center_horizontal"
android:textColor="#0388e5"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
main.java
package com.example.cusmizepopup;
import android.os.Bundle;
import android.os.Message;
import android.app.Activity;
import android.app.Dialog;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.Toast;
public class MainActivity extends Activity {
Dialog myDialog;
Button myButton;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.ClkBtn);
myButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
myDialog = new Dialog(MainActivity.this);
myDialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
myDialog.setContentView(R.layout.mydialog);
//myDialog.setTitle("My Dialog");
myDialog.setCancelable(true);
//myDialog.setCancelable(true);
//myDialog.setCancelMessage("Are you Sure ? ")
LinearLayout button = (LinearLayout) myDialog.findViewById(R.id.ll);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
//myDialog.dismiss();
Toast.makeText(getApplicationContext(), "hi", Toast.LENGTH_LONG).show();
}
});
myDialog.unregisterForContextMenu(myButton);
myDialog.show();
}
});
}
}

Tab host autoscroll – Programmatically

I want to develop an application simulating the tab browsing like Dolphin HD. Below is the XML file I have used to create the UI.
My queries are,
I am not able to get the fixed size tab like dolphin HD.
When I have created around 4-5 tabs, now I want to programmatically set the focus to new tab and scroll it to the current view.
Similar query was posted in the below link, but its not working still…
Android - Programmatic scrolling of TabWidget
In add new tab method, I tried inserting the below lines as mentioned in the above link…. but it didn’t work…
tabHost.setFocusableInTouchMode(true);
tabHost.setCurrentTab(z);
tabHost.setFocusable(true);
//Vinod
for ( i = 0; i < z; i++) {
getTabWidget().getChildAt(i).setFocusableInTouchMode(true);
}
XML file:-
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="wrap_content"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<RelativeLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<HorizontalScrollView android:layout_width="420px"
android:id="#+id/tab1"
android:layout_height="wrap_content"
android:fillViewport="true"
android:scrollbars="none">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="10px"
android:layout_height="wrap_content"
android:tag="tabPane">
</TabWidget>
</HorizontalScrollView>
<ImageButton
android:background="#android:color/transparent"
android:layout_marginTop="10px"
android:id="#+id/add_btn"
android:layout_height="70px"
android:layout_width="70px"
android:src="#android:drawable/ic_menu_add"
android:layout_toRightOf="#id/tab1"/>
</RelativeLayout>
<RelativeLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/address_bar"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="10px"
android:layout_width="fill_parent"
android:layout_height="70px"/>
</RelativeLayout>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="2dp" />
</LinearLayout>
</TabHost>
The delay was due to timing, so just a delay would work perfectly alright.
HorizontalScrollView hv = (HorizontalScrollView)findViewById(id.tab1);
//hv.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
for ( i = 0; i < z; i++) {
getTabWidget().getChildAt(i).setFocusableInTouchMode(true);
}
hv.postDelayed(new Runnable() {
public void run()
{
HorizontalScrollView hv = (HorizontalScrollView)findViewById(id.tab1);
hv.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}, 100L);