Android new blog source code
Here is an example of a basic Android blog app with a simple UI and CRUD (Create, Read, Update, Delete) functionality:
BlogApp
MainActivity.java
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private ListView listView;
private EditText titleEditText;
private EditText contentEditText;
private Button addButton;
private Button deleteButton;
private List<BlogPost> blogPosts = new ArrayList<>();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listView);
titleEditText = findViewById(R.id.titleEditText);
contentEditText = findViewById(R.id.contentEditText);
addButton = findViewById(R.id.addButton);
deleteButton = findViewById(R.id.deleteButton);
addButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String title = titleEditText.getText().toString();
String content = contentEditText.getText().toString();
BlogPost blogPost = new BlogPost(title, content);
blogPosts.add(blogPost);
listView.setAdapter(new BlogPostAdapter(MainActivity.this, blogPosts));
titleEditText.setText("");
contentEditText.setText("");
}
});
deleteButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int position = listView.getCheckedItemPosition();
if (position!= ListView.INVALID_POSITION) {
blogPosts.remove(position);
listView.setAdapter(new BlogPostAdapter(MainActivity.this, blogPosts));
}
}
});
}
}
BlogPost.java
public class BlogPost {
private String title;
private String content;
public BlogPost(String title, String content) {
this.title = title;
this.content = content;
}
public String getTitle() {
return title;
}
public String getContent() {
return content;
}
}
BlogPostAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.List;
public class BlogPostAdapter extends ArrayAdapter<BlogPost> {
public BlogPostAdapter(Context context, List<BlogPost> blogPosts) {
super(context, 0, blogPosts);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (view == null) {
view = LayoutInflater.from(getContext()).inflate(R.layout.blog_post_item, parent, false);
}
BlogPost blogPost = getItem(position);
TextView titleTextView = view.findViewById(R.id.titleTextView);
TextView contentTextView = view.findViewById(R.id.contentTextView);
titleTextView.setText(blogPost.getTitle());
contentTextView.setText(blogPost.getContent());
return view;
}
}
activity_main.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">
<EditText
android:id="@+id/titleEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Title" />
<EditText
android:id="@+id/contentEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Content" />
<Button
android:id="@+id/addButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Add" />
<ListView
android:id="@+id/listView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="@+id/deleteButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Delete" />
</LinearLayout>
blog_post_item.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="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18sp" />
<TextView
android:id="@+id/contentTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp" />
</LinearLayout>
This code creates a simple Android app with a UI that allows users to add, read, update, and delete blog posts. The app uses a ListView
to display the blog posts, and an EditText
and Button
to add new posts. The app also includes a Button
to delete posts.
Note that this is a basic example and you may want to add additional features such as data storage, validation, and error handling.
To run this code, create a new Android project in Android Studio and copy the code into the corresponding files. Then, run the app on an emulator or physical device to test it.