I am developing an application for children. Basic maths for children to learn with fun.
In this app I am using apple image for displaying on the screen. for addition when they give 1st input and second input it should show apple images as specified in 1st and 2nd input.
Is any one know solution for this or any one having source code thn help me
this is the way you can do it ,quite simple though :)
your xml should look like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/input1" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/input2" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="Result"/>
<HorizontalScrollView
android:id="#+id/scroll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/linear1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:id="#+id/scroll2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear2"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:id="#+id/scroll3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear3"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
and you Activity looks like this
package com.example.stackanswer;
import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class AppleActivity extends Activity {
private LinearLayout linear1;
private LinearLayout linear2;
private LinearLayout linear3;
private HorizontalScrollView scrollbar1;
private HorizontalScrollView scrollbar2;
private HorizontalScrollView scrollbar3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apple);
final EditText input1 = (EditText)findViewById(R.id.input1);
final EditText input2 = (EditText)findViewById(R.id.input2);
scrollbar1 = (HorizontalScrollView)findViewById(R.id.scroll1);
scrollbar2 = (HorizontalScrollView)findViewById(R.id.scroll2);
scrollbar3 = (HorizontalScrollView)findViewById(R.id.scroll3);
linear1 = (LinearLayout)findViewById(R.id.linear1);
linear2 = (LinearLayout)findViewById(R.id.linear2);
linear3 = (LinearLayout)findViewById(R.id.linear3);
Button output = (Button)findViewById(R.id.button);
output.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String input1A = input1.getText().toString();
String input1B = input2.getText().toString();
int value1 = Integer.parseInt(input1A);
int value2 = Integer.parseInt(input1B);
Log.i("1",""+value1);
Log.i("2",""+value2);
linear1.removeAllViews();
linear2.removeAllViews();
linear3.removeAllViews();
linear1.setScrollContainer(true);
ImageView image ;
for(int i=0;i<value1;i++){
image = new ImageView(getApplicationContext());
image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) );
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear1.addView(image);
scrollbar1.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
for(int i=0;i<value2;i++){
image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear2.addView(image);
scrollbar2.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
int sum = value1+value2;
for(int i=0;i<sum;i++){
image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear3.addView(image);
scrollbar3.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}
});
}
}
Complete source code here
Related
I'm new to android application development, I need to add buttons with different names, that is, the name of the button is taken from EditText and a button is created. The nuance of all this is that after I try to create another button and enter another name, this very name is transferred to other buttons. That is, if I create a button for the first time, then it is given the text Sometning1, but after I create the second button with the text Sometning2, then the first button gets the name of the second one, and so on, all previous buttons get the name of the new one, how to fix this? By the way, each button should be completely different, with different properties, but for now let's focus on the text in it.(sorry, hard to explain)
Xml code
<?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=".MainActivity6">
<TableLayout
android:id="#+id/Tb1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:ignore="MissingConstraints">
<TableRow
android:id="#+id/r1"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<TableRow
android:id="#+id/r2"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r4"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r5"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r6"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r7"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
<TableRow
android:id="#+id/r8"
android:layout_width="match_parent"
android:layout_height="match_parent"></TableRow>
</TableLayout>
<EditText
android:id="#+id/adress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="70dp"
android:inputType="text"
android:text="Something"
android:textColor="#424242"
app:layout_constraintBottom_toBottomOf="#+id/btn1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:ignore="LabelFor" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
MainActivity
package com.example.serverconnectionukaa;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TableLayout;
import android.widget.TableRow;
import android.widget.Toast;
public class MainActivity6 extends AppCompatActivity {
EditText adress;
Button btn1;
private TableLayout buttonTableLayout;
int clickcount=0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main6);
adress = findViewById(R.id.adress);
btn1 = (Button) findViewById(R.id.btn1);
buttonTableLayout = (TableLayout) findViewById(R.id.Tb1);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
clickcount=clickcount+1;
if(clickcount==0)
{
}
else
{
//check how many times clicked and so on
Toast.makeText(getApplicationContext(),"Button clicked count is"+clickcount, Toast.LENGTH_LONG).show();
}
for (int row = 0; row < buttonTableLayout.getChildCount(); ++row)
((TableRow) buttonTableLayout.getChildAt(row)).removeAllViews();
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int x = 0;
for (int row = 0; row < 5; row++) {
TableRow currentTableRow = getTableRow(row);
for (int column = 0; column < 2; column++) {
Button newGuessButton = new Button(MainActivity6.this);
String myName = adress.getText().toString();
newGuessButton.setText(myName);
currentTableRow.addView(newGuessButton);
x++;
if (x==clickcount) {
break;
}
}
if (x==clickcount) {
break;
}
}
}
});
}
private TableRow getTableRow(int row) {
return (TableRow) buttonTableLayout.getChildAt(row);
}
}
Thanks to everyone
MainActivity's code
How can I get values from the text boxes and the qty box in that user will input the value. price will come from server and than i want to calculate the bill and show it in next layout. plz give me some easy explanation as i am not expert in it. thanks in advance.
MainActivity .java
package com.example.myone;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends Activity {
EditText textIn;
Button buttonAdd;
LinearLayout container;
private Button done;
private TextView tv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textIn = (EditText)findViewById(R.id.textin);
buttonAdd = (Button)findViewById(R.id.add);
container = (LinearLayout)findViewById(R.id.container);
done = (Button) findViewById(R.id.cal);
tv= (TextView) findViewById(R.id.tv);
buttonAdd.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
LayoutInflater layoutInflater =
(LayoutInflater) getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View addView = layoutInflater.inflate(R.layout.row, null);
TextView textOut = (TextView)addView.findViewById(R.id.textout);
textOut.setText(textIn.getText().toString());
//(int1);
Button buttonRemove = (Button)addView.findViewById(R.id.remove);
buttonRemove.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
((LinearLayout)addView.getParent()).removeView(addView);
}});
container.addView(addView);
}});
}
}
MainActivity.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:background="#drawable/mainbg"
tools:context=".MainActivity" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/textin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Enter your item here"
/>
<Button
android:id="#+id/cal"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_below="#+id/textin"
android:text="Done!"
android:layout_alignParentLeft="true"
/>
<Button
android:id="#+id/add"
android:text="Add"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#+id/cal"
/>
</RelativeLayout>
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
Row.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#drawable/borderbg"
android:orientation="horizontal"
android:id="#+id/rel"
android:layout_height="wrap_content">
<TextView
android:id="#+id/textout"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="Item"
android:layout_marginTop="10dp"
android:layout_marginLeft="5dp"
/>
<EditText
android:id="#+id/qty"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignTop="#+id/price"
android:layout_toRightOf="#+id/price"
android:layout_marginLeft="50dp"
android:textSize="12sp"
android:hint="Qty" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textout"
android:layout_alignBottom="#+id/textout"
android:layout_marginLeft="42dp"
android:layout_toRightOf="#+id/textout"
android:hint="Price" />
<Button
android:id="#+id/remove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:gravity="left"
android:text="remove" />
</RelativeLayout>
Simply I want to create a basic shopping app that have 20 products. the price and product name will come from server and quantity will input by user so how do I that?
You are looking for something like this
int count = container.getChildCount();
for (int i = 0; i < count; i++) {
final View row = container.getChildAt(i);
TextView textOut = (TextView)row.findViewById(R.id.textout);
String data = textOut.getText().toString();
}
Where container is your LinearLayout in which you have added your inflated rows.
Try this:-
To take user input from text box:-
int qty = Integer.parseInt(qtyTextBox.getText().toString());
Caluculate bill:-
double bill = price*qty;
Send bill value to next activity:-
Intent in = new Intent();
in.putExtra("Bill", bill);
startActivity(in);
Get bill value in next activity:-
Intent in = getIntent();
int mBill = in.getExtra("Bill");
string bill = String.valueOf(mBill);
Now you can set the value of 'bill' to the TextView where you want to display it.
I am trying to put image in TextView. I had done it using image span but my problem is i cannot put onClickListener in every image (in same TextView, there are multipal image in same TextView). Please suggest me how can i do that.
make a custom.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/thumbnail_view"
android:src="#drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/message_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/thumbnail_view"
android:textSize="18sp"
android:text="MyText" />
</RelativeLayout>
then in main.xml , include this custom.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<include
android:id="#+id/customView"
layout="#layout/custom"/>
</LinearLayout>
This is my mainActivity.class
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private String TAG = MainActivity.class.getSimpleName();
ImageView img;
ImageView img1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView txt = (TextView)findViewById(R.id.message_view);
img = (ImageView) findViewById(R.id.thumbnail_view);
img1 = (ImageView) findViewById(R.id.thumbnail_view1);
img.setOnClickListener(this);
img1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v== img){
// do something for img
}
else if (v== img1){
//do something for img1
}
}
}
make a custom view instead. That would be much easier.
We have create a custom dialog box.
How do we set the custom dialog box at the Middle of the center position.
Sample code is dialog.getWindow().setGravity(Gravity.CENTER);
Now custom dialog box is displayed at the Top Of the center position.
Thanks in advance.
TestAActivity.java
package com.TestA;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class TestAActivity extends Activity {
private Button btn;
/** Called when the activity is first created. */
// TwitPicResponse tpResponse = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
show_Dialog();
}
});
}
protected void show_Dialog() {
// TODO Auto-generated method stub
//Context mContext = getApplicationContext();
Dialog dialog = new Dialog(TestAActivity.this);
dialog.setContentView(R.layout.test);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
dialog.show();
}
}
enter code here
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<ImageView android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
see below links it is default in CENTER.
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog
There are three problems I am facing:
When I click(+)button, the edit boxes are going underneath the button, whereas i want them to be displayed above.
currently displaying:
Want like this:
<edit text1> <edit text2>
<edit text3> <edit text4>
<edit text5> <edit text6>
<edit text7> <edit text8>
<button> <button2>
As you can see, I tried parsing the edit text value which i got from the xml, into the docalc() function, and displaying the value in textbox. but its not working out. showing me nothing.
can I parse values in dostuff, if yes how will i inter-relate those in docalc?
Thanks in advance...
Java Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PlusbuttonActivity extends Activity
implements OnClickListener {
TextView tt;
TextView j;
EditText amount1;
EditText amount2;
double x=0;
double y=0;
double a=0;
double z=0;
double b=0;
Button btnButton;
/** Called when the activity is first created. */
private LinearLayout root;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// modified
amount1=(EditText)findViewById(R.id.edittext1);
amount2=(EditText)findViewById(R.id.edittext2);
// modified
View btnButton =(Button) findViewById(R.id.button_next);
Button mButton = (Button) findViewById(R.id.button);
mButton.setGravity(Gravity.CENTER);
tt=(TextView)findViewById(R.id.tt);
j=(TextView)findViewById(R.id.j);
root = (LinearLayout) findViewById(R.id.linearLayout);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
View view = doStuff();
addViewToRoot(view);
break;
case R.id.button_next:
View view1 = doCalc();
addViewToRoot(view1);
break;
}
}
private View doCalc() {
// TODO Auto-generated method stub
x=Double.parseDouble(amount1.getText().toString());
y=Double.parseDouble(amount2.getText().toString());
z=(x*703);
tt.setText(Double.toString(z));
return tt;
}
private View doStuff() {
EditText t = new EditText(PlusbuttonActivity.this);
t.setGravity(Gravity.LEFT);
t.setWidth(250);
EditText a = new EditText(PlusbuttonActivity.this);
a.setGravity(Gravity.RIGHT);
a.setWidth(250);
LinearLayout l = new LinearLayout(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// t.setBackgroundColor(0xffCECECE);
a.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.addView(t);
l.addView(a);
return l;
}
private void addViewToRoot(View v){
root.addView(v);
}
}
xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
android:text="Units"
android:id="#+id/Units"
/>
<TextView android:layout_height="wrap_content"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="Grades"
android:id="#+id/j"></TextView>
</LinearLayout>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="250px"
android:layout_height="wrap_content"
android:id="#+id/edittext1">
</EditText>
<EditText
android:layout_height="wrap_content"
android:id="#+id/edittext2" android:layout_width="150dp">
</EditText>
</LinearLayout>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="250px"
android:layout_height="wrap_content"
android:id="#+id/edittext3">
</EditText>
<EditText
android:layout_height="wrap_content"
android:id="#+id/edittext4" android:layout_width="150dp">
</EditText>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button android:layout_height="wrap_content"
android:gravity="center" android:id="#+id/button" android:text="+" android:layout_width="wrap_content"></Button>
<Button
android:id="#+id/button_next"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:text="CALCULATE"
>
</Button>
</RelativeLayout>
<TextView android:text="TextView"
android:id="#+id/tt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
First, set your LinearLayout's id to something other than main. Like root. Main is the name of the xml file containing your layout, not the id of the LinearLayout item.
Second, Instead of using this in the constructor for the EditText, use PlusbuttonActivity.this. So change the line you use to create the EditText to this:
EditText t = new EditText(PlusbuttonActivity.this);
You have to do this because when you're in the onClick method your technically in the OnClickListener class, and this refers to the OnClickListener object. By using PlusbuttonActivity.this you're clarifying that you mean the PlusbuttonActivity object that you're currently in, not the OnClickListener.
Third, to address the other issue, you can't reference root from you anonymous OnClickListener class. Instead of calling root.addView(t), you can extract that out to a method. Putting it all together we get this:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class PlusbuttonActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout root;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButton = (Button) findViewById(R.id.button1);
root = (LinearLayout) findViewById(R.id.root);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText t = new EditText(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addViewToRoot(t);
}
});
}
private void addViewToRoot(View v){
root.addView(v);
}
}
Change the id of LinearLayout in main.xml, use different name.
Save & Clean project.
Fix like #Kurtis Nusbaum mentioned above!