Will only read and write one line - java

Program runs fine however I can not figure out why it will only read and write one line. It should be able to write multiple lines and read them into the textview. I have it setup so that when the user clicks add it automatically should read into the textview
public class MainActivity extends AppCompatActivity {
EditText Activity, Miles, Date;
TextView Log;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Activity = (EditText)(findViewById(R.id.editText));
Miles = (EditText)(findViewById(R.id.editText2));
Date = (EditText)(findViewById(R.id.editText3));
Log = (TextView)(findViewById(R.id.textView));
}
public void Add(View view)
{
String Myactivity = Activity.getText().toString() + "\t" + Miles.getText().toString() + "\t" + Date.getText().toString();
try {
FileOutputStream fileOutputStream = openFileOutput("myActivities.txt", MODE_PRIVATE);
fileOutputStream.write(Myactivity.getBytes());
fileOutputStream.close();
Toast.makeText(getApplicationContext(), "Activty Added", Toast.LENGTH_LONG);
FileInputStream fileInputStream = openFileInput("myActivities.txt");
InputStreamReader inputStreamReader = new InputStreamReader(fileInputStream);
BufferedReader bufferedReader = new BufferedReader(inputStreamReader);
StringBuffer stringBuffer = new StringBuffer();
String action;
while((action = bufferedReader.readLine())!= null)
{
stringBuffer.append(action + "\n");
}
Log.setText(stringBuffer.toString());
} catch (FileNotFoundException e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_LONG);
} catch (IOException e)
{
e.printStackTrace();
Toast.makeText(getApplicationContext(), "Fail", Toast.LENGTH_LONG);
}
}
}
<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: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="mbl404.phoenix.edu.week2appgk5343.MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:hint="Please Enter Type of Workout"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignEnd="#+id/editText2" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:hint="Please Enter Number of Miles"
android:layout_below="#+id/editText"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText3"
android:hint="Please Enter Date"
android:layout_below="#+id/editText2"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="Add"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView"
android:layout_below="#+id/button"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />
</RelativeLayout>

Add this in your textview xml
android:maxLines="100"
In your code:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView"
android:layout_below="#+id/button"
android:maxLines="100" <!-- add android:maxLines to specify the number of lines in textview -->
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentStart="true" />
Updated:
while((action = bufferedReader.readLine())!= null)
{
Log.setText(Log.getText() + action + "\n");
}

Related

Input from EditText only taking in hints instead of text inputted

I have a basic patient details activity that takes patient details and saves them to a text file. At the moment it works fine only that the input it is receiving from the edit textfields are the hints from edit text and not the actual info I am trying to input. My code to me seems perfect and I cannot see where I am going wrong:
public class PatientDetails extends AppCompatActivity {
public EditText IDNum, name, DOB, weight, height;
public String ID, dob, wght, hght, nme;
public Button b;
public TextView t;
private FileUtility myFile = new FileUtility();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_patient_details);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
IDNum = ((EditText) findViewById(R.id.IDnum));
DOB = ((EditText) findViewById(R.id.DOB));
weight = ((EditText) findViewById(R.id.Weight));
height = ((EditText) findViewById(R.id.Height));
name = ((EditText) findViewById(R.id.name));
b = ((Button) findViewById(R.id.submit));
t = ((TextView) findViewById(R.id.result));
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ID = IDNum.getText().toString();
dob = DOB.getText().toString();
wght = weight.getText().toString();
hght = height.getText().toString();
nme = name.getText().toString();
myFile.createFile(getApplicationContext(), "test");
myFile.writeLine(ID);
myFile.writeLine(dob);
myFile.writeLine(wght);
myFile.writeLine(hght);
myFile.writeLine(nme);
t.setText(myFile.readAll());
}
});
}
}
xml Code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" 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"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_patient_details"
tools:context="com.example.user.filetest.PatientDetails">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="Name"
android:ems="10"
android:id="#+id/name"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/IDnum"
android:layout_below="#+id/name"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="Patient ID number" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/Height"
android:layout_below="#+id/IDnum"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="Height"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="number"
android:ems="10"
android:id="#+id/Weight"
android:hint="Weight"
android:layout_below="#+id/Height"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="date"
android:ems="10"
android:id="#+id/DOB"
android:layout_below="#+id/Weight"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:hint="DOB" />
<Button
android:hint="Submit"
android:id="#+id/submit"
android:layout_width="300dp"
android:layout_height="70dp"
android:layout_below="#+id/DOB"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="#+id/result"
android:layout_below="#+id/submit"
android:layout_marginTop="53dp"
android:layout_alignLeft="#+id/submit"
android:layout_alignStart="#+id/submit"
android:layout_alignRight="#+id/submit"
android:layout_alignEnd="#+id/submit"
android:textSize="7dp" />
/////file utility/////////
public class FileUtility {
private File root;
private File file;
public FileUtility() {
root = Environment.getExternalStorageDirectory();
}
public void createFile(Context context, String fileName) {
try {
if (root.canWrite()) {
file = new File(root, "//" + fileName);
if (!file.exists()) {
file.createNewFile();
}
}
else
{
file = new File(context.getFilesDir(), "//" + fileName); // File(root, "//" + fileName);
if (!file.exists()) {
file.createNewFile();
}
}
} catch (IOException e) {
Log.e("Error", "fail to create a new file");
}
}
public String readAll() {
StringBuilder returnString = new StringBuilder();
try {
BufferedReader in;
FileReader datawriter = new FileReader(file);
in = new BufferedReader(datawriter);
if (file.exists()) {
String str = null;
while((str=in.readLine())!=null)
{
returnString.append(str + "\n");
}
}
in.close();
} catch (IOException e) {
Log.e("Error", "fail to write file");
}
return returnString.toString();
}
public void writeLine(String message) {
try {
BufferedWriter out;
FileWriter datawriter = new FileWriter(file,true);
out = new BufferedWriter(datawriter);
if (file.exists()) {
out.write(message + "\n");
out.flush();
}
out.close();
} catch (IOException e) {
Log.e("Error", "fail to write file");
}
}
}
Try search your code one more time. You cannot get hint text from getText() method... unless you have hard pre-filled that hint as a text in EditText element.
Try this maybe:
IDNum = (EditText) findViewById(R.id.IDnum);

Focus changes to listview once i clear the Edittext

I'm using broadcast receiver to show a dialog.So the flow of code is something like:
Step1 Getting the requestCode value
Step2 Based on this requestCode the broadCast receiver goes to if or else if or else part
Step3 If the value that i entered using some scanner into the EditText(i.e Scan) doesn't matches it shows a Toast "Item Not Available".
Step 4 Once "Item Not Available" toast comes the focus changes to the Listview which is my problem.
Step5 Again if i pass value to the Scan EditText the Listview get click automatically.
So my question is "How to remove focus from the Listview" and set it to the EditText(i.e Scan).
For Reference I'm attaching the snap with code snippet and the layout.xml.Please have a look and drop your suggestions why the focus is going to the listview.
.java snippet
final BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
if (intent != null) {
loc = mspinner.getItemAtPosition(mspinner.getSelectedItemPosition())
.toString();
final String ItemNo;
final String Desc;
final String StockUnit;
final String PickSeq;
final String qtyCount;
final String qtyonHand;
final Button mok;
final Button mcancel;
final Button mplus;
final Button mminus;
final EditText medtQtyCount;
final EditText medtItem;
final EditText medtdesc;
final EditText medtuom;
final DatabaseHandler dbHandler;
final String[] UOM = null;
int requestCode;
LayoutInflater li = LayoutInflater.from(InventoryCount.this);
View promptsView = li.inflate(R.layout.quantityupdate, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
InventoryCount.this);
alertDialogBuilder.setView(promptsView);
//requestCode=Integer.parseInt(intent.getStringExtra("idx"));
requestCode=intent.getIntExtra("idx", -1);
// create alert dialog
final AlertDialog alertDialog = alertDialogBuilder.create();
dbHandler = new DatabaseHandler(InventoryCount.this);
medtuom = (EditText) promptsView.findViewById(R.id.edt_mseshipuom_mic);
mok = (Button) promptsView.findViewById(R.id.btn_mseshipOk_mic);
mcancel = (Button) promptsView.findViewById(R.id.btn_mseshipCancel_mic);
mplus = (Button) promptsView.findViewById(R.id.btn_mseshipIncr_mic);
mminus = (Button) promptsView.findViewById(R.id.btn_mseshipDecr_mic);
medtQtyCount = (EditText) promptsView
.findViewById(R.id.edt_shipShiped_mic);
medtdesc = (EditText) promptsView
.findViewById(R.id.edt_mseshipQtyOrd_mic);
medtItem = (EditText) promptsView
.findViewById(R.id.edt_mseshipItemNo_mic);
if (requestCode == 1) {
}
else if (requestCode == 0) {
// ItemNo
/*if (resultCode == RESULT_OK) {
Log.i("Scan resul format: ",
intent.getStringExtra("SCAN_RESULT_FORMAT"));
*/
String itNo = intent.getStringExtra("SCAN_RESULT");
dbhelper.getReadableDatabase();
MIC_Inventory mic_inventory = dbhelper.getMicInventoryDetails(
loc, itNo);
dbhelper.closeDatabase();
if (mic_inventory != null) {
loc = mspinner.getItemAtPosition(
mspinner.getSelectedItemPosition()).toString();
ItemNo = mic_inventory.getItemno();
Desc = mic_inventory.getItemdescription();
PickSeq = mic_inventory.getPickingseq();
StockUnit = mic_inventory.getStockunit();
qtyonHand = mic_inventory.getQoh();// This value gives
// QOHand
qtyCount = mic_inventory.getQc();
medtItem.setText(ItemNo);
medtdesc.setText(Desc);
medtQtyCount.setText(qtyCount);
medtuom.setText(StockUnit);
mplus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String a = medtQtyCount.getText().toString();
int b = Integer.parseInt(a);
b = b + 1;
a = a.valueOf(b);
medtQtyCount.setText(a);
}
});
mminus.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
int c = Integer.parseInt(medtQtyCount.getText()
.toString());
c = c - 1;
medtQtyCount.setText(new Integer(c).toString());
}
});
mok.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
/*
* UOM[mspinnerUom.getSelectedItemPosition()] =
* medtQtyCount .getText().toString();
*/
MIC_UOMInternal mic_uom = new MIC_UOMInternal();
mic_uom.setLocation(loc);
mic_uom.setItemno(ItemNo);
String updatedqtyCount = medtQtyCount.getText()
.toString();
if (!qtyCount.equals(updatedqtyCount)) {
mic_uom.setQc(Double
.parseDouble(updatedqtyCount));
mic_uom.setUom(StockUnit);
MIC_Inventory mic_Inventory = new MIC_Inventory();
mic_Inventory.setItemdescription(Desc);
mic_Inventory.setItemno(ItemNo);
mic_Inventory.setLocation(loc);
mic_Inventory.setPickingseq(PickSeq);
mic_Inventory.setQc(updatedqtyCount);
mic_Inventory.setQoh(qtyonHand);
mic_Inventory.setStockunit(StockUnit);
dbHandler.getWritableDatabase();
String result = dbHandler
.insertIntoInternal(mic_uom);
if (result.equals("success")) {
result = dbHandler.updateMIC(mic_Inventory);
}
dbHandler.closeDatabase();
}
Intent i = new Intent(InventoryCount.this,
InventoryCount.class);
i.putExtra("et", 1);
i.putExtra("LOCATION", loc);
// i.putExtra("ID", ID);
startActivity(i);
// InventoryCount.this.finish();
}
});
mcancel.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
alertDialog.cancel();
}
});
// show it
alertDialog.show();
} else {
/*
* Toast.makeText(this, "Item not available",
* Toast.LENGTH_LONG).show();
*/
toastText.setText("Item not available");
Toast toast = new Toast(getBaseContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastLayout);
toast.show();
msearchtext.setText("");
/*msearchtext.setFocusableInTouchMode(true);
msearchtext.requestFocus();*/
/*msearchtext.setSelection(0);
lstView.setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
*/msearchtext.requestFocus();
}
else if (requestCode == 2) {
}
else
{
toastText.setText("Problem in Scanning");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 410);
toast.setDuration(Toast.LENGTH_SHORT);
toast.setView(toastLayout);
toast.show();
}
}
Layout.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="#drawable/border_green"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin" >
<TextView
android:id="#+id/txt_InvTitle"
style="#style/pageTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:text="#string/invTitle" />
<View
android:id="#+id/txt_InvView"
android:layout_width="match_parent"
android:layout_height="2dip"
android:layout_below="#+id/txt_InvTitle"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#2E9AFE" />
<LinearLayout
android:id="#+id/invLocation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_below="#+id/txt_InvView"
android:layout_marginTop="16dp" >
<TextView
android:id="#+id/txtLoc"
style="#style/textRegular"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="left|center"
android:text="#string/location" />
<Spinner
android:id="#+id/sploc"
style="#style/SpinnerItemAppTheme"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:editable="false" />
</LinearLayout>
<LinearLayout
android:id="#+id/invScanType"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/invLocation"
android:layout_gravity="center"
android:layout_marginBottom="3dp"
android:layout_marginLeft="3dp"
android:layout_marginTop="18dp"
android:orientation="horizontal" >
<EditText
android:id="#+id/edt_Search_mic"
style="#style/EditTextAppTheme_Scan"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_weight=".15"
android:gravity="center"
android:hint="#string/scan" />
<RadioGroup
android:id="#+id/radioScanBasedOn_mic"
style="#style/RadioButtonAppTheme"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/radioInum_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".25"
android:button="#drawable/radiobutton_selector"
android:checked="true"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/itemno" />
<RadioButton
android:id="#+id/radioNum_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="#drawable/radiobutton_selector"
android:checked="false"
android:layout_marginRight="5dp"
android:layout_weight=".25"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/manfno" />
<RadioButton
android:id="#+id/radioUpc_mic"
style="#style/textRegular"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:button="#drawable/radiobutton_selector"
android:checked="false"
android:layout_marginRight="5dp"
android:layout_weight=".25"
android:drawablePadding="50dp"
android:paddingLeft="10dip"
android:text="#string/upc" />
</RadioGroup>
</LinearLayout>
<HorizontalScrollView
android:id="#+id/scroll_full_mic"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/invScanType" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginTop="25dp"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/lay_fullTitle_mic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
style="#style/textRegular_list"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:text="#string/itemno"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="130dp"
android:layout_height="wrap_content"
android:gravity="center|left"
android:text="#string/description"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/pick_seq"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/qoh"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/qc"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
<TextView
style="#style/textRegular_list"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:gravity="center|left"
android:text="#string/uom"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textStyle="bold" />
</LinearLayout>
<ListView
android:id="#+id/lst_msefull_mic"
android:layout_width="match_parent"
android:layout_height="match_parent"
style="#style/ListViewAppTheme.White" >
</ListView>
</LinearLayout>
</HorizontalScrollView>
<LinearLayout
android:id="#+id/lay_PO_mic"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="41dp"
android:gravity="center_horizontal"
android:orientation="horizontal"
android:visibility="gone" >
<Button
android:id="#+id/btn_OrderLstImport_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
<Button
android:id="#+id/btn_OrderLstExport_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
<Button
android:id="#+id/btn_OrderLstExit_mic"
android:layout_width="100dp"
android:layout_height="100dp"
android:textSize="18dp"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
Add a textwatcher to edit text and check when text is not blank and it is not equal to expected text then only switch the focus.
/* Set Text Watcher listener */
yourEditText.addTextChangedListener(passwordWatcher);
and check for text once user enter text
private final TextWatcher passwordWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
public void afterTextChanged(Editable s) {
if (s.length() != 0 && passwordEditText.getText().equals("Your expected text")) {
// show your toast and change focus
}
}
}
You should make your listview not focusable by using setFocusable(false) when not required and when you get response correctly from barcode scanner then you can again make your listview focusable.

permanently save new entered data in list view [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
Hi i make a sales puchase app like olx. In which user post free add. Problem is when use post add it appeared in listview but when user again login with their device the add will disappear.....
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.single_adds_layout, parent, false);
TextView title = (TextView) rowView.findViewById(R.id.title);
TextView detailedMessage = (TextView) rowView.findViewById(R.id.detailed_message);
TextView ownerName = (TextView) rowView.findViewById(R.id.owner);
TextView ownerEmail = (TextView) rowView.findViewById(R.id.owner_email);
TextView price = (TextView) rowView.findViewById(R.id.price);
ImageView image = (ImageView) rowView.findViewById(R.id.imageView10);
Button btn33 = (Button) rowView.findViewById(R.id.buybutton);
btn33.setBackgroundResource(R.drawable.buy_corner);
btn33.setOnClickListener(this);
Advertisement dataObj = values[position];
title.setText(dataObj.getTitle());
detailedMessage.setText(dataObj.getDetailedMessage());
ownerName.setText(dataObj.getOwnerName());
ownerEmail.setText(dataObj.getOwnerEmail());
price.setText(String.valueOf(dataObj.getPrice()));
btn33.setTag(price.getText());
if(dataObj.getImageId().contains(">>>")){
String picturePath = dataObj.getImageId().replace(">>>", "");
image.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}else {
setDrawable(image, dataObj.getImageId());
}
return rowView;
}
private void setDrawable(ImageView image, String drawableName) {
AssetManager manager = image.getContext().getAssets();
// Read a Bitmap from Assets
InputStream open = null;
try {
open = manager.open(drawableName+".jpg");
Bitmap bitmap = BitmapFactory.decodeStream(open);
// Assign the bitmap to an ImageView in this layout
image.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (open != null) {
try {
open.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
}
And here is my Xml
<LinearLayout
android:id="#+id/parentLinear"
android:layout_width="match_parent"
android:layout_height="410dp"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="66dp" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Ad Title!"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold|italic"
android:typeface="serif" />
<EditText
android:id="#+id/editTextTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView1"
android:layout_alignBottom="#+id/textView1"
android:layout_alignParentRight="true"
android:ems="10"
android:hint="Enter Title Here" >
<requestFocus />
</EditText>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="66dp" >
<EditText
android:id="#+id/editTextDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Enter Description Here" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Ad\nDescription!"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold|italic"
android:typeface="serif" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="62dp" >
<EditText
android:id="#+id/editTextOwnerName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Enter Name Here" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/editTextOwnerName"
android:layout_alignParentLeft="true"
android:text="Owner\nName!"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold|italic"
android:typeface="serif" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="64dp" >
<EditText
android:id="#+id/editTextOwnerEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:ems="10"
android:hint="Enter Email id Here" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Owner\nEmail"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold|italic"
android:typeface="serif" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="62dp" >
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="Ad Price!"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold|italic"
android:typeface="serif" />
<EditText
android:id="#+id/editTextPrice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:ems="10"
android:hint="Enter Price Here" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="70dp" >
<Button
android:id="#+id/buttonAddImage"
android:layout_width="wrap_content"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:text="Add Image"
android:textStyle="bold|italic"
android:typeface="serif" />
<ImageView
android:id="#+id/creatae"
android:layout_width="100sp"
android:layout_height="100sp"
android:layout_alignParentBottom="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
<Button
android:id="#+id/buttonSaveAdd"
android:layout_width="68dp"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Save"
android:textStyle="bold|italic"
android:typeface="serif" />
</LinearLayout>
By click on save add
case R.id.buttonSaveAdd:
EditText editTextTitle = (EditText) findViewById(R.id.editTextTitle);
EditText editTextDes = (EditText) findViewById(R.id.editTextDescription);
EditText editTextOwner = (EditText) findViewById(R.id.editTextOwnerName);
EditText editTextOwnerEmail = (EditText) findViewById(R.id.editTextOwnerEmail);
EditText editTextPrice = (EditText) findViewById(R.id.editTextPrice);
//populating data object from the values received
//from view
String title = editTextTitle.getText().toString();
String description = editTextDes.getText().toString();
String ownerName = editTextOwner.getText().toString();
String ownerEmail = editTextOwnerEmail.getText().toString();
String pricce = editTextPrice.getText().toString();
Advertisement object = new Advertisement(title, description,
ownerName, ownerEmail, currentImageName, Integer.parseInt(pricce), 100);
Button_mak.ads.add(object);
this.finish();
break;
Save them in a local database using SQLite and then reload them back into the listview when the user opens that activity again. I made a blog post recently on using SQLite within Android. It can be found here

Invocation target exception in my method toString()

I am novice in Java and Android. I need to send some text data from an one activity to the another activity. This is a method which send these text data to the another activity:
public void commandListener(View target)
{
switch (target.getId())
{
case R.id.button1:
Intent intent = new Intent();
intent.setClass(this, SubActivity.class);
intent.putExtra("Send to the second activity", ++counter);
intent.putExtra("Send person info", somePerson.toString());// INVOCATION TARGET EXCEPTION ON THIS STRING!!!
startActivity(intent);
finish();
break;
case R.id.button2:
Intent intent2 = new Intent();
intent2.setClass(this, ThirdActivity.class);
intent2.putExtra("Send to the third activity", ++counter);
startActivity(intent2);
finish();
break;
default:
counter = 0;
finish();
break;
}
}
In this code I get acception on intent.putExtra("Send person info", somePerson.toString()). somePerson.toString() initiates that exception. Where is my mistake?
SomePerson has a type Person
Here is an implementation of a Person:
public class Person implements Serializable
{
/**
*
*/
private static final long serialVersionUID = 1L;
private String firstName = "Vasya";
private String lastName = "Pupkin";
private Integer age = 58;
private Integer phone = 02;
#Override
public String toString()
{
return "Person [firstName=" + firstName + ", lastName=" + lastName
+ ", age=" + age + "]";
}
public void setName(String name)
{
firstName = name;
}
public void setLastName(String lName)
{
lastName = lName;
}
public void setAge(Integer personAge)
{
age = personAge;
}
public void setPhone(Integer personPhone)
{
phone = personPhone;
}
}
This is a part of code from another activity, which accepts text data from another activity:
static int counter = 0;
String personInfo;
TextView counterView;
TextView personInfoView;
private static final String TAG = "myLogs";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
counter = getIntent().getExtras().getInt("Send to the second activity");
personInfo = getIntent().getExtras().getString("Send person info");
counterView = (TextView)findViewById(R.id.textView4);
counterView.setText(String.valueOf(counter));
personInfoView = (TextView)findViewById(R.id.textView6);
personInfoView.setText(personInfo);
Log.d(TAG, "Counter value:");
Log.d(TAG, String.valueOf(counter));
}
This is XML-code of activity which send data:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
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.switchactivity.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="string/this is the main activity" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="Counter" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView2"
android:layout_below="#+id/textView2"
android:layout_marginTop="17dp"
android:text="TextView" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:onClick="commandListener"
android:text="Forward" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="16dp"
android:onClick="commandListener"
android:text="Third Activity" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView2"
android:layout_below="#+id/button2"
android:text="Name" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView4"
android:layout_centerHorizontal="true"
android:ems="10" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView4"
android:layout_below="#+id/editText1"
android:text="Second name" />
<EditText
android:id="#+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView5"
android:layout_centerHorizontal="true"
android:ems="10" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView5"
android:layout_below="#+id/editText2"
android:text="Age" />
<EditText
android:id="#+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView6"
android:layout_centerHorizontal="true"
android:ems="10" />
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView6"
android:layout_below="#+id/editText3"
android:text="Phone" />
<EditText
android:id="#+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView7"
android:layout_centerHorizontal="true"
android:ems="10" />
</RelativeLayout>
This is XML-code of activity, which accepts text data:
<?xml version="1.0" encoding="UTF-8"?>
<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: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.switchactivity.SecondActivity$PlaceholderSub" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView2"
android:layout_below="#+id/textView1"
android:text="Counter" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView3"
android:layout_below="#+id/textView3"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView1"
android:layout_centerHorizontal="true"
android:text=" Second activity" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:onClick="commandListener2"
android:text="Back" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/button2"
android:layout_centerHorizontal="true"
android:text="Name" />
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignParentRight="true"
android:layout_below="#+id/textView5"
android:text="TextView" />
</RelativeLayout>
write all getter method for each variable in Person class and set each variable and use below line of code to send some text data from an one activity to the another activity
intent.putExtra("Firstname", somePerson.getFirstName());
intent.putExtra("LastName", somePerson.getLastName());
intent.putExtra("Age", somePerson.getAge());
remove toString() method from somePerson.toString() because u are implementing Serializable so there is no need of toString ()
replace
intent.putExtra("Send person info", somePerson.toString());
with
intent.putExtra("Send person info", somePerson);
I would suggest making the object parcelable instead of serializable since it is much quicker. Then you can just do a putExtra on the parcelable object.
See http://developer.android.com/reference/android/os/Parcelable.html for reference on how to create parcelable objects.

xml (activity) stays open in the back ground when it should close

I am making a quiz android app using Eclipse and I made it so that every question has his own activity, now everything works ok if i answer qustions slow but if i do it faster the xml stays open in the background I even added onPause method but it is still open. I do not know how to use threads but someone told me it would make the app faster so the xml would close. I hope there is an easy fix for my problem if not, can anyone explain me how to use threads.
Here is one of my xml layouts:
<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:background="#drawable/qh4"
tools:context=".POV6" >
<TextView
android:id="#+id/povrat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="150dp"
android:layout_marginTop="50dp"
android:paddingLeft="#dimen/activity_vertical_margin"
android:paddingRight="#dimen/activity_vertical_margin"
android:text="The highest peak in North America is ?"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#000000"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button2"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:layout_below="#+id/povrat"
android:layout_centerHorizontal="true"
android:onClick="tocan"
android:text="Mount Mckinley"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button3"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button2"
android:layout_below="#+id/button2"
android:onClick="netocanodgovor"
android:text="Mount Everest"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button4"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/button3"
android:layout_below="#+id/button3"
android:onClick="netocanodgovor"
android:text="Mount Logan"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button5"
android:layout_width="175dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button4"
android:layout_below="#+id/button4"
android:onClick="netocanodgovor"
android:text="Mount Rainier"
tools:ignore="HardcodedText" />
<SlidingDrawer
android:id="#+id/slidingDrawer1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:content="#+id/content"
android:handle="#+id/handle" >
<Button
android:id="#+id/handle"
style="?android:attr/buttonStyleSmall"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Pull up to use Jokers !"
tools:ignore="HardcodedText" />
<LinearLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:baselineAligned="false" >
<TabHost
android:id="#+id/tabhost"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/tab1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/rekord"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:paddingBottom="20dp"
android:text="This joker remowes one wrong answer !"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/joker1text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/rekord"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button6"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="joker1"
android:text="Use this joker !"
tools:ignore="HardcodedText" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/tab2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:paddingBottom="20dp"
android:text="This joker will sometimes give you the right answer (30% of all cases) ! "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/joker2text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/textView3"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/joker2odgovor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/joker2text"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:text="I think the right answer is Mount Mckinley "
android:textAppearance="?android:attr/textAppearanceMedium"
android:textStyle="bold"
android:visibility="invisible"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button8"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="joker2"
android:text="Use this joker !"
tools:ignore="HardcodedText" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/tab3"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:paddingBottom="20dp"
android:text="This joker will skip this question. But you will get no points for it !"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/joker3text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/textView4"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button10"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="joker3"
android:text="Use this joker !"
tools:ignore="HardcodedText" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/tab4"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical" >
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="50dp"
android:paddingBottom="20dp"
android:text="This joker will give you the right answer !"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/joker4text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#id/textView5"
android:layout_marginLeft="10dp"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#FFFFFF"
android:textSize="20sp"
android:textStyle="bold"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button12"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick="joker4"
android:text="Use this joker !"
tools:ignore="HardcodedText" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</LinearLayout>
</SlidingDrawer>
</RelativeLayout>
Here is my java code:
package com.peky.smartornot;
import java.util.Random;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.TabHost;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.TabHost.TabSpec;
public class POV6 extends Activity {
Sql ulaz = new Sql(this);
Sqlrecords rekordi = new Sqlrecords(this);
TextView joke4text;
TextView joke3text;
TextView joke2text;
TextView joke1text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pov6);
sve();
}
public void sve() {
// TODO Auto-generated method stub
ulaz.open();
int joker1 = ulaz.procitaj(), joker2 = ulaz.procitaj2(), joker3 = ulaz
.procitaj3(), joker4 = ulaz.procitaj4();
ulaz.close();
TabHost joker = (TabHost) findViewById(R.id.tabhost);
joker.setup();
TabSpec izgled = joker.newTabSpec("tag1");
izgled.setContent(R.id.tab1);
izgled.setIndicator("Joker 1");
joker.addTab(izgled);
izgled = joker.newTabSpec("tag2");
izgled.setContent(R.id.tab2);
izgled.setIndicator("Joker 2");
joker.addTab(izgled);
izgled = joker.newTabSpec("tag3");
izgled.setContent(R.id.tab3);
izgled.setIndicator("Joker 3");
joker.addTab(izgled);
izgled = joker.newTabSpec("tag4");
izgled.setContent(R.id.tab4);
izgled.setIndicator("Joker 4");
joker.addTab(izgled);
joke1text = (TextView) findViewById(R.id.joker1text);
joke1text.setText("You have " + joker1 + " jokers !");
joke2text = (TextView) findViewById(R.id.joker2text);
joke2text.setText("You have " + joker2 + " jokers !");
joke3text = (TextView) findViewById(R.id.joker3text);
joke3text.setText("You have " + joker3 + " jokers !");
joke4text = (TextView) findViewById(R.id.joker4text);
joke4text.setText("You have " + joker4 + " jokers !");
}
public void joker1(View view) {
Button netocan = (Button) findViewById(R.id.button5);
Button netocan2 = (Button) findViewById(R.id.button4);
Button netocan3 = (Button) findViewById(R.id.button3);
ulaz.open();
int joker1 = ulaz.procitaj(), joker2, joker3, joker4;
ulaz.close();
if (joker1 != 0) {
if (netocan.getVisibility() == View.VISIBLE) {
netocan.setVisibility(View.INVISIBLE);
ulaz.open();
joker1 = joker1 - 1;
joker2 = ulaz.procitaj2();
joker3 = ulaz.procitaj3();
joker4 = ulaz.procitaj4();
ulaz.spremijoker(joker1, joker2, joker3, joker4);
ulaz.close();
joke1text = (TextView) findViewById(R.id.joker1text);
joke1text.setText("You have " + joker1 + " jokers !");
} else if (netocan2.getVisibility() == View.VISIBLE) {
netocan2.setVisibility(View.INVISIBLE);
ulaz.open();
joker1 = joker1 - 1;
joker2 = ulaz.procitaj2();
joker3 = ulaz.procitaj3();
joker4 = ulaz.procitaj4();
ulaz.spremijoker(joker1, joker2, joker3, joker4);
ulaz.close();
joke1text = (TextView) findViewById(R.id.joker1text);
joke1text.setText("You have " + joker1 + " jokers !");
} else if (netocan3.getVisibility() == View.VISIBLE) {
netocan3.setVisibility(View.INVISIBLE);
ulaz.open();
joker1 = joker1 - 1;
joker2 = ulaz.procitaj2();
joker3 = ulaz.procitaj3();
joker4 = ulaz.procitaj4();
ulaz.spremijoker(joker1, joker2, joker3, joker4);
ulaz.close();
joke1text = (TextView) findViewById(R.id.joker1text);
joke1text.setText("You have " + joker1 + " jokers !");
} else {
Toast imasodgovor = Toast.makeText(getApplicationContext(),
"You can not use more JOKERS1 on this question !",
Toast.LENGTH_SHORT);
imasodgovor.show();
}
} else {
Toast nemasjokera = Toast.makeText(getApplicationContext(),
"Not enought JOKERS1 !", Toast.LENGTH_SHORT);
nemasjokera.show();
}
}
public void joker4(View view) {
ulaz.open();
int joker1, joker2, joker3, joker4 = ulaz.procitaj4();
ulaz.close();
Button netocan = (Button) findViewById(R.id.button5);
Button netocan2 = (Button) findViewById(R.id.button4);
Button netocan3 = (Button) findViewById(R.id.button3);
if (joker4 != 0) {
if (netocan.getVisibility() == View.VISIBLE
|| netocan2.getVisibility() == View.VISIBLE
|| netocan3.getVisibility() == View.VISIBLE) {
netocan.setVisibility(View.INVISIBLE);
netocan2.setVisibility(View.INVISIBLE);
netocan3.setVisibility(View.INVISIBLE);
ulaz.open();
joker1 = ulaz.procitaj();
joker2 = ulaz.procitaj2();
joker3 = ulaz.procitaj3();
joker4 = joker4 - 1;
;
ulaz.spremijoker(joker1, joker2, joker3, joker4);
ulaz.close();
joke4text = (TextView) findViewById(R.id.joker4text);
joke4text.setText("You have " + joker4 + " jokers !");
} else {
Toast imasodgovor = Toast.makeText(getApplicationContext(),
"You can not use more JOKERS4 on this question !",
Toast.LENGTH_SHORT);
imasodgovor.show();
}
} else {
Toast jokertext = Toast.makeText(getApplicationContext(),
"Not enought JOKERS4", Toast.LENGTH_SHORT);
jokertext.show();
}
}
public void joker3(View view) {
ulaz.open();
int joker3 = ulaz.procitaj3(), joker1, joker2, joker4;
ulaz.close();
if (joker3 != 0) {
ulaz.open();
joker1 = ulaz.procitaj();
joker2 = ulaz.procitaj2();
joker4 = ulaz.procitaj4();
joker3 = joker3 - 1;
ulaz.spremijoker(joker1, joker2, joker3, joker4);
ulaz.close();
Random crazy = new Random();
switch (crazy.nextInt(4)) {
case 0:
Intent pokreni = new Intent(this, POV3.class);
startActivity(pokreni);
finish();
break;
case 1:
Intent pokreni1 = new Intent(this, POV1.class);
startActivity(pokreni1);
finish();
break;
case 2:
Intent pokreni11 = new Intent(this, POV4.class);
startActivity(pokreni11);
finish();
break;
case 3:
Intent pokreni111 = new Intent(this, POV5.class);
startActivity(pokreni111);
break;
}
}
}
public void joker2(View view) {
TextView joker2odgovor = (TextView) findViewById(R.id.joker2odgovor);
ulaz.open();
int joker1, joker2 = ulaz.procitaj2(), joker3, joker4;
ulaz.close();
if (joker2 != 0 && joker2odgovor.getVisibility() == View.INVISIBLE) {
ulaz.open();
joker1 = ulaz.procitaj();
joker2 = joker2 - 1;
joker3 = ulaz.procitaj3();
joker4 = ulaz.procitaj4();
ulaz.spremijoker(joker1, joker2, joker3, joker4);
ulaz.close();
joker2odgovor.setVisibility(View.VISIBLE);
joke2text = (TextView) findViewById(R.id.joker2text);
joke2text.setText("You have " + joker2 + " jokers !");
} else {
Toast odgovor = Toast.makeText(getApplicationContext(),
"Not enought JOKERS2 or already used on this question !",
Toast.LENGTH_SHORT);
odgovor.show();
}
}
public void tocan(View view) {
Toast josip = Toast.makeText(getApplicationContext(), "Right answer !",
Toast.LENGTH_SHORT);
josip.show();
int rekord = 0, ukupno = 0;
rekordi.open();
ukupno = rekordi.procitajukupno();
rekord = rekordi.procitaj() + 10;
rekordi.spremi(rekord, ukupno);
rekordi.close();
Random crazy = new Random();
switch (crazy.nextInt(4)) {
case 0:
Intent pokreni = new Intent(this, POV3.class);
startActivity(pokreni);
finish();
break;
case 1:
Intent pokreni1 = new Intent(this, POV1.class);
startActivity(pokreni1);
finish();
break;
case 2:
Intent pokreni11 = new Intent(this, POV4.class);
startActivity(pokreni11);
finish();
break;
case 3:
Intent pokreni111 = new Intent(this, POV5.class);
startActivity(pokreni111);
break;
}
}
public void netocanodgovor(View view) {
Intent gotovo = new Intent(this, Records.class);
startActivity(gotovo);
finish();
Toast josip = Toast.makeText(getApplicationContext(),
"Incorrect answer !", Toast.LENGTH_SHORT);
josip.show();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
If you need more information, or even the whole app i can send it to you !

Categories