Load HTML file on Fragment Android Studio (Java) - android-studio

i have a problem to load my HTML file on the fragment, maybe someone can help me?
My code in Home_Fragment
public class HomeFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View myView = inflater.inflate(R.layout.fragment_home, container, false);
WebView myWebView = myView.findViewById(R.id.webView_home);
myWebView.loadUrl("file:///android_asset/tampilan_awal.html");
WebSettings webSettings = myWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
return myView;
}
}
My home_fragment.xml :
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/webView_home"/>

Related

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

cannot resolve symbol ListView and ArrayAdapter

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

Spinner not opening on touch in android

I am implementing spinner but when I am touching spinner or on click on that spinner , dropdown is not opening.Spinner array has three values.and parent layout is linear layout
XML
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinnerQuestions"
android:layout_weight="1"/>
activity :-
ques = getResources().getStringArray(R.array.select_question);
ArrayAdapter <String> adapter = new ArrayAdapter<String> (this, R.layout.spinner_item, ques)
{
public View getView(int position, View convertView, ViewGroup parent) {
View v = super.getView(position, convertView, parent);
return v;
}
public View getDropDownView(int position1, View convertView1, ViewGroup parent1) {
View v =super.getDropDownView(position1, convertView1, parent1);
return v;
}
};
adapter.setDropDownViewResource(R.layout.spinner_dropdown_item);
spinnerQues.setAdapter(adapter);
spinnerQues.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
questionId = String.valueOf(position);
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
Issue Resolved
I am using ActionBar activity.
so that I am getting issue. As I changed ActionBarActivity to activity issue resolved.

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

Resources