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.
Related
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()
}
Why do I always see the last id no matter which text I click?
I would like to see the right fragment displayed in the layout that i created according to the relevant text clicked. I always see the 'alternatives' fragment.
I understood that tags could be helped, but I could not figure out how to use them.
In addition, I tried to use diffrent versions of FragmentManager and FragmentTransaction and even to remove the switch and call each setOnClickListener of textview separately but nothing helped.
This is my Activity file:
IngredientsFragment ingredientsFragment;
FavouritesFragment favouritesFragment;
FeedbacksFragment feedbacksFragment;
Write_Feedback_Fragment writeFeedbackFragment;
AlternativesFragment alternativesFragment;
TextView ingredients;
TextView favourites;
TextView feedbacks;
TextView alternatives;
TextView writeFeedback;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_product_details);
window=this.getWindow();
window.setStatusBarColor(this.getResources().getColor(R.color.colorPrimaryDark));
ingredients= (TextView) findViewById(R.id.ingredients_option);
favourites= (TextView) findViewById(R.id.favorites_option);
feedbacks= (TextView) findViewById(R.id.watch_feedback_option);
writeFeedback= (TextView) findViewById(R.id.add_feedback_option);
alternatives= (TextView) findViewById(R.id.alternatives_option);
ingredients.setOnClickListener(this);
favourites.setOnClickListener(this);
feedbacks.setOnClickListener(this);
writeFeedback.setOnClickListener(this);
alternatives.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.ingredients_option:
ingredientsFragment= new IngredientsFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,ingredientsFragment).commit();
break;
case R.id.favorites_option:
favouritesFragment= new FavouritesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,favouritesFragment).commit();
break;
case R.id.watch_feedback_option:
feedbacksFragment= new FeedbacksFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,feedbacksFragment).commit();
break;
case R.id.add_feedback_option:
writeFeedbackFragment= new Write_Feedback_Fragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,writeFeedbackFragment).commit();
break;
case R.id.alternatives_option:
alternativesFragment= new AlternativesFragment();
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container,alternativesFragment).commit();
break;
}
}
}
This is my XML file:
<RelativeLayout 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:layout_height="match_parent"
tools:context=".ProductDetails">
<RelativeLayout
android:id="#+id/recommendation_template"
android:layout_width="match_parent"
android:layout_height="142dp"
android:background="#android:color/white">
<ImageView
android:id="#+id/registartion_arrow"
android:layout_width="45dp"
android:layout_height="34dp"
android:layout_alignParentRight="true"
android:src="#drawable/ic_arrow_forward_black_24dp" />
<TextView
android:id="#+id/rec_tv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="10dp"
android:text="Recommendation:"
android:textSize="25dp"
android:textStyle="bold"
android:textColor="#android:color/black"/>
<ImageView
android:id="#+id/vi_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/rec_tv"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="26dp"
android:paddingLeft="80dp"
android:src="#drawable/ic_check" />
<TextView
android:id="#+id/vi_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/vi_image"
android:layout_marginLeft="4dp"
android:layout_marginTop="-29dp"
android:layout_toRightOf="#id/vi_image"
android:text="This is for you"
android:textColor="#android:color/black"
android:textSize="20dp" />
<ImageView
android:id="#+id/not_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/vi_image"
android:layout_alignParentLeft="true"
android:layout_marginLeft="0dp"
android:layout_marginTop="3dp"
android:paddingLeft="80dp"
android:src="#drawable/ic_do_not" />
<TextView
android:id="#+id/not_tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/not_image"
android:layout_marginLeft="5dp"
android:layout_marginTop="-27dp"
android:layout_toRightOf="#id/not_image"
android:text="Not for you"
android:textColor="#android:color/black"
android:textSize="20dp" />
</RelativeLayout>
<HorizontalScrollView
android:id="#+id/recommendation_scrollView"
android:layout_width="match_parent"
android:layout_height="83dp"
android:layout_below="#id/recommendation_template"
android:layout_marginTop="7dp">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/recommendation_template"
style="?android:attr/borderlessButtonStyle">
<TextView
android:id="#+id/ingredients_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:drawableTop="#drawable/ingredients"
android:text="Ingredients" />
<TextView
android:id="#+id/favorites_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:drawableTop="#drawable/ic_star"
android:paddingLeft="80dp"
android:text="Favorites" />
<TextView
android:id="#+id/watch_feedback_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:drawableTop="#drawable/customer_review"
android:paddingLeft="145dp"
android:text="Feedbacks" />
<TextView
android:id="#+id/add_feedback_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:drawableTop="#drawable/write_feedback"
android:paddingLeft="220dp"
android:text="Write feedback" />
<TextView
android:id="#+id/alternatives_option"
android:layout_width="408dp"
android:layout_height="90dp"
android:layout_gravity="left"
android:drawableTop="#drawable/ic_alternatives"
android:paddingLeft="320dp"
android:text="Alternatives" />
</FrameLayout>
</HorizontalScrollView>
<ScrollView
android:id="#+id/scrollView_container"
android:layout_width="match_parent"
android:layout_height="499dp"
android:layout_below="#id/recommendation_scrollView">
<RelativeLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="463dp">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
Your code should work like that. The problem is your layout. See the textviews in your FrameLayout. Your defining a paddingLeft and layout_width so, that your textview alternatives_option is overlaying all the other textviews. Thats why in the onclick you always get the id of that view.
I suggest doing a tutorial about XML layout.
<TextView
android:id="#+id/alternatives_option"
android:layout_width="408dp"
android:layout_height="90dp"
android:layout_gravity="left"
android:drawableTop="#drawable/ic_alternatives"
android:paddingLeft="320dp"
android:text="Alternatives" />
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
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
I have a custom listview having the following layout:
I have defined an onClick method for 'qadd' button and 'qsub' button in xml using android:onClick property.
In my Activity class how do I select the 'quantity' textView for current position?
I tried this but it only updates 'quantity' textView at first position.
public void addClicked(View v) {
final int position = list.getPositionForView((View) v.getParent().getParent());
long rec_id = adapter.getItemId(position);
JSONObject o = (JSONObject) fullList.get(rec_id + "");
TextView q = (TextView)findViewById(R.id.quantity);
int quantity = Integer.parseInt((String) q.getText());
quantity++;
q.setText(quantity + "");
try {
String price = o.getString("price");
subtotal = subtotal + (Integer.parseInt(price) * quantity);
total_text = (TextView) findViewById(R.id.subtotal);
total_text.setText(subtotal + "");
// Log.d("position", price);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
The above code goes wrong at this line:
TextView q = (TextView)findViewById(R.id.quantity);
It always selects the textView at first position.
Please ask if any other piece of code is needed.
Entire custom_list_layout.xml is as follows:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:padding="5dip" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/checkBox1" >
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:background="#drawable/image_bg"
android:padding="3dip" >
<ImageView
android:id="#+id/item_img"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/ic_launcher" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="2.5"
android:orientation="vertical" >
<TextView
android:id="#+id/item_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Item name"
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
<TextView
android:id="#+id/item_desc"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Item description"
android:textColor="#343434"
android:textSize="10dip" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="0.75"
android:gravity="right"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="right"
android:orientation="horizontal" >
<TextView
android:id="#+id/rupee"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="right"
android:text="Rs. "
android:textColor="#EC1669"
android:textSize="10dip"
android:textStyle="bold" />
<TextView
android:id="#+id/item_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dip"
android:gravity="right"
android:text="0 "
android:textColor="#EC1669"
android:textSize="10dip"
android:textStyle="bold" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="bottom|right" >
<Button
android:id="#+id/qadd"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:minWidth="24dp"
android:onClick="addClicked"
android:text="+" />
<TextView
android:id="#+id/quantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0"
android:textAppearance="?android:attr/textAppearanceSmall" />
<Button
android:id="#+id/qsub"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="24dp"
android:minWidth="24dp"
android:onClick="subClicked"
android:text="-" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Link to my entire project.
Your textview to address should be the one within the list item row you specify in XML for the list adapter.
If you're using a custom adapter, within getView you should wire up the textview as follows:
// create a new TextView for each item referenced by the Adapter
public View getView(final int position, View convertView, final ViewGroup parent) {
View listView = convertView;
if (listView == null) { // if it's not recycled, initialize some attributes
// get layout from xml
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
listView = inflater.inflate(R.layout.browse_product_item, null);
}
final ListProductHolder holder;
if (listView.getTag() != null) {
holder = (listProductHolder) listView.getTag();
} else {
holder = new listProductHolder();
listView.setTag(holder);
holder.text= (TextView) listView.findViewById(R.id.quantity);
One you have the proper TextView from within each item in the listView, then it's a simple process to wire up an OnClickListener for that view and respond to those click events.