Android Studio RSS Reader. - java

I'm currently working on attempting to make an RSS Reader using a ListView in Android studio. I've already managed to make the ListView, but i have no idea where to go next. I cant seem to find any good tutorials online on how i should tackle this, and Anything i do find is out of date by 3 years. Any tips?
activity_main.xml
<?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.micha.rssreader.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/listviewAD" />
MainActivity.java
package com.example.micha.rssreader;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
public ListView AdFeed; // maakt listview aan
public String[] items;
public ArrayAdapter<String> adapt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AdFeed = (ListView)findViewById(R.id.listviewAD);
items = new String[]{"blue", "red", "black", "orange", "purpol"};
adapt = new ArrayAdapter<String>(this, R.layout.items, items);
AdFeed.setAdapter(adapt);//zet de data acther de listview
AdFeed.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}

Try this example
http://www.androidauthority.com/simple-rss-reader-full-tutorial-733245/
it is up to date since it uses buildToolsVersion 25.0.1.
Only difference is that it uses RecyclerView instead of ListView, which is recommended since it is lighter than ListView

Related

How to add images when using spinner

I am a beginner at using Android Studio Java, I have been assigned to make a dropdown list with all the car names. I have to include 30 car images but to start things off I am trying to include 5 images, I have been trying to add an image on each of the cars that are on the dropdown list. I figured out how to do the spinner and I have the code for it but now when I click on the dropdown list and click the Ford car and I want the images to appear as well.
For the main.java (called Activity2.java) here is the code for it
package com.example.myapplication;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.Toast;
import java.lang.reflect.Array;
import java.util.ArrayList;
public class Activity2 extends AppCompatActivity {
private Spinner carsSpinner;
private ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_2);
carsSpinner = findViewById(R.id.carsSpinner);
//final ArrayList<String> cars = new ArrayList<>();
// cars.add("Audi");
// cars.add("Bentley");
// cars.add("BMW");
// cars.add("Chevrolet");
//cars.add("Citroen");
//ArrayAdapter<String> carsAdapter = new ArrayAdapter<>(this,
android.R.layout.simple_spinner_dropdown_item, cars);
//carsSpinner.setAdapter(carsAdapter);//
carsSpinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(Activity2.this, carsSpinner.getSelectedItem().toString() + " Selected",
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
For the main activity.xml (called activity2.xml) here is the code for it, I know I have to include the ImageView there.
<?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"
tools:context=".MainActivity">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:id="#+id/carsSpinner"
android:entries="#array/cars"/>
</RelativeLayout>
For the string.xml here is the code for it
<resources>
<string name="app_name">My Application</string>
<string-array name="cars">
<item>Audi</item>
<item>Bentley</item>
<item>BMW</item>
<item>Ferrari</item>
<item>Citroen</item>
</string-array>
This is how my application is looking but now I want to include the car image for each car on it, it will mean a lot if you guys could show me through the right direction.
You have to make a custom layout and adapter.
More information on how to implement it on the link below:
https://www.codingdemos.com/android-custom-spinner-images-text/

Facing problem in findView (Android Studio)

I'm new on Android Studio and I'm working on a project.
I'm facing this error
I've declared listview but don't know why it's not working.
CODE:
package com.example.animation;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class view_rooms extends AppCompatActivity {
ListView listView;
String mTitle[]={"Room1","Room2","Room3","Room4","Room5"};
String mDescription[]={"Access Room1","Access Room2","Access Room3","Access Room4","Access Room5"};
int images[]={R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_rooms);
listView=findViewById(R.id.ListView);
}
findViewById() searches the view by id into the layout file. In this case into the layout: R.layout.activity_view_rooms. You need to define into this layout one ListView widget and assign one id to search with findViewById().
For example into the layout activity_view_rooms you need to include the view:
<?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">
<ListView
android:id="#+id/list_view_id"
android:layout_width="match_parent"
android:layout_height="match_parent">
</ListView>
</LinearLayout>
And later you can get the instance of view with finViewById() in this way:
package com.example.animation;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class view_rooms extends AppCompatActivity {
ListView listView;
String mTitle[]={"Room1","Room2","Room3","Room4","Room5"};
String mDescription[]={"Access Room1","Access Room2","Access Room3","Access Room4","Access Room5"};
int images[]={R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open,R.drawable.lights_open};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_rooms);
listView=findViewById(R.id.list_view_id);
}
You need to add this line android:id="#+id/ListView" in your activity_view_rooms.xml
So the code will looks like this:
<ListView
android:id="#+id/ListView"
android:layout_width="match_parent"
android:layout_height="match_parent">

Grid view item menu

I would like to ask how to add a menu(three points with options like the actionBar has) to a grid view item? I need it to look similar to youtube's mobile application videos: they all have these three points with a couple of options inside of them. How can I achieve this?
Here's the code:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ListAdapter;
import java.util.ArrayList;
public class teamTasks extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_individual_tasks);
Button btn3 = (Button) findViewById(R.id.addTasks);
final ArrayList<String> listItemsTasks=new ArrayList<String>();
final ListAdapter addAdapterTasks = new ArrayAdapter<String>(this, R.layout.custom_box,
R.id.customBox, listItemsTasks);
GridView gvtask = (GridView) findViewById(R.id.gvTasks);
gvtask.setAdapter(addAdapterTasks);
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
listItemsTasks.add("New Task");
}
});
gvtask.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
}
});
}
}
The layout:
<?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">
<Button
android:layout_width="60dp"
android:layout_height="60dp"
android:text="+"
android:textSize="20sp"
android:id="#+id/addTasks"/>
<GridView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/addTasks"
android:id="#+id/gvTasks"
android:numColumns="2">
</GridView>
</RelativeLayout>
Custom gridView item:
<?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">
<TextView
android:layout_width="wrap_content"
android:layout_height="120dp"
android:id="#+id/customBox"/>

Android ListView Integer array

I am trying to get an integer array into ListView using an adapter but do not seem to know where to begin.
I want the user to be able to open the listview, select a number from 1-99 and have the selected number be deducted from the current year and result displayed in a TextView.
Any help in the right direction would be fantastic!
first you have to create xml of listview given below
<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=".MainActivity" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" >
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
</RelativeLayout>
make one arraylist<String> list=new arraylist<string>;
add value 1-99 using for loop
and then set the array adpter on that listview as like
package com.example.arraydemo;
import java.util.ArrayList;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
ArrayList<String> list;
ListView li;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=new ArrayList<String>();
li=(ListView)findViewById(R.id.listView1);
for(int i=1;i<=99;i++){
list.add(""+i);
}
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
android.R.layout.simple_list_item_1,
list );
li.setAdapter(arrayAdapter);
li.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position,
long id) {
// TODO Auto-generated method stub
}
});
}
}
and ontemclicklistener you get current date from system deduct position of listview from that date

Error setListAdapter in simple program

I'm new to android programming and I still have a few things that I don't understand. For example, I'm with a program that when you click on a button, it tries to find any devices near. The problem is that I don't know how to solve a mistake I seem to have made from the beggining. This is what I have
This is the main activity:
import java.util.ArrayList;
import java.util.List;
import java.util.Set;
import android.app.Activity;
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.ArrayAdapter;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void buscar(View view){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
List<String> s = new ArrayList<String>();
for(BluetoothDevice bt : pairedDevices)
s.add(bt.getName());
setListAdapter(new ArrayAdapter<String>(this, R.id.list, s));
}
}
And here is the xml file
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
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=".MainActivity" >
<Button
android:id="#+id/botonvinculo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:clickable="true"
android:onClick="buscar"
android:text="Buscar dispositivos" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/botonvinculo"
android:layout_below="#+id/botonvinculo" >
</ListView>
</RelativeLayout>
The thing is that I cannot make the call to setListAdapter unless my main activity extends from ListActivity, and even trying so, it returns an error saying "You must have a ListView whose attribute is "android.R.id.list".
I'd appreciate any help. Thank you very much.
If you use ListActivity then you should use android:id="#android:id/list" in your XML instead of android:id="#+id/list"
or else you can get an object of ListView in your XML like
ListView list = (ListView) findViewById(R.id.list);
and then use list.setAdapter
setListAdapter will work only when your activity extends from ListActivity. use this in your case
listview.setAdapter();
if you want to use setListAdapter() only make change these things
public class MainActivity extends Activity
to
public class MainActivity extends ListActivity
and
android:id="#android:id/list" for your list view in xml
Your class extends Activity. setListAdapter is a method of ListActivity
ListView lv;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
lv = (ListView)findViewById(R.id.list);
}
Then
public void buscar(View view){
BluetoothAdapter mBluetoothAdapter = BluetoothAdapter.getDefaultAdapter();
Set<BluetoothDevice> pairedDevices = mBluetoothAdapter.getBondedDevices();
List<String> s = new ArrayList<String>();
for(BluetoothDevice bt : pairedDevices)
s.add(bt.getName());
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,android.R.layout.simple.list_item_1,s);
lv.setAdapter(adapter);
}

Categories

Resources