AsyncTask, LinearLayout Android Update visibility - android

I am trying to change visibility of a Linear Layout through findViewById() and then SetVisiblity(). But When I debug, I am seeing null values only.
Please find the snippet in the BackgroundMainActivity.java below in the onPostExecute() method.
I commented it. Please help.
MainActivity.java
button_getUsername.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BackgroundMainActivity backgroundMainActivity = new BackgroundMainActivity(MainActivity.this, v);
backgroundMainActivity.execute("random", editText_username.getText().toString());
}
});
BackgroundMainActivity.java
public class BackgroundMainActivity extends AsyncTask<String, Void , String> {
public BackgroundMainActivity(Context ct, View view) {
context = ct;
rootView = view;
}
#Override
protected String doInBackground(String... params) {
String login_URL = "some url";
try {
String username = params[1];
URL url = new URL(login_URL);
HttpURLConnection httpURLConnection = (HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoOutput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream,"UTF-8"));
String postData = URLEncoder.encode("username","UTF-8")+"="+URLEncoder.encode(username,"UTF-8");
bufferedWriter.write(postData);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream,"iso-8859-1"));
result = bufferedReader.readLine();
bufferedReader.close();
inputStream.close();
httpURLConnection.disconnect();
return result;
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPreExecute(){
}
#Override
protected void onPostExecute (String result){
if(result.equals("success")){
//need to make this change
//LinearLayout someLayout = (LinearLayout)rootview.findViewById(R.id.layout_content);
//someLayout.setVisibility(View.VISIBLE);
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage(result)
.setPositiveButton("Ok",null)
.show();
}
}
#Override
protected void onProgressUpdate(Void... values){
super.onProgressUpdate(values);
}
}
EDIT:
activity_main.xml
ss_layout_questions is gone by default. I want to make it visible once "result.equals( "success") " in BackgroundMainActivity.java.
<?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="fill_parent"
android:layout_height="fill_parent"
tools:context="com.xtremustechnologies.chotu.useractivity.MainActivity">
<android.support.v4.widget.NestedScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true"
android:background="#color/lightGreen"
android:layout_gravity="top">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/textviewr2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginTop="30dp"
android:text="CHOTU"
android:textColor="#color/white"
android:textSize="40dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="40dp"
android:text="Your Personal Assistant"
android:textColor="#color/white"
android:textSize="12dp" />
</LinearLayout>
<TextView
android:id="#+id/ss_textview_questions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="40dp"
android:text="Security Questions "
android:textColor="#color/black"
android:textSize="20dp"
android:visibility="gone"/>
<EditText
android:id="#+id/ss_et_username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:hint="Enter Username" />
<Button
android:id="#+id/ss_button_getusername"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ss_et_username"
android:layout_centerHorizontal="true"
android:onClick="clickGetUsername"
android:layout_gravity="center_horizontal"
android:text="Submit" />
<LinearLayout
android:id="#+id/ss_layout_questions"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textviewr3"
android:layout_centerHorizontal="false"
android:layout_marginLeft="40dp"
android:layout_marginRight="40dp"
android:orientation="horizontal">
<Button
android:id="#+id/ss_button_dob"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginRight="50dp"
android:text="Date of Birth" />
<TextView
android:id="#+id/ss_textView_dob"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical|center_horizontal"
android:layout_weight="1"
android:text="TextView"
android:textSize="20dp" />
</LinearLayout>
<TextView
android:id="#+id/ss_textView_question1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp" />
<EditText
android:id="#+id/ss_textView_ans1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:hint="Enter your answer here" />
<TextView
android:id="#+id/ss_textView_question2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp" />
<EditText
android:id="#+id/ss_textView_ans2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:hint="Enter your answer here" />
<TextView
android:id="#+id/ss_textView_question3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginRight="10dp"
android:layout_marginTop="30dp" />
<EditText
android:id="#+id/ss_textView_ans3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:hint="Enter your answer here" />
<Button
android:id="#+id/ss_button_submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#color/black"
android:text="Submit"
android:textColor="#color/white" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>

Its working finally. Here is what I did
I added a new class inside BackgroundMainActivity.java and then called it in the onPostExecute() method.
class a{
public Button button_reset, button_dob;
public LinearLayout layout_questions;
public TextView textView_title;
public EditText editText_username;
public void makeVisible(ForgotPasswordActivity context){
LinearLayout layout_questions = (LinearLayout) context.findViewById(R.id.ss_layout_questions);
layout_questions.setVisibility(View.VISIBLE);
}
onPostExecute() in BackgroundMainActivity.java
a obj = new a();
obj.makeVisible((ForgotPasswordActivity) context);

Yes, you should get NPE error. Why? Because look at this code:
button_getUsername.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
BackgroundMainActivity backgroundMainActivity = new BackgroundMainActivity(MainActivity.this, v);
backgroundMainActivity.execute("random", editText_username.getText().toString());
}
});
Here, you are trying to pass a Button (your button_getUserName) to your AsyncTask. Then when you find the view, of course, there's no layout in your button.
How to resolve it? Actually, it's very easy, just pass your layout in the constructor of the AsyncTask as:
BackgroundMainActivity backgroundMainActivity = new BackgroundMainActivity(MainActivity.this, findViewById(R.id.layout_content));
// do on onPost:v.setVisibility...

Related

Spinner with fragment and MySQL database

This is the output image:
Currently I am working with a project of registration form. In that I used four Spinner and three edit text, but when I run the project the app is not getting in a correct order and when varying screen size the form is also changing. I want the app in a correct order and supported for all screen size. I used Fragment but that also is not working.
public class MainActivity extends ActionBarActivity {
ArrayList<String> listItems = new ArrayList<>();
ArrayList<String> listItems1 = new ArrayList<>();
ArrayList<String> listItems2 = new ArrayList<>();
ArrayList<String> listItems3 = new ArrayList<>();
ArrayAdapter<String> adapter;
Spinner sp, sp1, sp2, sp3;
EditText ed1, ed2, ed3;
Button b1;
String crop,state,cat,soil,GetpH, Getphos, Getpottas;
String DataParseUrl = "http://localhost/insert_agri.php";
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// ed1 = (EditText) findViewById(R.id.editText);
sp = (Spinner) findViewById(R.id.spinner);
BackTask bt = new BackTask();
bt.execute();
sp1 = (Spinner) findViewById(R.id.spinner2);
BackTask1 bt1 = new BackTask1();
bt1.execute();
sp2 = (Spinner) findViewById(R.id.spinner3);
BackTask2 bt2 = new BackTask2();
bt2.execute();
sp3 = (Spinner) findViewById(R.id.spinner4);
BackTask4 bt4 = new BackTask4();
bt4.execute();
ed1 = (EditText) findViewById(R.id.editText);
ed2 = (EditText) findViewById(R.id.editText2);
ed3 = (EditText) findViewById(R.id.editText3);
b1=(Button)findViewById(R.id.button);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
GetDataFromEditText();
sendDatatoServer(crop,state,cat,soil,GetpH, Getphos, Getpottas);
}
});
}
private void GetDataFromEditText() {
crop=sp.getSelectedItem().toString();
state=sp1.getSelectedItem().toString();
cat=sp2.getSelectedItem().toString();
soil=sp3.getSelectedItem().toString();
GetpH=ed1.getText().toString();
Getphos=ed2.getText().toString();
Getpottas=ed3.getText().toString();
}
private class BackTask extends AsyncTask<Void, Void, Void> {
ArrayList<String> list;
protected void onPreExecute() {
super.onPreExecute();
list = new ArrayList<>();
}
protected Void doInBackground(Void... params) {
InputStream is = null;
String result = "";
try {
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://........php");
HttpResponse response = httpclient.execute(httppost);
HttpEntity entity = response.getEntity();
// Get our response as a String.
is = entity.getContent();
} catch (IOException e) {
e.printStackTrace();
}
//convert response to string
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(is, "utf-8"));
String line = null;
while ((line = reader.readLine()) != null) {
result += line;
}
is.close();
//result=sb.toString();
} catch (Exception e) {
e.printStackTrace();
}
try {
JSONObject jsono = new JSONObject(result);
JSONArray jArray = jsono.getJSONArray("agriculture");
for (int i = 0; i < jArray.length(); i++) {
JSONObject jsonObject = jArray.getJSONObject(i);
list.add(jsonObject.getString("crop"));
// list.add(jsonObject.getString("country"));
}
} catch (JSONException e) {
e.printStackTrace();
}
return null;
}
protected void onPostExecute(Void result) {
listItems.addAll(list);
adapter = new ArrayAdapter<String>(MainActivity.this, R.layout.spinner_layout, R.id.txt, listItems);
sp.setAdapter(adapter);
}
}
My XML file also attaching with this.
activity_main
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
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"
tools:context="com.example.tony.agri.MainActivity"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:text="Enter Details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="136dp"
android:layout_marginStart="136dp"
android:layout_marginTop="14dp"
android:id="#+id/textView15" />
<TextView
android:text="crop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView15"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="41dp"
android:id="#+id/textView16" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView16"
android:layout_centerHorizontal="true"
android:id="#+id/spinner" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView17"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:id="#+id/spinner2" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/spinner3" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView19"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:id="#+id/spinner4" />
<TextView
android:text="pH"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner4"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="12dp"
android:id="#+id/textView20" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="#+id/spinner4"
android:layout_alignLeft="#+id/textView15"
android:layout_alignStart="#+id/textView15"
android:layout_marginLeft="28dp"
android:layout_marginStart="28dp"
android:id="#+id/editText" />
<TextView
android:text="pospurs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="11dp"
android:id="#+id/textView21" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:text="pottasm"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="13dp"
android:id="#+id/textView22" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:layout_below="#+id/editText2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/editText3" />
<TextView
android:text="state"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="23dp"
android:id="#+id/textView17"
android:layout_below="#+id/spinner"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView18"
android:layout_above="#+id/spinner3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:text="soil"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="13dp"
android:id="#+id/textView19"
android:layout_below="#+id/spinner3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/textView15"
android:layout_alignEnd="#+id/textView15"
android:id="#+id/button" />
spinnerlayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
/>
<TextView
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
/>
<TextView
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
/>
<TextView
android:id="#+id/txt3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
/>
Replace below code with your xml
It would work for your design issues.. i am not able to understand yout order issue. please give me more details.
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:id="#+id/activity_main"
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"
tools:context="com.example.tony.agri.MainActivity"
android:orientation="vertical"
>
<TextView
android:text="Enter Details"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView15" />
<TextView
android:text="crop"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView16" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner2" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner3" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner4" />
<TextView
android:text="pH"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner4"
android:id="#+id/textView20" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:id="#+id/editText" />
<TextView
android:text="pospurs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView21" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText2" />
<TextView
android:text="pottasm"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView22" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText3" />
<TextView
android:text="state"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView17" />
<TextView
android:text="category"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView18" />
<TextView
android:text="soil"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView19" />
<Button
android:text="Button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button" />
Please remove the fixed size of text views and edit texts and use weight to get responsive design.
I make sure you will get layout or design according to your expectations.
if you need code then I can code also but you need to go through first because you will learn it.

How to clear inflate error in android?

Hi I am creating a layout and a ListView within it to display small icon in 5 columns. I am using AysncTask to parse JSON to get the images details which I need to display in list view. Now I am getting error. I don't know how to solve. Please help me to solve.
My layout is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_seatvisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.notebook.runtowin.Activity.Seatvisible">
<TextView
android:id="#+id/from_to"
android:layout_width="match_parent"
android:layout_height="35sp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/colorPrimaryDark"
android:text="Select your seat"
android:padding="5sp"
android:gravity="center_vertical"
android:textStyle="bold"
android:textColor="#color/colorwhite" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="#+id/from_to">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#FFEB3B"
android:id="#+id/headinglayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/busname"
android:padding="5sp"
android:text="RUN TO WIN"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/busname"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/seatnumber"
android:padding="5sp"
android:text="Seats :"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#+id/seatnumber"
android:text="25,24"
android:id="#+id/seatsnumber"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ticketprice"
android:layout_alignParentRight="true"
android:text="hsdshd"
android:textStyle="bold"
android:padding="5sp"
android:textSize="30sp"
android:layout_marginRight="10sp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="#+id/headinglayout"
android:layout_marginTop="3sp"
android:paddingTop="10sp"
android:paddingRight="25sp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/iconsteer"
android:id="#+id/imageView2"
android:layout_marginEnd="50sp"
android:layout_alignParentEnd="true"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView2"
android:layout_marginTop="20sp"
android:id="#+id/listviewseat">
</ListView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="25sp"
android:layout_alignParentEnd="true"
android:gravity="center_horizontal"
android:background="#color/colorPrimaryDark"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BOOK TICKETS"
android:padding="10sp"
android:id="#+id/bt_book"
android:textSize="20sp"/>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
My Listview Layout is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8sp"
android:gravity="top">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageViewseat"/>
</LinearLayout>
My code is:
public class Seatvisible extends AppCompatActivity {
ArrayList<HashMap<String, String>> busdetails1;
private String BusID, servicedate;
private static String url="http://appsilonz.com/demo/v2/bus_seat_abhi.php?journey_date=2017-01-05&bus_id=20";
private ListView lv;
private TextView busname, from_to, seatsnumber, ticketprice;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_seatvisible);
lv = (ListView) findViewById(R.id.listviewbusdetails);
inilize();
new Seatparsing().execute();
}
private void inilize() {
Intent intent = getIntent();
BusID = intent.getStringExtra("BusID");
servicedate = intent.getStringExtra("servicedate");
busname = (TextView) findViewById(R.id.busname);
from_to = (TextView) findViewById(R.id.from_to);
seatsnumber = (TextView) findViewById(R.id.seatsnumber);
ticketprice = (TextView) findViewById(R.id.ticketprice);
}
public class Seatparsing extends AsyncTask<String, String, ArrayList<HashMap<String, String>>> {
String response;
String jsonstr, seat_name, ticket_status, seatladies;
ProgressDialog dialog = new ProgressDialog(Seatvisible.this);
AndroidHttpClient maclient = AndroidHttpClient.newInstance("");
#Override
protected void onPreExecute() {
dialog.setCancelable(false);
dialog.setMessage("Please wait..");
dialog.setIndeterminate(false);
dialog.show();
}
#Override
protected ArrayList<HashMap<String, String>> doInBackground(String... params) {
HttpGet request = new HttpGet(url);
ResponseHandler<String> responseHandler = new BasicResponseHandler();
try {
response = maclient.execute(request, responseHandler);
} catch (ClientProtocolException exception) {
exception.printStackTrace();
return null;
} catch (IOException exception) {
exception.printStackTrace();
return null;
}
Log.i("Seats", "" + response);
Log.i("seaturl", "" + url);
jsonstr = response;
busdetails1 = new ArrayList<>();
if (jsonstr != null) {
HashMap<String, String> map2 = new HashMap<>();
try {
JSONArray array1 = new JSONArray(jsonstr);
for (int i = 0; i < array1.length(); i++) {
JSONObject objct1 = array1.getJSONObject(i);
JSONArray array2 = objct1.getJSONArray("row1");
for (int j = 0; j < array2.length(); j++) {
JSONObject objct2 = array2.getJSONObject(j);
seat_name = objct2.getString("seat_name ").toString();
ticket_status = objct2.getString("ticket_status");
seatladies = objct2.getString("seatIsLadies");
map2.put("seat_name", seat_name);
map2.put("ticket_status", ticket_status);
map2.put("seatIsLadies", seatladies);
}
busdetails1.add(map2);
}
} catch (Exception e) {
e.printStackTrace();
}
}
return busdetails1;
}
protected void onPostExecute(ArrayList<HashMap<String, String>> busdetails1) {
dialog.dismiss();
customadapter adapter = new customadapter(getApplicationContext(), R.layout.seatlistview, busdetails1);
lv.setAdapter(adapter);
}
}
public class customadapter extends ArrayAdapter {
private ArrayList<HashMap<String, String>> list;
private int resource;
private LayoutInflater inflater;
public customadapter(Context context, int resource, ArrayList<HashMap<String, String>> busdetails1) {
super(context, resource, busdetails1);
list = busdetails1;
this.resource = resource;
inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
}
#NonNull
#Override
public View getView(int i, View convertView, ViewGroup parent) {
convertView = inflater.inflate(resource, null);
ImageView imageView = (ImageView) convertView.findViewById(R.id.imageViewseat);
String seat_name = list.get(i).get("seat_name");
String ticket_status = list.get(i).get("ticket_status");
String seatIsLadies = list.get(i).get("seatIsLadies");
if (!seat_name.equals(null)) {
if (ticket_status.equals("booked")) {
if (seatIsLadies.equals("true")) {
imageView.setImageResource(R.drawable.reservedladies);
imageView.setClickable(false);
} else {
imageView.setImageResource(R.drawable.bookedseat);
imageView.setClickable(false);
}
} else {
imageView.setImageResource(R.drawable.available_seat_img);
}
} else {
imageView.setImageDrawable(null);
imageView.setClickable(false);
}
return convertView;
}
}
}
You should use dp for dimensions and sp for textSize. Currently you are using sp for dimensions.
See the edited layout below,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_seatvisible"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.notebook.runtowin.Activity.Seatvisible">
<TextView
android:id="#+id/from_to"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#color/colorPrimaryDark"
android:text="Select your seat"
android:padding="5dp"
android:gravity="center_vertical"
android:textStyle="bold"
android:textColor="#color/colorwhite" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="#+id/from_to">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#FFEB3B"
android:id="#+id/headinglayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/busname"
android:padding="5dp"
android:text="RUN TO WIN"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/busname"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/seatnumber"
android:padding="5dp"
android:text="Seats :"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:text="25,24"
android:id="#+id/seatsnumber"/>
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/ticketprice"
android:layout_alignParentRight="true"
android:text="hsdshd"
android:textStyle="bold"
android:padding="5dp"
android:textSize="30sp"
android:layout_marginRight="10dp"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_below="#+id/headinglayout"
android:layout_marginTop="3dp"
android:paddingTop="10dp"
android:paddingRight="25dp"
android:paddingLeft="25dp">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/iconsteer"
android:id="#+id/imageView2"
android:layout_marginEnd="50dp"
android:layout_alignParentEnd="true"/>
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView2"
android:layout_marginTop="20dp"
android:id="#+id/listviewseat">
</ListView>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="25dp"
android:layout_alignParentEnd="true"
android:gravity="center_horizontal"
android:background="#color/colorPrimaryDark"
>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BOOK TICKETS"
android:padding="10dp"
android:id="#+id/bt_book"
android:textSize="20sp"/>
</RelativeLayout>
</LinearLayout>
For ListView layout,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:gravity="top">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageViewseat"/>
</LinearLayout>
Also you can't layout_marginRight to an id at this line,
android:layout_marginRight="#+id/seatnumber"
You probably meant,
android:layout_marginRight="5dp"
See #Baseem Samy's answer too.
I see sp units in ur layout files for padding size. sp is for font size only where dp or dip is for all dimensions.
in the textview with id android:id="#+id/seatsnumber"
you set layout_marginRight to an id, that's not available in android you can't set a padding with a view
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="#+id/seatnumber"
android:text="25,24"
android:id="#+id/seatsnumber"/>

Gridview shows only two items android

// mainactivity.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:background="#color/red"
android:gravity="bottom" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:background="#android:color/background_light" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_action_logo" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="14dp"
android:layout_toRightOf="#+id/imageView1"
android:text="#string/app_name" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_overflow" />
</RelativeLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="0.01dp" >
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.91"
android:background="#android:color/background_light" >
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="3dp"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="3dp"
android:src="#drawable/add"
android:onClick="moreEvent" />
</RelativeLayout>
<RelativeLayout
android:layout_width="162dp"
android:layout_height="match_parent"
android:background="#android:color/background_light" >
<ImageView
android:id="#+id/imageView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp"
android:src="#drawable/book"
android:onClick="chatNow" />
</RelativeLayout>
</LinearLayout>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="35dp">
<RelativeLayout
android:id="#+id/relativeLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<RelativeLayout
android:id="#+id/relativeLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="5dp"
android:layout_marginTop="5dp"
android:background="#android:color/background_light"
android:gravity="center" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/imageView7"
android:layout_marginLeft="14dp"
android:layout_marginTop="14dp"
android:text="Event Name" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView3"
android:layout_below="#+id/textView3"
android:text="Venue, Date"
android:textSize="12sp" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/textView4"
android:text="Description"
android:textSize="12sp" />
<ImageView
android:id="#+id/imageView7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_marginTop="14dp"
android:src="#drawable/demo" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Event of the Week" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout2"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:background="#android:color/background_light" >
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="Today&apos;s Events" />
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="30dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
android:verticalSpacing="10dp" >
</GridView>
</RelativeLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
//mainactivity.java
class LoadProfile extends AsyncTask<String, String, String> {
#Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(EventHome.this);
pDialog.setMessage("Loading...");
pDialog.setIndeterminate(false);
pDialog.setCancelable(false);
pDialog.show();
}
protected String doInBackground(String... args) {
// Building Parameters
String json = null;
try {
List<NameValuePair> params = new ArrayList<NameValuePair>();
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost(PROFILE_URL);
httppost.setEntity(new UrlEncodedFormEntity(params));
// Execute HTTP Post Request
HttpResponse response = httpclient.execute(httppost);
HttpEntity resEntity = response.getEntity();
json = EntityUtils.toString(resEntity);
Log.i("All Events: ", json.toString());
} catch (Exception e) {
e.printStackTrace();
}
return json;
}
#Override
protected void onPostExecute(String json) {
super.onPostExecute(json);
pDialog.dismiss();
try{
event = new JSONObject(json);
final ArrayList<HashMap<String, String>> arraylist = new ArrayList<HashMap<String, String>>();
JSONArray user = event.getJSONArray("events");
for (int i = 0; i < user.length(); i++) {
JSONObject object = user.getJSONObject(i);
HashMap<String, String> map = new HashMap<String, String>();
map.put("id", object.getString("id"));
map.put("name", object.getString("name"));
map.put("date_d", object.getString("date_d"));
map.put("location", object.getString("location"));
map.put("images", "http://www.example.com/"+object.getString("images"));
arraylist.add(map);
}
String[] from = {"name", "date_d", "location", "images"};
int[] to = {R.id.textView1, R.id.textView2, R.id.textView3, R.id.iv_flag};
ListAdapter adapters = new MyAdapter(EventHome.this,arraylist,R.layout.list_event_home,from,to);
gv1.setAdapter(adapters);
gv1.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), ""+arg2, Toast.LENGTH_LONG).show();
}
});
}catch(Exception e)
{
e.printStackTrace();
}
}
//MyAdapter.java
public class MyAdapter extends SimpleAdapter{
public MyAdapter(Context context, List<? extends Map<String, ?>> data, int resource, String[] from, int[] to){
super(context, data, resource, from, to);
}
public View getView(int position, View convertView, ViewGroup parent){
// here you let SimpleAdapter built the view normally.
View v = super.getView(position, convertView, parent);
// Then we get reference for Picasso
ImageView img = (ImageView) v.getTag();
if(img == null){
img = (ImageView) v.findViewById(R.id.iv_flag);
v.setTag(img); // <<< THIS LINE !!!!
}
// get the url from the data you passed to the `Map`
#SuppressWarnings("rawtypes")
String url = (String) ((Map)getItem(position)).get("images");
// do Picasso
Picasso.with(v.getContext()).load(url).into(img);
// return the view
return v;
}
}
while executing the above code my gridview shows only 2 items. Why is it so. Is there any mistake in my xml. Can anyone knows how to fix it. I am stuck in here. Waiting for answers.
your gridview contains scroll view so there is a chance to reduce the actual items in your gridview. So try to remove scroll view.

Android: how to Populate ListView smoothly (x items/server side call) and let user interact with it while loading?

[EDIT] code updated
I have a listview which going to have about 100-200 rows, I'have created a AsyncTask class which loads the data from server side using http request and because there is many items I decided to load 10 items/call, and display them in the list and let the user to interact with them while I call the AsyncTask class again to load more 10 items....
Here is the design of the listview (photoshop)
It works great BUT when the listview is being updated it's NOT responding for few moments to scroll, and then it responds good and then it's being updated again and it's not responding for few moments...till it fully loaded from server side. I also tried RUNNABLE thread but the same behavior :-(
How can I populate the listview SMOOTHLY and let the user interact with it while it's populating.
class DBreadInvItems extends AsyncTask<Void, Void, String> {
Connection dbConnection = null;
Statement statement = null;
ResultSet resultSet = null;
ImageView splash;
CstmrInvoice activity;
JSONObject jObj;
String phpResult;
String db;
ItemsDialog itemsdialog;
Integer ttl_read=0;
//----------------------------------------------------------------------------------------------
protected DBreadInvItems(CstmrInvoice a,ItemsDialog itemsdialog) {
this.activity = a;
this.itemsdialog=itemsdialog;
Intent iin= this.activity.getIntent();
Bundle b = iin.getExtras();
db = b.getString("db");
}
//----------------------------------------------------------------------------------------------
#Override
protected void onPreExecute() {
phpResult="";
AnimateImgLoad();
}
//----------------------------------------------------------------------------------------------
#Override
protected String doInBackground(Void... params) {
fetchitems();
return null;
}
//----------------------------------------------------------------------------------------------
#Override
protected void onPostExecute(String result) {
if(phpResult.equals(""))
{
Toast.makeText(itemsdialog.dialog.getContext(), R.string.msg_nointernet, Toast.LENGTH_LONG).show();
return;
}
else
{
if(ttl_read>=10) {
itemsdialog.adapter_items.notifyDataSetChanged();
itemsdialog.finalData = itemsdialog.list_items;
itemsdialog.setListViewHeightBasedOnChildren((ListView) itemsdialog.dialog.findViewById(R.id.listViewInvPopupItems));
//try reading more...
AnimateImgLoad();
itemsdialog.ReadMoreData();
}
}
AnimateImgLoad();
}
//----------------------------------------------------------------------------------------------
private void fetchitems()
{
try
{
JSONObject json=new JSONObject();
json.put("db",db);
json.put("action","fetchitems");
json.put("seq",itemsdialog.SEQ.toString());
HttpParams httpparams= new BasicHttpParams();
HttpConnectionParams.setConnectionTimeout(httpparams, 10000);
HttpClient client=new DefaultHttpClient(httpparams);
String url="http://roya4u.com:8972/roya/invoices.php";
HttpPost request=new HttpPost(url);
request.setEntity(new ByteArrayEntity(json.toString().getBytes("UTF8")));
request.setHeader("json",json.toString());
HttpResponse response=client.execute(request);
HttpEntity entity=response.getEntity();
if (entity != null) {
InputStream instream = entity.getContent();
phpResult= RestClient.convertStreamToString(instream);
}
}
catch (Throwable t)
{
}
if(phpResult.equals(""))
{
//Toast.makeText(itemsdialog.dialog.getContext(), R.string.msg_nointernet, Toast.LENGTH_LONG).show();
return;
}
try
{
JSONObject object = new JSONObject(phpResult);
final String islogged = object.getString("success");
if(Boolean.valueOf(islogged))//success=true
{
ttl_read=Integer.parseInt(object.getString("total").toString());
if(ttl_read>0) {
itemsdialog.SEQ = Integer.parseInt(object.getString("seq"));
//Toast.makeText(itemsdialog.dialog.getContext(),itemsdialog.SEQ, Toast.LENGTH_SHORT).show();
for (int i = 0; i < object.getJSONArray("items").length(); i++) {
JSONObject j = object.getJSONArray("items").optJSONObject(i);
itemsdialog.list_items.add(new MyInvoiceItems(Integer.parseInt(j.get("id").toString()), j.get("mkat").toString(), 1, Double.parseDouble(j.get("ccost").toString()), j.get("iname").toString(), 0));
}
}
}
else
{
//Toast.makeText(itemsdialog.dialog.getContext(), R.string.msg_errorlogin, Toast.LENGTH_LONG).show();
}
}
catch (JSONException e)
{
//Toast.makeText(itemsdialog.dialog.getContext(),R.string.msg_nointernet, Toast.LENGTH_LONG).show();
}
}
//----------------------------------------------------------------------------------------------
//------------------------------
private void AnimateImgLoad()
{
RotateAnimation anim;
if(splash==null)
{
anim = new RotateAnimation(0f, 360f,
Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF,
0.5f);
anim.setInterpolator(new LinearInterpolator());
anim.setRepeatCount(Animation.INFINITE);
anim.setDuration(700);
//Start animating the image
splash = (ImageView) itemsdialog.dialog.findViewById(R.id.imgReload);
splash.startAnimation(anim);
ProgressBar progressbar=(ProgressBar) itemsdialog.dialog.findViewById(R.id.invPopupProgressBar);
progressbar.setVisibility(View.VISIBLE);
}
else
{
splash.setAnimation(null);
splash=null;
ProgressBar progressbar=(ProgressBar) itemsdialog.dialog.findViewById(R.id.invPopupProgressBar);
progressbar.setVisibility(View.GONE);
}
}
}
ADAPTER:
public class SearchableItemsAdapter extends BaseAdapter implements Filterable
{
ArrayList<MyInvoiceItems> filteredData = new ArrayList<MyInvoiceItems>();
private ItemFilter mFilter;
private LayoutInflater mInflater;
//
public SearchableItemsAdapter(Context context, ArrayList<MyInvoiceItems> data) {
filteredData = data;
mInflater = LayoutInflater.from(context);
}
//
public int getCount() {
return filteredData.size();
}
//
public Object getItem(int position) {
return filteredData.get(position);
}
//
public long getItemId(int position) {
return position;
}
//
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolderItems itemsholder;
if(convertView==null)
{
convertView=mInflater.inflate(R.layout.lstcstmrinvoicepopupitems,null);
itemsholder=new ViewHolderItems();
itemsholder.iTaken=(CheckBox) convertView.findViewById(R.id.inv_popupTaken);
itemsholder.iName=(TextView) convertView.findViewById(R.id.inv_popupName);
itemsholder.iQuan=(EditText) convertView.findViewById(R.id.inv_popupQuan);
itemsholder.iPrice=(EditText) convertView.findViewById(R.id.inv_popupPrice);
itemsholder.iTotal=(TextView) convertView.findViewById(R.id.inv_popupTotal);
convertView.setTag(itemsholder);
}
else
{
itemsholder=(ViewHolderItems) convertView.getTag();
}
if(filteredData!=null) {
itemsholder.iTaken.setChecked(false);
if (filteredData.get(position).getTaken() == 1)
itemsholder.iTaken.setChecked(true);
itemsholder.iName.setText(filteredData.get(position).getItem_desc());
itemsholder.iQuan.setText(filteredData.get(position).getItem_num().toString());
itemsholder.iPrice.setText(filteredData.get(position).getItem_cost().toString());
itemsholder.iTotal.setText(filteredData.get(position).getAmountTotal());
}
return convertView;
}
//
public class ViewHolderItems {
CheckBox iTaken;
TextView iName;
EditText iQuan;
EditText iPrice;
TextView iTotal;
}
//
public Filter getFilter() {
if(mFilter==null) mFilter = new ItemFilter();
return mFilter;
}
//
private class ItemFilter extends Filter {
#Override
protected FilterResults performFiltering(CharSequence constraint) {
String filterString = constraint.toString().toLowerCase();
FilterResults results = new FilterResults();
//validate
if(filterString.toString().trim().equals(""))
{
results.values = list_items;
results.count = list_items.size();
}
else
{
ArrayList<MyInvoiceItems> tempData =list_items;
finalData = new ArrayList<MyInvoiceItems>();
//mhmd
int count = tempData.size();
String filterableString ;
for (int i = 0; i < count; i++) {
filterableString = tempData.get(i).getItem_desc();
if (filterableString.toLowerCase().contains(filterString)) {
finalData.add(new MyInvoiceItems(tempData.get(i).getItem_key(),tempData.get(i).getItem_mkat(),tempData.get(i).getItem_num(),tempData.get(i).getItem_cost(),tempData.get(i).getItem_desc(),tempData.get(i).getTaken()));
}
}
results.values = finalData;
results.count = finalData.size();
}
return results;
}
#Override
protected void publishResults(CharSequence constraint, Filter.FilterResults results) {
filteredData =(ArrayList<MyInvoiceItems>) results.values;
notifyDataSetChanged();
}
}
}
popup_invitems.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:background="#ffe3e3e3">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="48dp"
android:id="#+id/tbl_mainTitle"
android:background="#ffffffff">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbl_searchIcon"
android:layout_alignParentRight="false"
android:layout_marginTop="8dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="8dp">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgReload"
android:src="#drawable/ic_reload" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="false"
android:id="#+id/tbl_txtName"
android:layout_marginTop="11dp"
android:layout_marginRight="4dp"
android:layout_toLeftOf="#+id/tbl_sort">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="#string/inv_popup_itemname_title"
android:singleLine="true"
android:id="#+id/txtName"
android:textSize="16dp"
android:textColor="#ff3c3c3c" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tbl_edtName"
android:layout_toLeftOf="#+id/tbl_txtName"
android:layout_marginTop="6dp"
android:layout_toRightOf="#+id/tbl_searchIcon"
android:layout_marginLeft="10dp"
android:stretchColumns="*">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/edtName"
android:background="#drawable/edittext" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:id="#+id/tbl_sort"
android:layout_marginTop="13dp"
android:layout_marginRight="8dp" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgSort"
android:src="#drawable/ic_sortb"
android:cropToPadding="false" />
</TableRow>
</TableLayout>
<ProgressBar
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:indeterminateOnly="false"
style="#style/ProgressBar.Horizontal"
android:indeterminate="true"
android:id="#+id/invPopupProgressBar"
android:visibility="gone"
android:layout_below="#+id/tbl_sort"
android:layout_marginTop="2dp"
android:layout_marginBottom="2dp" />
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/scrollView"
android:layout_below="#+id/tbl_mainTitle"
android:layout_above="#+id/Relative_Buttons"
android:layout_marginBottom="2dp"
android:layout_marginTop="5dp">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ListView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/listViewInvPopupItems"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#fff4f4f4" />
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:id="#+id/Relative_Buttons"
android:layout_marginRight="8dp"
android:layout_marginLeft="8dp">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbl_yes"
android:layout_alignParentRight="true">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="161dp"
android:layout_height="50dp"
android:text="#string/inv_popup_btnOK_title"
android:id="#+id/txtYes"
android:drawableRight="#drawable/ic_ok"
android:drawablePadding="10dp"
android:singleLine="true"
android:textSize="16dp"
android:background="#fff4f4f4"
android:paddingRight="50dp"
android:gravity="center_vertical"
android:textColor="#ff828282" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbl_no"
android:layout_toLeftOf="#+id/tbl_yes"
android:layout_marginRight="2dp">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="161dp"
android:layout_height="50dp"
android:text="#string/inv_popup_btnCancel_title"
android:id="#+id/txtNo"
android:drawableRight="#drawable/ic_delete"
android:drawablePadding="10dp"
android:singleLine="true"
android:textSize="16dp"
android:background="#fff4f4f4"
android:paddingRight="50dp"
android:gravity="center_vertical|right"
android:textColor="#ff828282" />
</TableRow>
</TableLayout>
</RelativeLayout>
</RelativeLayout>
lstcstmrinvoicepopupitems.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:background="#fff4f4f4"
android:paddingBottom="8dp"
android:paddingTop="3dp">
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbl_check"
android:layout_alignParentRight="true">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/inv_popupTaken" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="285dp"
android:layout_height="wrap_content"
android:id="#+id/tbl_name"
android:layout_toLeftOf="#+id/tbl_check"
android:layout_marginTop="7dp">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="צבע יסוד טרי מתאים לברזל וגם לעץ מהיר בייבוש"
android:id="#+id/inv_popupName"
android:textSize="15dp"
android:textColor="#ff000000"
android:singleLine="true" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbl_txtQuan"
android:layout_below="#+id/tbl_check"
android:layout_alignParentRight="true"
android:layout_marginRight="7dp"
android:layout_marginTop="3dp">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="#string/inv_quan_title"
android:id="#+id/inv_txtQuan"
android:textSize="15dp"
android:textColor="#ff8c8a8a"
android:singleLine="true" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbl_edtQuan"
android:layout_below="#+id/tbl_check"
android:layout_toLeftOf="#+id/tbl_txtQuan">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<EditText
android:layout_width="60dp"
android:layout_height="wrap_content"
android:id="#+id/inv_popupQuan"
android:background="#drawable/edittext"
android:text="15400"
android:textColor="#ff8c8a8a"
android:textSize="15dp"
android:singleLine="true" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbl_txtPrice"
android:layout_below="#+id/tbl_check"
android:layout_alignParentRight="false"
android:layout_marginRight="12dp"
android:layout_marginTop="3dp"
android:layout_toLeftOf="#+id/tbl_edtQuan">
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="45dp"
android:layout_height="wrap_content"
android:text="#string/inv_price_title"
android:id="#+id/txt_Price"
android:textSize="15dp"
android:textColor="#ff8c8a8a"
android:singleLine="true" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbl_edtPrice"
android:layout_below="#+id/tbl_check"
android:layout_toLeftOf="#+id/tbl_txtPrice" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<EditText
android:layout_width="70dp"
android:layout_height="wrap_content"
android:id="#+id/inv_popupPrice"
android:background="#drawable/edittext"
android:text="19540.66"
android:textColor="#ff8c8a8a"
android:textSize="15dp"
android:singleLine="true" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbl_equal"
android:layout_below="#+id/tbl_check"
android:layout_alignParentRight="false"
android:layout_marginRight="2dp"
android:layout_marginTop="3dp"
android:layout_toLeftOf="#+id/tbl_edtPrice" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="="
android:id="#+id/txt_equal"
android:textSize="15dp"
android:textColor="#ff8c8a8a"
android:singleLine="true" />
</TableRow>
</TableLayout>
<TableLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tbl_total"
android:layout_below="#+id/tbl_check"
android:layout_alignParentRight="false"
android:layout_marginRight="2dp"
android:layout_marginTop="3dp"
android:layout_toLeftOf="#+id/tbl_equal" >
<TableRow
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="128549.87"
android:id="#+id/inv_popupTotal"
android:textSize="15dp"
android:textColor="#ff8c8a8a"
android:singleLine="true" />
</TableRow>
</TableLayout>
</RelativeLayout>
If the listview is not responding while being updated it means the "update" is doing heavy work on the main thread. What you want to do to make it smoother is delegate as much of this work to an other thread. This includes :
Network operations or any I/O operations
reading a database
parsing / unserializing JSON, xml, ... into objects
EDIT: As per your edit on the question, I can see you are doing some parsing in your onPostExecute() method of your asynctask which is executed on the main thread. You should do this work in the doInBackground() method and only update the adapter in the onPostExecute() method. In order to do that, you can return a list of MyInvoiceItems from your doInBackground method which will be passed as a parameter on your onPostExecute() method and then do something like this:
#Override
protected void onPostExecute(List<MyInvoiceItems> result) {
if(result == null)
Toast.makeText(itemsdialog.dialog.getContext(), R.string.msg_nointernet, Toast.LENGTH_LONG).show();
else{
itemsdialog.list_items.add(result);
itemsdialog.adapter_items.notifyDataSetChanged();
}
}
I think, your heavy work is here:setListViewHeightBasedOnChildren
this method will measure each child and get each item height,and add up to a total height for listView. (does it work so?).
as you have many rows,it will become a dangerous work.
this method works well at showing a few rows all in a vertical scrollview.
and, why you call notifyDatasetChange before dataset Changed?
itemsdialog.adapter_items.notifyDataSetChanged();
itemsdialog.finalData = itemsdialog.list_items;
It sounds like 10 items may even be too many. Try loading 1 at a time and see if performance improves.

Issue with Android Check Box Visibility?

In my application I have two Check boxes one is PayByCredit and one more is PaybySMS For Both the check boxes are not visible what was the issue ? To display the Check box i gave code like this android:visibility="visible" but still no use . Can any one help me out of this please ?
My XML Code is :
<?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="wrap_content"
android:background="#drawable/homescreeen2"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/toplayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:fontFamily="Optima"
android:gravity="center"
android:text="#string/palmtreeimg"
android:textColor="#ffffff"
android:textStyle="bold" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="16dp"
android:layout_marginTop="4dp"
android:background="#null"
android:contentDescription="#string/backbt"
android:src="#drawable/backbt" />
</RelativeLayout>
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/toplayout"
android:paddingBottom="10dp"
android:scrollbars="none" >
<RelativeLayout
android:id="#+id/mainlayout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:layout_marginLeft="10dp"
android:fontFamily="#+fonts/MontereyFLF"
android:text="#string/realname"
android:textColor="#ff0000" />
<LinearLayout
android:id="#+id/mainlayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_margin="15dp"
android:background="#drawable/layout_bg"
android:orientation="vertical"
android:paddingBottom="5dp" >
<LinearLayout
android:id="#+id/getaplague"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" >
<include
android:id="#+id/cmpndbtnName"
android:layout_gravity="center"
android:layout_margin="10dp"
android:layout_marginStart="#+id/treesplanted"
layout="#layout/compound_button" />
</LinearLayout>
<View
android:id="#+id/border"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88"
android:visibility="gone" />
<LinearLayout
android:id="#+id/entername"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:alpha="0.8"
android:orientation="horizontal"
android:visibility="gone" >
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name"
android:textColor="#ff0000" />
</LinearLayout>
<ImageButton
android:id="#+id/imageButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="5dp"
android:background="#null"
android:contentDescription="#string/btn_save"
android:src="#drawable/savesmall"
android:visibility="gone" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/mainlayout2"
android:layout_margin="15dp"
android:background="#drawable/layout_bg"
android:orientation="vertical" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" >
<include
android:id="#+id/cmpndbtnfrnname"
android:layout_marginStart="#+id/treesplanted"
layout="#layout/compound_buttonfrnname" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88" />
<LinearLayout
android:id="#+id/enterfrnname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:alpha="0.8"
android:orientation="horizontal"
android:visibility="gone" >
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88" />
<LinearLayout
android:id="#+id/enterfrnmailid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:orientation="horizontal"
android:visibility="gone" >
<EditText
android:id="#+id/editTextfrnmail"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_weight="1"
android:background="#null"
android:hint="#string/enterfrnmailid"
android:inputType="text"
android:textSize="12sp"
android:paddingLeft="5dp"
android:textColor="#000000"
android:textColorHint="#999898" >
</EditText>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88" />
<LinearLayout
android:id="#+id/enterfrnphoneno"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:orientation="horizontal"
android:visibility="gone" >
</EditText>
</LinearLayout>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88" />
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88" />
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout1"
android:layout_margin="15dp"
android:layout="#+id/amntlayout"
android:orientation="horizontal" >
</LinearLayout>
<LinearLayout
android:id="#+id/mainlayoutpay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout2"
android:layout_margin="15dp"
android:background="#drawable/layout_bg"
android:orientation="vertical"
android:paddingBottom="5dp" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/txpaycredit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:fontFamily="Optima"
android:text="#string/paycredit"
android:textColor="#ff0000" />
<TextView
android:id="#+id/dum"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:fontFamily="Optima"
android:text=""
/>
<CheckBox
android:id="#+id/paycredit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#null"
android:visibility="visible"
android:textColor="#000000" />
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:background="#a79a88" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_marginTop="0dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/paybysms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text="#string/paysms"
android:textColor="#ff0000" />
<TextView
android:id="#+id/dummy"
android:layout_width="70dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:text=""
android:textColor="#ff0000" />
<CheckBox
android:id="#+id/paysms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:background="#null"
android:visibility="visible"
android:layout_marginLeft="10dp"
android:fontFamily="#+fonts/MontereyFLF"
android:textColor="#000000" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
And My .java Code is here:
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.paypal.android.sdk.payments.PayPalConfiguration;
import com.paypal.android.sdk.payments.PayPalPayment;
import com.paypal.android.sdk.payments.PayPalService;
import com.paypal.android.sdk.payments.PaymentActivity;
import com.paypal.android.sdk.payments.PaymentConfirmation;
public class PalmTreeActivity extends Activity {
//Used for payment
double currencyAftconversion;
public String treeValue;
private static final String CONFIG_ENVIRONMENT = PayPalConfiguration.ENVIRONMENT_NO_NETWORK;
private static final String CONFIG_CLIENT_ID = "AWg-FRC9GM1CdolC2XnKnYBEgpi01jDlKi6IEXFcEnu4QIfer6cVE0iH9YNI";
private static PayPalConfiguration config = new PayPalConfiguration()
.environment(CONFIG_ENVIRONMENT)
.clientId(CONFIG_CLIENT_ID)
.merchantName("Isaaf")
.merchantPrivacyPolicyUri(
Uri.parse("https://www.example.com/privacy"))
.merchantUserAgreementUri(
Uri.parse("https://www.example.com/legal"));
String AmountOfTree = null;
#Override
protected void onCreate(Bundle savedInstanceState) {
String price = null;
//call currency conversion method
new DownloadCurrencyConversion().execute();
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_palmtree);
ImageButton btnNext = (ImageButton) findViewById(R.id.imageButton2);
ImageButton btBack = (ImageButton) findViewById(R.id.imageButton1);
final CheckBox paybycard = (CheckBox) findViewById(R.id.paycredit);
//paycredit.setVisibility(View.VISIBLE);
final CheckBox paybysms = (CheckBox) findViewById(R.id.paysms);
final CompoundButton switchrealname= (CompoundButton) findViewById(R.id.enabled);
final CompoundButton switchfrnname=(CompoundButton) findViewById(R.id.enabledfrnname);
final LinearLayout friendName=(LinearLayout) findViewById(R.id.enterfrnname);
final LinearLayout friendMailId=(LinearLayout) findViewById(R.id.enterfrnmailid);
final LinearLayout friendPhoneno=(LinearLayout) findViewById(R.id.enterfrnphoneno);
final LinearLayout Name=(LinearLayout) findViewById(R.id.entername);
final EditText username=(EditText)findViewById(R.id.editText1);
final EditText frnName=(EditText)findViewById(R.id.editTextfrnName);
final EditText frnmail=(EditText)findViewById(R.id.editTextfrnmail);
final EditText frnphoneno=(EditText)findViewById(R.id.editTextfrnphoneno);
// final ImageButton savename=(ImageButton)findViewById(R.id.imageButton3);
// final ImageButton savefrnname=(ImageButton) findViewById(R.id.imageButton4);
if (getIntent().getExtras() != null && !TextUtils.isEmpty("price"))
{
price = getIntent().getExtras().getString("price");
TextView amount = (TextView) findViewById(R.id.amountvalue);
amount.setGravity(Gravity.CENTER);
String Pricedouble = price.substring(price.indexOf('('));
Pricedouble = Pricedouble.replaceAll("\\D+", "");
amount.setText(Pricedouble.toString()); // Get Number
//EditText amounttext = (EditText) findViewById(R.id.editText1);
//amounttext.setText(Pricedouble);
AmountOfTree = Pricedouble.toString();
Intent intentActivity = new Intent(this, PayPalService.class);
intentActivity.putExtra(PayPalService.EXTRA_PAYPAL_CONFIGURATION, config);
startService(intentActivity);
}
//button next click
btnNext.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if (paybycard.isChecked()) {
Bundle bundle=new Bundle();
if(!TextUtils.isEmpty(username.getText().toString()))
bundle.putString("name", username.getText().toString());
if(!TextUtils.isEmpty(frnName.getText().toString()))
bundle.putString("frnname", frnName.getText().toString());
if(!TextUtils.isEmpty(frnmail.getText().toString()))
bundle.putString("frnmailid", frnmail.getText().toString());
if(!TextUtils.isEmpty(frnphoneno.getText().toString()))
bundle.putString("frnphoneno", frnphoneno.getText().toString());
PayPalPayment payment = new PayPalPayment(new BigDecimal(
currencyAftconversion), "USD", "Tree",
PayPalPayment.PAYMENT_INTENT_SALE);
Intent intent = new Intent(getApplicationContext(),
PaymentActivity.class);
intent.putExtra(PaymentActivity.EXTRA_PAYMENT, payment);
startActivityForResult(intent, 0);
}
else if(paybysms.isChecked())
{
StringBuilder tmp=new StringBuilder();
if(!TextUtils.isEmpty(username.getText().toString()));
tmp.append("User details are :"+"\n");
tmp.append("UserName is:"+username.getText().toString());
tmp.append("\n");
//tmp.append(username.getText().toString());
if(!TextUtils.isEmpty(frnName.getText().toString()))
tmp.append("Friend Name is:"+frnName.getText().toString());
tmp.append("\n");
if(!TextUtils.isEmpty(frnmail.getText().toString()))
tmp.append("Friend Email id is:"+frnmail.getText().toString());
tmp.append("\n");
if(!TextUtils.isEmpty(frnphoneno.getText().toString()))
tmp.append("Friend phone number is:"+frnphoneno.getText().toString());
PayByCash senddetailstoadmin=new PayByCash();
if(!TextUtils.isEmpty(tmp.toString()))
senddetailstoadmin.execute(tmp.toString());
Toast paymessage=Toast.makeText(getApplicationContext(), "Our customer care people will contact you shortly..!!!!", Toast.LENGTH_SHORT);
paymessage.show();
Intent intent=new Intent( getApplicationContext(),PaycashthanksActivity.class);
startActivity(intent);
}
else
{
Toast paywarning=Toast.makeText(getApplicationContext(), "Please pay using card or pay by cash", Toast.LENGTH_SHORT);
paywarning.show();
}
}
});
btBack.setOnClickListener(new OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(getApplicationContext(),
FindmarkedLocationActivity.class);
startActivity(intent);
}
});
//switchrealname toggle
switchrealname.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
if(switchrealname.isChecked())
{
Name.setVisibility(View.VISIBLE); //Set the layout visible
// savename.setVisibility(View.VISIBLE);
}
else
{
Name.setVisibility(View.GONE);
// savename.setVisibility(View.GONE);
}
}
});
//Switch friend details switch toggle checked event
switchfrnname.setOnCheckedChangeListener(new OnCheckedChangeListener()
{
#Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked)
{
if(switchfrnname.isChecked())
{
//Set the layout visible
friendName.setVisibility(View.VISIBLE);
friendMailId.setVisibility(View.VISIBLE);
friendPhoneno.setVisibility(View.VISIBLE);
// savefrnname.setVisibility(View.VISIBLE);
}
else
{
//Set the layout visible
friendName.setVisibility(View.GONE);
friendMailId.setVisibility(View.GONE);
friendPhoneno.setVisibility(View.GONE);
// savefrnname.setVisibility(View.GONE);
}
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK) {
PaymentConfirmation confirm = data
.getParcelableExtra(PaymentActivity.EXTRA_RESULT_CONFIRMATION);
if (confirm != null) {
try {
Log.i("paymentExample", confirm.toJSONObject().toString(4));
// TODO: send 'confirm' to your server for verification or
// consent
// completion.
// see
// https://developer.paypal.com/webapps/developer/docs/integration/mobile/verify-mobile-payment/
// for more details.
Intent intent = new Intent(getApplicationContext(),
CongratulationActivity.class);
startActivity(intent);
}
catch (JSONException e)
{
sorryActivity();
Log.e("paymentExample",
"an extremely unlikely failure occurred: ", e);
}
}
} else if (resultCode == Activity.RESULT_CANCELED) {
Log.i("paymentExample", "The user canceled.");
} else if (resultCode == PaymentActivity.RESULT_EXTRAS_INVALID)
{
sorryActivity();
Log.i("paymentExample",
"An invalid Payment was submitted. Please see the docs.");
}
}
//back button click
public String getJson(String url) throws ClientProtocolException,
IOException {
StringBuilder build = new StringBuilder();
HttpClient client = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse response = client.execute(httpGet);
HttpEntity entity = response.getEntity();
InputStream content = entity.getContent();
BufferedReader reader = new BufferedReader(new InputStreamReader(
content));
String con;
while ((con = reader.readLine()) != null) {
build.append(con);
}
return build.toString();
}
public void sorryActivity()
{
Intent intent = new Intent(getApplicationContext(),
SorryPaymentActivity.class);
startActivity(intent);
}
#Override
public void onDestroy() {
stopService(new Intent(this, PayPalService.class));
super.onDestroy();
}
public class DownloadCurrencyConversion extends
AsyncTask<Void, Integer, String> {
#Override
protected String doInBackground(Void... params) {
String s;
String theResult = "";
try {
s = getJson("http://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.xchange%20where%20pair%20in%20%28%22BHDUSD%22%29&format=json&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys&callback=");
JSONObject jObj;
jObj = new JSONObject(s);
theResult = jObj.getJSONObject("query")
.getJSONObject("results").getJSONObject("rate")
.getString("Rate");
System.out.println(theResult);
}
catch (JSONException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return theResult;
}
#Override
protected void onPostExecute(String theResult) {
super.onPostExecute(theResult);
System.out.println("theResult:" + theResult);
if (!TextUtils.isEmpty(theResult) &&!TextUtils.isEmpty(AmountOfTree) )
currencyAftconversion = (Double.parseDouble(AmountOfTree))
* (Double.parseDouble(theResult));
}
}
}
Please help me out of this issue ??
Remove android:background="#null"
In your code try this:
paybycard.setVisibility(0);
paybysms.setVisibility(0);
Hope this help, thanks
These layouts work for me:
<CheckBox
android:id="#+id/paysms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:background="#null"
android:visibility="visible"
android:text="pay with sms"
android:layout_marginLeft="10dp"
android:textColor="#000000" />
<CheckBox
android:id="#+id/paycredit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/paysms"
android:background="#null"
android:text="pay credit"
android:visibility="visible"
android:layout_marginLeft="10dp"
android:textColor="#000000" />

Categories

Resources