I am working on a setup screen that allows the user to change the color theme of the app. I want to use a GridView of colored square images for the user to choose from. Right now I have a toast message that pops with the position in the color image array when clicked. The following opens my theme activity from a setup menu:
themeButton = (ImageButton) findViewById(R.id.imBtnAccentColor);
themeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(SetupContactActivity.this, ThemeActivity.class);
startActivity(intent);
}
});
Here's an image of the GridView that the user will choose a theme color from.
gridViewTiles
Theme Activity:
public class ThemeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.themes);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(ThemeActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
}
Here is an ImageAdapter class that helps build the image view based on the number of images in my array of images:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// Create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
// If it's not recycled, initialize some attributes.
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(320, 300));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// References to the color images
private Integer[] mThumbIds = {
R.drawable.orange,
R.drawable.yellow,
R.drawable.yellow_orange,
R.drawable.brown,
R.drawable.medium_grey,
R.drawable.dark_grey,
R.drawable.pink,
R.drawable.light_blue,
R.drawable.medium_green,
R.drawable.purple,
R.drawable.blue,
R.drawable.brownish_green,
R.drawable.dark_purple,
R.drawable.dark_blue,
R.drawable.green
};
}
Related
I have an array of images and string like this:
String[] stations = new String[] {
"GHOTKI 91 Radio FM","UmerKot 91.4 Radio","TMK 100.20 Radio"
};
public static int [] images ={R.drawable.ghotkifmlogo,R.drawable.umerkotfmlogo,R.drawable.tmkfmlogo};
In an custom listview how can I replace these in another Image view and texview?
I need to change that imageview and textview everytime with the clicked item's image and text.
Here's my Custom Adapter:
public class TrackAdapter extends BaseAdapter{
String [] description;
Context context;
int [] imageId;
public TrackAdapter(Context c, String[] d, int[] prgmImages) {
description=d;
context= c;
imageId=prgmImages;
}
#Override
public int getCount() {
return description.length;
}
#Override
public Object getItem(int position) {
return position;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if(convertView == null){
convertView = LayoutInflater.from(context).inflate(R.layout.cardviewlayout,null);
}
//get the textview and set its text
TextView tv=(TextView) convertView.findViewById(R.id.tracktitle);
//get the img view and set its img icon
ImageView im=(ImageView) convertView.findViewById(R.id.trackimage);
tv.setText(description[position]);
im.setImageResource(imageId[position]);
return convertView;
}
Here's Main Activity:
private TextView mSelectedTrackTitle;
private ImageView mSelectedTrackImage;
private MediaPlayer mMediaPlayer;
private ImageView mPlayerControl;
ListView lv_tracks;
TrackAdapter track_adapter;
String[] stations = new String[] {
"GHOTKI 91 Radio FM","UmerKot 91.4 Radio","TMK 100.20 Radio"
};
public static int [] images ={R.drawable.ghotkifmlogo,R.drawable.umerkotfmlogo,R.drawable.tmkfmlogo};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mMediaPlayer = new MediaPlayer();
//get a reference to our ListView so we can associate it with our custom ArrayAdapter
lv_tracks = (ListView) findViewById(R.id.track_list);
//create a new CustomAdapter
track_adapter =new TrackAdapter(this,stations,images);
lv_tracks.setAdapter(track_adapter);//connect the ListView with myCustomAdapter
//Want to set selected image and title here in these
mSelectedTrackTitle = (TextView)findViewById(R.id.selected_track_title);
mSelectedTrackImage =(ImageView)findViewById(R.id.selected_track_image);
lv_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
??
}
Do not really understand what to do with onitemselect here .
in order to change image and text.
you can get current item position by using listview OnItemClickListener it will returns position of an Item clicked then you can use this position as your desired operation
lv_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
mSelectedTrackTitle.setText(stations[position]);
mSelectedTrackImage.setImageDrawable(ContextCompat.getDrawable(this,images [position]));
// or
mSelectedTrackImage.setImageResource(images [position]);
}
Try something like this
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mSelectedTrackTitle.setText( stations[position] );
mSelectedTrackImage.setImageResource( images[positions] );
}
It will get the station name and image from the clicked position in your arrays, and update your views.
You can achieve it like this:
lv_tracks.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
mSelectedTrackTitle.setText(stations[position]);
mSelectedTrackImage.setImageResource(images[position]);
}
I am making a project in Android studio.
I have a gridview and want to assign the number of items in it according to what the user inputs in an AlertDialog.
I tried to create a method in ImageAdapter that contains a for loop to fill the array mThumbIds with images depending on what number the user chose, but that didn't work.
How can I do this ?
Here is the ImageAdapter:
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public Integer[] mThumbIds = {
R.drawable.pic_1, R.drawable.pic_1,
R.drawable.pic_1,R.drawable.pic_1,
R.drawable.pic_1, R.drawable.pic_1,
};
// Constructor
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
//Calculation of ImageView Size - density independent.
//maybe you should do this calculation not exactly in this method but put it somewhere else.
Resources r = Resources.getSystem();
float px = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 60, r.getDisplayMetrics());
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams((int)px, (int)px));
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
//imageView.setPadding(8, 8, 8, 8);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
}
And this is the MainActivity:
public class MainActivity extends Activity {
EditText input;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
GridView gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("How many tables do you have ?");
input = new EditText(this);
builder.setView(input);
builder.setPositiveButton("Submit", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String txt = input.getText().toString();
Toast.makeText(getApplicationContext(),txt, Toast.LENGTH_LONG).show();
}
});
AlertDialog ad = builder.create();
ad.show();
}
}
In your adapter add the following:
private int count;
public void setCount(int count){
this.count = count;
notifyDataSetChanged();
}
Your getCount() method will be
#Override
public int getCount() {
if(count == 0) return mThumbIds.length;
else return count;
}
In your button onClick call following:
imageAdapter.setCount(count);
This is Adapter for grid view. I included image and letters in adapter class.
public class GridAdapter extends BaseAdapter {
private int icons[];
private String letters[];
private Context context;
private LayoutInflaterinflater;
public GridAdapter(Context context , int icons[] , String letters[]){
this.context=context;
this.icons=icons;
this.letters=letters;
}
#Override
public int getCount() {
return letters.length;
}
#Override
public Object getItem(int position) {
return letters[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View gridView = convertView;
if(convertView == null){
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
gridView = inflater.inflate(R.layout.custom_layout,null);
}
ImageView icon = (ImageView) gridView.findViewById(R.id.icons);
TextView letter = (TextView) gridView.findViewById(R.id.letters);
icon.setImageResource(icons[position]);
letter.setText(letters[position]);
return gridView;
}
}
This is main class. I made 8 card and want to add an activity for each card.how to add activity for each card.
public class NavigationMainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
GridView gridView;
String letterList[] = {"Types of industries", "Safety Sign's",
"Tools & equipments", "Slogens", "Low", "Definitions", "First aid kit", "Near by fire stations"};
int lettersIcon[] = {R.drawable.safetyicon, R.drawable.signs, R.drawable.tools,
R.drawable.slogen, R.drawable.low, R.drawable.definitions,
R.drawable.firstkit, R.drawable.firestations};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
gridView = (GridView) findViewById(R.id.gridView);
GridAdapter adapter = new GridAdapter(NavigationMainActivity.this, lettersIcon, letterList);
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position, long id)
{
// Sending image id to FullScreenActivity
Intent i = new Intent(getApplicationContext(), TypesOfIndusturies.class);
}
});
I want to load images from external storage to Gallery in android. I have the file path. I want to display all the pics of Mix folder in gallery.
File file = new File(Environment.getExternalStorageDirectory().getPath() +"/Gallery/Mix/");
I am able to load the images in gallery when I put the images in drawable folder but where and what changes should I make So I could display the images from Mix Folder.
PS. I know Gallery is deprecated but still I need to use it.
MainActivity.java
public class MainActivity extends Activity {
Gallery gallery;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
gallery = (Gallery)findViewById(R.id.gallery1);
imageView = (ImageView)findViewById(R.id.imageView1);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(), position+"", Toast.LENGTH_SHORT).show();
imageView.setImageResource(ImageAdapter.ThumbsIds[position]);
}
});
}
}
ImageAdapter.java
public class ImageAdapter extends BaseAdapter implements SpinnerAdapter {
private Context context;
public ImageAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return ThumbsIds.length;
}
#Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return null;
}
#Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = null;
if(convertView==null){
imageView = new ImageView(context);
imageView.setLayoutParams(new Gallery.LayoutParams(600,550));
imageView.setPadding(4, 4, 4, 4);
}else{
imageView = (ImageView)convertView;
}
imageView.setImageResource(ThumbsIds[position]);
return imageView;
}
public static Integer[] ThumbsIds={
R.drawable.ppic1,
R.drawable.ppic2,
R.drawable.ppic3
};
}
I follow some tutorial "How to use GridView in android" and try to make Gallery....
but why?
I already did same with tutorial did....
this file GridActivity.java :
public class MainActivity extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridView = (GridView) findViewById(R.id.grid_view);
gridView.setAdapter(new ImageAdapter(this));
}
and this for file ImageAdapter.java :
public class ImageAdapter extends BaseAdapter {
private Context mContext;
public ImageAdapater(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds.length;
}
public Object getItem(int position) {
return mThumbIds[position];
}
public long getItemId (int position) {
return 0;
}
public View getView(int position, View converView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70,70));
return imageView;
}
public Integer[] mThumbIds = {
R.drawable.image1
};
}
there's no error...
but when I tried to run with emulator...
"Unfotunetly, ........... has stop"
please....help...me..
new ImageAdapter(this) Needs a constructor that takes context as the param.
Change this
public void ImageAdapater(Context c) { //remove void for constructor
to
public ImageAdapater(Context c) {
More info
http://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html