I have recently started working with Android. I don't have that much experience with Android. This is my first time working with Android. I need to divide the screen in two equal half and then perform few tasks.
After dividing the screen in two equal half, In second half (means bottom half) I need to make a Label named Latitude then corresponding to that a Textbox that will show the current latitude values and another label named Longitude and corresponding to that another Textbox and that will show current longitude value.
I started working on this, And I made some progress- Below is my XML file that I am using currently to divide the screen in two equal half.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1.8"
android:background="#000000" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#000000" >
<!-- Latitude Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Latitude"
android:textColor="#372c24" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:singleLine="true" />
<!-- Longitude Label -->
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Longitude"
android:textColor="#372c24" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:password="true"
android:singleLine="true" />
</LinearLayout>
</LinearLayout>
My Question is-
After using the above XML file, I am not able to see my both label and both textbox properly on the Android Screen, Is there anything I am missing here in my XML file?
Secondly, how I can show the current latitude and longitude value in the corresponding textbox.
Any help will be appreciated as I am new to Android world.
Update:- Below is the diagram that I made in a paint, I want something Like that.
If you look at the screen, it got divided in two parts, and in the bottom half part, there are two Labels (Latitude and Longitude) and it should be like that, and infront of each label, there will be a textbox that will hold the corresponding latitude and longitude values.
Update Code:- For Current Location-
public class MainActivity extends Activity implements LocationListener {
private TextView latituteField;
private TextView longitudeField;
private LocationManager locationManager;
private String provider;
private static final String TAG = "CurrentLocation";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
latituteField = (TextView) findViewById(R.id.lat1);
longitudeField = (TextView) findViewById(R.id.lat2);
// Get the location manager
locationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
// Define the criteria how to select the location provider -> use
// default
Criteria criteria = new Criteria();
provider = locationManager.getBestProvider(criteria, false);
locationManager.requestLocationUpdates(provider, 0, 0, this);
Location location = locationManager.getLastKnownLocation(provider);
onLocationChanged(location);
}
#Override
protected void onDestroy() {
super.onDestroy();
locationManager.removeUpdates(this);
}
#Override
public void onLocationChanged(Location location) {
if (location != null) {
System.out.println("Provider " + provider + " has been selected.");
int lat = (int) (location.getLatitude());
int lng = (int) (location.getLongitude());
Log.v(TAG, lat+ " "+ lng);
latituteField.setText(String.valueOf(lat));
longitudeField.setText(String.valueOf(lng));
} else {
latituteField.setText("Provider not available");
longitudeField.setText("Provider not available");
}
}
#Override
public void onProviderDisabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onProviderEnabled(String provider) {
// TODO Auto-generated method stub
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras) {
// TODO Auto-generated method stub
}
}
But the below code is not showing any latitude or longitude values on the corresponding text box? ANything wrong I am doing?
"After using the above XML file, I am not able to see my both label and
both textbox properly on the Android Screen, Is there anything I am
missing here in my XML file?"
It appeared, you may have had your LinearLayout tags messed up.
See if this is closer to what you are looking for:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#android:color/black" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<!-- currently empty -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<!-- Latitude Label -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Latitude"
android:textColor="#372c24" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_marginTop="5dip"
android:layout_weight="0.35"
android:singleLine="true"
android:text="test1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" >
<!-- Longitude Label -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Longitude"
android:textColor="#372c24" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dip"
android:layout_weight="0.35"
android:password="true"
android:singleLine="true"
android:text="test2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
Picture
"Secondly, how I can show the current latitude and longitude value in
the corresponding textbox."
What have you attempted?
You need to get the current lat/long.
Then just set the text on the EditText.
Related
I recently made a small app that should change the background color & text of the application when a certain amount of money is generated (here 100000). However, whenever I go past 10000, the application crashes. I am not sure why and can not get around it. I saw that this problem is with background color only because it is otherwise displaying text correctly.
Here is the XML for activity_main
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout 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:id="#+id/Trial"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#EC5353"
tools:context=".MainActivity">
<TextView
android:id="#+id/Title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="92dp"
android:gravity="center"
android:text="#string/Title"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/money"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:text="#string/moneyPresent"
android:textSize="22sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/Title" />
<Button
android:id="#+id/moneyGenerator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="72dp"
android:background="#FFFFFF"
android:backgroundTint="#732424"
android:backgroundTintMode="multiply"
android:text="#string/generateMoneyButton"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/money" />
<TextView
android:id="#+id/richTag"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:text="#string/grind_boi"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/moneyGenerator" />
</androidx.constraintlayout.widget.ConstraintLayout>
Here is my Java Code (Omitted the packages as it would take a lot of space. Do tell me if anyone wants to read it.)
public class MainActivity extends AppCompatActivity {
private Button makeMoney;
private int totalMoney=0;
private TextView moneyText;
private TextView richAlert;
private ConstraintLayout background;
NumberFormat currency=NumberFormat.getCurrencyInstance(new Locale("en","in"));
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
makeMoney.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
totalMoney+=1000;
moneyText.setText(currency.format(totalMoney));
Log.d("MoneyTag", "Money Made: "+totalMoney);
Toast.makeText(getApplicationContext(),"Total Money= \u20B9"+totalMoney,Toast.LENGTH_LONG)
.show();
if(totalMoney>=10000) {
richAlert.setText("Welcome to moneyland bro 😁");
background.setBackgroundColor(16776960);
}
}
});
}
}
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
You're finding all the views, except background(ConstraintLayout), that's why it is throwing NullPointerException (Most likely, because you haven't attached logs).
You need to find the layout first
background = findViewById(R.id.Trial);
in onCreate.
you haven't defined "background" view in the above.
to use .setBackgroundColor you need to use it with some view
try making object of layout view then change the color of that layout
mConstraintLayout =
(ConstraintLayout)findViewById(R.id.Trial);
makeMoney = findViewById(R.id.moneyGenerator);
moneyText=findViewById(R.id.money);
richAlert=findViewById(R.id.richTag);
if(totalMoney>=10000) {
richAlert.setText("Welcome to moneyland bro 😁");
mConstraintLayout.setBackgroundColor(ContextCompat.getColor(this, R.color.yellow));
background = findViewById(R.id.Trial);
just find layout and null pointer excpetion will removed
I have tried to search in android developers but I had no luck.
My onClick method does not print any message. I want to change my text from ... to ...; however nothing happens when I click it.
JAVA
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the Cookie button is clicked.
*/
public void eatCookie(View view) {
display("I'm so full");
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(String status) {
TextView statusTextView = (TextView) findViewById(
R.id.status_text_view);
statusTextView.setText("" + status);
}
}
XML
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#B388FF"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="#+id/android_cookie_image_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="#drawable/before_cookie" />
<TextView
android:id="#+id/status_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="I'm so hungry"
android:textColor="#android:color/white"
android:textSize="34sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="EAT COOKIE"
android:onClick="eatCookie"/>
</LinearLayout>
Your code is working correct in my device
Try to Clean your project and re run the code again.
Build -> Clean
and then run the code
Context
I am writing an app for Android, and I use OSMBonusPack to easily display markers.
I display a marker to show the user's position, got with the phone GPS, and I add a FolderOverlay to group other markers (these indicate POIs from Wikipedia, retrieved with a custom API).
I included the bonuspack_bubble.xml layout file in my projet, because I wanted to modify it to scale the "More info" button properly.
Problem
The problem is that the POI markers are correctly added on the map (they show the icon, and when tapped, the Info bubble shows, filled with the title and description), but the first marker indicating the user's position displays an empty Info bubble.
I have tried to remove the FolderOverlay, I tried to add the starterMarker to the same overlay as the POIs, I tried to only display the starterMarker... Nothing works.
What am I doing wrong? If you need clarification on some points or code, feel free to ask!
Thanks!
Note: using osmbonuspack v5.3 (AAR), osmdroid 4.3, Android Studio 1.2.2, and testing on a Galaxy S4 with Android 4.2.2
Code:
Function in main Fragment
public void drawMap(Location location, MapView map, LocationManager locationManager, LocationListener locationListener) {
...
final GeoPoint startPoint = new GeoPoint(location.getLatitude(), location.getLongitude());
// Now we add a marker using osmBonusPack
Marker startMarker = new Marker(map);
startMarker.setPosition(startPoint);
startMarker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
map.getOverlays().add(startMarker);
// We can change some properties of the marker (don't forget to refresh the map !!)
startMarker.setInfoWindow(new CustomInfoWindow(map));
startMarker.setIcon(ContextCompat.getDrawable(getActivity(), R.drawable.ic_place));
startMarker.setTitle(getString(R.string.you_are_here));
map.invalidate();
...
// We create an Overlay Folder to store every POI, so that they are grouped in clusters
// if there are too many of them
RadiusMarkerClusterer poiMarkers = new RadiusMarkerClusterer(getActivity());
Drawable clusterIconD = ContextCompat.getDrawable(getActivity(), R.drawable.marker_cluster);
Bitmap clusterIcon = ((BitmapDrawable)clusterIconD).getBitmap();
poiMarkers.setIcon(clusterIcon);
map.getOverlays().add(poiMarkers);
// poiList is an ArrayList of custom POIs
for (POI poi:poiList) {
double mLat = poi.getLatitude();
double mLong = poi.getLongitude();
GeoPoint poiWaypoint = new GeoPoint(mLat, mLong);
Marker marker = new Marker(map);
marker.setPosition(poiWaypoint);
marker.setAnchor(Marker.ANCHOR_CENTER, Marker.ANCHOR_BOTTOM);
marker.setRelatedObject(poi);
marker.setInfoWindow(new CustomInfoWindow(map));
marker.setTitle(poi.getName());
marker.setSnippet(poi.getSitelink());
Drawable icon = ContextCompat.getDrawable(getActivity(), R.drawable.ic_place);
marker.setIcon(icon);
poiMarkers.add(marker);
}
map.invalidate();
}
CustomInfoWindow.java
public class CustomInfoWindow extends MarkerInfoWindow {
private POI mSelectedPoi;
public CustomInfoWindow(MapView mapView) {
super(R.layout.bonuspack_bubble, mapView);
Button btn = (Button) (mView.findViewById(R.id.bubble_moreinfo));
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
if (mSelectedPoi.getSitelink() != null) {
Intent myIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(mSelectedPoi.getSitelink()));
view.getContext().startActivity(myIntent);
}
}
});
}
#Override
public void onOpen(Object item){
super.onOpen(item);
mView.findViewById(R.id.bubble_moreinfo).setVisibility(View.VISIBLE);
Marker marker = (Marker)item;
mSelectedPoi = (POI)marker.getRelatedObject();
}
}
POI.java
public class POI {
// We define every variable returned by the WikiJourney API
private double latitude;
private double longitude;
private String name;
private String sitelink;
private String type_name;
private int type_id;
private int id;
public POI(...) { }
}
_bonuspack_bubble.xml_
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/bonuspack_bubble" >
<ImageView android:id="#+id/bubble_image"
android:layout_width="65dp"
android:layout_height="65dp"
android:visibility="gone" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="5dp"
android:paddingEnd="5dp"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView android:id="#+id/bubble_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:maxEms="17"
android:layout_gravity="start"
android:layout_weight="1"
android:text="Title" />
<Button android:id="#+id/bubble_moreinfo"
android:background="#drawable/btn_moreinfo"
android:visibility="gone"
android:layout_width="25sp"
android:layout_height="25sp"
android:layout_gravity="end"
android:layout_weight="0" />
</LinearLayout>
<TextView android:id="#+id/bubble_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="12sp"
android:maxEms="17"
android:text="Description" />
<TextView android:id="#+id/bubble_subdescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="10sp"
android:maxEms="17"
android:text="Address"
android:visibility="gone" />
</LinearLayout>
</LinearLayout>
What is really surprising is that your app doesn't crash:
You set your CustomInfoWindow to the startMarker, without setting any related POI object.
But in CustomInfoWindow.onOpen, you get the related object, and use it without testing if it's not null - and if it's really a POI.
This can not work.
Either define a "StartInfoWindow" class, or ensure your CustomInfoWindow fully supports your startMarker.
A hint: share the same CustomInfoWindow object between all your POI markers. It is faster, and better for memory.
I target my app to work with API 11 or above. In Activity A, I use the following code lines to capture and send TEXT in Clipboard to Activity B (which is the previous activity).
onCreate:
wvContent = (WebView) findViewById(R.id.wvContent);
LinearLayout ll= (LinearLayout)findViewById(R.id.layout_view);
webkit=new Webkit(this);
wvContent.addView(webkit, 0,0);
WebviewSelectedText webkitSelectedText=new WebviewSelectedText(getApplicationContext(), webkit);
webkitSelectedText.init();
ll.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
And the copied text to trigger Activity B (back to previous activity with text from clipboard):
public class WebviewSelectedText
{
private Context context;
Webkit webkit;
private final String have_word="have_word";
public WebviewSelectedText(Context context,Webkit webkit)
{
this.context=context;
this.webkit=webkit;
}
#SuppressWarnings("deprecation")
public void init()
{
final ClipboardManager clipboardManager = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE);
clipboardManager.setText(null);
setOnSelectTextListener(clipboardManager, webkit);
}
private void setOnSelectTextListener(#SuppressWarnings("deprecation") final ClipboardManager clipboardManager, final Webkit webkit) {
webkit.setOnSelectTextListener(new OnSelectTextListener() {
#SuppressWarnings("deprecation")
public void onSelectText() {
String text = clipboardManager.getText().toString();
text = text.toLowerCase();
//Remove all non-word elements starting and/or ending a string
String strippedInput = text.replaceAll("^\\W+|\\W+$", "");
System.out.println("Stripped string: " + strippedInput);
//receivedText = txtView.getText().toString().toLowerCase();
if(strippedInput!=null)
{
Intent intent=new Intent(context,ActivityB);
intent.putExtra(have_word, strippedInput);
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP|Intent.FLAG_ACTIVITY_NEW_TASK|Intent.FLAG_ACTIVITY_BROUGHT_TO_FRONT);
ActivityA.this.startActivity(intent);
}
}
});
}
Everything is just fine on Emulator, i.e., text is copied and then Activity B is called with the text from Clipboard.
HOWEVER, on my real device (an HTC and a Tablet), Activity B with the copied text does not automatically start until a soft button is touched (any button: back, menu...).
And FYI, here is my layout:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layout_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical"
android:layout_weight="1">
<WebView
android:id="#+id/wvContent"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:layout_gravity="top"
android:layout_weight="1"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal"
>
<ImageButton
android:id="#+id/btnHome"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/home"
android:layout_weight="0.25"
/>
<ImageButton
android:id="#+id/btnPronounce"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/pronounce"
android:layout_weight="0.25"
/>
<ImageButton
android:id="#+id/btnShowHistory"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/history"
android:layout_weight="0.25"
/>
<ImageButton
android:id="#+id/btnAddFavourite"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:src="#drawable/add_favourite"
android:layout_weight="0.25"
/>
</LinearLayout>
I wonder if you can have a look at my code and tell me what the problem is. Thank you very much.
Good day everyone,
I'm learning Android development and I try to build some dynamic lists for my application. And I'm stuck... like for 4 hours already.
I have two layouts:
1. main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/action_buttons"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:gravity="center"
>
<Button
android:id="#+id/button_count"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_calculate" />
<Button
android:id="#+id/button_add"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/button_count"
android:text="#string/button_add" />
</LinearLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/action_buttons"
>
<LinearLayout
android:id="#+id/action_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
</RelativeLayout>
action_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/action_list_item_root"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/action_list_item_edittext_drinkName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/action_list_item_button_remove"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/action_list_item_title"
android:padding="10dp" />
<EditText
android:id="#+id/action_list_item_edittext_drinkPercent"
android:text="40"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/action_list_item_edittext_drinkAmount"
android:inputType="number"
android:padding="10dp"/>
<EditText
android:id="#+id/action_list_item_edittext_drinkAmount"
android:text="0.0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/action_list_item_button_remove"
android:layout_alignBottom="#+id/action_list_item_button_remove"
android:inputType="numberDecimal"
android:width="50dp"
android:padding="10dp" />
<Button
android:id="#+id/action_list_item_button_remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="#string/action_list_item_button_remove" />
And code that creates dynamic list:
public class MainActivity extends Activity {
LinearLayout actionList = null;
private Button btnAdd;
private Button btnCalculate;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
initActivityElements();
}
private void initActivityElements() {
initAddButton();
initCalculateButton();
}
private void initCalculateButton() {
btnCalculate = (Button) findViewById(R.id.button_count);
btnCalculate.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
TextView amount = (TextView) findViewById(R.id.action_list_item_edittext_drinkAmount);
//this retrives TextView value from first list
Toast.makeText(getApplicationContext(), amount.getText().toString(),
Toast.LENGTH_SHORT)
.show();
}
});
}
private void initAddButton() {
actionList = (LinearLayout) findViewById(R.id.action_list);
btnAdd = (Button) findViewById(R.id.button_add);
btnAdd.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout listItem = (RelativeLayout) View.inflate(
MainActivity.this, R.layout.action_list_item, null);
TextView name = (TextView) listItem
.findViewById(R.id.action_list_item_edittext_drinkName);
name.setText("Here is the Title " + actionList.getChildCount());
listItem.findViewById(R.id.action_list_item_button_remove)
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
actionList.removeView((View) v.getParent());
}
});
actionList.addView(listItem);
}
});
}
My problem is that I need to get all data from TextView boxes (Amount, Percent), but I can retrive that data only from first item of a list (look at onClickListener for btnCalculate) and can't figure out how to do it, but tried to add 'unique ids' for view (but that brakes layout, badly), tried setting Tags but again with no luck.
Maybe anyone can give a tip? I bet there is some easy way to do it, but I'm failing to find it and google is no help here.
Thanks
I'm not really sure what exactly you are trying to do but it sounds like you are trying to take in information and populate it into a view. I would recommend having a static text box that takes in your information and builds that into a listview. A good example of how to do that is located here http://developer.android.com/resources/tutorials/notepad/notepad-ex1.html.