I would like to add two gallery view to my app. To be clear with my question as it shows in the image below. I would like to add another horizontal scroll view below the image view that will also display images and function exactly as the one above.
Here is the code that I've come up with so far. It's the same example as the developer samples. Just some minor changes to it.
public class Gallery2DemoActivity extends Activity {
private Gallery gallery,gallery1;
private ImageView imgView;
private Integer[] Imgid = {
R.drawable.a_1, R.drawable.a_2, R.drawable.a_3, R.drawable.a_4, R.drawable.a_5, R.drawable.a_6, R.drawable.a_7
};
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this));
gallery1 = (Gallery)findViewById(R.id.examplegallery1);
gallery1.setAdapter(new AddImgAdp(this));
imgView = (ImageView)findViewById(R.id.ImageView01);
imgView.setImageResource(Imgid[0]);
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
imgView.setImageResource(Imgid[position]);
}
});
}
public class AddImgAdp extends BaseAdapter {
int GalItemBg;
private Context cont;
public AddImgAdp(Context c) {
cont = c;
TypedArray typArray = obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount() {
return Imgid.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imgView = new ImageView(cont);
imgView.setImageResource(Imgid[position]);
imgView.setLayoutParams(new Gallery.LayoutParams(110, 100));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}
}
Below is my XML file. Which is pretty straight forward and simple.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/examplegallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
<Gallery
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/examplegallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Can't I set two gallery Views to one AddImg adapter ?
Where am I going wrong ? The Second Horizontal Scroll View is not showing up.
How can I work around this problem ?
I thinks that the problem is in your layout implementation. Try setting android:layout_weight="1" on the image view. I think that will solve the issue. Modified layout is provided below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Gallery
android:id="#+id/examplegallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"/>
<Gallery
android:id="#+id/examplegallery1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Related
I have a soundboard app, and I want to make a search bar so users can find the sounds by search. I have multiple tabs for the buttons. The buttons have different images, which are stored in an array. Each buton has textview below them, as a title. My questions is how to make a search bar so the users can find sounds from all of the 10 Tabs(around 300 sounds overall) at the same time? I have 10 Tabs so that means I have 30 arrays with different informations like the sound of the button, the image of the button, and the text for the textview below the buttons. Here are the codes:
single_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent" android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/button"
android:longClickable="true"
android:onClick="werbung"
android:padding="0dp"
android:text=""
android:textColor="#ffffff"
android:textSize="17dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textview"
android:layout_width="0dp"
android:layout_height="42dp"
android:layout_below="#id/button"
android:layout_alignLeft="#id/button"
android:layout_centerInParent="true"
android:gravity="center"
android:text=""
android:textColor="#color/black"
android:textSize="17sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/button" />
</android.support.constraint.ConstraintLayout>
The arrays containing the texts for the textview, and the sounds and images for the buttons in Tab1.java(all of the tabs' code is the same, just with different arrays):
public String[] items = {
"text1", "text2", "text3"
};
public static int[] soundfiles ={
R.raw.sound1, R.raw.sound2, R.raw.sound3
};
public static int[] images ={
R.drawable.image1, R.drawable.image2, R.drawable.image3
};
The buttons with the text below them are created from the single_item.xml with this code(this is also in the Tab.java):
// CustomGrid Adapter
public class CustomGridAdapter extends BaseAdapter {
private Context context;
private String[] items;
LayoutInflater inflater;
public CustomGridAdapter(Context c, String[] items) {
this.context = c;
this.items = items;
inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return items.length;
}
#Override
public Object getItem(int position) {
return items[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
if (convertView == null) {
convertView = inflater.inflate(R.layout.single_item, null);
}
Button button = (Button) convertView.findViewById(R.id.button);
TextView textView = (TextView) convertView.findViewById(R.id.textview);
textView.setText(items[position]);
button.setBackgroundResource(images[position]);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (context instanceof MainActivity) {
((MainActivity) context).TabEightItemClicked(position);
}
}
});
return convertView;
}
}
Tab 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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/tab1">
<GridView
android:background="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tabOneGridView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:numColumns="auto_fit"
android:horizontalSpacing="0dp"
android:verticalSpacing="0dp"
android:columnWidth="155dp"
android:stretchMode="spacingWidthUniform"/>
</RelativeLayout>
Tab design: https://i.stack.imgur.com/NlC09.png
My ListView is goind to show the rank of users, usernames, and their score, smth like: 1. John 25
and I'd like to display the score just like it's shown on the example attached.
What you need to use is a CustomAdapter instead of the default adapter. Please refer to this other answer Custom Adapter for List View
Basically you create your own item layout: how you wish each item in the list will be displayed (it could be a TextView for the username and another TextView for the Score).
And then in the getView method of the Adapter set the value of each one.
You can achieve this using a custom list adapter and custom layout xml
Try this:-
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.list_view);
listView.setAdapter(new NameRankAdapter(this));
}
}
NameRankAdapter.java
public class NameRankAdapter extends BaseAdapter {
private Context context;
public NameRankAdapter(Context context) {
this.context = context;
}
#Override
public int getCount() {
return 20;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (view == null) {
view = LayoutInflater.from(context).inflate(R.layout.list_item, null);
}
TextView name = view.findViewById(R.id.name);
name.setText("Name " + i);
TextView rank = view.findViewById(R.id.rank);
rank.setText(String.valueOf(i));
return view;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".MainActivity">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:text="name"
android:id="#+id/name"
android:textSize="30sp"
android:textColor="#android:color/black"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="name"
android:textSize="25sp"
android:id="#+id/rank"
android:textColor="#android:color/black"/>
</FrameLayout>
I want to implement a ListView with internet images. I use Glide to load images in list items, but I found sizes of images always be confusing. I wrote some test code to reappear this problem.
At first it is normal.
However when I slide the ListView fastly, the sizes of images become confusing.
How can I fix the problem without modify the source code of glide?
MainActivity.java
public class MainActivity extends AppCompatActivity {
private String[] urls = new String[]{"https:......"};
//urls of images.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.list_view);
final SparseArray<String> sparseArray = new SparseArray<>();
listView.setAdapter(new BaseAdapter() {
#Override
public int getCount() {
return 100000;
}
#Override
public Object getItem(int i) {
String url = sparseArray.get(i);
if(url == null){
url = urls[(int)(Math.random() * urls.length)];
sparseArray.put(i,url);
}
return url;
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if(view == null){
view = View.inflate(getBaseContext(),R.layout.list_view_item_image,null);
}
ImageView imageView = view.findViewById(R.id.image_view);
Glide.with(getBaseContext())
.load((String)getItem(i))
.into(imageView);
return view;
}
});
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</android.support.constraint.ConstraintLayout>
list_view_item_image.xml
<?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="wrap_content">
<ImageView
android:id="#+id/image_view"
android:adjustViewBounds="true"
android:maxHeight="200dp"
android:maxWidth="200dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
I am creating an app that has a fragment where a user can open a spinner using a button and select a food item. The food item will then be displayed under the button in a list. I have tried creating textviews and making invisible textviews and setting the text later, but none have worked. I was wondering if anyone knew of a better way of creating this that I'm not seeing.
<FrameLayout 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"
tools:context="com.lastineindustries.ingredismartv2.Kitchen">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:text="Add An Ingredient"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/add"
android:textSize="30sp"
android:layout_gravity="center" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="320dp"
android:layout_height="match_parent"
android:id="#+id/main"
android:orientation="vertical">
</LinearLayout>
<LinearLayout
android:layout_width="60dp"
android:layout_height="match_parent"
android:id="#+id/valueButton"
android:orientation="vertical">
</LinearLayout>
</LinearLayout>
</LinearLayout>
Is this what you want?
Then, read my code.
code of ChooseFoodFragment.java and its layout file:
public class ChooseFoodFragment extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_choose_food, container, false);
initViews(v);
return v;
}
private Button btn_add;
private ListView listView;
private List<String> mData = new ArrayList<>();
private ArrayAdapter<String> mAdapter;
private void initViews(View v) {
listView = v.findViewById(R.id.list);
mAdapter = new ArrayAdapter<>(getActivity(), android.R.layout.simple_list_item_1, mData);
listView.setAdapter(mAdapter);
btn_add = v.findViewById(R.id.add);
btn_add.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ChooseFoodDialog dialog = new ChooseFoodDialog();
dialog.setOnSelectedListener(new ChooseFoodDialog.OnSelectedListener() {
#Override
public void onSelected(String name) {
mData.add(name);
mAdapter.notifyDataSetChanged();// update the list of selected food
}
});
dialog.show(getFragmentManager(), "");//show the spinner items to select
}
});
}
}
fragment_choose_food.xml,just remove other views except the root view and the button, then add a ListView.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context="com.lastineindustries.ingredismartv2.Kitchen">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/add"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Add An Ingredient"
android:textSize="30sp" />
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp" />
</LinearLayout>
</FrameLayout>
the code of ChooseFoodDialog.java and its layout file.
public class ChooseFoodDialog extends DialogFragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = LayoutInflater.from(getContext()).inflate(R.layout.dialog_choose_food, container, false);
initViews(v);
return v;
}
private ListView lv;
private List<String> mData = new ArrayList<>();
private void initViews(View v) {
// prepare some temp data
for (int i = 0; i < 10; i++) {
mData.add("Ingredients_" + i);
}
lv = v.findViewById(R.id.lv_ingredients);
ArrayAdapter<String> adapter = new ArrayAdapter<>(getContext(), android.R.layout.simple_list_item_1, mData);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
String ingredient = mData.get(i);
if (mListener != null) {
mListener.onSelected(ingredient);// when the item of food is selected
}
dismiss();
}
});
}
private OnSelectedListener mListener;
public void setOnSelectedListener(OnSelectedListener listener) {
mListener = listener;
}
interface OnSelectedListener {
void onSelected(String name);
}
}
dialog_choose_food.xml,that's simple.
<?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/lv_ingredients"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
run the program, check it.
You should use ListView or RecyclerView for such tasks. Check out example from official Android documentation: link.
How can I make my images in the grid view fill the screen?
Here is my layout code:
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="10dp"
android:padding="10dp"
android:columnWidth="100dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp" >
</GridView>
I will post a picture of the screen .
But it is displaying the images small and not using the entire screen.
Here is my adapter class:
public class MenuCategoryAdapter extends BaseAdapter {
private GlobalObjects global;
private Context mContext;
// Keep all Objects in array
public ArrayList<MenuCategory> categories = new ArrayList<MenuCategory>();
// Constructor
public MenuCategoryAdapter(Context c, GlobalObjects _global) {
mContext = c;
global = _global;
JSONArray categoriesObj = getCategoriesFromServer();
if (categoriesObj != null) {
putCategoriesIntoArray(categoriesObj);
} else {
// Categories is null
Log.e("ImageAdapter", " Categories is null");
}
}
#Override
public int getCount() {
return categories.size();
}
#Override
public Object getItem(int position) {
return categories.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = categories.get(position).getImg();
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setLayoutParams(new GridView.LayoutParams(70, 70));
return imageView;
}
}
here is grid view it will fill your screen. it works for me do it. if anything happens wrong inform me immediate.
<?xml version="1.0" encoding="utf-8"?>
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/grid_view"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:gravity="center"
android:stretchMode="columnWidth" >
</GridView>
Also You need To Edit In Your java Code. Make scale from 70,70 to fill_parant please.
I Have also sample app for it. if you require it please tell me i will upload it for you.