cannot resolve symbol ListView and ArrayAdapter - android-layout

I am getting cannot resolve symbol for ListView and ArrayAdapter ,trying to resolve for a long time,suggestions plz.
res/layout contains activity_main.xml,fragment.xml and layout_textview.xml
fragment.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="com.example.sunshine.BlankFragment">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_gravity="center_vertical" >
</ListView>
</FrameLayout>
layout_textview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeight"
android:id="#+id/text_view">
</TextView>
BlankFragment.java
public class BlankFragment extends Fragment {
private View rootview;
private ListView listView;
private ArrayAdapter<String> itemAdapter;
public BlankFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview= inflater.inflate(R.layout.fragment_blank, container, false);
String[] forecastArray={"Monday-sunny","Tuesday-cloudy","wednesday-sunny","Thursday-rain","friday-cloudy","saturday-sunny","sunday-cloudy"};
List<String> weekforecast=new ArrayList<String>(Arrays.asList(forecastArray));
itemAdapter = new ArrayAdapter<String>(getActivity(), android.R.layout.layout_textview,R.id.text_view, weekforecast);
listView=(ListView)rootview.findViewById(R.id.listView);
listView.setAdapter(weekforecast);
return rootview;
}
}

public class BlankFragment extends Fragment {
private View rootview;
private ListView listView;
private ArrayAdapter<String> itemAdapter;
public BlankFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View rootview= inflater.inflate(R.layout.fragment_blank, container, false);
String[] forecastArray={"Monday-sunny","Tuesday-cloudy","wednesday-sunny","Thursday-rain","friday-cloudy","saturday-sunny","sunday-cloudy"};
final List<String> weekforecast=new ArrayList<String>();
for (int i = 0; i < forecastArray.length; ++i)
{
weekforecast.add(forecastArray[i]);
}
final StableArrayAdapter adapter = new StableArrayAdapter(this,
android.R.layout.simple_list_item_1, weekforecast);
listView=(ListView)rootview.findViewById(R.id.listView);
listView.setAdapter(adapter);
return rootview;
}
Try this code

Related

My app Crashes after creating a button to a RelativeLayout

I want to create a viewpager that uses relative layout.
but before that screen pops out, i have a constraintlayout screen.
So i create a button to direct me to that viewpager, but app crashes and i dont know why...
Java:
public class TelaPrincipal extends AppCompatActivity {
private Button bt_to_treinos;
FirebaseFirestore db = FirebaseFirestore.getInstance();
String usuarioID;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tela_principal);
getSupportActionBar().hide();
IniciarComponentes();
//THIS BUTTON OVER HERE
bt_to_treinos.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(TelaPrincipal.this,SlideAdapter.class);
startActivity(intent);
}
});
}
}
i did 2 RelativeLayouts:
<?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">
<ImageView
android:id="#+id/slide_image"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginLeft="100dp"
android:layout_marginTop="80dp"
android:src="#drawable/segunda" />
<TextView
android:id="#+id/slide_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="350dp"
android:layout_marginLeft="110dp"
android:text="Segunda"
android:textColor="#color/white"
android:textSize="50sp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/slide_desc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="500dp"
android:text="#string/example"
android:textColor="#color/white"
android:textSize="15sp"
android:textStyle="bold" />
Second one:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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=".treinos"
android:background="#drawable/background"
>
<androidx.viewpager.widget.ViewPager
android:id="#+id/slideViewPager"
android:layout_width="match_parent"
android:layout_height="650dp" />
this screen should appear but crashes:
public class treinos extends AppCompatActivity {
private ViewPager mSlideViewPager;
private LinearLayout mDotLayout;
private TextView[] mDots;
private SlideAdapter sliderAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_treinos);
getSupportActionBar().hide();
mSlideViewPager = findViewById(R.id.slideViewPager);
sliderAdapter = new SlideAdapter(this);
mSlideViewPager.setAdapter(sliderAdapter);
}}
Slidepager class other java code:
public class SlideAdapter extends PagerAdapter {
Context context;
LayoutInflater layoutInflater;
public SlideAdapter(Context context){
this.context = context;
}
//Arrays
public int [] slide_images = {
R.drawable.segunda,
R.drawable.terca,
R.drawable.quarta,
R.drawable.quinta,
R.drawable.sexta,
R.drawable.sabado,
R.drawable.domingo
};
public String[] slide_headings = {
"Segunda",
"Terça",
"Quarta",
"Quinta",
"Sexta",
"Sábado",
"Domingo"
};
public String[] slide_descs = {
"Btest1",
"test 2",
"test 3",
"test 4",
"test 5",
"test 6",
"test 7 "
};
#Override
public int getCount() {
return slide_headings.length;
}
#Override
public boolean isViewFromObject(View view, Object o) {
return view == (RelativeLayout) o;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater)
context.getSystemService(context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.slide_layout, container, false);
ImageView slideImageView = (ImageView) view.findViewById(R.id.slide_image );
TextView slideHeading = (TextView) view.findViewById(R.id.slide_heading );
TextView slideDescription = (TextView) view.findViewById(R.id.slide_desc);
slideImageView.setImageResource(slide_images[position]);
slideHeading.setText(slide_headings[position]);
slideDescription.setText(slide_descs[position]);
container.addView(view);
return view;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((RelativeLayout) object);
}}
Thankyou Everyone

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; }
}

How to Show Different Layouts inside Fragments

I am working with two fragments in Android Honeycomb (Tab). In the left is a ListView and in the right is a preview of the item selected from the list. When one of the buttons is clicked, I want to show different layouts on the left. How is it possible?
Thanks in advance.
You can do this, I made the same thing with use of these links, here is my code which I am sharing with you in the hope that it will be helpful for you... You will first have to create 4 layouts. 2 of which will be for landscape mode, one for portrait mode and another for tablets. You have to create a couple more folders for layouts and their name should be like layout-xlarge and layout-xlarge-port, this way you can create fragments for both mobile devices and tablets.
MasterFragment Activity:
public class MasterFragment extends ListFragment {
Boolean isDualPane;
int position;
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayList<String> parkNames = new ArrayList<String>();
for (Park park : Resort.PARKS) {
parkNames.add(park.getName());
}
setListAdapter(new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, parkNames));
View detailFrame = getActivity().findViewById(R.id.detail);
isDualPane = detailFrame != null && detailFrame.getVisibility() == View.VISIBLE;
if (savedInstanceState != null) {
position = savedInstanceState.getInt("position", 0);
}
if (isDualPane) {
getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
showDetail(position);
}
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("position", position);
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
showDetail(position);
}
void showDetail(int position) {
this.position = position;
if (isDualPane) {
getListView().setItemChecked(position, true);
DetailFragment detailFragment = (DetailFragment) getFragmentManager()
.findFragmentById(R.id.detail);
if (detailFragment == null || detailFragment.getIndex() != position) {
detailFragment = new DetailFragment(position);
FragmentTransaction ft = getFragmentManager()
.beginTransaction();
ft.replace(R.id.detail, detailFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
} else {
Intent intent = new Intent();
intent.setClass(getActivity(), DetailActivity.class);
intent.putExtra("position", position);
startActivity(intent);
}
}
}
Second Activity - DetailFragment Activity:
public class DetailActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_act);
Bundle bundle = getIntent().getExtras();
int position = bundle.getInt("position");
System.out.println("RR : position is : " + position);
Integer[] images = { R.drawable.pic1, R.drawable.pic2, R.drawable.pic3,
R.drawable.pic4, R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8, R.drawable.pic9,
R.drawable.pic10, R.drawable.pic11, R.drawable.pic12,
R.drawable.pic13 };
final ImageView imgview = (ImageView) findViewById(R.id.imageView1);
imgview.setImageResource(images[position]);
// DetailFragment detailFragment = new DetailFragment(position);
// FragmentManager fm = getSupportFragmentManager();
// FragmentTransaction ft =fm.beginTransaction();
// ft.add(android.R.id.content, detailFragment).commit();
}
}
Now you have to create a third activity, MasterGridActivity for my images which I am using for showing in fragment in GridView.
public class MasterGridActivity extends Fragment {
Boolean isDualPane;
GridView gridView;
ListView listView;
int position;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.gridview, container, false);
gridView = (GridView) view.findViewById(R.id.gridViewImage);
gridView.setAdapter(new MyAdapter(view.getContext()));
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
View detailFrame = getActivity().findViewById(R.id.detail);
isDualPane = detailFrame != null && detailFrame.getVisibility() == View.VISIBLE;
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int pos, long arg3) {
if (!isDualPane) {
Intent intent = new Intent();
intent.setClass(getActivity(), DetailActivity.class);
intent.putExtra("position", pos);
startActivity(intent);
} else {
DetailFragment detailFragment = (DetailFragment) getFragmentManager().findFragmentById(R.id.detail);
if (detailFragment == null || detailFragment.getIndex() != pos) {
detailFragment = new DetailFragment(pos);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.detail, detailFragment);
ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
ft.commit();
}
}
}
});
super.onActivityCreated(savedInstanceState);
}
}
Now here is my image adapter - MyAdapter - for my images which extends a BaseAdapter.
public class MyAdapter extends BaseAdapter {
private Context mContext;
public MyAdapter(Context c) {
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
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(100, 100));
imageView.setImageResource(mThumbIds[position]);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(0, 0, 0, 0);
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
return imageView;
}
static Integer[] mThumbIds = { R.drawable.pic1, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4, R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8, R.drawable.pic9,
R.drawable.pic10, R.drawable.pic11, R.drawable.pic12,
R.drawable.pic13,
};
}
Now I am sharing the XML files for these fragments.
Main.xml
<?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"
android:orientation="horizontal">
<fragment
android:id="#+id/master"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="org.fragment.MasterGridActivity" />
</LinearLayout>
gridview.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridViewImage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:numColumns="auto_fit"
android:columnWidth="90dp"
android:horizontalSpacing="10dp"
android:verticalSpacing="10dp"
android:gravity="center"
android:stretchMode="columnWidth" />
</LinearLayout>
detail_fragment.xml: This XML is for showing the detail in another fragment.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="8dp" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="8dp" />
</LinearLayout>
</ScrollView>
detail_act.xml
<?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"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:src="#drawable/ic_launcher" />
</LinearLayout>
Make the same XML for landscape mode and for tablets. It's working fine for me. Hope it will helpful for you.
You need to define an event callback to the activity activity callback. That is, your left fragment must first notify the container activity that an event occurred (i.e. one of the list items were selected). The container activity will then pass this information to the right fragment, which will then update its UI accordingly.
I could explain this in more detail, but there are several tutorials on the internet that teach just that. I suggest you read through some of them, as the concept will make a lot more sense once you do.

Resources