Switch Widget Inside RecyclerView Clickable - java

I'm trying to use a Switch widget inside of a RecyclerView to set the particular component as active or not. How can I detect the switch toggling from the RecyclerView? My current OnItemClickListener intercepts the click on the Switch widget also.
Current Click Listener Code:
mRecyclerView.addOnItemTouchListener(
new RecyclerItemListener(getApplicationContext(), mRecyclerView,
new RecyclerItemListener.RecyclerTouchListener() {
public void onClickItem(View v, int position) {
Toast.makeText(MainActivity.this, "Clicked, position " + position + ". Name: " + GlobalData.totalAlarms.get(position).getAlarmName(), Toast.LENGTH_LONG).show();
}
public void onLongClickItem(View v, int position) {
}
}));
}
Layout for RecyclerView, including Switch:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:focusable="true"
android:clickable="true">
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/recycler_mainactivityName"
android:textAppearance="#style/TextAppearance.AppCompat"
android:textSize="16sp"
android:layout_marginTop="15dp"
android:paddingLeft="20dp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/recycler_mainactivityTime"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/recycler_mainactivityDays"
android:paddingLeft="20dp"
android:textSize="12sp"
android:layout_marginBottom="5dp" />
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/recycler_mainactivityTime"
android:paddingTop="2dp"
android:textSize="12sp"
android:paddingLeft="20dp"
android:layout_below="#+id/recycler_mainactivityName"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/recycler_mainactivityActive"
android:paddingRight="20dp"
android:layout_alignBaseline="#+id/recycler_mainactivityTime"
android:layout_alignBottom="#+id/recycler_mainactivityTime"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hapticFeedbackEnabled="true" />
</RelativeLayout>
When I remove the addOnItemTouchListener, the switches do toggle on click rather than being forced to swype.. Beyond that, how can I detect the touch? The addOnItemClickListener seems to intercept all clicks (unless I do a long click, then the switches toggle).

Add a OnCheckChangeListener of the switch inside your recyclerView adapter to get the state of the switch

Related

Giving the same id to two different components in an xml file Android

I am using a constraint layout in my XML file. I have a view like in the example. Imageview and textview. I want both of these to have the same action after clicking. How can I group the two together and give them an id?
xml :
<ImageView
android:id="#+id/menu_drawerLogout"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_menu_exit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view6" />
<TextView
android:id="#+id/menu_drawerLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="17dp"
android:text="#string/sign_out_text"/>
You can't have 2 components with the same id in an XML layout resource file.
Method 1
If you want both to have the same action set a common onClickListener to both like,
xml
<ImageView
android:id="#+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_menu_exit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view6" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="17dp"
android:text="#string/sign_out_text"/>
inside onCreate method
ImageView imageView = findViewById(R.id.imageView);
TextView textView = findViewById(R.id.textView);
View.OnClickListener myListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// action
}
});
imageView.setOnClickListener(myListener);
textView.setOnClickListener(myListener);
Method 2
You can put both the views in a container and then set a onClickListener to the container
xml
<LinearLayout
android:id="#+id/container"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_menu_exit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view6" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="17dp"
android:text="#string/sign_out_text"/>
</LinearLayout>
inside onCreate
LinearLayout layout = findViewById(R.id.container);
View.OnClickListener myListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
// action
}
});
layout.setOnClickListener(myListener);
Consider the code below:
<TextView
android:id="#+id/location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="0dp"
android:layout_marginTop="12dp" />
<ImageView
android:id="#+id/location"
android:layout_width="70dp"
android:layout_height="70dp"
android:layout_marginStart="33dp"
android:layout_marginTop="5dp"
app:srcCompat="#drawable/openLoc" />
You can but it is not recommended.
Set ids in a way that elements can be easily identifiable by ID like android:id="#+id/txtLocation" android:id="#+id/imgLocation" it makes it easy to identify element type just by reading ID. You can make it even easier by appending layout name in beginning like android:id="#+id/profileTxtLocation". Now this will help you while coding as autocomplete feature will assist you. Just type layout name you will get the list of all layout elements, then you will type the kind of element you get the list of all asked elements(es: textViews) in layout.
My layout was inside a constraintlayout and in this case I used the constraintlayout group component.
xml :
<ImageView
android:id="#+id/img_logout"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginStart="20dp"
android:layout_marginTop="8dp"
android:src="#drawable/ic_menu_exit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/view6" />
<TextView
android:id="#+id/menu_drawerLogout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginStart="16dp"
android:layout_marginTop="17dp"
android:text="#string/sign_out_text"
android:textColor="#color/black_choice"
android:textSize="15sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toEndOf="#+id/img_logout"
app:layout_constraintTop_toBottomOf="#+id/img_star" />
<androidx.constraintlayout.widget.Group
android:id="#+id/logout_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="img_logout,menu_drawerLogout" />
Then I created the Group.setAllOnClickListener field in MainActivity :
private fun Group.setAllOnClickListener(listener: View.OnClickListener?) {
referencedIds.forEach { id ->
rootView.findViewById<View>(id).setOnClickListener(listener)
}
}
and
logoutGroup.setAllOnClickListener()
logOutDialog()
}

How to make CheckBox inside InfoWindow true or false

I'm using a custom InfoWindowAdapter in order to use my Marker InfoWindow layout, with my data.
Each Marker object has associated an AnimalMarker object (stored in a HashMap)
The Checkboxes however won't update , even though there is no error printed and the Log.d(..) prints the boolean that should be in the checkbox.
Is there something that I'm doing wrong or that I'm not aware of regarding CheckBoxes inside InfoWindows?
code inside my MapActivity
mMap.setInfoWindowAdapter(new GoogleMap.InfoWindowAdapter() {
#Override
public View getInfoWindow(Marker marker) {
View view = getLayoutInflater().inflate(R.layout.marker_info_window, null);
AnimalMarker animalMarker = allMarkersHashMap.get(marker);
if (animalMarker == null) {
animalMarker = myMarkersHashMap.get(marker);
}
Log.d(TAG, "getInfoWindow: animalMarker --> " + animalMarker);
TextView miwLocation = (TextView) view.findViewById(R.id.miwLocation);
TextView miwAnimalName = (TextView) view.findViewById(R.id.miwAnimalName);
TextView miwAnimalAge = (TextView) view.findViewById(R.id.miwAnimalAge);
ImageView miwImage = (ImageView) view.findViewById(R.id.miwImage);
CheckBox miwAdultCB = (CheckBox) view.findViewById(R.id.miwAdultCB);
CheckBox miwNeuteredCB = (CheckBox) view.findViewById(R.id.miwNeuteredCB);
miwLocation.setText(marker.getTitle());
miwAnimalName.setText(animalMarker.getAnimal().getAnimalName());
miwAnimalAge.setText(animalMarker.getAnimal().getAproxAge().toString() + " yrs");
miwAdultCB.setChecked(animalMarker.getAnimal().isAdult());
Log.d(TAG, "getInfoWindow: made AdultCB = " + animalMarker.getAnimal().isAdult());
miwNeuteredCB.setChecked(animalMarker.getAnimal().isNeutered());
switch (animalMarker.getAnimal().getSpecies()) {
case "dog":
miwImage.setImageResource(R.drawable.dog_icon);
break;
case "cat":
miwImage.setImageResource(R.drawable.cat_icon);
break;
default:
miwImage.setImageResource(R.drawable.cat_icon);
}
return view;
}
#Override
public View getInfoContents(Marker marker) {
return null;
}
});
R.layout.marker_info_window :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/white_border"
android:orientation="horizontal"
android:padding="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/miwLocation"
android:layout_centerHorizontal="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="5dp"
android:ellipsize="end"
android:maxLines="1"
android:text="miwLocation"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold" />
<ImageView
android:src="#drawable/dog_icon"
android:id="#+id/miwImage"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_below="#+id/miwLocation"
android:layout_alignParentStart="true"
android:layout_marginStart="18dp"
android:layout_marginTop="3dp" />
<TextView
android:id="#+id/miwAnimalName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/miwLocation"
android:layout_marginStart="30dp"
android:layout_marginTop="7dp"
android:layout_toEndOf="#+id/miwImage"
android:layout_toRightOf="#id/miwImage"
android:ellipsize="end"
android:maxLines="2"
android:textStyle="bold"
android:text="miwAnimalName"
android:textColor="#000000"
android:textSize="15sp" />
<TextView
android:id="#+id/miwAnimalAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/miwLocation"
android:layout_marginStart="20dp"
android:layout_marginTop="7dp"
android:layout_toRightOf="#id/miwAnimalName"
android:text="3 yrs"
android:textStyle="bold"
android:textColor="#000000"
android:textSize="15sp" />
<CheckBox
android:id="#+id/miwAdultCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/miwAnimalName"
android:layout_marginLeft="20dp"
android:layout_marginTop="8dp"
android:layout_toRightOf="#id/miwImage"
android:text=" Adult"
android:textSize="18dp"
android:clickable="false"/>
<CheckBox
android:id="#+id/miwNeuteredCB"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/miwAnimalName"
android:layout_marginLeft="20dp"
android:layout_marginTop="8dp"
android:layout_toRightOf="#id/miwAdultCB"
android:text=" Neutered"
android:textSize="18dp"
android:clickable="false"/>
</RelativeLayout>
</LinearLayout>
Images of the layout design and of the resulting InfoWindow:
This may be a little late but as stated here:
The info window that is drawn is not a live view. The view is rendered as an image (using View.draw(Canvas)) at the time it is returned. This means that any subsequent changes to the view will not be reflected by the info window on the map. To update the info window later (for example, after an image has loaded), call showInfoWindow(). Furthermore, the info window will not respect any of the interactivity typical for a normal view such as touch or gesture events. However you can listen to a generic click event on the whole info window as described in the section below.

Can't click on EditText when disabling and re-enabling

I have a CheckBox that enables/disables a couple of EditText views. At first, when the activity is started, I'm able to click on the EditText, but when I press the CheckBox (sets enabled to false) and then press the checkbox again(which supposedly sets the EditText to enabled), I can't click on the EditText.
This is the java code
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
final EditText etPersonTitle = (EditText)v.findViewById(R.id.et_person_title);
final EditText etFirstname = (EditText)v.findViewById(R.id.et_first_name);
final EditText etSurname = (EditText)v.findViewById(R.id.et_surname);
if(isChecked){
etPersonTitle.setText(R.string.unknown);
etFirstname.setText(R.string.unknown);
etSurname.setText(R.string.unknown);
}
else {
etPersonTitle.setText("");
etFirstname.setText("");
etSurname.setText("");
}
etPersonTitle.setEnabled(!isChecked);
etPersonTitle.setFocusable(!isChecked);
etFirstname.setEnabled(!isChecked);
etFirstname.setFocusable(!isChecked);
etSurname.setEnabled(!isChecked);
etSurname.setFocusable(!isChecked);
}
and here is the xml:
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="#string/title" />
<EditText
android:id="#+id/et_person_title"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="15"
android:inputType="textPersonName"
android:textAppearance="?android:attr/textAppearanceSmall" >
</EditText>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="#string/first_name" />
<EditText
android:id="#+id/et_first_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="15"
android:inputType="textPersonName"
android:textAppearance="?android:attr/textAppearanceSmall" >
</EditText>
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="#string/surname" />
<EditText
android:id="#+id/et_surname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:ems="15"
android:inputType="textPersonName"
android:textAppearance="?android:attr/textAppearanceSmall" >
</EditText>
</TableRow>
<LinearLayout
android:id="#+id/ll_unknown_customer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:text="#string/unknown_customer" />
<CheckBox
android:id="#+id/cb_unknown_customer"
style="#style/checkbox_style"
android:background="#drawable/checkbox_background"
android:button="#null"
android:checked="false" />
</LinearLayout>
one thing to note is that the TableLayout is inside a ScrollView. I'm not sure if that's what's causing this issue, but that's my first suspicion.
I tried to setClickable(true) but it doesn't work either.
Thanks in advance.
Do this.
if (isChecked) {
etPersonTitle.setText("asd");
etFirstname.setText("asd");
etSurname.setText("asd");
etPersonTitle.setEnabled(!isChecked);
etPersonTitle.setFocusable(!isChecked);
etFirstname.setEnabled(!isChecked);
etFirstname.setFocusable(!isChecked);
etSurname.setEnabled(!isChecked);
etSurname.setFocusable(!isChecked);
}
else {
etPersonTitle.setText("");
etFirstname.setText("");
etSurname.setText("");
etPersonTitle.setEnabled(true);
etPersonTitle.setFocusableInTouchMode(true);
etFirstname.setEnabled(true);
etFirstname.setFocusableInTouchMode(true);
etSurname.setEnabled(true);
etSurname.setFocusableInTouchMode(true);
}
setFocusable mainly used for enable/disable view's focus event on both touch mode and keypad mode( using up/down/next key).
setFocusableInTouchMode mainly used for enable/disable view's focus event on touch mode alone.
If you are disabled setFocusable it also disabled the view's focus event on touch mode.

Error when generating dynamic content

I'm trying to generate form elements from a template when the user clicks on a button. I created the template and the container layout for the new forms with XML. Its successfully generating the first form where I'm telling it to generate, but when I try to generate more forms beyond the first one its giving me an error: "the specified child already has a parent. You must call removeView() on the child's first parent". Any clue as to what I should do so as to generate more than one element? I've tried changing the id of the newly created forms but that's giving me a null pointer exception and crashing. Thank you.
public class MakeQuestion extends Activity implements OnClickListener{
private static final int MY_BUTTON = 9000;
int templateID = 1;
Button b;
Button target;
View insertPoint;
Button testTemplate;
View v1;
RelativeLayout.LayoutParams templateParams;
LayoutInflater vi;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.make_question);
Initialize();
}
public void Initialize(){
//button for adding new forms
b = (Button) findViewById(R.id.makeLayoutButton);
b.setOnClickListener(this);
//get the template form to be duplicated
vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v1 = vi.inflate(R.layout.form_template, null);
//set the params for the element that will have dynamically generated content below it
templateParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
//container where forms will be contained in
insertPoint = findViewById(R.id.questionsContainer);
//view where new form will go below
View belowContainer = findViewById(R.id.questionTemplateFake);
//set id for layout params of view
belowContainer.setId(1);
//set rule for new forms to go below view 'belowContainer'
templateParams.addRule(RelativeLayout.BELOW, belowContainer.getId());
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.makeLayoutButton:
v1 = vi.inflate(R.layout.form_template, null);
//add view to the insertPoint
((LinearLayout) insertPoint).addView(v1);
break;
}
}
}
Added forms should go in "questionsContainer" which is set to be below "questionTemplateFake"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/grey_background" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="wrap_content"
android:layout_height="50dp"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#8B459A"
android:baselineAligned="false"
android:clipToPadding="false" >
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/logo_small" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:src="#drawable/settings" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:src="#drawable/search" />
</RelativeLayout>
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/relativeLayout1" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relative12"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="WHAT IS YOUR QUESTION?" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/save_button"
android:layout_alignBottom="#+id/save_button"
android:layout_alignRight="#+id/textView1"
android:text="ADD PICTURE OR VIDEO"
android:textSize="10sp" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:layout_marginTop="14dp"
android:background="#drawable/textlines"
android:ems="10"
android:hint="50 WORDS OR LESS"
android:inputType="textMultiLine"
android:paddingLeft="5dp" />
<Button
android:id="#+id/save_button"
android:layout_width="75dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/editText1"
android:layout_marginTop="16dp"
android:background="#drawable/purplebutton"
android:text="BROWSE"
android:textColor="#drawable/button_text_color"
android:textSize="10sp" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/save_button"
android:layout_marginTop="25dp"
android:text="CREATE AN ANSWER" />
<RelativeLayout
android:id="#+id/questionTemplateFake"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/TextView01" >
<Button
android:id="#+id/Button02eew"
android:layout_width="75dp"
android:layout_height="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="#drawable/purplebutton"
android:text="BROWSE"
android:textColor="#drawable/button_text_color"
android:textSize="10sp" />
<EditText
android:id="#+id/EditText02"
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_above="#+id/Button02"
android:layout_alignParentLeft="true"
android:background="#drawable/textlines"
android:ems="10"
android:hint="50 WORDS OR LESS"
android:inputType="textMultiLine"
android:paddingLeft="5dp" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/TextView03"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginRight="19dp"
android:layout_toLeftOf="#+id/Button02"
android:text="ADD PICTURE OR VIDEO"
android:textSize="10sp" />
</RelativeLayout>
<LinearLayout
android:id="#+id/questionsContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="80dp"
android:layout_marginTop="20dp"
android:orientation="vertical"
android:layout_below="#+id/questionTemplateFake"
>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/makeLayoutButton"
android:layout_width="100dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/scrollView1"
android:layout_marginRight="17dp"
android:layout_marginTop="79dp"
android:background="#drawable/purplebutton"
android:text="MORE OPTIONS"
android:textColor="#drawable/button_text_color"
android:textSize="10sp" />
</RelativeLayout>
You are trying to add the same instance over and over again. Thus the message that "specific child already exists". You would have to create a new template layout instance with different ID (I suppose) and then try to add it in.
So rather than:
((RelativeLayout) insertPoint).addView(v1, templateParams);
and adding v1 again. Create a new instance
v1 = vi.inflate(R.layout.form_template, null);
set the id for good measures and then add it again in view.
I suppose you don't need to set the id, but you can read more about how id's work here

by clicking on TextView i want to see the list of items

In my android application, i want to display a list of items when i click on a Textview, it display a list of items and i can add and delete items from that list. how can i do it through java code
Kindly guide. i will be very thankful to you
my code is:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/cus_name"
android:gravity="center"
android:clickable="true"
android:focusable="true"
android:onClick="onClick"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/cus_name_txta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/contact_no"
android:clickable="true"
android:focusable="true"
android:onClick="onClick"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/contact_no_txta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/ticket_no"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/ticket_no_txta"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceLarge" />
<requestFocus />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:text="#string/task_detail"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/task_detail_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"/>
</LinearLayout>
If I understood right I would suggest extending a Dialog to make custom dialog that pops out whenever user clicks on TextView (as included in Imran Khan's answer). This dialog would contain ListView and whatever else you need for handling the list. Example of such approach as I used it some time ago:
public class LogOverlay extends Dialog{
private Server mItem;
private boolean end=false;
private int mLimitHigh = 15;
private int mLimitLow = 0;
private ListView mListView;
private LogListAdapter mAdapter;
private ArrayList<LogUnit> mLog = new ArrayList<LogUnit>();
private boolean mScroll;
private Context context;
ProgressDialog pd;
public LogOverlay(Context context,Session session,Server server) {
super(context);
this.setContentView(R.layout.overlay_logs);
mListView=(ListView) this.findViewById(R.id.log_list);
mAdapter = new LogListAdapter(context, new ArrayList<LogUnit>(), new String[] {});
mListView.setDivider(new GradientDrawable(Orientation.RIGHT_LEFT, colors));
mListView.setDividerHeight(1);
mListView.setAdapter(mAdapter);
mListView.setOnScrollListener(new OnScrollListener() {
#Override
public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
....
}
}
}
#Override
public void onScrollStateChanged(AbsListView view, int scrollState) {
// TODO Auto-generated method stub
}
});
}

Categories