So I made a form with 3 EditTexts and two Buttons, "Ok" and "Cancel"
Here's my Code for it:
package com.examples.edtTxtCustomList;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class InputPage extends Activity implements OnClickListener{
private Button btnGo,btnCancel;
EditText et_name,et_email,et_phone;
List<String> data = new ArrayList<String>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
et_name = (EditText) findViewById(R.id.et_name);
et_email =(EditText)findViewById(R.id.et_email);
et_phone = (EditText)findViewById(R.id.et_phno);
btnGo = (Button) findViewById(R.id.btn_ok);
btnGo.setOnClickListener(this);
data.add("Welcome");
}
#Override
public void onClick(View src) {
String nm=et_name.getText().toString();
String no = et_email.getText().toString();
String mail= et_phone.getText().toString();
Intent i = new Intent(this,CustomLayout.class);
i.putExtra("name", nm);
i.putExtra("email", mail);
i.putExtra("phone", no);
startActivity(i);
}
} `
And here's the XML file of my form:
<?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" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dip"
android:layout_marginTop="20dip"
android:layout_marginBottom="10dip"
android:text="Name :"
android:textSize="20dip"/>
<EditText
android:id="#+id/et_name"
android:layout_width="230dip"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tv_name"
android:layout_alignTop="#id/tv_name"
android:layout_marginBottom="10dip"
android:layout_marginLeft="8dip"
android:textSize="15dip"/>
<TextView
android:id="#+id/tv_email"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dip"
android:layout_marginTop="20dip"
android:layout_marginBottom="10dip"
android:layout_below="#id/tv_name"
android:text="E-Mail :"
android:textSize="20dip"/>
<EditText
android:id="#+id/et_email"
android:layout_width="230dip"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tv_email"
android:layout_alignTop="#id/tv_email"
android:layout_below="#id/et_name"
android:inputType="textEmailAddress"
android:layout_marginLeft="6dip"
android:layout_marginBottom="10dip"
android:textSize="15dip"/>
<TextView
android:id="#+id/tv_phno"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="5dip"
android:layout_marginTop="20dip"
android:layout_marginBottom="10dip"
android:layout_below="#id/tv_email"
android:text="Phone :"
android:textSize="20dip"/>
<EditText
android:id="#+id/et_phno"
android:layout_width="230dip"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/tv_phno"
android:layout_alignTop="#id/tv_phno"
android:layout_below="#id/et_email"
android:inputType="phone"
android:layout_marginLeft="5dip"
android:layout_marginBottom="10dip"
android:textSize="15dip"/>
<Button
android:id="#+id/btn_ok"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/et_phno"
android:layout_marginTop="30dip"
android:layout_marginLeft="50dip"
android:text="OK"
android:textSize="20dip"/>
<Button
android:id="#+id/btn_cancel"
android:layout_width="100dip"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#id/et_phno"
android:layout_marginTop="30dip"
android:layout_marginRight="50dip"
android:text="Cancel"
android:textSize="20dip"/>
</RelativeLayout>
</LinearLayout>
now what I want is,when i click on "OK" all the data from all three of the edit texts should be displayed in the customlistview.
I tried to make a dynamic listview as follows:
package com.example.customlist;
import java.util.ArrayList;
import java.util.HashMap;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.SimpleAdapter;
public class CustomListLayoutActivity extends ListActivity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.custom_list_view);
SimpleAdapter adapter = new SimpleAdapter(this,list,R.layout.custom_row_layout,
new String[] {"Name","Email","Phone"},
new int[] {R.id.text1,R.id.text2, R.id.text3}
);
populateList();
setListAdapter(adapter);
}
static final ArrayList<HashMap<String,String>> list =
new ArrayList<HashMap<String,String>>();
private void populateList() {
HashMap<String,String> temp = new HashMap<String,String>();
temp.put("Name","Anurag Kulkarni");
temp.put("Email", "kulkarni_anurag#yahoo.com");
temp.put("Phone", "+91-9904475805");
list.add(temp);
HashMap<String,String> temp1 = new HashMap<String,String>();
temp1.put("Name","Rahul Shah");
temp1.put("Email", "rahul201290#gmail.com");
temp1.put("Phone", "+91-9898434909");
list.add(temp1);
HashMap<String,String> temp2 = new HashMap<String,String>();
temp2.put("Name","Pratik Thakkar");
temp2.put("Email", "iamcool#cooldood.com");
temp2.put("Phone", "+91-8539524925");
list.add(temp2);
HashMap<String,String> temp3 = new HashMap<String,String>();
temp3.put("Name","Utsav Patel");
temp3.put("Email", "patel_in_a_hotel#dhokla.com");
temp3.put("Phone","+91-9843500999");
list.add(temp3);
HashMap<String,String> temp4 = new HashMap<String,String>();
temp4.put("Name","Karan Mohan");
temp4.put("Email", "masala_dosa#sankalp.co.in");
temp4.put("Phone", "+91-9944843974");
list.add(temp4);
}
}
Can you please help me with it? I am still learning the basics,so any help would be appriciated.
Thanks.
As in another post of mine...
Whenever you want to do processing with the views in a ListView you
need to create a custom adapter that will handle your logic
implementation and pass that information to the views as necessary.
A custom adater would inflate the views piece by piece, this can be
dynamic of fixed.
Example:
Link 1
Related
I've battling a volley timeout error when attempting to post a registration form to a localhost (WAMP) for testing purposes. I've been looking at the code for sometime and cannot seem to find the issue. Timeout errors from what I understand are generally due to connection issues. I don't seem to be getting any errors via logcat, and the URL to my localhost php file seem to be correct.
I hope it's something small. In my db_function.php file, I did (include/connectiondb.php) but not sure if its correct. I originally had (connectiondb.php) but still no go.
Any assistance is appreciated.
All my code is below.
database details
CREATE TABLE jobseekerusers (
jobSeekerId int,
jobSeekerFirstName VARCHAR,
jobSeekerLastName VARCHAR,
jobSeekerEmaiLAddress VARCHAR,
jobSeekerPasswordVARCHAR,
);
connectdb.php
<?php
define('DB_HOST', 'localhost');
define('DB_USER', 'root');
define('DB_PASSWORD', '');
define('DB_DATABASE', 'Discover');
$con = mysqli_connect(DB_HOST,DB_USER,DB_PASSWORD,DB_DATABASE);
?>
db_function.php
<?php
if($_SERVER['REQUEST_METHOD']=='POST')
{
$jobFirstName = $_POST['jobFirstName'];
$jobLastName = $_POST['jobLastName'];
$jobEmailAddress = $_POST['jobEmailAddress'];
$jobPassword = $_POST['jobPassword'];
require_once('include/connectdb.php');
$sql = "INSERT INTO jobseekerusers(jobSeekerFirstName, jobSeekerLastName,
jobSeekerEmailAddress, jobSeekerPassword)
values ('$jobFirstName', '$jobLastName', '$jobEmailAddress',
'$jobPassword')";
if(mysql_query($con,$sql))
{
echo "Success";
}
else
{
echo "Fail";
}
}
else
{
echo "Something Failed";
}
?>
job_seeker_sign_up.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFF"
android:fitsSystemWindows="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="#drawable/shape_gradient_orange"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/search_icon" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_full_name"
android:textColor="#FAFAFA"
android:textSize="20sp" />
</LinearLayout>
<android.support.v7.widget.CardView
android:layout_width="350dp"
android:layout_height="320dp"
android:paddingBottom="20dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp"
android:background="#drawable/border"
android:id="#+id/cardView">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="6"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_weight="1"
android:padding="10dp"
android:text="#string/signUp"
android:textSize="20dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/jobFirstName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:singleLine="true"
android:layout_weight="1"
android:layout_marginTop="15dp"
android:background="#FAFAFA"
android:drawableLeft="#mipmap/rate"
android:drawablePadding="10dp"
android:hint="#string/employerFirstName"
android:maxLines="1"
android:textSize="15sp" />
<EditText
android:id="#+id/jobLastName"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:singleLine="true"
android:layout_weight="1"
android:background="#FAFAFA"
android:drawableLeft="#mipmap/employer_icon"
android:drawablePadding="10dp"
android:hint="#string/employerLastName"
android:maxLines="1"
android:textSize="15sp" />
<EditText
android:id="#+id/jobEmailAddress"
android:layout_width="match_parent"
android:layout_height="50dp"
android:inputType="textEmailAddress"
android:layout_gravity="center"
android:singleLine="true"
android:layout_marginTop="15dp"
android:background="#FAFAFA"
android:layout_weight="1"
android:drawableLeft="#mipmap/email_icon"
android:drawablePadding="10dp"
android:hint="#string/employerEmailAddress"
android:maxLines="1"
android:textSize="15sp" />
<EditText
android:id="#+id/jobPassword"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="50dp"
android:singleLine="true"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:layout_weight="1"
android:background="#FAFAFA"
android:drawableLeft="#mipmap/password_icon"
android:drawablePadding="10dp"
android:hint="#string/employerPassword"
android:maxLines="1"
android:textSize="15sp" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
<LinearLayout
android:layout_width="match_parent"
android:layout_below="#+id/cardView"
android:gravity="center"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button
android:id="#+id/jobSubmit"
android:layout_width="200dp"
android:layout_marginTop="10dp"
android:textColor="#FAFAFA"
android:layout_height="wrap_content"
android:text="Submit"
android:background="#drawable/shape_gradient_orange"/>
<TextView
android:layout_width="wrap_content"
android:id="#+id/jobHaveAccount"
android:layout_height="wrap_content"
android:textColor="#999"
android:padding="10dp"
android:text="#string/haveAccount"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
jobSeekerPage.java
package com.example.dennis.discover;
import android.app.DownloadManager;
import android.content.Intent;
import android.provider.ContactsContract;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.AuthFailureError;
import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.android.volley.toolbox.StringRequest;
import com.android.volley.toolbox.Volley;
import org.w3c.dom.Text;
import java.util.HashMap;
import java.util.Map;
public class JobSeekerSignUp extends AppCompatActivity implements
View.OnClickListener {
private TextView jobHaveAccount;
private TextView jobFirstName;
private TextView jobLastName;
private TextView jobEmailAddress;
private TextView jobPassword;
private Button jobSubmit;
String URL = "http://192.168.0.9/Discover/db_functions.php";
public static final String FIRSTNAME = "jobSeekerFirstName";
public static final String LASTNAME = "jobSeekerLastName";
public static final String EMAILADDRESSID = "jobSeekerEmailAddress";
public static final String PASSWORD = "jobSeekerPassword";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_job_seeker_sign_up);
jobHaveAccount = (TextView) findViewById(R.id.jobHaveAccount);
jobFirstName = (TextView) findViewById(R.id.jobFirstName);
jobLastName = (TextView) findViewById(R.id.jobLastName);
jobEmailAddress = (TextView) findViewById(R.id.jobEmailAddress);
jobPassword = (TextView) findViewById(R.id.jobPassword);
jobSubmit = (Button) findViewById(R.id.jobSubmit);
jobSubmit.setOnClickListener(this);
jobHaveAccount.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent jobSeekerSignIn = new Intent(JobSeekerSignUp.this,
JobSeekerLogIn.class);
startActivity(jobSeekerSignIn);
}
});
}
#Override
public void onClick(View view) {
final String firstName = jobFirstName.getText().toString().trim();
final String lastName = jobLastName.getText().toString().trim();
final String emailAddress = jobEmailAddress.getText().toString().trim();
final String password = jobPassword.getText().toString().trim();
//Initiate request queue
RequestQueue requestQueue = Volley.newRequestQueue(this);
//get String Response using URL (variable defined above - currently WAMP)
StringRequest stringrequest = new StringRequest(Request.Method.POST,
URL, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Toast.makeText(JobSeekerSignUp.this, response,
Toast.LENGTH_LONG).show();
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(JobSeekerSignUp.this, error.toString(),
Toast.LENGTH_LONG).show();
}
})
{
#Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<String, String>();
params.put(FIRSTNAME, firstName);
params.put(LASTNAME, lastName);
params.put(EMAILADDRESSID, emailAddress);
params.put(PASSWORD, password);
return super.getParams();
}
};
// Adding the request to the request queue
requestQueue.add(stringrequest);
}
}
Try disabling your firewall if on windows, or disabling Avast shields. It worked for me.
This is my main file where i am calling the click event on button to pass the value.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setTitle("aakash");
SetContentView(R.id.lst);
Button b = (Button) findViewById(R.id.sendbtn);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(android.view.View v) {
startActivity(new Intent(View.this,View.class));
}
});
}
**xml**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:text="aakash"
android:background="#c0c0c0"
/>
<ScrollView
android:id="#+id/lst"
android:background="#fefefe"
android:layout_below="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_holder"
>
</ScrollView>
<EditText
android:id="#+id/chattxt"
android:layout_width="230dp"
android:layout_height="45dp"
android:hint="type here"
android:inputType="textMultiLine" />
<Button
android:id="#+id/sendbtn"
android:layout_width="75dp"
android:layout_height="45dp"
android:text="Send" />
</LinearLayout>
I want the value of textbox to display on the same screen(scrollview)box and also i want to pass a httppost request to pass a value..plz help me out
Thnks in advance
I think, you can use a ListView insted of scroll view. and add list items from the chat text value dynamically.
call notifydatasetchanged() every time when you add a new item to it.
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:text="aakash"
android:background="#c0c0c0"
/>
<ListView
android:id="#+id/listView_chats"
android:layout_width="match_parent"
android:layout_height="100dp"
>
</ListView>
<EditText
android:id="#+id/chattxt"
android:layout_width="230dp"
android:layout_height="45dp"
android:hint="type here"
android:inputType="textMultiLine" />
<Button
android:id="#+id/sendbtn"
android:layout_width="75dp"
android:layout_height="45dp"
android:text="Send" />
</LinearLayout>
Write a custom adapter for the listView and add the code to manage the list data.
i got the answer to my question
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ButtonActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
}
public void setText(){
Button myButton = (Button)findViewById(R.id.button1);
final TextView myText = (TextView)findViewById(R.id.text1);
final EditText myInput = (EditText)findViewById(R.id.edit);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
myText.setText(myInput.getText());
}
});
}
}
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.
Hi I have an m file which is a relative layout and which contains 9 image buttons.
So I would like to reuse my layout multiple times just by changing the names and ID's.
I am able to do with gridview but my problem is I want use the same customs design which I have using relative layout.
So can any one help how to do it.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageButton
android:id="#+id/day0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/day1"
android:layout_marginLeft="14dp"
android:scaleType="fitXY"
android:src="#drawable/day0" />
<ImageButton
android:id="#+id/day3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day0"
android:layout_below="#+id/day0"
android:layout_marginTop="54dp"
android:src="#drawable/day3" />
<ImageButton
android:id="#+id/day6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day3"
android:layout_below="#+id/day3"
android:layout_marginTop="74dp"
android:src="#drawable/day6" />
<ImageButton
android:id="#+id/day4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/day6"
android:layout_marginLeft="25dp"
android:layout_toRightOf="#+id/day3"
android:src="#drawable/day4" />
<ImageButton
android:id="#+id/day7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day4"
android:layout_alignTop="#+id/day6"
android:src="#drawable/day7" />
<ImageButton
android:id="#+id/day1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day4"
android:layout_alignParentTop="true"
android:layout_marginTop="39dp"
android:scaleType="fitXY"
android:src="#drawable/day1" />
<ImageButton
android:id="#+id/day8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day5"
android:layout_alignTop="#+id/day7"
android:src="#drawable/day8" />
<ImageButton
android:id="#+id/day2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/day1"
android:layout_marginLeft="28dp"
android:layout_toRightOf="#+id/day1"
android:scaleType="fitXY"
android:src="#drawable/day2" />
<ImageButton
android:id="#+id/day5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/day6"
android:layout_alignLeft="#+id/day2"
android:src="#drawable/day5" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView4"
android:layout_alignBottom="#+id/textView4"
android:layout_alignLeft="#+id/day7"
android:text="Emotions"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView8"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textView7"
android:layout_alignLeft="#+id/Ans6b"
android:text="Leadership"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/Ans6b"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/question6a"
android:layout_alignBottom="#+id/question6a"
android:layout_alignRight="#+id/day1"
android:text="gruppstart"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/day3"
android:layout_toLeftOf="#+id/day4"
android:text="Adaption"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/day6"
android:layout_toLeftOf="#+id/day4"
android:text="Behavior"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/day8"
android:text="Satisfaction"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/day5"
android:layout_below="#+id/day5"
android:text="Insight"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/day2"
android:layout_below="#+id/day2"
android:text="relationer"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/question6a"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView7"
android:layout_below="#+id/day0"
android:layout_marginLeft="15dp"
android:text="Goal"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/textView3"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
This is my xml file I would like to call this xml file for different activities but just by changing the names ID and some display image buttons.
How to do that can any one please suggest me
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.HashMap;
import com.xylem.flygt.R;
import android.os.AsyncTask;
import android.os.Bundle;
import android.app.Activity;
import android.app.ProgressDialog;
import android.util.Log;
import android.view.KeyEvent;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.TranslateAnimation;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.TableLayout;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewFlipper;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
public class MenuActivity extends Activity
{
static GridView gridView;
Bitmap bmp = null;
public ArrayList<Bitmap> bitmapArray = new ArrayList<Bitmap>();
Context context;
ImageButton day0, day1, day2, day3, day4, day5, day6, day7, day8;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
day0 = (ImageButton) findViewById(R.id.day0);
day1 = (ImageButton) findViewById(R.id.day1);
day2 = (ImageButton) findViewById(R.id.day2);
day3 = (ImageButton) findViewById(R.id.day3);
day4 = (ImageButton) findViewById(R.id.day4);
day5 = (ImageButton) findViewById(R.id.day5);
day6 = (ImageButton) findViewById(R.id.day6);
day7 = (ImageButton) findViewById(R.id.day7);
day8 = (ImageButton) findViewById(R.id.day8);
gridView = (GridView) findViewById(R.id.grid_view);
gridView.setBackgroundColor(Color.parseColor("#E3E4FA"));
gridView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id)
{
System.out.println("Clicked id " + position);
if(position == 0)
{
System.out.println(" Day 0 ");
}
if(position == 1) {
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "1");
System.out.println(" Day 1 ");
startActivity(i);
}
if(position == 2)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "2");
System.out.println(" Day 2 ");
startActivity(i);
}
if(position == 3)
{ Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "3");
System.out.println(" Day 3 ");
startActivity(i);
}
if(position == 4)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "4");
System.out.println(" Day 4 ");
startActivity(i);
}
if(position == 5)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "5");
System.out.println(" Day 5 ");
startActivity(i);
}
if(position == 6)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "6");
System.out.println(" Day 6");
startActivity(i);
}
if(position == 7)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "7");
System.out.println(" Day 7 ");
startActivity(i);
}
if(position == 8)
{
Intent i = new Intent(getApplicationContext(), nextActivity.class);
i.putExtra("dayno", "8");
System.out.println(" Day 8 ");
startActivity(i);
}
}
});
new Checkday().execute();
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
class Checkday extends AsyncTask<String, String, String>
{
protected void onPreExecute()
{
super.onPreExecute();
}
#Override
protected String doInBackground(String... arg0)
{
return null;
}
protected void onPostExecute(String file_url)
{
gridView.setAdapter(new CalenderAdaptor(MenuActivity.this));
}
}
}
I have written my activity java it itself creates the image buttons in a gridview. But I use like this I want to use in such way that I call my customs layout in different activities because the layout is same only the IDs and some images will change.
If I understand it right - you have a custom design layout with buttons and you want to use this same layout in other screen as well.
You don't need to rename anything, just put the layout in a separate xml, let's say CommonButtonLayout.xml. This should contain like this:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
.... your layout .....
</merge>
Now you want to use this in other layout files. You need to include this xml directly there like the following.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
...
...
>
<include
android:layout_centerVertical="true"
layout="#layout/CommonButtonLayout" />
</RelativeLayout>
I have an activity that is works fine and displays data obtained from a table query. With an OnItemClick method, I have it set up so that if a row is selected, a new activity starts with a new ListView, but a lot of the elements from the original ListView are in the new listview, including a toggle button on each row, and a text view. Any idea what could be causing this?
This is the initial ListView. The buttons don't show up, just everything in the ListView:
<?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="wrap_content"
android:background="#81BEF7"
android:orientation="vertical" >
<Button
android:id="#+id/btnAddNurseToRoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/add"
android:focusable="true"/>
<Button
android:id="#+id/btnRemoveNurseFromRoster"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/btnAddNurseToRoster"
android:layout_toRightOf="#+id/btnAddNurseToRoster"
android:text="#string/delete"
android:focusable="true"/>
<ListView
android:id="#+id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/btnAddNurseToRoster"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
</RelativeLayout>
New ListView:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#81BEF7"
android:orientation="vertical" >
<TextView
android:id="#+id/tvAssignmentsText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#FFFFFF"
android:text="Assignments"
android:gravity="center_horizontal"
android:background="#663399"
/>
<ListView
android:id="#+id/lvAssignmentsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/tvAssignmentsText"
android:paddingTop="20dp"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
</
And here's the code that calls the new ListView:
package com.deadEddie.staffingmanagement;
import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.util.Log;
import android.widget.ListAdapter;
import android.widget.ListView;
public class ShowAssignments extends Activity {
DbCommunicator getAssignmentsList;
ListView assignmentsList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.assignments);
displayList();
}
private void displayList() {
// instantiate ListView object
assignmentsList = (ListView) findViewById(R.id.lvAssignmentsList);
// instantiate variables for CursorAdapter
int [] to = new int [] {R.id.rbAssignmentsList };
String [] from = new String [] {DbCommunicator.KEY_ROOM_NUMBER};
// instantiate getAssignmentsList with new DbCommunicator object and open
getAssignmentsList = new DbCommunicator(this);
getAssignmentsList.open();
// get and manage cursor
Cursor assignmentsCursor = getAssignmentsList.getAssignments(this);
startManagingCursor(assignmentsCursor);
// list adapter
ListAdapter assignmentsListAdapter = new SimpleCursorAdapter(this, R.layout.nurse_list, assignmentsCursor, from, to, 0);
assignmentsCursor.moveToNext();
// set ListView
assignmentsList.setAdapter(assignmentsListAdapter);
assignmentsList.setItemsCanFocus(true);
}
}
Adding the code used to populate the original ListView:
<?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="wrap_content" >
<ToggleButton
android:id="#+id/rosterDutyStatus"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:textOff="Off Duty"
android:textOn="On Duty"
android:textSize="10dp"
android:focusable="false"/>
<TextView
android:id="#+id/rosterListLname"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterDutyStatus"
android:layout_toRightOf="#+id/rosterDutyStatus"
android:paddingTop="6dp"
android:textColor="#FFFFFF"
android:textSize="18dp"
android:textStyle="bold"
android:focusable="false"
android:focusableInTouchMode="false"/>
<TextView
android:id="#+id/rosterListFname"
android:layout_width="60dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterListLname"
android:layout_toRightOf="#+id/rosterListLname"
android:paddingTop="6dp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="#+id/rosterListMI"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterListFname"
android:layout_toRightOf="#+id/rosterListFname"
android:paddingTop="6dp"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="#+id/rosterListID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterListMI"
android:layout_toRightOf="#+id/rosterListMI"
android:visibility="invisible" />
<TextView
android:id="#+id/rosterViewAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/rosterDutyStatus"
android:paddingRight="50dp"
android:text="#string/assigned"
android:textColor="#FFFFFF"
android:textStyle="bold" />
<TextView
android:id="#+id/firstAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/rosterViewAssignment"
android:layout_toRightOf="#+id/rosterViewAssignment"
android:text="#string/noAssignments"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/secondAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/firstAssignment"
android:layout_toRightOf="#+id/firstAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/thirdAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/secondAssignment"
android:layout_toRightOf="#+id/secondAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/fourthAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/thirdAssignment"
android:layout_toRightOf="#+id/thirdAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/fifthAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/fourthAssignment"
android:layout_toRightOf="#+id/fourthAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>
<TextView
android:id="#+id/sixthAssignment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/fifthAssignment"
android:layout_toRightOf="#+id/fifthAssignment"
android:textColor="#FFFFFF"
android:textStyle="bold"/>"
-->
</RelativeLayout>
Code that calls original layout I'm trying to change:
package com.deadEddie.staffingmanagement;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;
import android.widget.AdapterView.OnItemClickListener;
public class EditRoster extends ListActivity implements OnClickListener {
String TAG = "EditRoster";
Button addNurse;
Button deleteNurse;
DbCommunicator rosterView;
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.edit_roster);
addNurse = (Button) findViewById(R.id.btnAddNurseToRoster);
deleteNurse = (Button) findViewById(R.id.btnRemoveNurseFromRoster);
displayNurseRoster();
displayDialog();
addNurse.setOnClickListener(this);
deleteNurse.setOnClickListener(this);
}
// method to put timer on dialog
public void timerDelayRemoveDialog(long time, final Dialog dialogView){
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
dialogView.dismiss();
}
}, time);
}
private void displayDialog() {
Dialog d = new Dialog(this);
d.setTitle("Select Nurse to Adjust Assignments");
d.show();
timerDelayRemoveDialog(2000, d);
}
public void displayNurseRoster(){
listView = (ListView) findViewById(android.R.id.list);
// int variables filled with NURSE_TABLE data
int[] to_nurseTable = new int[] {
// int list for data from NURSE_TABLE fields
R.id.rosterDutyStatus,
R.id.rosterListLname,
R.id.rosterListFname,
R.id.rosterListMI,
R.id.rosterListID,
// int list for data from ASSIGNMENTS_TABLE fields
R.id.firstAssignment};
// String array holding data fields from NURSE_TABLE
String[] from_nurseTable = new String [] {
// fields from NURSE_TABLE
DbCommunicator.KEY_DUTY_STATUS,
DbCommunicator.KEY_LNAME,
DbCommunicator.KEY_FNAME,
DbCommunicator.KEY_MI,
DbCommunicator.KEY_NURSE_ROWID,
DbCommunicator.KEY_ROOM_NUMBER};
// instantiate instance of DbCommunicator object
rosterView = new DbCommunicator(this);
// open instance
rosterView.open();
// get & manage cursor for NURSE_TABLE data
Cursor nurseTableCursor = rosterView.getNurseRosterCursor(this);
startManagingCursor(nurseTableCursor);
// instantiate cursor adaptor
ListAdapter nurseTableAdapter = new SimpleCursorAdapter(this,
R.layout.nurse_list, nurseTableCursor, from_nurseTable, to_nurseTable);
nurseTableCursor.moveToNext();
// set listView
listView.setAdapter(nurseTableAdapter);
rosterView.close();
listView.setItemsCanFocus(true);
listView.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Log.i(TAG, "onItemClickListener set");
Intent showAssignments = new Intent("com.deadEddie.staffingmanagement.SHOWASSIGNMENTS");
startActivity(showAssignments);
}
});
}// displayNurseRoster()
#Override
public void onClick(View v)
{
switch (v.getId())
{
case R.id.btnAddNurseToRoster:
Intent add = new Intent("com.deadEddie.staffingmanagement.ADDNURSETOROSTER");
startActivity(add);
break;
case R.id.btnRemoveNurseFromRoster:
break;
} // switch
}
}
Okay. Got it. Just realized that I mistakenly used the same layout (nurse_list) when I instantiate the ListAdapter in each method so it tried to apply my new data query to the original layout (which was screwed up of course). Thanks for taking a look though, really appreciate it.
Sorry if this wastes anyone's time. Maybe a lesson for other newbies out there.