Keyboard not shown on inflated editText even after clicking on it - android

Background
I have a form-like activity, which has some views that can be created dynamically upon pressing.
I'm using an xml for each field that is inflated upon clicking on a button.
What I need is that upon choosing to add a new item, it will get focus, scroll if needed, and show the keyboard so that the user can type things into it. The keyboard may be shown upon adding the field or when clicking on the EditText.
The problem
For some reason, on some devices (and I don't think it's even an android version issue) when inflating the new view, the editText within it can get focus but it doesn't show the keyboard, even if it has focus and the user clicks on it.
In such a case, the only way to show the keyboard is to click on another EditText.
I've tested it, and noticed that it doesn't occur when I don't use xml at all, meaning when i create the views only in code.
The question
Why does it occur? What can I do in order to fix it?
I've already tried so many possible solutions, but none work on all devices.
Sample Code
the next code has the described issue on xperia j (android 4.0.4) an galaxy mini (android 2.3.6) .
#Override
protected void onCreate(final Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewGroup container = (ViewGroup) findViewById(R.id.LinearLayout1);
final LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
final View view = inflater.inflate(R.layout.field, null);
// using the next code works when using it instead of inflating a layout:
// final EditText editText = new EditText(MainActivity.this);
// editText.setText("item " + container.getChildCount());
container.addView(view);
}
});
}
the field layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFffffff"
android:gravity="center_vertical" >
<Spinner
android:id="#+id/typeSpinner"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:background="#null"
android:padding="5dp" />
<include
android:id="#+id/removeItemView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
layout="#layout/remove_item_view" />
<EditText
android:id="#+id/fieldEditText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/removeItemView"
android:layout_toRightOf="#+id/typeSpinner"
android:ems="10"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="field"
android:imeOptions="actionDone"
android:inputType="phone"
android:minWidth="200dp"
android:padding="5dp"
android:singleLine="true"
tools:ignore="HardcodedText" >
<requestFocus />
</EditText>
</RelativeLayout>
the main layout file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
tools:ignore="HardcodedText" />
</LinearLayout>
</ScrollView>
EDIT: found a partial solution which won't show the keyboard right away, but at least it will be shown when pressing the editText:
public static void forceFocusOnView(final View view) {
if (view == null)
return;
view.post(new Runnable() {
#Override
public void run() {
view.clearFocus();
view.post(new Runnable() {
#Override
public void run() {
view.requestFocus();
}
});
}
});
}

I'm not sure in a reason of that problem. Could be some missmatches when Android sdk were rewritten in order to match some devices.
So I think that the best solution to you is to show your keyboard manually and do not try to move a train, just take the path of the least resistance:
EditText et=(EditText)this.findViewById(R.id.edit_text);
et.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, 0);<----------------
}
});
Here you go , man. Wish you luck.
Oh i see...
private EditText et;
private Runnable setFocus;
......
protected void onCreate(...){
.
.
.
setFocus=new Runnable(){
#Override
public void run() {
et.requestFocus();
}
};
}
....
findViewById(R.id.button).setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
final View view = inflater.inflate(R.layout.field, null);
//try this:
et=(EditText)view.findViewById(r.id.fieldEditText);
et.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
imm.showSoftInput(et, 0);<----------------
}
});
container.addView(view);
container.requestLayout();<--------------
et.postDelayed(setFocus,300);
}
});

Related

Is it impossible to dynamically add custom layouts using addView()?

I am making a window to open a dialog window and write a comment.
When you open the dialog window, there is a layout where you can enter your first comment.
Comment can only be written in one line and the following comment layout is created by pressing EnterKey in the input field.
I originally implemented this as a RecyclerView, but now I am trying other methods as well. (using addView())
But when I used addView() I got the following error.
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
The solution to this is shown in the here. (And I would appreciate it if you could explain why this error occurs.)
There is no error when using this.
However, no more items are added.
There is only the first item that existed when the dialog box was opened, and there is no response when pressing the Enter key.
I want to add items dynamically in real time.
What should I do?
fragment_wc_dialog.xml
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
tools:context=".fragment.WritingCommentDialogFragment">
</LinearLayout>
wc_item.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<EditText
android:id="#+id/comment_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center_vertical"
android:drawableLeft="#drawable/ic_bullet_point"
android:drawablePadding="5dp"
android:layout_marginHorizontal="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22"
android:imeOptions="actionNone"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
WCDialogFragment.java
public class WritingCommentDialogFragment extends DialogFragment {
LinearLayout mContainer; // input layout
View inputView;
EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
initViews(view);
mContainer.addView(inputView); // The first item to be present when the dialog is opened
editText.setOnEditorActionListener(new TextView.OnEditorActionListener() {
#Override
public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
if(event.getAction() == KeyEvent.ACTION_DOWN) {
mContainer.addView(inputView);
}
return true;
}
});
return view;
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
#Override
public void onResume() {
super.onResume();
Toast.makeText(getContext(), "onResume()", Toast.LENGTH_SHORT).show();
setDialogSize();
}
private void initViews(View view) {
mContainer = view.findViewById(R.id.container);
inputView = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, null);
editText = inputView.findViewById(R.id.comment_edit);
}
}
ADDED
Your problem and the linked issue are different, as far as I can tell. The link has two views that they want to swap back and forth between, so the answer is to remove one view before adding the second. You want to build up a list of views, so you must use different instances rather than adding the same view over and over.
If you want the newly-added view to also respond to the Enter key, you can set the same listener to the new view too:
if(event.getAction() == KeyEvent.ACTION_DOWN) {
EditText addedView = (EditText) LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, mContainer, false);
addedView.setOnEditorActionListener(this);
mContainer.addView(addedView);
}

EditText in listview inside dialogfragment will not open sw keyboard

I have a dialog with dynamic content, so I've created a listview inside this dialog and then an Adapter which decides what layout each item has,
Adapter chooses from 3 layouts,
one contains checkbox - works fine
second contains spinner - works fine
third contains edittext - SW keyboard will never display
Here is the edittext item layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="?android:listPreferredItemHeight"
android:paddingStart="24dp"
android:paddingEnd="24dp"
>
<TextView
android:id="#+id/name"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:text="Test"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
/>
<EditText
android:id="#+id/value"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical"
android:layout_gravity="center_vertical"
android:focusable="true"
/>
</LinearLayout>
and the adapter choosing a layout:
class FieldAdapter extends ArrayAdapter<ProfileField> {
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ProfileField field = getItem(position);
switch (field.type) {
case ProfileFieldType.CUSTOM_TEXT:
convertView = inflater.inflate(R.layout.scanprofile_dialog_edit_item, parent, false);
break;
case ProfileFieldType.CUSTOM_CHECK:
convertView = inflater.inflate(R.layout.scanprofile_dialog_checkbox_item, parent, false);
break;
default:
ProfileField.ListProfileField listField = (ProfileField.ListProfileField) field;
convertView = inflater.inflate(R.layout.scanprofile_dialog_item, parent, false);
break;
}
return convertView;
}
...
}
I've tried adding this to onCreateDialog
final AlertDialog alertDialog = builder.create();
final Window dialogWindow = alertDialog.getWindow();
dialogWindow.clearFlags(
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE | WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
dialogWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
return alertDialog;
But it didn't help at all.
Any ideas why the SW keyboard is not shown?
EditText View inside ListView is not supported in android as both need focus to work properly.
In Your case all the event are consume by ListView that's why keyboard is not opening when you are trying to clicking on EditText view.
You can give focus to EditTextView but it will create problem in ListView functionality.
You can either put EditText in header/footer of ListView or if you have
fix number of view like only 3 then directly plot those view on dialog view instead of inflating them inside ListView.
Refer the here if you still want to use EditText inside ListView.
add in xml
<EditText...>
<requestFocus />
add in code
boolean checkFocus=youredittext.requestFocus();
Log.i("CheckFocus", ""+checkFocus);
if(checkFocus==true)
{
InputMethodManager mgr = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
mgr.showSoftInput(youredittext, InputMethodManager.SHOW_IMPLICIT);
}
you can try this code (help you proactively do it) any time you want)
//edittext demo code
final EditText myEditText = (EditText)findViewById(R.id.editText);
myEditText.setOnFocusChangeListener(new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
myEditText.postDelayed(new Runnable() {
#Override
public void run() {
InputMethodManager keyboard = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
keyboard.showSoftInput(myEditText, 0);
}
}, 50); //require system show soft keyboard after 50 miliseconds
}
});

Button not working when restored using SharedPreferences

I have an app which inflates button views on a click of a button(Button A). The button views inflated work properly, executing the code in their onClick method.
Now to save the layout generated, I keep a count of number of times the Button A is clicked and save this value using SharedPreferences. In onCreate method I create a for loop for the number of times the Button A was clicked and re-inflate those buttons, thus generating the same layout that was on screen when the app was killed. Though now, the buttons generated in the onCreate method no longer function.
I hope I have made some sense. The problem, when the app is started again in future, it builds the layout as it was when the app was clicked, but the button views no longer function for the onCLick method.
Here's the code for reference:
MainActivity.java
public class MainActivity extends Activity {
EditText et;
String z, r;
LinearLayout ll;
SharedPreferences sp;
Button fb1;
int c=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
et=(EditText) findViewById(R.id.et);
sp=getSharedPreferences("MY_PREFS", 0);
if (sp.contains("counter")) {
for (int i=1;i<=sp.getInt("counter", 1);i++) {
cr();}
}
c=sp.getInt("counter", 0);
}
public void cr() {
ll=(LinearLayout) findViewById(R.id.ll);
LayoutInflater li=getLayoutInflater();
View vw=li.inflate(R.layout.infla, null);
ll.addView(vw);
fb1=(Button) vw.findViewById(R.id.btn);
fb1.setId(c);
}
public void click(View v) {
int z=v.getId();
switch (z) {
case (R.id.button):
et=(EditText) findViewById(R.id.et);
cr();
et.setText("");
c=c+1;
break;
}
Editor e=sp.edit();
//e.putString("check", r);
e.putInt("counter", c);
//e.putInt("id", c);
e.commit();
for (int i=0; i<=c;i++){
if (z%2==0) {//Present
et.setText("Present");}
else if(z%2==1)
et.setText("Absent");
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/ll"
tools:context=".MainActivity" >
<EditText
android:id="#+id/et"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:ems="10"
android:hint="Enter something" >
<requestFocus />
</EditText>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:onClick="click"
android:text="Click" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="remove" />
</LinearLayout>
infla.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button"
android:onClick="click" />
</LinearLayout>
Edit:
You seem to have assigned redundant id's to the button during initialization.Each button would be assigned the value 'c' instead of 0 to 'c'.
Additionally It seems only logical to create click listeners on the fly as well ,since you are inflating button on the go.Try implementing the onclicklistener instead of the hardwired click() method in the xml.
i.e
public void cr() {
ll=(LinearLayout) findViewById(R.id.ll);
LayoutInflater li=getLayoutInflater();
View vw=li.inflate(R.layout.infla, null);
ll.addView(vw);
fb1=(Button) vw.findViewById(R.id.btn);
fb1.setId(c);
fb1.setOnClickListener(new View.OnClickListener(){
//Stuff you wish to do on click
});
}

Android: many equal Buttons, onclick doesn't work for certain

I have seven equal Buttons in LinearLayout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:weightSum="7" >
<Button
android:id="#+id/btn_mon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_weight="1"
android:background="#color/white"
android:text="0" />
<Button
android:id="#+id/btn_tus"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_wen"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_thu"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_fri"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_sat"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
<Button
android:id="#+id/btn_sun"
android:layout_width="50dp"
android:layout_height="50dp"
android:text="0"
android:background="#color/white"
android:layout_weight="1"/>
</LinearLayout>
Their OnClickListener is also equal (and initializing too):
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.calendar_row, null);
}
final Button btn_mon = (Button)convertView.findViewById(R.id.btn_mon);
final Button btn_tus = (Button)convertView.findViewById(R.id.btn_tus);
final Button btn_wen = (Button)convertView.findViewById(R.id.btn_wen);
final Button btn_thu = (Button)convertView.findViewById(R.id.btn_thu);
final Button btn_fri = (Button)convertView.findViewById(R.id.btn_fri);
final Button btn_sat = (Button)convertView.findViewById(R.id.btn_sat);
final Button btn_sun = (Button)convertView.findViewById(R.id.btn_sun);
btn_mon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_mon, weekdays.get(0).data); }});
btn_tus.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_tus, weekdays.get(1).data); }});
btn_wen.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_wen, weekdays.get(2).data); }});
btn_thu.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_thu, weekdays.get(3).data); }});
btn_fri.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_fri, weekdays.get(4).data); }});
btn_sat.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_sat, weekdays.get(5).data); }});
btn_sun.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_sun, weekdays.get(6).data); }});
onCalBtnClick method:
private void onCalBtnClick(Button btn, int day){
Log.d("debug", String.valueOf(day));
btn.setTextColor(mContext.getResources().getColor(R.color.orange));
//selectedYear, month are global
Intent intent = new Intent();
intent.putExtra("year", selectedYear);
intent.putExtra("month", month);
intent.putExtra("day", day);
setResult(RESULT_OK, intent);
finish();
}
However, if I put Log.d into onCalBtnClick method (it is called from each clicklistener), only middle three buttons work. Two buttons from left side (btn_mon, btn_tus) and two buttons from right side (btn_sat, btn_sun) don't react on user click. Middle buttons work fine.
This is similar question Android LinearLayout make button doesn't work but my layout file corresponds to pattern in the answer there and buttons don't work nevertheless
UPDATE
When I removed fixed button height and width in layout file (from 50dp to wrap_content), all buttons started to work!
However, now it doesn't look as needed. There's space needed between text's on buttons.
And main question: WHY?
If you using weights in your layout, you are telling that it should be filled with some objects with some proportions. It just opposite to "wrap_content". With weights outer layout defines size of inner views, while "wrap_content" means that outer layout size is defined by inner views.
Please decide what approach is better in that case - removing weights or fixed inner views sizes.
set your Linear Layout's Width and Height to FILL_PARENT .it will work for sure.
Try to declare all your setOnClickListener for buttons outside of your adapter class and after initialization of weekdays array.
That will solve your problem.
Or try to set condition like this for all buttons :
if (btn_mon != null && position < weekdays.size()) {
btn_mon.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) { onCalBtnClick(btn_mon, weekdays.get(0).data); }});
}

How to implement different behavior when the user clicks on different views in a ListView?

I a new to Android development, so this is kind of a basic question.
I would like to implement the same behavior as in the Contacts app. You have a ListView with a series of Contacts | phone icons. There you have one behavior when you click on the contact name, and another behavior when you click on the phone icon.
Here is my code.
Any help is much appreciated.
In summary, what is wrong with the approach
switch (v.getId()) {
case R.id.imageButtonAction:
Activity Class
public class CompaniesActivity extends Activity {
MyApp app;
ListView listCompanies;
Cursor cursor;
// Adapter and its corresponding FROM and TO statements. The number and sequence of the arguments must match in FROM / TO arguments.
SimpleCursorAdapter adapter;
static final String[] FROM = { MenuNavigationData.C_COMPANY, MenuNavigationData.C_DESCRIPTION};
static final int[] TO = { R.id.textCompany, R.id.textDescription }; //
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.companies);
//Gets a reference to the application
app = (MyApp) getApplication();
// Find your views
listCompanies = (ListView) findViewById(R.id.listCompanies);
addButton = (Button) findViewById(R.id.buttonAdd);
// Add actions to user interaction
listCompanies.setOnItemClickListener(new OnItemClickListener() {
#Override
**public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
switch (v.getId()) {
case R.id.imageButtonAction:
startActivity(new Intent(app, InstructionsActivity.class));
break;
default:
int i = adapter.getItemViewType(position);
startActivity(new Intent(app, EditMenuNavigationActivity.class));
break;
}**
}
});
}
Activity xml
<!-- Companies ListView-->
<ListView android:id="#+id/listView1" android:layout_height="wrap_content" android:layout_width="match_parent"></ListView>
<ListView
android:id="#+id/listCompanies"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_weight="1"
android:background="#5555"/>
</LinearLayout>
Row xml
android:background="#ffff"
android:padding="6dip">
<!-- Company TextView -->
<TextView
android:id="#+id/textCompany"
android:text="TIM"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:singleLine="true"
android:ellipsize="marquee"
android:textColor="#c000"
android:textStyle="bold"
android:textSize="25sp"/>
<!-- Description TextView -->
<TextView
android:id="#+id/textDescription"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:layout_below="#id/textCompany"
android:layout_alignWithParentIfMissing="true"
android:layout_alignParentLeft="true"
android:singleLine="true"
android:ellipsize="marquee"
android:textColor="#c000"></TextView>
<!-- Action ImageView -->
<ImageView
android:id="#+id/imageButtonAction"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_gravity="right"
android:background="#drawable/icon"/>
</RelativeLayout>
Your onItemClick() callback receives both the specific view as well as its position (0-based), each of these can help you decide which view was clicked. The position is an index into the items you've added, and for more complicated scenarios you can view.setTag(Object o), and use getTag() to retrieve it from your callback.
New much better approach to solve this issue elegantly, and with less code!!!
With the following modifications, the User interface is much more responsive, no more double-clicking issues. :)
Much, much less code that simply works!
Modifications to Row xml
Insert a Linear layout to wrap both the
In this Linear layout, insert a tag named android:onClick="editCompanyClick"
This is the click handler that will be called in the Activity.
Insert a Linear layout to wrap the
In this Linear layout, insert a tag named android:onClick="dialClick"
This is the click handler that will be called in the Activity.
Modifications to Activity class
Remove the previous code
listCompanies.setOnItemClickListener(new OnItemClickListener() { #Override public void onItemClick(AdapterView arg0, View v, int position, long id) {
TextView company = (TextView) v.findViewById(R.id.textCompany);
ImageView dial = (ImageView) v.findViewById(R.id.imageButtonDTMFDial);
company.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(app, EditMenuNavigationActivity.class));
}
});
dial.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(app, InstructionsActivity.class));
}
});
}
Insert the code
public void dialClick(View v) {
startActivity(new Intent(app, InstructionsActivity.class));
}
public void editCompanyClick(View v) {
startActivity(new Intent(app, EditMenuNavigationActivity.class));
record
}
Row xml
<?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="?android:attr/listPreferredItemHeight"
android:padding="6dip" android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linearLayout1"
android:orientation="vertical"
android:onClick="editCompanyClick"
android:layout_weight="1">
<!-- Company TextView -->
<TextView android:singleLine="true" android:text="TIM" android:id="#+id/textCompany" android:ellipsize="marquee" android:layout_height="wrap_content" style="#android:style/TextAppearance.Medium" android:layout_width="match_parent" android:gravity="top"></TextView>
<!-- Description TextView -->
<TextView android:singleLine="true" android:text="Chamar atendente" android:id="#+id/textDescription" android:ellipsize="marquee" android:layout_height="wrap_content" style="#android:style/TextAppearance.Small" android:layout_width="match_parent" android:gravity="bottom"></TextView>
</LinearLayout>
<LinearLayout
android:layout_height="match_parent"
android:id="#+id/linearLayout2"
android:onClick="dialClick"
android:layout_width="wrap_content" android:layout_gravity="right">
<!-- DTMFDial ImageView -->
<ImageView android:layout_height="wrap_content" android:background="#drawable/icon" android:id="#+id/imageButtonDTMFDial" android:layout_gravity="right" android:layout_width="wrap_content"></ImageView>
</LinearLayout>
</LinearLayout>
I finally found a solution.
This solves the issue. But adds another one. As expected, the ListView now behaves differently when the user clicks on different views(either TextView or ImageView).
But it seems unresponsive. I have to "double-click" in order to trigger either the company.setOnClick or dial.setOnClick. Any suggestions?
// Add actions to user interaction
listCompanies.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int position, long id) {
TextView company = (TextView) v.findViewById(R.id.textCompany);
ImageView dial = (ImageView) v.findViewById(R.id.imageButtonDTMFDial);
company.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(app, EditMenuNavigationActivity.class));
}
});
dial.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(app, InstructionsActivity.class));
}
});
}

Categories

Resources