I am beginner to both Java and Android development, I am sure that I am doing this wrong; it’s just the way I've figured it out so far. That said, I have an app that will show something like a golf scorecard. There is a section (major row) for each of 4 players. Each player row comprises 3 sub-rows; one for Par (18 values in the row), one for Hole Handicap (18 values), one for Score (18 values). I want the user to click on a major row and have another activity appear based on the player named in the clicked row.
So, I created an XML layout with a single ListView. I created an XML layout for the page header with a GridView defining 12 columns each with a TextView for column headers, and I created an XML layout for the major row with a GridView comprising TextViews for 54 items (3 sub-rows of 18 items). I also added an onClick listener to the GridLayout declaration in the row def XML. Throw in a custom arrayAdapter to handle the 4 players and I get my scorecard on the screen.
Everything displays okay, and when I click a major row, I get to a debug breakpoint in the clickListener and I can fire off an Intent to kick off the activity showing player detail. But, I don’t know how to determine which player (major row) was clicked.
String item = ((TextView)view).getText().toString();
in the click handler crashes the app, I suppose because the clickListener was established on a GridLayout or some such thing.
So, what’s wrong with the setup and how can I get what I need to go forward?
Thanks
Here is relevant code. Sorry if it's a lot...
activity_display_scorecard.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#FFFFFF">
<ListView
android:id="#+id/listView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Here is activity_sisplay_scorecard_row.xml (I edited out columns 4-12)
<GridLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:useDefaultMargins="true"
android:alignmentMode="alignBounds"
android:columnOrderPreserved="false"
android:columnWidth="90dp"
android:columnCount="12"
android:id="#+id/SCRow"
android:onClick="displayScore"
>
<TextView
android:text=""
android:layout_column="0"
android:textSize="15sp"
android:layout_gravity="left"
/>
<TextView
android:text="Par"
android:layout_column="1"
android:textSize="15sp"
android:layout_gravity="right"
/>
<TextView
android:id="#+id/Par_Hole_1"
android:text="V_1"
android:textSize="15sp"
android:layout_gravity="center_horizontal"
/>
<!-- A bunch more for this row -->
<TextView
android:text=""
android:layout_column="0"
android:textSize="15sp"
android:layout_gravity="left"
/>
<TextView
android:text="HCP"
android:layout_column="1"
android:textSize="15sp"
android:layout_gravity="right"
/>
<TextView
android:id="#+id/HCP_Hole_1"
android:text="V_1"
android:textSize="15sp"
android:layout_gravity="center_horizontal"
/>
<!-- A bunch more for this row -->
<TextView
android:text=""
android:textSize="15sp"
android:layout_gravity="center_horizontal"
/>
<TextView
android:text="Name"
android:id="#+id/Row_Name"
android:layout_column="0"
android:textSize="15sp"
android:layout_gravity="left"
/>
<TextView
android:text="Bet"
android:layout_column="1"
android:textSize="15sp"
android:layout_gravity="right"
/>
<TextView
android:id="#+id/Bet_Hole_1"
android:text="B1"
android:textSize="15sp"
android:layout_gravity="center_horizontal"
/>
<!-- A bunch more for this row -->
<TextView
android:text=""
android:layout_column="0"
android:textSize="15sp"
android:layout_gravity="left"
/>
<TextView
android:text="Score"
android:layout_column="1"
android:textSize="15sp"
android:layout_gravity="right"
/>
<TextView
android:id="#+id/Score_Hole_1"
android:text="V_1"
android:textSize="15sp"
android:layout_gravity="center_horizontal"
/>
<!-- A bunch more for this row -->
</GridLayout>
and here is the activity DisplayScorecard
public class DisplayScorecard extends Activity {
private ListView listView1;
#SuppressLint("NewApi")
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_scorecard);
CardRow scoreCard_data[] = new CardRow[]
{
new CardRow(0),
new CardRow(1),
new CardRow(2),
new CardRow(3)
};
CardRowAdapter adapter = new CardRowAdapter(this,
R.layout.activity_display_scorecard_row, scoreCard_data);
listView1 = (ListView)findViewById(R.id.listView1);
View header = (View)getLayoutInflater().inflate(R.layout.activity_display_scorecard_header, null);
listView1.addHeaderView(header);
listView1.setAdapter(adapter);
}
/** Called when the user Selects a scorecard row */
public void displayScore(View view) {
String item = ((TextView)view).getText().toString();
Toast.makeText(getApplicationContext(), "ABC" + item, Toast.LENGTH_LONG).show();
Intent intent = new Intent(this, DisplayScore.class);
startActivity(intent);
}
}
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()
}
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.
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
okay, i cannot post images yet :/
Basically, i have an application with three edittext fields, three check boxes, a calculation button and a text field to output the answer. i want the program to scan to see which value is missing, so that from this it know which calculation it should do. Another method would be for the user to check one of the checkbox's and then the program would only run the calculation for the corresponding checkbox.
for instance, if the user inputs Q = 10, T = 2, and checks the checkbox for I, the program would then do the math and display I = 20 ?
this make much sense yet
i spose in psuedo code it would look like
" if checkbox one is checked and values for Q and T have been entered, then calculate value for I and display in answers field" however i will be adding some error checks and validation methods in there.
if you need the xml file and java file then please ask, im just looking for a method at the moment as to how i could do it, i wouldnt expect you all to waste your free time writing a whole program for me.
cheers guys and girls
<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="match_parent"
android:background="#drawable/background1"
android:gravity="fill"
android:orientation="vertical"
android:textColor="#ffffff"
tools:context=".CurrentPage" >
<!--
The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc.
-->
<!--
This FrameLayout insets its children based on system windows using
android:fitsSystemWindows.
-->
<TextView
android:id="#+id/Current_hint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginTop="33dp"
android:text="Welcome to the Current Calculation page."Hint - Use the check boxes to find the values that are missing "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/Equation_letter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/current_calculate"
android:layout_marginTop="37dp"
android:layout_toLeftOf="#+id/current_calculate"
android:text=" HELLO "
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/Distances_answer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Equation_letter"
android:layout_alignBottom="#+id/Equation_letter"
android:layout_alignLeft="#+id/current_calculate"
android:layout_alignParentRight="true"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
<TextView
android:id="#+id/t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Q"
android:layout_alignRight="#+id/Q"
android:layout_below="#+id/Q"
android:layout_marginTop="36dp"
android:gravity="right|top"
android:text="t ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<CheckBox
android:id="#+id/custom3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/t"
android:layout_alignLeft="#+id/custom2"
android:background="#drawable/check_box_new"
android:button="#drawable/check_box_new" />
<CheckBox
android:id="#+id/custom1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/number_input_3"
android:layout_alignLeft="#+id/custom3"
android:background="#drawable/check_box_new"
android:button="#drawable/check_box_new" />
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Current_hint"
android:layout_alignRight="#+id/Current_hint"
android:layout_below="#+id/Equation_letter"
android:layout_marginTop="25dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/CurrentmainHelp"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.31"
android:text="HELP! This equation can be used to calculate the current of an object in amps. The correct equation to discover amps is I=Q/t. Where I = current, Q = charge flowing past a point in the circuit, and t = time taken for the charge to flow. Please note, Q is measure in coulombs and t is measured in seconds, with I being measured in amperes.
Still need more help? "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textSize="18sp"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/Yes_Please"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="36dp"
android:layout_weight="1.31"
android:onClick="CurrentHelp"
android:text="Yes Please"
android:textColor="#ffffff" />
</LinearLayout>
</ScrollView>
<Button
android:id="#+id/current_calculate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/Calculate_Current"
android:textColor="#ffffff"
/>
<EditText
android:id="#+id/number_input_2"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_above="#+id/t"
android:layout_alignLeft="#+id/number_input_1"
android:layout_alignRight="#+id/number_input_1"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:textColor="#ffffff" >
<requestFocus />
</EditText>
<EditText
android:id="#+id/number_input_3"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/t"
android:layout_alignLeft="#+id/number_input_2"
android:layout_alignRight="#+id/number_input_2"
android:ems="10"
android:focusable="true"
android:inputType="numberDecimal"
android:textColor="#ffffff" />
<CheckBox
android:id="#+id/custom2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/number_input_1"
android:layout_marginLeft="45dp"
android:layout_toRightOf="#+id/number_input_1"
android:background="#drawable/check_box_new"
android:button="#drawable/check_box_new" />
<EditText
android:id="#+id/number_input_1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_above="#+id/Q"
android:layout_alignRight="#+id/current_calculate"
android:ems="10"
android:focusable="false"
android:inputType="numberDecimal"
android:singleLine="true"
android:textColor="#ffffff" />
<TextView
android:id="#+id/Q"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Equation_letter"
android:layout_below="#+id/I"
android:layout_marginTop="36dp"
android:gravity="right|top"
android:text="Q ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/I"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Q"
android:layout_alignRight="#+id/Q"
android:layout_below="#+id/Current_hint"
android:layout_marginTop="65dp"
android:gravity="right|top"
android:text="I ="
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff"
tools:ignore="HardcodedText" />
okay, that was the xml, and this is the java so far
package com.kieran.whetherfieldphysicscalculator;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
// i=q/t this will be the first calculation on the list
public class Current extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_current);
// Show the Up button in the action bar.
setupActionBar();
Button calc1 = (Button)findViewById(R.id.current_calculate);
calc1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText Charge1 = (EditText)findViewById(R.id.number_input_2);
EditText Time1 = (EditText)findViewById(R.id.number_input_3);
TextView Distances_answer = (TextView)findViewById(R.id.Distances_answer);
double charge = Double.parseDouble(Charge1.getText().toString());
double time = Double.parseDouble(Time1.getText().toString());
//Time is a class in Java
Distances_answer.setText("" +charge*time);
}
});
}
public void CurrentHelp(View view) {
// Do something in response to button
Intent intent = new Intent(this, CurrentHelp.class);
startActivity(intent);
}
/**
* Set up the {#link android.app.ActionBar}.
*/
private void setupActionBar() {
getActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
// This ID represents the Home or Up button. In the case of this
// activity, the Up button is shown. Use NavUtils to allow users
// to navigate up one level in the application structure. For
// more details, see the Navigation pattern on Android Design:
//
// http://developer.android.com/design/patterns/navigation.html#up-vs-back
//
NavUtils.navigateUpFromSameTask(this);
return true;
}
return super.onOptionsItemSelected(item);
}
}
I am asking if there is an easier way to formate the program to find the values, rather than have to code each individual segment?
cheers
In your case I guess that you want the user to check only one checkbox and only answer corresponding to that checkbox should be displayed to the user.
so for that you would need to set OnClickListener on checkbox so that the corresponding operation is selected.
Read here on how to set listener on checkbox and use it. Paticularly look for the addListenerOnChkIos() method as it has the code you want.
Also set another OnClickListener on the calculate button and in that check which checkbox is checked depending on which you will perform the operation and update the textview.