Problem with ImageButton in Android Studio - java

I try to create two ImageButton in an activity but I can only see one of these. Someone can help me?
activity_ristorante.xml :
?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Ristoranti">
<ImageButton
android:id="#+id/b2"
style="#style/Widget.AppCompat.ImageButton"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="232dp"
android:layout_marginTop="50dp"
android:layout_marginEnd="30dp"
android:visibility="visible"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageButton
android:id="#+id/b1"
style="#style/Widget.AppCompat.ImageButton"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_marginStart="30dp"
android:layout_marginTop="50dp"
android:visibility="visible"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
Class:
public class Ristoranti extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageButton b1 = findViewById(R.id.b1);
ImageButton b2 = findViewById(R.id.b2);
b1.setBackgroundResource(R.drawable.putia) ;
b2.setBackgroundResource(R.drawable.pos);
b1.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Uri uri = Uri.parse("https://maps.app.goo.gl/FoRGA2CbJdrV5cfT8");
Intent in = new Intent(Intent.ACTION_VIEW, uri);
startActivity(in);
}
});
what's errors? I can't see the button even though it's set to "visible" within android studioCan you help me? thank you

Just change setContentView(R.layout.activity_main); to setContentView(R.layout.activity_ristorante); or copy your XML code from activity_ristorante.xml to activity_main.xml

Related

OnClickListener error: "[..] invoke virtual method [...] on a null object reference", athough string declared

I'm simply trying to use 2 buttons in my MainActivity. One opens a website (which works), the other should directly jump into my second class (which for simplification here, I only set as a second website). However, although button setup is similar to other button, using setOnClickListener as well as variable defined in resources, I'm receiving the error message below.
I already excluded several fragments - the error message seems to be concerning the button for accessing the 2nd class.
Apologies for getting lost with those trivialities...I'm lost but grateful for any hint.
java.lang.RuntimeException: Unable to start activity ComponentInfo{paperpad.app/paperpad.app.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
strings.xml
<resources>
<string name="logo">Logo</string>
<string name="app_name">PaperPad2</string>
<string name="button_go2activity">Start Drawing</string>
<string name="button_openPicture">Load Picture</string>
<string name="button_closePicture">Stop</string>
<string name="button_help">help</string>
<string name="websiteAddress">PaperPad.app</string>
</resources>
MainActivity.java
package paperpad.app;
import androidx.annotation.NonNull;
//etc.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button_openHelp = findViewById(R.id.button_openHelp);
button_openHelp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openHelp = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com"));
startActivity(openHelp);
}
});
Button button_go2activity = findViewById(R.id.button_go2activity);
button_go2activity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openLink = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(openLink);
}
});
}
}
my activity_main.xml
<?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/parentLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
tools:context=".MainActivity">
<ImageView
android:id="#+id/imageView"
android:layout_width="254dp"
android:layout_height="237dp"
android:layout_marginTop="48dp"
android:layout_marginBottom="8dp"
android:contentDescription="#string/logo"
app:layout_constraintBottom_toTopOf="#+id/imageView2"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo_paperpad" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="59dp"
android:layout_marginTop="96dp"
android:layout_marginBottom="100dp"
app:layout_constraintBottom_toTopOf="#+id/button_openPicture"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView"
app:srcCompat="#drawable/logo_titel"
tools:ignore="ContentDescription" />
<Button
android:id="#+id/button_go2activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="269dp"
android:text="#string/button_go2activity"
android:textSize="24sp"
android:typeface="normal"
app:layout_constraintBottom_toTopOf="#+id/button_openHelp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2" />
<Button
android:id="#+id/button_openHelp"
android:layout_width="45dp"
android:layout_height="25dp"
android:layout_marginBottom="10dp"
android:background="#FFFFFF"
android:text="#string/button_help"
android:textColor="#android:color/black"
android:textSize="12sp"
app:layout_constraintBottom_toTopOf="#+id/linkWebsite"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_go2activity" />
<TextView
android:id="#+id/linkWebsite"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:inputType="none"
android:text="#string/websiteAddress"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button_openHelp" />
</androidx.constraintlayout.widget.ConstraintLayout>
Your button is not in the activity_main layout xml (currently). If in a fragment, it has not been loaded yet.
If it's defined in a fragment you should move this code to the fragment.
Try to declare Buttons on class level and initialize them later:
public class MainActivity extends AppCompatActivity {
Button button_openHelp;
Button button_go2activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_openHelp = findViewById(R.id.button_openHelp);
button_openHelp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openHelp = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.youtube.com"));
startActivity(openHelp);
}
});
button_go2activity = findViewById(R.id.button_go2activity);
button_go2activity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent openLink = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(openLink);
}
});
}
}

Problem with locating button on map (on same activity) in Android

I'm writing an application in Android where background is a map and I want to put on this background some buttons like "Search", "Login".
I have created xml like below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<Button
android:id="#+id/buttonLogReg"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="top"
android:layout_weight="10"
android:baselineAligned="false"
android:text="#string/log_amp_reg"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0" />
<org.osmdroid.views.MapView
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
tools:ignore="MissingConstraints" >
</org.osmdroid.views.MapView>
<org.osmdroid.views.MapView
android:id="#+id/mapview"
tilesource="Mapnik"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
and also Java code like:
public class MainActivity extends Activity {
MapView map = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Context ctx = getApplicationContext();
Configuration.getInstance().load(ctx, PreferenceManager.getDefaultSharedPreferences(ctx));
setContentView(R.layout.activity_main);
map = (MapView)findViewById(R.id.map);
map.setTileSource(TileSourceFactory.MAPNIK);
map.setBuiltInZoomControls(true);
map.setMultiTouchControls(true);
IMapController mapController = map.getController();
mapController.setZoom(12.5);
GeoPoint startPoint = new GeoPoint(49.20, 19.94);
mapController.setCenter(startPoint);
Button logreg = findViewById(R.id.buttonLogReg);
logreg.setOnClickListener(onClickListener);
}
public void onResume(){
super.onResume();
map.onResume();
}
public void onPause(){
super.onPause();
map.onPause();
}
private View.OnClickListener onClickListener = new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(), LoginRegister.class);
startActivity(intent);
}
};
}
So my question is:
Why I can't see the button on android screen, I see only moveable map?
Anybody could help me with this issue?
There are two problem maps in the map sizes. You can edit them.......
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"
android:orientation="vertical"
android:gravity="center"
tools:context=".MainActivity">
<Button
android:id="#+id/buttonLogReg"
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_gravity="top"
android:layout_weight="10"
android:text="#string/log_amp_reg" />
<org.osmdroid.views.MapView
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="100dp"
tools:ignore="MissingConstraints" >
</org.osmdroid.views.MapView>

How to pass information between activities using rating bar

Ok, so im pretty new to android and the whole app making but im just doing like a generic app rating thing just for practice. Here are the details I have just 2 generic pictures on the main activity with a rate button beside each and a text view below them both so when you click the rate button it takes you to the second activity which has the rating bar. This is my problem in the view with the rating bar I just have the display for the rating as a toast but i want to be able to take that rating and display it inside the textview in my main activity and its posing more of a problem than i thought it was going to so im looking for a bit of help
this is the code for my main activity
package com.example.brent.appmanagement;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ratingapp1();
ratingapp2();
getrating();
}
private void getrating(){
}
private void ratingapp1(){
Button rateapp1 = findViewById(R.id.rateapp1);
rateapp1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, firstapp.class));
}
});
}
private void ratingapp2(){
Button rateapp2 = findViewById(R.id.rateapp2);
rateapp2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this, secondapp.class));
}
});
}
}
and this is the code for the activity with the rating bar
public class firstapp extends AppCompatActivity {
public RatingBar ratingBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_firstapp);
ratingBar = findViewById(R.id.ratingBar);
return1();
}
public void rateMe(View view){
Toast.makeText(getApplicationContext(),
String.valueOf(ratingBar.getRating()),
Toast.LENGTH_LONG).show();
}
private void return1(){
Button returntomain1 = findViewById(R.id.returntomain1);
returntomain1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(firstapp.this, MainActivity.class));
}
});
}
}
also I have the XML which i dont know if you need to see but here it is
main activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.brent.appmanagement.MainActivity">
<Button
android:id="#+id/rateapp1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="96dp"
android:layout_marginTop="124dp"
android:text="Rate"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/rateapp2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="176dp"
android:layout_marginEnd="96dp"
android:text="Rate"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginTop="112dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#mipmap/ic_launcher" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="164dp"
android:layout_marginStart="96dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:srcCompat="#mipmap/ic_launcher" />
<TextView
android:id="#+id/savedrating1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginTop="12dp"
android:text=""
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView3" />
<TextView
android:id="#+id/savedrating2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="96dp"
android:layout_marginTop="12dp"
android:text=""
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView4" />
</android.support.constraint.ConstraintLayout>
and the second activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.brent.appmanagement.firstapp">
<Button
android:id="#+id/returntomain1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginStart="8dp"
android:text="Return to main menu"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent" />
<TextView
android:id="#+id/rate_me"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:text="Rate Me"
android:textSize="18dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<RatingBar
android:id="#+id/ratingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/rate_me"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:numStars="5"
android:stepSize="1.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rate_me" />
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ratingBar"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:onClick="rateMe"
android:text="Submit"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/ratingBar" />
</android.support.constraint.ConstraintLayout>
I would like to have the rating from the rating bar inside firstactivity be displayed in the textview savedrating1 inside my main activity
thanks all in advance for the help
for primitive values you can stuff them inside the intent using key value pairsIntent intent =new Intent(); String myValue="something";intent.putExtra("keyMyValue",myValue);startActivity(intent); for passing custom objects use parcelable and stuff the parcelable into the intent as above or here

View Flipper that uses Only One ImageView

I'm new to Android, and am currently using a ViewFlipper. I'd like to know if it's possible to use only one ImageView? That way I can just create an Arraylist of my images. I think using multiple ImageViews is not a good practice since I have 50+ images.
public void initContent() {
imageArrayList = new ArrayList<Integer>();
imageArrayList.add(R.drawable.pig2);
imageArrayList.add(R.drawable.pig3);
imageArrayList.add(R.drawable.pig4);
imageArrayList.add(R.drawable.pig5);
imageArrayList.add(R.drawable.pig6);
imageArrayList.add(R.drawable.pig7);
imageArrayList.add(R.drawable.pig8);
imageArrayList.add(R.drawable.pig9);
imageArrayList.add(R.drawable.pig10);
imageArrayList.add(R.drawable.pig11);
imageArrayList.add(R.drawable.pig12);
imageArrayList.add(R.drawable.pig13);
imageArrayList.add(R.drawable.pig14);
imageArrayList.add(R.drawable.pig24);
imageArrayList.add(R.drawable.pig25);
imageArrayList.add(R.drawable.theend);
MainActivity.java
public class MainActivity extends Activity {
ViewFlipper viewFlipper;
Button Next;
private Integer images[] = {R.drawable.ic_launcher,
R.drawable.ic_no_image, R.drawable.calendar52};
ImageView imageView1;
private int currImage = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewFlipper = (ViewFlipper) findViewById(R.id.ViewFlipper01);
Next = (Button) findViewById(R.id.Next);
imageView1 = (ImageView) findViewById(R.id.imageView1);
Next.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
currImage++;
if (currImage == 3) {
currImage = 0;
}
final ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageResource(images[currImage]);
viewFlipper.showNext();
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/RelativeLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ViewFlipper
android:id="#+id/ViewFlipper01"
android:layout_width="fill_parent"
android:layout_height="400dp" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#4B0082" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="117dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</ViewFlipper>
</RelativeLayout>
<RelativeLayout
android:id="#+id/RelativeLayout03"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#000000"
android:gravity="center" >
<Button
android:id="#+id/Next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="5dp"
android:layout_marginRight="20dp"
android:text="Next" />
</RelativeLayout>
</LinearLayout>
Yes. Use the frame animation, delete your arraylist. Here is some sample code: Link

Get values from Dynamic list

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.

Categories

Resources