Decimal Format
DecimalFormat currency = new DecimalFormat("$###,###.##");
currency.format(totalCost));
|
Pass And Get Value Into/From Another Activity
Intent i = new Intent(MainActivity.this,
SecondActivity.class);
i.putExtra("data",totalCost);
startActivity(i);
Intent intent = getIntent();
Double value = itent.getDoubleExtra(Key,0);
|
Splash Activity
public class SplashActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_splash);
TimerTask task = new TimerTask() {
@Override
public void run() {
finish();
startActivity(new Intent(SplashActivity.this,
MainActivity.class));
}
};
Timer t = new Timer();
t.schedule(task,4000);
}
}
|
Working with Files
FileOutputStream fout =
openFileOutput("File1.txt",MODE_APPEND);
fout.write(data.getBytes());
fout.write("\n".getBytes());
fout.close();
FileInputStream fin = openFileInput("File1.txt");
String temp = "";
int c;
char ch;
while((c=fin.read())!=-1){
ch = (char)c;
temp += Character.toString(ch);
}
output.setText(temp);
fin.close();
|
Calling DBHelper
//to view student records
/*Cursor c = dbh.viewData();
StringBuilder str = new StringBuilder();
if(c.getCount()>0){
while (c.moveToNext()){
str.append("ID : " +c.getString(0));
str.append(" First Name : " +c.getString(1));
str.append(" Last Name : "+c.getString(2));
str.append(" Cell number : "+ c.getString(3));
str.append(" prov Id : " + c.getString(4));
str.append("\n");
}
result.setText(str);
//Create Record
boolean isAdded= db.addRec(fn.getText().toString(),ln.getText().toString(),
cn.getText().toString(),pi.getText().toString());
|
Database
public class DatabaseHelper extends SQLiteOpenHelper{
final static String DATABASE_NAME = "Information.db";
final static int DATABASE_VERSION = 4;
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
//creating student_table and province_table
String query = "Create Table [TABLENAME] ( [COLUMN] INTEGER PRIMARY KEY ,COLUMN2 TEXT,...)";
//Only INTEGER, TEXT OR REAL
try{
db.execSQL(query);
}
catch (Exception e){
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("Drop table if exists " + TABLE1_NAME);
db.execSQL("Drop table if exists " + TABLE2_NAME);
onCreate(db);
}
//Add Record
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("Column",value);
long r = db.insert(TABLE1_NAME,null,values);
if(r == -1){
return false;
}
else{
return true;
}
//method to view data
public Cursor viewData(){
SQLiteDatabase db = this.getWritableDatabase();
String query = "Select * from " + TABLE1_NAME;
Cursor c = db.rawQuery(query,null);
return c;
}
|
|
|
Play Media
mpG = new MediaPlayer();
mpG = MediaPlayer.create(this,R.raw.ukulele);
mpD = new MediaPlayer();
mpD = MediaPlayer.create(this,R.raw.drums);
mpG.start();
mpG.pauset();
|
Shared Prefs
final SharedPreferences sharedPref =
PreferenceManager.getDefaultSharedPreferences
(this);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
intYear = Integer.parseInt(years.getText().toString());
decLoan = Float.parseFloat(loan.getText().toString());
decInterest = Float.parseFloat(interest.getText().toString());
SharedPreferences.Editor editor = sharedPref.edit();
editor.putInt("key1",intYear);
editor.putFloat("key2",decLoan);
editor.putFloat("key3",decInterest);
editor.commit();
startActivity(new Intent
(MainActivity.this,Payment.class));
}
});
//Get the values
SharedPreferences sp = PreferenceManager.
getDefaultSharedPreferences(this);
int years = sp.getInt("key1",0);
float loan = sp.getFloat("key2",0);
float interest = sp.getFloat("key3",0);
|
Working With Fragments
public class FragmentOne extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_fragment_one, container, false);
}
//Main
public void clickOne(View view){
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
FragmentOne f1 = new FragmentOne();
ft.add(R.id.rlFrag1,f1);
ft.commit();
}
//Add this to the layout
android:onClick="clickOne"
//Dont forget to change fragment layout to relative
|
|
Created By
Metadata
Comments
No comments yet. Add yours below!
Add a Comment
Related Cheat Sheets