The code is working fine in Main activity. How to extend this spinner class to all the activities in my project?
The items of spinner are only visible if I add spinner entries in XML
Moreover, There is no call to onItemSelected. I have followed every tutorial possible But couldn't figure out how to do it.
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:entries="#array/MenuItems" />
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:entries="#array/MenuItems" />
No item is added programmatically.
public class MainMenu extends Activity implements OnItemSelectedListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner)findViewById(R.id.spinner1);
ArrayAdapter<String> spinnerAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, getResources().getStringArray(R.array.MenuItems));
spinner.setAdapter(spinnerAdapter);
}
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
String item = adapterView.getItemAtPosition(i).toString();
// Showing selected spinner item
Toast.makeText(adapterView.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
}
My activity_main is as follows
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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="com.example.supreetkaur.pocbeacon.MainActivity">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:elevation="4dp"
android:background="?attr/colorPrimary" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:entries="#array/MenuItems" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
</android.support.design.widget.CoordinatorLayout>
I am a beginner in Android Development. Would really appreciate any help.
Related
I want to add longClickListener for my every button in my fragment.For this, i wrote this code, i call it in OnCreate().My project is Tabbed Activity.
I have implement OnLongClickListener in my main activity.
public class MainActivity extends AppCompatActivity implements View.OnLongClickListener
And i also have onLongClick method in MainActivity.java
#Override
public boolean onLongClick(View v) {.......}
Here is my method in onCreate() ;
public void InflateFragments(){
final RelativeLayout lay = (RelativeLayout) findViewById(R.id.Page1Lay);
View viev = getLayoutInflater().inflate(R.layout.fragment_page1, lay, false);
for(int i = 0; i < IDs.length; i++){
Button button = (Button) viev.findViewById(IDs[i]);
if(button != null)
button.setOnLongClickListener(this);
Log.i("OK","Listener set on button : " + IDs[i]);
}
}
IDs, is a int array that include my buttons id's.This code can reach every button in my fragment and set OnLongClickListener for every button, because there is no exception or any error.I can se Log.i()'s message on console.
But when i run this code on virtual device, there is nothing happend when i clicked long.I do not know why.
My buttons are in a layout and my every button has ;
android:longClickable="true"
My main_activity.xml ;
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.aslan.rte3.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay"
android:background="#android:color/black">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabGravity="center"
app:tabMode="scrollable" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:background="#E0DFDE">
</android.support.v4.view.ViewPager>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#E0DFDE">
<com.google.android.gms.ads.AdView
android:id="#+id/adView"
android:layout_width="match_parent"
android:layout_height="match_parent"
ads:adSize="SMART_BANNER"
ads:adUnitId="..."
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true" />
</FrameLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
instead of:
View viev = getLayoutInflater().inflate(R.layout.fragment_page1, lay, false);
try this:
View viev = LayoutInflater.from(this).inflate(R.layout.fragment_page1, lay, false);
Try this once;
#Override
public boolean onLongClick(View v) {
Log.e("ID", ""+v.getId());
return true;
}
And, then check if the view's Id here is same as one the buttons.
I am trying a list of items using RecyclerView , everything works fine but the last item is not being displayed! The length of the list is 2 and onBindView is called twice and the when I used Log to print the items it aslo prints the last item. So the list is fine but the RecyclerView is not displaying it.
This is my code RecyclerView Adapter:
public class ModuleList extends RecyclerView.Adapter<ModuleList.ViewHolder> {
private List<Module> moduleList;
public ModuleList(List<Module> moduleList){
this.moduleList = moduleList;
}
public ModuleList(){
this.moduleList = new ArrayList<>();
}
public List<Module> getModuleList() {
return moduleList;
}
public void setModuleList(List<Module> moduleList) {
this.moduleList = moduleList;
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.list_modules,parent,false);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
Module module = moduleList.get(position);
// function is called twice and the all items are logged works fine
Log.d("ALL",module.getModuleName());
Log.d("ALL", String.valueOf(module.getNumberOfLevels()));
holder.moduleName.setText(String.valueOf(module.getModuleName()));
holder.numberOfLevels.setText(String.valueOf(module.getNumberOfLevels()));
}
#Override
public int getItemCount() {
//size is 2
return moduleList.size();
}
public class ViewHolder extends RecyclerView.ViewHolder{
public TextView moduleName;
public TextView numberOfLevels;
public ViewHolder(View itemView) {
super(itemView);
moduleName = (TextView) itemView.findViewById(R.id.list_module_name);
numberOfLevels = (TextView) itemView.findViewById(R.id.list_no_of_levels);
}
}
}
This is my xml where recyclerview is:
<?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">
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/all_modules_list"
android:scrollbars="vertical"
/>
</LinearLayout>
This is my xml where RecyclerView Fragment is displayed:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_screen"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:fitsSystemWindows="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/main_screen_container"
android:orientation="vertical"
>
<android.support.v7.widget.Toolbar
android:id="#+id/main_screen_toolbar"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
/>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/fragment_container">
</FrameLayout>
</LinearLayout>
<ListView
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="#+id/navigation_items"
android:layout_gravity="start"
android:background="#android:color/white"
>
</ListView>
</android.support.v4.widget.DrawerLayout>
Edit:list_module.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="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_module_name"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_no_of_levels"
/>
</LinearLayout>
Hey can you try the solution below?
Change this in the list_module.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="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/list_module_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/list_no_of_levels"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I have replaced match_parent to wrap_content to parent layout.
I have a problem with Recycleview in linearLayout. In the below code When I click on chat section(Linear Layout) Toast is not getting visible when I set LayoutManager to RecycleView. Please help me the toast will be visible when I click on linear layout.
Activity code:
public class PracticeActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practice);
setLayoutViews();
}
private void setLayoutViews(){
// setting up messages list view
RecyclerView mMessagesView = (RecyclerView) findViewById(R.id.messages);
// if i uncomment below line Toast will not be visible.
//mMessagesView.setLayoutManager(new LinearLayoutManager(this));
(findViewById(R.id.chat_section)).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(PracticeActivity.this, " Chat section was clicked", Toast.LENGTH_SHORT).show();
}
});
}
activity_practice.xml
<RelativeLayout
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"
android:orientation="vertical"
android:id="#+id/activity_main">
<LinearLayout
android:id="#+id/chat_section"
android:layout_width="match_parent"
android:layout_height="320dp"
android:background="#color/chat_back_ground">
<android.support.v7.widget.RecyclerView
android:id="#+id/messages"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:scrollbars="vertical"
android:scrollbarStyle="outsideOverlay"/>
</LinearLayout>
<TextView
android:layout_below="#id/chat_section"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Just text"/>
</RelativeLayout>
You can follow this tutorial. The process followed is to create a separate itemtouchlistener class from which the selection is identified
http://sapandiwakar.in/recycler-view-item-click-handler/
I've a listview in an activity and I want to open a fragment when an item is clicked. For that I used FrameLayout along with ListView but it causes overlapping of fragment class over the activity. Is there any way to remove this solution?
Activity's Listview Item Click code:
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
DetailsView details = new DetailsView();
getSupportFragmentManager().beginTransaction().addToBackStack(null).add(R.id.frame, details).commit();
}
});
activity layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.baoyz.swipemenulistview.SwipeMenuListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</RelativeLayout>
Try to use FragmentManager.Replace() instead of .add()
I've implemented a swipe refresh layout onto one of my List Views however the view is not sliding down when I swipe and the icon is not visible. Even though it seems it doesn't work, it does actually fire off the OnRefresh listener.
Also I think its worth mentioning that I'm using the ListView on a fragment so i'm initializing the listener in the fragment activity which is displayed below.
Swipe Refresh XML
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_gravity="center_horizontal"
android:dividerHeight="1sp"
android:translationZ="5dp"
android:background="#color/background_gray"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:clickable="true"
/>
</android.support.v4.widget.SwipeRefreshLayout>
Fragment Activity
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
final SwipeRefreshLayout mSwipeRefreshLayout = (SwipeRefreshLayout) getActivity().findViewById(R.id.swipe_refresh_layout);
ListView lView = (ListView) getActivity().findViewById(R.id.listView);
mSwipeRefreshLayout.setColorScheme(android.R.color.holo_blue_bright,
android.R.color.holo_green_light,
android.R.color.holo_orange_light,
android.R.color.holo_red_light);
mSwipeRefreshLayout.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
#Override
public void onRefresh() {
mSwipeRefreshLayout.setRefreshing(true);
((MainActivity) getActivity()).refresh();
new Handler().postDelayed(new Runnable() {
#Override public void run() {
mSwipeRefreshLayout.setRefreshing(false);
}
}, 5000);
}
});
lView.setOnScrollListener(new AbsListView.OnScrollListener() {
#Override
public void onScrollStateChanged(AbsListView absListView, int i) {
}
#Override
public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
if (firstVisibleItem == 0)
mSwipeRefreshLayout.setEnabled(true);
else
mSwipeRefreshLayout.setEnabled(false);
}
});
}
I solved it, it appears that this is a current bug with the SwipeRefreshLayout. It will only work when Listview is wrapped inside a FrameLayout.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SwipeRefreshLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/swipe_refresh_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_gravity="center_horizontal"
android:dividerHeight="1sp"
android:translationZ="5dp"
android:background="#color/background_gray"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:clickable="true"
/>
</FrameLayout>
</android.support.v4.widget.SwipeRefreshLayout>
Simply set the property [android:clickable="true"] for the Immediate clild layout of SwipeRefreshLayout which occupies complete screen area.i.e. whose width and height is match_parent.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/srlayout_Container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true">
<RelativeLayout
android:id="#+id/rl_MainContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true">
<!-- Placeholder for Other View or ViewGroup as Clild -->
</RelativeLayout>
</android.support.v4.widget.SwipeRefreshLayout>
</RelativeLayout>