Users average reviews - android

I need my ratings bar to keep up with the user average rating. I have a main menu with several items which each have their own screen and description. I'm adding a rating bar to each screen so users can submit their product rating/see the average rating by others/see the number of ratings. Kind of like the customer reviews on the amazon app but without the bar graph. I have the xml file set up but need help with the java. Any help would be appreciated.
Here is an example of just one of the screens.
xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/tv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:text="TextView"
android:textSize="50dp" />
<TextView
android:id="#+id/tv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TextView" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="40dp"
android:layout_weight="50" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="150dp"
android:text="Submit" />
</LinearLayout>
java
public class MainActivity extends Activity {
RatingBar ratings;
TextView tv1, tv2;
Button b;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RatingBar rb = (RatingBar) findViewById(R.id.ratingbar);
}
}

In order to achieve something like that you will have to create an SQLiteDatabase on your app.
Store the ratingBar values for every product a user rates, inside your Database.
Post the values from your local SQLiteDatabase to a server.
Edit the values that you get in the server. Find max-min-middle etc. and store them on a "global" Database.
Post back to your Application the edited results.
Repeat the above steps on your own timeshift and you will have the desired result!

Related

Sending Data From Android Without Email Or Server?

Answer Found:
How to send email in background in Android ?
I'm working on an android app and one of the features requested is the ability to sign up for a news letter, which is simply sending a name and an email address. The client however does not have a server to host an application to receive this information and I would like to do it without sending it as an email. I'd like it just to say "You've successfully signed up for our newsletter" or something along those lines.
Is there anyway I can do this? If so, example code would be appreciated as my background is in C# and I'm working in Java.
Edit: Even sending a hidden e-mail without asking the client to login would be acceptable.
I think the notification method you might be looking for is Toasts. However, the validation of whether the customer has been registered will have to occur elsewhere.
Example:
Toast toast = Toast.makeText(getApplicationContext(), "You've successfully signed up for out newsletter", Toast.LENGTH_LONG);
toast.show();
It looks like you just want to display the message locally without server for demo purpose, right?
Well first of all, I don't really think you should learn Android in C# because of many reasons. First of all, you need Java to do Android development.
Here the simple scenario.
You have one activity called MainActivity where you have two text boxes called name and email.
You will now have a button called submit
Once user enters the name and email address and presses Submit button, it will take the user to a new activity called WelcomeActivity.
In android, you need an xml file that sets up the layout of your activity. I'll call this activity_email.xml.
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.maxphone.LoginActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Signup for email"
android:id="#+id/welcomeTextView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_marginStart="40dp"
android:layout_marginTop="20dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:layout_below="#+id/welcomeTextView"
android:layout_marginTop="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Name"
android:id="#+id/usrnameTextView"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/userEditText"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:maxLines="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Email Address"
android:id="#+id/emailTextView"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/emailEditText"
android:layout_marginTop="20dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:focusableInTouchMode="true"
android:maxLines="1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/email_signed_up_smg"
android:id="#+id/loginErrorMsg"
android:layout_gravity="center_horizontal|end"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:singleLine="false"
android:textColor="#ffff0000"
android:visibility="invisible" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/signupConfirmBtn"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="40dp" />
</LinearLayout>
</RelativeLayout>
Now in your MainActivity,
public class MainActivity extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
setContentView(R.layout.activity_login);
TextView txt = (TextView) findViewById(R.id.welcomeTextView);
txt.setTextSize(40);
final EditText usrname = (EditText) findViewById(R.id.userEditText);
final EditText email = (EditText) findViewById(R.id.emailEditText);
final TextView errorMsg = (TextView) findViewById(R.id.emailConfirmMsg);
final Button submitBtn = (Button) findViewById(R.id.emailConfirmBtn);
// Login Up button behavior
submitBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// Staring MainActivity
Intent i = new Intent(getApplicationContext(), WelcomeActivity.class);
startActivity(i);
finish();
}
}
}
}
This class will display the Views (such as text views, edit texts, etc) and listen to user behavior. Once submitBtn is clicked, it will create an Intent (please do a research) and take the user to a new activity that intent defined.
and you can do similar work for WelcomeActivity to display welcome messages like Thank you for signing up! and such.
This is all locally done and does not need any kind of web activity. So this is basically for demo purpose.
Good luck!
I found the answer here, unfortunately it requires the hard coding of an e-mail and password which is highly insecure. I will have to make more considerations.
How to send email in background in Android ?

Android screen missing elements defined in XML

I am new to Android development. I have created many screens and all are working quite nicely except one.
I am not sure why screen is not coming up as per XML. Below is my code :
<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:orientation="vertical"
android:padding="10dip"
android:id="#+id/linearMain">
<TextView
android:id="#+id/courseTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textColor="#2258A2"
android:layout_marginBottom="10dp"
/>
<TextView
android:id="#+id/assignmentTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
/>
<TextView
android:id="#+id/lesson"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
android:text="Lesson : Lecture 5"
/>
<TextView
android:id="#+id/dueDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
android:text="Due Date : Nov 20, 2014"
/>
<TextView
android:id="#+id/marks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
android:text="Marks : 20"
/>
<Button android:id="#+id/btnDownload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download"
/>
</LinearLayout>
And here is main class :
public class AssignmentDetail extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_announcement_detail);
Intent intent = getIntent();
//String course = intent.getStringExtra(Assignments.COURSE);
//TextView textview = (TextView) findViewById(R.id.courseTitle);
//textview.setText(course);
String announcementTitle = intent.getStringExtra(Announcement.ANNOUCEMENT_TITTLE);
TextView announcementTitleTextView = (TextView) findViewById(R.id.annoucementTitle);
announcementTitleTextView.setText(announcementTitle);
}
And here is output :
You can see there are 4 textView and one button in my xml but i can see only onr textView in output, why it is so and how to fix it.
Thanks..
Anjum
Make sure following stuff is correct in ur activity :
Make some changes in layout file and see it display that changed stuff
Correct xml file layout is specified at setContentView.
It seems that you are using some other layout xml..try removing textview and run it..if it still shows that Assignment #1 then ur xml itslef is wrong
I think you are not inflating the right XML layout file.
You can see that the TextView that you modify at the end of your code is not present in the XLM file. Therefore the layout that you inflate cannot be the one you are showing us or you would have an error at run time.

Custom layout for each row in a ListView

I am trying to get a button to work in a ListView Row. I been Reading about Custom Adapter but can't get it to work. Basically what i have a List_item class that have 4 TextView and 1 button which fieds a ListView in another class. one of the textViews has id/Name. so what i want is when i press the button on that row. it will get that Value of TextView "name" and add it to a ArrayList and if i press another button on different row it will add the Value of "name" to the same arraylist. so basically will add all the ones that i pressed on the button to an ArrayList. is that possible??.
JUST TO ADD. the textView Are being feed in by my database ..thanks upfront for any replies.
List_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="10dp" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false" >
<TextView
android:id="#+id/id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:visibility="gone" />
<TextView
android:id="#+id/name"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="left"
android:layout_weight="3"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="serif" />
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="center"
android:layout_weight="1"
android:gravity="right"
android:paddingRight="15dp"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="30sp"
android:textStyle="bold"
android:typeface="serif" />
<Button
android:id="#+id/order"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_weight="0"
android:focusable="false"
android:minHeight="0dp"
android:minWidth="50dp"
android:text="+"
android:textStyle="bold" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tableRow1"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAllCaps="true"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
android:typeface="serif"
android:visibility="gone" />
</TableRow>
</RelativeLayout>
my menu.xml has
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="697dp" >
</ListView>
now i want to get Values of TextView " name " when i press the button on its row.
that is how i used to fill my listView before i add the button and it was working fine
ListAdapter adapter = new SimpleAdapter (getCategoryItems.this,itemsList,R.layout.list_item, new String[]{ TAG_ID, TAG_NAME,TAG_PRICE, TAG_DESCRIPTION},new int[] { R.id.id, R.id.name, R.id.price, R.id.description});
setListAdapter(adapter);
but now after i added the button it doesn't get any values so i reaserched and i found that i have to change my sampleAdapter to a custom Adapter. and add the button inside getView() but i can't get that hang of it.
my button OnClickListener
Button order = (Button) view.findViewById(R.id.order);
order.setOnClickListener(new Button.OnClickListener() {
#Override
public void onClick(View v) {
String name = ((TextView)view.findViewById(R.id.name)).getText().toString();
Log.d("Selected : ", name);
}
});
You will need to create a custom adapter class for your ListView. Since you are getting data from a database, I suggest that you extend CursorAdapter and override newView() and bindView(). From my experience, how to do this correctly is poorly documented, so I'll try to give some pointers here.
In newView(), you need to use a LayoutInflator which is provided to you by calling getLayoutInflator() on your ListActivity. After you call inflate() on this LayoutInflator, you can then get the Button for that specific row and set its OnClickListener.
In bindView(), you do just about all the same things, except that you don't need to inflate a new view since Android is requesting that you reuse an existing View object. You might want to create some extra methods in order to reduce the amount of repeated code.
I hope this helps even though it is explained in English rather than with a code example.

How to removeDialog(int dialogID) Android?

I have extended my class with a dialog in which I have set content view and other button actionlisteners etc... what is happening is that when I create my dialog it shows background properly.
but opening it again and again is crating problems with content view, things are added haphazardly and most of the UI elements are repeating and background image is disappeared.
I know that dialog is created once and is used as a cache, I don't want that I know there is a method in Android Activity removeDialog(int dialogID) but I don't know who to use this, I don't give any id to my dialog I don't know how to give id to dialog.
#dialog code
public class OptionsDailog extends Dialog implements OnClickListener {
public OptionsDailog(Activity pContext) {
super(pContext, android.R.style.Theme_Translucent_NoTitleBar_Fullscreen);
mContext = pContext;
setContentView(R.layout.option_menu);
setBasicContents();
}
#options_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" android:id="#+id/rl_root_option_menu">
<RelativeLayout android:layout_height="fill_parent" android:layout_width="fill_parent" android:background="#drawable/bg_episode" android:id="#+id/rl_option_screen">
<LinearLayout android:id="#+id/ll_options" android:orientation="vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true">
<Button android:text="Achievements" android:textSize="18sp" android:id="#+id/btn_achievements" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:text="Tutorial" android:textSize="18sp" android:id="#+id/btn_tutorial" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:text="Leaderboard" android:textSize="18sp" android:id="#+id/btn_leaderboard" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:id="#+id/btn_music" android:text="Music" android:textSize="18sp" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
<Button android:id="#+id/btn_sound" android:textSize="18sp" android:text="Sound" android:layout_marginBottom="10dip" android:background="#drawable/bg_options_menu" android:textColor="#FFFFFF" android:layout_width="250dp" android:layout_height="60dp"></Button>
</LinearLayout>
</RelativeLayout>
<ImageButton android:background="#drawable/btn_back" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:layout_marginRight="10dp" android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/ib_back_options_menu"></ImageButton>
</RelativeLayout>
#Dialog showing code
final OptionsDailog mDailog = new OptionsDailog(JungleCrashLand.this);
mDailog.show();
The Dialog class works with its own set of identifiers. If you create a dialog with createDialog(int x) you can pass this x as your own identifier. removing the dialog follows the same structure: removeDialog(int x) where x is, again, the identifier of your dialog type. It is done this way to be able to distinguish between different "types" of dialogs, defined by your own, without the need to extend the dialog class.
so basically:
static final int OPTIONS_DIALOG = 0;
[...]
createDialog(OPTIONS_DIALOG);
[...]
removeDialog(OPTIONS_DIALOG);
as you can have only one dialog per activity, this will remove only this dialog and every(!) reference to it. Read http://developer.android.com/guide/topics/ui/dialogs.html to fully understand what is going on here.

Add ExpandableListView to a tab with other elements

I am developing an app with tabs. On one of the tabs,
I have two EditText fields and a couple of buttons. I want to add an ExpandableListView
to the tab, below the rest of the elements.
My problem is that the current class extends Activity. All the example I've found extend ExpandableListActivity, but I can't extend both for the same class.
How can I add this list to the tab? I've added an ExpandableListView element in the XML file, but whenever I run it, it doesn't show up. I understand that I need to bind the view to some data, but how do I go about doing this?
Thank you.
Here is my class code:
public class DirectionsTab extends Activity
{
private Button clear_btn;
private Button go_btn;
private EditText origin_txt;
private EditText dest_txt;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.directions_tab);
// initialize views and onClick event handler
origin_txt = (EditText)findViewById(R.id.origin_txt);
dest_txt = (EditText)findViewById(R.id.dest_txt);
clear_btn = (Button)findViewById(R.id.clear_btn);
// clear all text fields and return focus to dest_txt
clear_btn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v)
{
origin_txt.setText("");
dest_txt.setText("");
origin_txt.requestFocus();
}
});
} // end public void onCreate(Bundle savedInstanceState)
} // end public class DirectionsTab extends Activity
and XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/origin_lbl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Origin"/>
<EditText
android:id="#+id/origin_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/origin_lbl"/>
<TextView
android:id="#+id/dest_lbl"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/origin_txt"
android:text="Destination" />
<EditText
android:id="#+id/dest_txt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/editbox_background"
android:layout_below="#id/dest_lbl" />
<ExpandableListView
android:id="#+id/listView"
android:layout_below="#+id/dest_txt"
android:layout_height="wrap_content"
android:layout_width="wrap_content" /><!--android:id="#android:id/list"-->
<Button style="?android:attr/buttonStyleSmall"
android:id="#+id/clear_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/dest_txt"
android:layout_alignParentRight="true"
android:layout_marginLeft="10dip"
android:layout_marginTop="10dip"
android:text="Clear" />
<Button style="?android:attr/buttonStyleSmall"
android:id="#+id/go_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#id/clear_btn"
android:layout_alignTop="#id/clear_btn"
android:text="Go!" />
</RelativeLayout>
I got this!, If anyone is having problems with this, make sure you follow these steps:
Copy and paste ExpandableList3 example (located in ApiDemos project from Samples folder in your Android SDK). This is just a good example of how to set up a list.
Make your class inherit from ExpandableListActivity
Create the following in your XML layout file:
<ExpandableListView
android:id="#android:id/list"
android:layout_below="#+id/go_btn"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
It is VERY important that the ID property matches what you see above, otherwise, nothing will work. This little detail wasted plenty of hours for me and was discovered seating neatly as a small detail in the android docs.

Categories

Resources