How to add images when using spinner - java

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/

Related

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">

Android Studio RSS Reader.

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

Built a custom file in XML, Can't call it in Java by any means

I am working on ListViews. I decided to make a customized listview. For it, definitely another xml file (customized) is required. I built it but I just cannot call it by any means in java. Look the following line as row_layout is always RED with error "Cannot Resolve Symbol "row_layout":
ListAdapter myadapter = new ArrayAdapter<String> (this, android.R.layout.row_layout, cars);
Following is code of my customized xml file row_layout :
<?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:id="#+id/row_layout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/myTextView1"
android:textSize="32dp"
android:textStyle="italic"/>
</LinearLayout>
Following is my Java file code :
package com.ranatalha.mylistapp;
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.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String [] cars=
{
"Mehran",
"Corolla",
"Faw v2",
"Honda City"
};
//to link my above created array with in a list
//ListAdapter myadapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, cars);
ListAdapter myadapter = new ArrayAdapter<String> (this, android.R.layout.row_layout, cars);
//referring the listview createdin xml
ListView mylistview = (ListView) findViewById(R.id.mylistView);
//connecting listview with adapter
mylistview.setAdapter(myadapter);
//Catching clicks on listview
mylistview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
//implements method
public void onItemClick(AdapterView<?> adapterView, View view, int position, long l) {
String CarsPicked = "Your Picked The Car: " + String.valueOf(adapterView.getItemAtPosition(position));
Toast.makeText(MainActivity.this, CarsPicked, Toast.LENGTH_SHORT).show();
}
}
);
}
}
android.R uses built in android resources. Try android.R.layout.simple_list_item1.
Or create your own layout and call its ID from R.layout.
Instead of android.R.layout.row_layout we should use R.layout.row_layout because android.R access the resources of the OS whereas R can help in importing custom layouts. Thanks to #BlackBelt for helping me in the comments section above :)

Adding views to another line (another LinearLayout)?

So I'm creating a small application, and thanks to this site, I've progressed on it a lot more. The idea of it is that I will just have a button, and when it is clicked, it will add a new row with two "EditText" and one "CheckBox". I've somewhat accomplished this through dynamically adding these with a custom ListView and a custom adapter. I'm still iffy on custom ListView's and adapters.
Anyways, I've gotten it to add the two EditText and one CheckBox when the button is clicked, but it will not add any more after this (this is a separate problem though). The problem I'm having, is that it will add them on the same row as the button. This is where I get lost. I'm not sure how to get this custom ListView to display on a new row, rather than the same row as the button.
Now, here is my MainActivity code:
import android.content.Context;
import android.graphics.Color;
import android.support.v7.app.ActionBar;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.InputType;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.view.ViewGroup.LayoutParams;
import java.lang.reflect.Array;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
ArrayList<CheckBox> checkBoxes = new ArrayList<>();
ArrayList<EditText> courses = new ArrayList<>();
ArrayList<EditText> credits = new ArrayList<>();
int numOfCheckBoxes = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
ll.setOrientation(LinearLayout.HORIZONTAL);
Button addCourse = new Button(this);
addCourse.setLayoutParams(new ActionBar.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));
addCourse.setText("Add Course");
addCourse.setTextColor(Color.parseColor("#FFFFFF"));
addCourse.setBackgroundColor(Color.parseColor("#FF0000"));
addCourse.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ListView myListView = new ListView(MainActivity.this);
List<CustomRow> rows = new ArrayList<CustomRow>();
rows.add(new CustomRow(MainActivity.this));
MyListArrayAdapter myAdapter = new MyListArrayAdapter(MainActivity.this, rows);
myListView.setAdapter(myAdapter);
ll.addView(myListView);
}
});
ll.addView(addCourse);
} // End of onCreate
public void onCheckBoxChecked(View v) {
for (int i = 0; i < numOfCheckBoxes; i++) {
if (checkBoxes.get(i).isChecked()) {
courses.get(i).setEnabled(false);
courses.get(i).setFocusable(false);
courses.get(i).setInputType(InputType.TYPE_NULL);
credits.get(i).setEnabled(false);
credits.get(i).setFocusable(false);
credits.get(i).setInputType(InputType.TYPE_NULL);
} else {
courses.get(i).setEnabled(true);
courses.get(i).setFocusable(true);
courses.get(i).setFocusableInTouchMode(true);
courses.get(i).setInputType(InputType.TYPE_CLASS_TEXT);
credits.get(i).setEnabled(true);
credits.get(i).setFocusableInTouchMode(true);
credits.get(i).setFocusable(true);
credits.get(i).setInputType(InputType.TYPE_CLASS_NUMBER);
} // End of if else
} // End of for loop
} // End of onCheckBoxChecked
} // End of MainActivity
Here is my "MyListArrayAdapter" class (my custom adapter):
import android.app.Activity;
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.view.LayoutInflater;
import android.widget.CheckBox;
import android.widget.EditText;
import java.util.List;
/**
* Created by Thomas on 5/21/2016.
*/
public class MyListArrayAdapter extends BaseAdapter {
LayoutInflater inflater;
List<CustomRow> rows;
public MyListArrayAdapter(Activity context, List rows){
super();
this.rows = rows;
this.inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return rows.size();
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
CustomRow row = rows.get(position);
View v = convertView;
if (convertView==null) {
v = inflater.inflate(R.layout.new_course_row, null);
}
EditText course = (EditText) v.findViewById(R.id.course);
EditText credits = (EditText) v.findViewById(R.id.credits);
CheckBox checkBox = (CheckBox) v.findViewById(R.id.checkBox);
course.setHint("Enter Course");
credits.setHint("Enter Credits");
checkBox.setText("Check if complete");
course.setMaxHeight(150);
credits.setMaxHeight(150);
checkBox.setMaxHeight(150);
return v;
}
}
My "CustomRow" class (the custom class with the two EditText and one CheckBox):
import android.content.Context;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.LinearLayout;
/**
* Created by Thomas on 5/21/2016.
*/
public class CustomRow extends LinearLayout{
public CustomRow(Context context) {
super(context);
setOrientation(LinearLayout.HORIZONTAL);
EditText course = new EditText(context);
EditText credits = new EditText(context);
CheckBox checkBox = new CheckBox(context);
addView(course);
addView(credits);
addView(checkBox);
}
}
My "new_course_row.xml" file:
<?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">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/course"
android:layout_weight="1"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/credits"
android:layout_weight="1"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/checkBox"
android:onClick="onCheckBoxChecked"/>
</LinearLayout>
Lastly, my "activity_main.xml":
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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="exporian.collegetracker.MainActivity"
android:id="#+id/ll">
</LinearLayout>
</ScrollView>
Thank you in advance everyone! I've been stuck here for a few days. Not looking for anyone to write the app for me, just looking for some direction.
make sure that after adding a button you have called these two methods in your adapter to update the list
BuddyFeedsAdapter.this.notifyDataSetChanged();
notifyDataSetInvalidated();
If your code is working properly and adding row on same row, then please check below solution.
As you have write below line ,
List<CustomRow> rows = new ArrayList<CustomRow>();
in OnButton CLick event, so whenever button will be clicked, it will initialise rows again and remove all previous records from it, so just write above line at top or in onCreate() method after setContentView, then you will get all rows on click.
Hope this will help you.

Error popping up while setting on click listner in spinners

Im new to android and searched a lot regarding this problem but couldnt find solution,im getting this error while setting on click listner option on the spinner options,please help me in solving this error though its a small one.
Error
The method setOnItemSelectedListener(AdapterView.OnItemSelectedListener) in the type AdapterView is not applicable for the arguments (MainActivity)
#MainActivity.java
package com.example.spinners;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class MainActivity extends ActionBarActivity implements OnItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner1);
spinner.setOnItemSelectedListener(this);
// Create an ArrayAdapter using the string array and a default spinner layout
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.planets_array, android.R.layout.simple_spinner_item);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
;
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
// code
}
}
activity_main.xml
<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.spinners.MainActivity" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="110dp" />
</RelativeLayout>
spinner.setOnItemSelectedListener(this);
This expects this to be an object that implements the onItemSelectedListener interface, you can do it as you want, but you need to change this
public class MainActivity extends ActionBarActivity
to
public class MainActivity extends ActionBarActivity implements onItemSelectedListener
and implement the method
#Override
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
// code
}
Edit:
and this method
#Override
public void onNothingSelected (AdapterView<?> parent) {
// code
}

Categories

Resources