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.
Related
This is the code of MyPageAdapter
public class MyPageAdapter extends PagerAdapter {
int[] images = {R.drawable.img1, R.drawable.img2, R.drawable.img3, R.drawable.img4, R.drawable.img5, R.drawable.img6, R.drawable.img7, R.drawable.img8, R.drawable.img9, R.drawable.img10, R.drawable.img11, R.drawable.img12, R.drawable.img13
, R.drawable.img14, R.drawable.img15, R.drawable.img16, R.drawable.img17, R.drawable.img18, R.drawable.img19, R.drawable.img20, R.drawable.img21, R.drawable.img22, R.drawable.img23, R.drawable.img24
, R.drawable.img25, R.drawable.img26, R.drawable.img27, R.drawable.img28, R.drawable.img29, R.drawable.img30};
private LayoutInflater inflater;
private Context context;
public MyPageAdapter(Context context){
this.context = context;
}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == ((LinearLayout)object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.fragment_slideshow, container, false);
EditText editText = v.findViewById(R.id.editTextNumberDecimal);
ImageView imageView = (ImageView)v.findViewById(R.id.imageView2);
imageView.setImageResource(images[position]);
int text = position+1;
editText.setText(text);
container.addView(v);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.invalidate();
}
}
AND this is the code of SlideshowFragment.
public class SlideshowFragment extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_slideshow, container, false);
ViewPager2 viewPager2 = (ViewPager2) root.findViewById(R.id.pager);
MyPageAdapter adapter = new MyPageAdapter(this);
viewPager2.setAdapter(adapter);
return root;
}
}
I would like to add a view pager to the slide fragment so that 30 photos can be viewed as a slide show.
But I can't do setAdapter in viewpager.
Really, is there anything other than a way to make and put each fragment for each of the 30 pictures?
Please let me know how to modify my code.
You are mixing Viewpager and Viewpager2 these are implemented in 2 totally different ways.
try
ViewPager viewPager = (ViewPager) root.findViewById(R.id.pager);
MyPageAdapter adapter = new MyPageAdapter(this);
viewPager.setAdapter(adapter);
and change your xml to also be a ViewPager widget.
Alternatively you could probably change your adapter to a RecyclerView.Adapter https://developer.android.com/reference/androidx/recyclerview/widget/RecyclerView.Adapter
As Viewpager2 is just a customised RecyclerView.
I have a spinner and I want to do a long click on an item on my list but the OnItemLongClick doesn't work ...
Does anybody know the new way to set a long click on a spinner item ?
Thank you
As per the android documentation there is no way to perform itemlong click on spinner item. Here is the Link
So if you want to achieve spinner with on item long click you have to make a custom adapter and set long click on view.
public class TestSpinnerAdapter extends BaseAdapter {
private String[] mArray;
public TestSpinnerAdapter(String[] array) {
mArray = array;
}
#Override
public int getCount() {
return mArray.length;
}
#Override
public Object getItem(int position) {
return mArray[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position, View convertView, final ViewGroup parent) {
LayoutInflater layoutInflater =LayoutInflater.from(parent.getContext());
convertView = layoutInflater.inflate(R.layout.adapter_spinner_item,parent,false);
((TextView)convertView.findViewById(R.id.tv_name)).setText(mArray[position]);
convertView.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Toast.makeText(parent.getContext(),"On Long Click",Toast.LENGTH_SHORT).show();
return false;
}
});
return convertView;
}
}
And set this adapter to your spinner
mTestSPN = (Spinner) findViewById(R.id.spn_test);
TestSpinnerAdapter testSpinnerAdapter = new TestSpinnerAdapter(getResources().getStringArray(R.array.array_name));
mTestSPN.setAdapter(testSpinnerAdapter);
In string.xml
<string-array name="array_name">
<item>Array Item One</item>
<item>Array Item Two</item>
<item>Array Item Three</item>
</string-array>
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;
}
}
I have this arryalist of items, each item has hour as string and string of minutes. I can split minutes to array and for each minute I want to add textview with on clicklistener.
I have implemented arrayadapter for listview. Everything works like expected but only one thing : I don't see all textviews as they are not shown properly on the end of the display.
ListView row item layout has LinearLayout and textview to show hours.
Code for listview adapter :
public class DotazAdapter extends ArrayAdapter<Hours> {
private final ArrayList<Hours> model;
private final Context context;
public DotazAdapter(Context context, ArrayList<Hours> model) {
super(context, R.layout.hours_minutes,model);
this.model=model;
this.context=context;
// TODO Auto-generated constructor stub
}
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row = null;
row = inflater.inflate(R.layout.hours_minutes, parent,false);
LinearLayout riadok = (LinearLayout)row.findViewById(R.id.horny);
TextView hodiny = (TextView)row.findViewById(R.id.hodinyBTV);
hodiny.setText(model.get(position).getHour());
String[] minuty = model.get(position).getMinutes().split(" ");
for(int i = 0; i<minuty.length;i++){
final TextView minutka = new TextView(context);
minutka.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
minutka.setText(minuty[i]+" ");
minutka.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Stlacil si riadok "+position+ " a cislo v riadku :", Toast.LENGTH_SHORT).show();
}
});
riadok.addView(minutka, );
}
return row;
}
}
Layout for listitem :
xml version=1.0 encoding=utf-8
LinearLayout xmlnsandroid=httpschemas.android.comapkresandroid
androidid=#+idhorny
androidlayout_width=match_parent
androidlayout_height=wrap_content
TextView
androidid=#+idhodinyBTV
androidlayout_width=wrap_content
androidlayout_height=wrap_content
androidlayout_marginRight=5dp
androidbackground=#F00C0C
androidtextColor=#FFFFFF
androidtextSize=18sp
LinearLayout
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; }
}