Listview Solution - android-studio

I have created 3 edit text and a button i saved my all 3 edit text in a array list and showed it in a list :But Problem is that i want to
1. first they are showing in vertically manner i want it in horizontal manner so that if i add more items it should be separated from the previous one .
2. I want to add as many as list i want to add please provide me a hint either i should use for loop or what to get as many list as i want.
i was thinking to try for loop to get as many input to be added in listview.
Main code Oncreate MEthod for java
public class secondActivity extends AppCompatActivity {
EditText edit1,edit2,edit3;
Button button;
ListView listView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
edit1=findViewById(R.id.name);
edit2=findViewById(R.id.desigination);
edit3=findViewById(R.id.post);
listView = findViewById(R.id.list_view);
button = findViewById(R.id.list_button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<String> arryList = new ArrayList<String>();
arryList.add(edit1.getText().toString());
arryList.add(edit2.getText().toString());
arryList.add(edit3.getText().toString());
ArrayAdapter list = new ArrayAdapter(secondActivity.this,android.R.layout.simple_list_item_1,arryList);
listView.setAdapter(list);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(secondActivity.this, "hello "+ position, LENGTH_SHORT).show();
}
});
}
});
}
No error comming just its hsowing my list in vertically and i want it in a same line and want to add as many as items in list at same time its not working only i am able to add it for one time

You can use the HorizontalListView or add orientation property to the ListView or RecyclerView instead of ArrayList,

You can use this model of code to add item to listview any time :
public class MyListView extends ListActivity {
ArrayList<String> listItems = new ArrayList<String>();
ArrayAdapter<String> adapter;
int myCounter = 0;
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1,
listItems);
setListAdapter(adapter);
}
//METHOD WHICH WILL HANDLE DYNAMIC INSERTION
public void addItemsToList(View v) {
listItems.add("Clicked : " + MyCounter++);
adapter.notifyDataSetChanged();
}
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="#+id/addBtn"
android:text="Add New Item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItemsToList"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
/>
</LinearLayout>
And also you can use HorizontalListView for add item horizontally to list

Related

Android studio how to use one recycleview for more than one view?

I have a search fragment which allow users to search for both users and posts. Here is the XML:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/search_bar"
android:background="#drawable/white_rounded"
android:hint="Search for users or recipes ..."
android:drawableEnd="#drawable/ic_search"
android:maxLines="1"
android:inputType="textFilter"
android:layout_margin="10dp"
android:singleLine="true"
android:lines="1"
android:layout_marginStart="25dp"
android:layout_marginLeft="10dp"/>
<!-- android:digits="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz1234567890" -->
</androidx.appcompat.widget.Toolbar>
<com.taimoorsikander.myapplication.Widgets.B_RecycleView
android:id="#+id/recycle_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:layout_below="#+id/bar" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recycle_view2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:overScrollMode="never"
android:layout_marginBottom="#dimen/_50sdp"
android:layout_below="#+id/bar" />
and in my SearchFragment I implemented the search like this:
public class SearchFragment extends Fragment {
private B_RecycleView recyclerView;
private SearchAdapter searchAdapter;
private List<User> mUsers;
// for posts
EditText search_bar;
private RecyclerView recyclerView2;
DatabaseReference DataRef;
private List<Post> mPosts;
PostSearchAdapter postSearchAdapter;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_search, container, false);
DataRef = FirebaseDatabase.getInstance().getReference().child("posts");
recyclerView2 = view.findViewById(R.id.recycle_view2);
search_bar = view.findViewById(R.id.search_bar);
recyclerView2.setLayoutManager(new LinearLayoutManager(getContext()));
recyclerView2.setHasFixedSize(true);
mPosts = new ArrayList<>();
postSearchAdapter = new PostSearchAdapter(getContext(), mPosts);
recyclerView2.setAdapter(postSearchAdapter);
readPosts();
search_bar.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().isEmpty()) {
mPosts.clear();
recyclerView2.removeAllViews();
// recyclerView2.hideIfEmpty(recyclerView2);
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().isEmpty()) {
searchPosts(s.toString().toLowerCase());
}else
{
mPosts.clear();
recyclerView2.removeAllViews();
}
}
#Override
public void afterTextChanged(Editable s) {
if(s.toString().isEmpty()) {
mPosts.clear();
recyclerView2.removeAllViews();
}
}
});
recyclerView = view.findViewById(R.id.recycle_view);
recyclerView.setHasFixedSize(true);
recyclerView.setLayoutManager(new LinearLayoutManager(getContext()));
search_bar = view.findViewById(R.id.search_bar);
mUsers = new ArrayList<>();
searchAdapter = new SearchAdapter(getContext(), mUsers);
recyclerView.setAdapter(searchAdapter);
readUsers();
search_bar.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
if(s.toString().isEmpty()) {
mUsers.clear();
recyclerView.removeAllViews();
recyclerView.hideIfEmpty(recyclerView);
}
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (!s.toString().isEmpty()) {
searchUsers(s.toString().toLowerCase());
}else
{
mUsers.clear();
recyclerView.removeAllViews();
}
}
#Override
public void afterTextChanged(Editable s) {
if(s.toString().isEmpty()) {
mUsers.clear();
recyclerView.removeAllViews();
}
}
});
and I used two adapters since I'm using two different things in the search (post and users). it is working now but the problem is that it doesn't look neat having two separate recyclerViews. is there a way to merge them ? I want them to be in one view. is it possible?
There's a few alternative, but if you didn't want to reinvent the wheel, try to use the following approach:
Have a different item or layout (views):
tl;dr if you want to build a layout-like a Playstore homepage or AirBnb hotel list
Android has ConcatAdapter see here
Also, to have a better understanding about the multiple view adapter, see recommended article here,
tl;dr: Adapter delegate purpose was similar to ConcatAdapter to have a composite (or multiple) RecyclerView (with different layout adapter in 1 RecyclerView)
Inspired by: http://hannesdorfmann.com/android/adapter-delegates/
Since you've already created two adapters you can call them conditionally on the single RecyclerView; like if the search query is for posts call posts-adapter and if the the search query is for users then call users-adapter.

how can add GIF images in viewPager in android

I am trying to create image slider using viewPager into fragment in Bottom Navigation Bar in android.
I wanna load GIF images into viewPager but it is not working.If it is possible please let me know how it is possible
This is my xml code.
<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=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="60dp"
android:paddingTop="60dp">
<android.support.v4.view.ViewPager
android:id="#+id/mainviewPager1"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_alignParentLeft="true" />
</LinearLayout>
</FrameLayout>
This is my HomeFragment class where i am adding viewPager into fragment.
public class HomeFragment extends Fragment
{
ViewPager viewPager;
public HomeFragment()
{
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_home, container,false);
viewPager = (ViewPager) v.findViewById(R.id.mainviewPager1);
// Inflate the layout for this fragment
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(getContext());
viewPager.setAdapter(viewPagerAdapter);
}
return v;
}
This is my viewPagerAdaper.java code.
public class ViewPagerAdapter extends PagerAdapter
{
Context context;
LayoutInflater layoutInflater;
Integer[] images={R.drawable.fimage,R.drawable.simage,R.drawable.timage};
public ViewPagerAdapter(Context context) {
this.context = context;
}
#Override
public int getCount()
{
return images.length;
}
#Override
public boolean isViewFromObject(View view, Object object)
{
return view==object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view=layoutInflater.inflate(R.layout.custom_layout,null);
ImageView imageView=(ImageView)view.findViewById(R.id.customimageView2);
imageView.setImageResource(images[position]);
ViewPager vp=(ViewPager) container;
vp.addView(view,0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp=(ViewPager) container;
View v=(View) object;
vp.removeView(v);
}
}
ImageView doesn't support GIF format, if you want to deal with GIF images you can use this library https://github.com/koral--/android-gif-drawable and include pl.droidsonroids.gif.GifImageView inside your custom_layout.xml.
Replace your ImageView with a GifImageView

How add a listview inside a viewpager in android

I've been trying to get a list view that can be swipe to change the its items to show the following or preceding week details. Here is what my layout file looks like
'
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/home_pannels_pager">
<ListView
android:id="#+id/week_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</android.support.v4.view.ViewPager>
But I didn't get what i want. Could you please help me? Thanks in advance.
This is not how ViewPager works. You feed the pages to ViewPager with a PagerAdapter. Your ListView will be contained within a Fragment created by the PagerAdapter.
In the layout:
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/home_pannels_pager" />
In the FragmentActivity with this layout:
ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new PagerAdapter(getSupportFragmentManager()));
Example of simle PagerAdapter:
public class PagerAdapter extends FragmentPagerAdapter {
public FrontPageAdapter(FragmentManager Fm) {
super(Fm);
}
#Override
public Fragment getItem(int position) {
Bundle arguments = new Bundle();
arguments.putInt("position", position);
FragmentPage fragment = new FragmentPage();
fragment.setArguments(arguments);
return fragment;
}
#Override
public int getCount() {
// return count of pages
return 3;
}
}
Example of FragmentPage:
public class FragmentPage extends Fragment {
public FragmentPage() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_frontpage, container, false);
// probably cast to ViewGroup and find your ListView
Bundle arguments = getArguments();
int position = b.getInt("position");
return view;
}
}

How to add a widget to a ListView to scroll with items, but also be visible when the ListView's adapter is empty

I have a ListView and a widget. I want the widget to be always on the top of ListView, and it should be able to scroll with the items but when there is no items in adapter it should still be visible. This is how it doesn't scroll:
The layout file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/btn_togle_empty"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="toggle empty"
/>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
<ListView
android:id="#+id/list_test"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
<ViewStub
android:id="#+id/empty_layout"
android:layout="#layout/empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</FrameLayout>
</LinearLayout>
The code for the activity:
public class MyActivity extends Activity {
private MyAdapter adapter;
private ListView v;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
v = (ListView) findViewById(R.id.list_test);
View empty = findViewById(R.id.empty_layout);
v.setEmptyView(empty);
final MyAdapter adapter = new MyAdapter();
v.setAdapter(adapter);
Button btn = (Button) findViewById(R.id.btn_togle_empty);
btn.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
adapter.togleEmpty();
}
});
}
private class MyAdapter extends BaseAdapter {
boolean empty = false;
#Override
public int getCount() {
return empty ? 0 : 50;
}
#Override
public Object getItem(int i) {
return new Object();
}
#Override
public long getItemId(int i) {
return i;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
TextView v = new TextView(MyActivity.this);
v.setText("STRING");
return v;
}
public void togleEmpty() {
empty = !empty;
notifyDataSetChanged();
}
}
}
I had one more idea, add the widget as header, but it disappears when the ListView is empty. How can I achieve the result I want?
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
if (i == 0) { return custom_widget_view; }
}

Button + Spinner onItemSelectedListener doesn't work

I'm having trouble using buttons and programmatically spinners, because onItemSelectedListener doesn't work as it has to do properly. I've been googling and searching in this forum, and even there are some problems related to mine, I can't solve it, because I don't have the Spinner declared in my layout.
Here is my layout:
...<LinearLayout
android:layout_width="wrap_content"
android:layout_height="50dp"
android:orientation="horizontal"
android:background="#010D00"
android:paddingTop="5dp"
android:layout_gravity="center_vertical|center_horizontal">
<Button
android:id="#+id/magic_filter"
android:layout_height="fill_parent"
android:layout_width="80dp"
android:text="Mágico"/>
<Button
android:id="#+id/mision_time_filter"
android:layout_height="fill_parent"
android:layout_width="80dp"
android:text="Tiempo"/>
</LinearLayout>...
Then I do this:
setContentView(R.layout.gig_noloc_list_fragment);
initializeLayout(); // ...(Button) findViewById(R.id.reputation_filter); x4
initializeSpinners();//...magicSpin = new Spinner(this); x4
initializeAdapters();....
magicFil.setOnClickListener(magicListener);
and
private void initializeAdapters() {
magicAdap = new ArrayAdapter<String>(this, R.layout.gig_noloc_filter_spinner_view, getResources().getStringArray(R.array.magicItems));
magicSpin.setAdapter(magicAdap);
magicSpin.setOnItemSelectedListener(magicSListener);
}
and
private OnClickListener magicListener = new OnClickListener() {
#Override
public void onClick(View v) {
magicSpin.performClick();
}
};
and
private OnItemSelectedListener magicSListener = new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
Log.v("HELLO", "BYE");
magicFil.setText("HOLA");
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
magicFil.setText("no tira");
}
};
I don't get any of these messages or texts on the screen or log.
Anybody can tell me what's happening?
Thank you in advance.
Do you have a layout height + width assigned to your elements (spinners, etc)? If not, you may not have anything to click.

Resources