Java files
1.MainActivity
public class MainActivity extends AppCompatActivity {
ListView lv;
TextView tv3,tv5;
SQLiteDatabase db;
Cursor cursor;
RelativeLayout rl;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
lv = (ListView)findViewById(R.id.listView);
sp = getSharedPreferences("my_background",MODE_PRIVATE);
rl = (RelativeLayout)findViewById(R.id.r2);
int no = sp.getInt("k1",5475);
if (no==101)
{
rl.setBackgroundColor(Color.WHITE);
}
else if (no==102)
{
rl.setBackgroundColor(Color.RED);
}else if (no==103)
{
rl.setBackgroundColor(Color.GREEN);
}
db = openOrCreateDatabase("StudentRecordsDB", Context.MODE_PRIVATE, null);
try {
cursor = db.rawQuery("select * from news_inf", null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
MainActivity.this, R.layout.line, cursor
, new String[]{"_id", "news_title"}
, new int[]{R.id.id1, R.id.my_title});
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
tv5 = (TextView) view.findViewById(R.id.id1);
tv3 = (TextView) view.findViewById(R.id.my_title);
String n1 = tv3.getText().toString();
String n3 = tv5.getText().toString();
Intent i = new Intent(getApplicationContext(), Delete.class);
i.putExtra("k1", n1);
i.putExtra("k3", n3);
startActivity(i);
}
});
}
catch (Exception e)
{
Log.e("Read Data", "" + e);
Toast.makeText(MainActivity.this, "No Data", Toast.LENGTH_SHORT).show();
}
}
public void one(View v)
{
Intent i = new Intent(this,Add.class);
startActivity(i);
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
}
public void six (View v)
{
tv5 = (TextView)v.findViewById(R.id.id1);
String n1 = tv5.getText().toString();
db.execSQL("DELETE FROM news_inf WHERE _id='" +n1+ "'");
Toast.makeText(MainActivity.this, "deleted", Toast.LENGTH_SHORT).show();
cursor.requery();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu1, menu);
return true;
}
public void one(MenuItem mi)
{
SharedPreferences.Editor se = sp.edit();
rl.setBackgroundColor(Color.WHITE);
se.putInt("k1", 101);
se.commit();
}
public void two(MenuItem mi)
{
SharedPreferences.Editor se = sp.edit();
rl.setBackgroundColor(Color.RED);
se.putInt("k1", 102);
se.commit();
}
public void three(MenuItem mi)
{
SharedPreferences.Editor se = sp.edit();
rl.setBackgroundColor(Color.GREEN);
se.putInt("k1", 103);
se.commit();
}
public void ten(View v)
{
Intent i = new Intent(this,Add.class);
startActivity(i);
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
}
}
1.MainActivity
public class MainActivity extends AppCompatActivity {
ListView lv;
TextView tv3,tv5;
SQLiteDatabase db;
Cursor cursor;
RelativeLayout rl;
SharedPreferences sp;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.a);
lv = (ListView)findViewById(R.id.listView);
sp = getSharedPreferences("my_background",MODE_PRIVATE);
rl = (RelativeLayout)findViewById(R.id.r2);
int no = sp.getInt("k1",5475);
if (no==101)
{
rl.setBackgroundColor(Color.WHITE);
}
else if (no==102)
{
rl.setBackgroundColor(Color.RED);
}else if (no==103)
{
rl.setBackgroundColor(Color.GREEN);
}
db = openOrCreateDatabase("StudentRecordsDB", Context.MODE_PRIVATE, null);
try {
cursor = db.rawQuery("select * from news_inf", null);
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
MainActivity.this, R.layout.line, cursor
, new String[]{"_id", "news_title"}
, new int[]{R.id.id1, R.id.my_title});
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
tv5 = (TextView) view.findViewById(R.id.id1);
tv3 = (TextView) view.findViewById(R.id.my_title);
String n1 = tv3.getText().toString();
String n3 = tv5.getText().toString();
Intent i = new Intent(getApplicationContext(), Delete.class);
i.putExtra("k1", n1);
i.putExtra("k3", n3);
startActivity(i);
}
});
}
catch (Exception e)
{
Log.e("Read Data", "" + e);
Toast.makeText(MainActivity.this, "No Data", Toast.LENGTH_SHORT).show();
}
}
public void one(View v)
{
Intent i = new Intent(this,Add.class);
startActivity(i);
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
}
public void six (View v)
{
tv5 = (TextView)v.findViewById(R.id.id1);
String n1 = tv5.getText().toString();
db.execSQL("DELETE FROM news_inf WHERE _id='" +n1+ "'");
Toast.makeText(MainActivity.this, "deleted", Toast.LENGTH_SHORT).show();
cursor.requery();
}
@Override
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
getMenuInflater().inflate(R.menu.menu1, menu);
return true;
}
public void one(MenuItem mi)
{
SharedPreferences.Editor se = sp.edit();
rl.setBackgroundColor(Color.WHITE);
se.putInt("k1", 101);
se.commit();
}
public void two(MenuItem mi)
{
SharedPreferences.Editor se = sp.edit();
rl.setBackgroundColor(Color.RED);
se.putInt("k1", 102);
se.commit();
}
public void three(MenuItem mi)
{
SharedPreferences.Editor se = sp.edit();
rl.setBackgroundColor(Color.GREEN);
se.putInt("k1", 103);
se.commit();
}
public void ten(View v)
{
Intent i = new Intent(this,Add.class);
startActivity(i);
overridePendingTransition(R.anim.abc_fade_in, R.anim.abc_fade_out);
}
}
2.ADD
public class Add extends Activity {
SQLiteDatabase db;
EditText et1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.add);
et1 = (EditText)findViewById(R.id.et10);
db = openOrCreateDatabase("StudentRecordsDB", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS news_inf(_id integer primary key autoincrement,news_title VARCHAR);");
}
public void three(View v)
{
String n1 = et1.getText().toString().trim();
insertData(db, n1);
Toast.makeText(Add.this, "Added", Toast.LENGTH_SHORT).show();
Intent main = new Intent(Add.this, MainActivity.class);
startActivity(main);
et1.setText("");
}
private void insertData(SQLiteDatabase db
, String n1) {
db.execSQL("insert into news_inf values(null , ? )"
, new String[]{" " +n1});
}
}
3.Delete
public class Delete extends Activity {
SQLiteDatabase db;
TextView tv6,tv8;
EditText e1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.delete);
e1 = (EditText)findViewById(R.id.textView);
tv8 = (TextView)findViewById(R.id.textView3);
Intent i = getIntent();
String n6 = i.getStringExtra("k1");
String n8 = i.getStringExtra("k3");
e1.setText(n6);
tv8.setText(n8);
db = openOrCreateDatabase("StudentRecordsDB", Context.MODE_PRIVATE, null);
}
public void four (View v) {
// delete data from table
db.execSQL("update news_inf SET news_title='"+e1.getText()+"'WHERE _id='" + tv8.getText() + "'");
Toast.makeText(Delete.this, "updated", Toast.LENGTH_SHORT).show();
this.returnHome();
}
private void returnHome() {
Intent home_intent = new Intent(getApplicationContext(),
MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(home_intent);
}
}
Layouts
1.a
<?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:id="@+id/r2"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.bhanu1.notepad5.MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ADD DETAILS"
android:id="@+id/button"
android:onClick="one"
android:layout_marginTop="0dp"
android:background="@drawable/file1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:divider="@android:color/transparent"
android:dividerHeight="3dp"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="59dp" />
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@drawable/ie"
android:layout_marginBottom="30dp"
android:id="@+id/button2"
android:onClick="ten"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
2.add
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_margin="20dp"
>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/et10"
android:textColor="#001aff"
android:layout_marginTop="10dp"
android:textSize="25dp"
android:hint="Enter text"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="SAVE"
android:onClick="three"
android:layout_marginTop="20dp"
android:textSize="25dp"
android:id="@+id/button3"
android:background="@drawable/file1"
android:layout_gravity="center_horizontal" />
</LinearLayout>
3. delete
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:layout_marginLeft="15dp"
android:textSize="20dp"
android:textColor="#ff0d00"
android:id="@+id/textView" />
<TextView
android:layout_marginTop="10dp"
android:textSize="20dp"
android:layout_marginLeft="15dp"
android:textColor="#ff0d00"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:textAppearance="?android:attr/textAppearanceLarge"
android:id="@+id/textView3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="UPDATE"
android:layout_marginTop="25dp"
android:id="@+id/button3"
android:onClick="four"
android:background="@drawable/file1"
android:layout_gravity="center_horizontal" />
</LinearLayout>
4.line
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="@drawable/aa"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:id="@+id/id1"
android:onClick="six"
android:layout_width="50dp"
android:textColor="@android:color/transparent"
android:background="@drawable/d2"
android:layout_height="50dp"
android:layout_gravity="right"
android:layout_alignParentTop="true"
android:layout_alignBottom="@+id/my_title"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:id="@+id/my_title"
android:layout_width="wrap_content"
android:layout_marginLeft="20dp"
android:layout_height="50dp "
android:textSize="20dp"
/>
</RelativeLayout>
Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.bhanu1.notepad5">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Add"/>
<activity android:name=".Delete"/>
</application>
</manifest>
No comments:
Post a Comment