How to create a Vertical Dropdown with Menu with Gridview? - android

I want to know how to develop a custom dropdown icons Menu with GridView options from Menu.
This is my code for custom down icons
<?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:background="#c2ec97"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="viralandroid.com.androidxmluserinterfacetutorial.MainActivity">
<Button
android:id="#+id/dropdown_custom_icon_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#5bace6"
android:drawableRight="#android:drawable/arrow_down_float"
android:onClick="verticalDropDownIconMenu"
android:padding="16dp"
android:text="DropDown\t"
android:textColor="#eee" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_custom_icon_menu"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Android Custom Vertical Dropdown Menu IconsAndroid Custom Vertical. Break line after Icon in Menu Item
Android.
Dropdown Menu IconsAndroid Custom Vertical Dropdown. popup menu with icon in android example.
Android Custom Vertical Dropdown Menu Icons. Android Custom Vertical Dropdown Menu Icons" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:text="Android Custom Vertical Dropdown Menu Icons" />
</LinearLayout>
<LinearLayout
android:id="#+id/vertical_dropdown_icon_menu_items"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/dropdown_custom_icon_menu"
android:background="#333"
android:orientation="vertical"
android:padding="3dp"
android:visibility="invisible">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_send"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_attach"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_mail"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_refresh"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_attach"
android:text="Android Vertical Custom DropDown Menu" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginTop="2dp"
android:background="#drawable/onclick_press_color"
android:onClick="menuItemClick"
android:paddingBottom="5dp"
android:paddingLeft="26dp"
android:paddingRight="26dp"
android:paddingTop="5dp"
android:src="#drawable/ic_action_mail"
android:text="Android Vertical Custom DropDown Menu" />
</LinearLayout>
</RelativeLayout>
This is what i have :
This is what i want :

You need to use the spinner and that will lead to use an adapter and a couple of layouts.
First in your activity_main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Spinner
android:id="#+id/sSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp" />
</RelativeLayout>
then create a new layout under /res/layout folder and just name it anything like custom_spinner_layout.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="horizontal">
<ImageView
android:id="#+id/myImageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:padding="10dp"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="10dp"
android:text="test"
android:textColor="#000" />
</LinearLayout>
then in you MainActivity.java you need to do your code:
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener{
String[] carNames={"Chevy","Dodge","Mazda","Honda","BMW","Toyota"};
int icons[] = {R.drawable.Chevy, R.drawable.Dodge, R.drawable.Mazda, R.drawable.Honda, R.drawable.BMW, R.drawable.Toyota};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spin = (Spinner) findViewById(R.id.sSpinner);
spin.setOnItemSelectedListener(this);
CustomAdapter customAdapter=new CustomAdapter(getApplicationContext(),icons,carNames);
spin.setAdapter(customAdapter);
}
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int position,long id) {
//your code goes here///
Toast.makeText(getApplicationContext(), carNames[position], Toast.LENGTH_LONG).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// something happens here
}
}
You will need to create a new java class file and name it CustomAdapter.java and thats gonna be extended fro BaseAdapter:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends BaseAdapter {
Context context;
int icons[];
String[] carNames;
LayoutInflater inflter;
public CustomAdapter(Context applicationContext, int[] icons, String[] carNames) {
this.context = applicationContext;
this.icons = icons;
this.carNames = carNames;
inflter = (LayoutInflater.from(applicationContext));
}
#Override
public int getCount() {
return icons.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = inflter.inflate(R.layout.custom_spinner_layout, null);
ImageView icon = (ImageView) view.findViewById(R.id.imageView);
TextView names = (TextView) view.findViewById(R.id.textView);
icon.setImageResource(icons[i]);
names.setText(carNames[i]);
return view;
}
}
you can get the working code in :
https://github.com/edgebasis/spinnerExample

Related

White space between each item in recycler view

I'm using Firebase Recycler in my app and I implemented the code in my fragment. The data loads fine but when I scroll to end of list the items gets white spaces between each and that spaces clears only when changed from one activity to other.
My XML Code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="55dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/home_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary">
<TextView
android:id="#+id/map_view"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:drawableStart="#drawable/ic_location_on_black_24dp"
android:drawablePadding="10dp"
android:ellipsize="end"
android:fontFamily="#font/roboto_medium"
android:gravity="center"
android:maxLines="1"
android:padding="5dp"
android:text="#string/text1" />
</androidx.appcompat.widget.Toolbar>
<androidx.cardview.widget.CardView
app:cardCornerRadius="10dp"
android:layout_margin="14dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.smarteist.autoimageslider.SliderView
android:id="#+id/imageSlider"
android:layout_width="match_parent"
android:layout_height="200dp"
app:sliderAnimationDuration="5000"
app:sliderAutoCycleEnabled="true"
app:sliderIndicatorAnimationDuration="5000"
app:sliderIndicatorGravity="center_horizontal|bottom"
app:sliderIndicatorMargin="15dp"
app:sliderIndicatorOrientation="horizontal"
app:sliderIndicatorPadding="3dp"
app:sliderIndicatorRadius="1dp"
app:sliderIndicatorSelectedColor="#5A5A5A"
app:sliderIndicatorUnselectedColor="#FFF"
app:sliderScrollTimeInSec="3"
app:sliderStartAutoCycle="true" />
</androidx.cardview.widget.CardView>
<RadioGroup
android:id="#+id/layout_change_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginLeft="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:gravity="center_horizontal"
android:orientation="horizontal">
<RadioButton
android:id="#+id/btn_for_list1"
android:layout_width="150dp"
android:gravity="center"
android:layout_height="30dp"
android:background="#drawable/radio_flat_selector"
android:button="#android:color/transparent"
android:checked="true"
android:text="Home Services"
android:textColor="#color/radio_flat_text_selector" />
<RadioButton
android:id="#+id/btn_for_list2"
android:gravity="center"
android:layout_width="150dp"
android:layout_height="30dp"
android:background="#drawable/radio_flat_selector"
android:button="#android:color/transparent"
android:text="Home Improvements"
android:textColor="#color/radio_flat_text_selector" />
</RadioGroup>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/main_list1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="15dp"/>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/main_list2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="invisible"
android:layout_marginStart="10dp"
android:layout_marginEnd="10dp"
android:layout_marginTop="15dp"/>
</RelativeLayout>
</LinearLayout>
My Adapter Code:
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;
import com.bumptech.glide.Glide;
import com.coderedinnovations.allioservices.R;
import com.firebase.ui.database.FirebaseRecyclerAdapter;
import com.firebase.ui.database.FirebaseRecyclerOptions;
public class MainList1_Adapter extends FirebaseRecyclerAdapter<MainList1, MainList1_Adapter.MainList1_Holder> {
public MainList1_Adapter(#NonNull FirebaseRecyclerOptions<MainList1> options) {
super(options);
}
#Override
protected void onBindViewHolder(#NonNull MainList1_Holder holder, int position, #NonNull MainList1 model) {
holder.heading_view.setText(model.getTitle());
holder.desc_view.setText(model.getDescription());
holder.offer_view.setText(model.getOffer());
Glide.with(holder.image_view.getContext())
.load(model.getImageLink())
.into(holder.image_view);
}
#NonNull
#Override
public MainList1_Holder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext())
.inflate(R.layout.main_item_card, parent, false);
return new MainList1_Holder(view);
}
class MainList1_Holder extends RecyclerView.ViewHolder{
TextView heading_view, desc_view, offer_view;
ImageView image_view;
public MainList1_Holder(#NonNull View itemView) {
super(itemView);
heading_view = itemView.findViewById(R.id.item_heading);
desc_view = itemView.findViewById(R.id.item_description);
offer_view = itemView.findViewById(R.id.item_offer);
image_view = itemView.findViewById(R.id.item_image);
}
}
}
Watch this video that will clear explain my problem.
Video Link
Thanks for everyone who tried to answer my question. I just made a small mistake which gave the white spaces between each item.
In the Parent layout of item. I added
android:layout_height:"match_parent"
instead of wrap_content and that made the error.

ScrollView not working with GridLayout in android studio

Hi I want to create a responsive scrollable grids. As I search, I came across some codes. I tweaked this code to allow scrolling of the grids. The code creates a 2 column grids and 4 rows with cardViews. The cardViews shrinks to allow more cards into the viewport rather than scrolling. Am open to any ideas and alternative methods to make this work.Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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=".MainActivity"
android:scrollbars="vertical"
android:fillViewport="true">
<LinearLayout
android:orientation="vertical"
android:weightSum="10"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_weight="2"
android:layout_width="match_parent"
android:layout_height="0dp">
<TextView
android:id="#+id/textGrid"
android:text="Games"
android:textSize="34sp"
android:textColor="#android:color/black"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
<android.support.v7.widget.GridLayout
android:id="#+id/gridView"
android:layout_weight="8"
app:columnCount="2"
app:rowCount="2"
android:padding="14dp"
app:alignmentMode="alignMargins"
app:columnOrderPreserved="false"
android:layout_width="match_parent"
android:layout_height="0dp"
>
<android.support.v7.widget.CardView
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginBottom="16dp"
app:cardCornerRadius="8dp"
app:cardElevation="8dp"
app:layout_columnWeight="1"
app:layout_rowWeight="1">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_margin="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/sampleimage" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Grid One
android:textAlignment="center"
android:textColor="#000"
android:textSize="18sp"
android:textStyle="bold" />
</LinearLayout>
</android.support.v7.widget.CardView>
</android.support.v7.widget.GridLayout>
</LinearLayout>
</ScrollView>
I have a pattern like that in one of my apps. Tickets get added to the 3-wide grid and you can scroll the grid. Here is the code I use.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/your_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:baselineAligned="false"
android:orientation="vertical">
<ScrollView
android:id="#+id/your_scrollview"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="2dip"
android:layout_gravity="center_horizontal">
<LinearLayout
android:id="#+id/your_grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:visibility="visible">
<GridView
android:id="#+id/gridViewTickets"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:numColumns="3"
android:padding="8dp"
android:horizontalSpacing="8dp"
android:verticalSpacing="8dp"
android:minHeight="250dip"
android:gravity="center_horizontal"
android:layout_gravity="center_horizontal">
</GridView>
</LinearLayout>
</ScrollView
</LinearLayout>
After a lot of research I came across RecyclerView. It was a little confusing but it worked fine. I had to use two(2) layouts (activity_main.xml and row_data.xml) with MainActivity.java.
activity_main.xml
<?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="MainActivity">
<GridView
android:id="#+id/gridview"
android:numColumns="2"
android:verticalSpacing="1dp"
android:background="#e5e5e5"
android:horizontalSpacing="1dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</GridView>
</RelativeLayout>
row_data.xml
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
android:id="#+id/gridviewdata"
android:layout_margin="12dp"
app:cardCornerRadius="12dp"
app:cardElevation="6dp"
android:layout_rowWeight="1"
android:layout_columnWeight="1"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:layout_centerInParent="true"
>
<LinearLayout
android:orientation="vertical"
android:padding="16dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/images"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="#drawable/apple" />
<TextView
android:id="#+id/fruits"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Apple something"
android:textStyle="normal|italic"
android:textSize="25dp" />
</LinearLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
MainActivity.java
package com.example.gridrecycler;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
GridView gridView;
String[] fruitNames = {"Apple","Orange","strawberry","Melon","Kiwi","Banana"};
int[] fruitImages = {R.drawable.apple,R.drawable.oranges,R.drawable.strawberry,R.drawable.watermelon,R.drawable.kiwi,R.drawable.banana};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//finding listview
gridView = findViewById(R.id.gridview);
CustomAdapter customAdapter = new CustomAdapter();
gridView.setAdapter(customAdapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// Toast.makeText(getApplicationContext(),fruitNames[i],Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),GridItemActivity.class);
intent.putExtra("name",fruitNames[i]);
intent.putExtra("image",fruitImages[i]);
startActivity(intent);
}
});
}
private class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
return fruitImages.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1 = getLayoutInflater().inflate(R.layout.row_data,null);
//getting view in row_data
TextView name = view1.findViewById(R.id.fruits);
ImageView image = view1.findViewById(R.id.images);
name.setText(fruitNames[i]);
image.setImageResource(fruitImages[i]);
return view1;
}
}
}
I watched diff tuts to come up with this. Am now doubting whether it comforms to best practices. Thanks to you all for your suggestions.

Android spinner does not scroll, why?

I have a spinner in my app where you can select categories and sub categories.
It was working perfect in the past but since i did alot of modifications in the app, the spinner does not scroll anymore, not just one spinner, but all spinners in this layout (Drawer menu).
Here's the XML for the right menu drawer that contains the spinner:
<?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"
xmlns:rsb="http://schemas.android.com/apk/res-auto"
android:id="#+id/rightadvancedsearch"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Grey"
android:gravity="center"
tools:context="com.chno.v1.Home">
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginBottom="50dp"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="16dp">
<RelativeLayout
android:id="#+id/AllCat"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="false"
android:layout_alignParentStart="false"
android:paddingBottom="16dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/query"
android:layout_width="match_parent"
android:layout_height="52dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="20dp"
android:layout_weight="1"
android:backgroundTint="#color/Blue"
android:drawableLeft="#android:drawable/ic_menu_search"
android:ems="10"
android:hint="Search"
android:inputType="textPersonName"
android:paddingLeft="5dp"
android:textColor="#color/Black"
android:textColorHint="#color/GreyDark" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="6dp"
android:text="#string/Category"
android:textColor="#color/Blue"/>
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:minHeight="40dp"
android:textColor="#color/Blue" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/city"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/AllCat"
android:paddingBottom="16dp"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal">
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingBottom="6dp"
android:text="#string/Location"
android:textColor="#color/Blue" />
<TextView
android:id="#+id/textView28"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/SelectCity" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal">
<!--
<CheckBox
android:id="#+id/nearby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Nearby" />-->
<CheckBox
android:id="#+id/checkNearby"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/Nearby" />
<TextView
android:id="#+id/currentray"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:text="#string/defaultRay"
android:textAlignment="textEnd"
android:visibility="gone" />
</LinearLayout>
<com.yahoo.mobile.client.android.util.rangeseekbar.RangeSeekBar
android:id="#+id/rangeSeekbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:visibility="gone"
rsb:absoluteMaxValue="100"
rsb:absoluteMinValue="1"
rsb:singleThumb="true" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/includeLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/city"
android:orientation="vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp">
</LinearLayout>
</RelativeLayout>
</ScrollView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:background="#color/BgLight">
<RelativeLayout
android:id="#+id/reset"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/GreyLight"
android:padding="10dp"
android:gravity="center">
<TextView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/Black"
android:layout_marginRight="12dp"
android:text="RĂ©-initialiser"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/Orange"
android:padding="10dp"
android:gravity="center">
<TextView
android:id="#+id/imageView3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textColor="#color/White"
android:textStyle="bold"
android:text="Recherche"/>
</RelativeLayout>
</LinearLayout>
This is the spinner adapter:
package com.chno.v1;
import android.app.Activity;
import android.content.Context;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
/**
* List view
*/
public class SpinnerAdapter extends ArrayAdapter<ItemDate> {
private int groupid;
private Activity context;
private ArrayList<ItemDate> list;
private LayoutInflater inflater;
public SpinnerAdapter(Activity context, int groupid, int id, ArrayList<ItemDate> list) {
super(context,id, list);
this.list = list;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.groupid = groupid;
}
public View getView(int position, View convertView, ViewGroup parent) {
View itemView = inflater.inflate(groupid,parent,false);
ImageView imageView = (ImageView)itemView.findViewById(R.id.img);
ItemDate item = list.get(position);
if(item != null) {
if(item.getType() != null) {
if (item.getType().equals("sub_category")) {
imageView.setVisibility(View.GONE);
} else {
imageView.setImageResource(item.getImageId());
}
} else {
imageView.setImageResource(item.getImageId());
}
} else {
imageView.setImageResource(item.getImageId());
}
imageView.setImageResource(item.getImageId());
TextView textView = (TextView)itemView.findViewById(R.id.txt);
textView.setText(item.getText());
return itemView;
}
public View getDropDownView(int position, View convertView, ViewGroup
parent) {
return getView(position,convertView,parent);
}
}
The thing is i can select items from the spinner but i cannot scroll it down for more items.
Here's the code that populates the spinner:
JSONObject result = config.getCategories();
Iterator<String> iter = result.keys();
while (iter.hasNext()) {
String key = iter.next();
try {
JSONObject cat = result.getJSONObject(key);
String category = cat.getString("n");
int drawable = helper.getCategoryDrawable(Integer.parseInt(key));
Log.e("" + category, "" + key);
list.add(new ItemDate("category", category, Integer.parseInt(key), drawable));
JSONObject subs = cat.getJSONObject("l");
Iterator<String> it2 = subs.keys();
while (it2.hasNext()) {
String sub_key = it2.next();
JSONObject sub_category = subs.getJSONObject(sub_key);
String name = sub_category.getString("n");
Log.e("FFROM LIST", "" + name);
list.add(new ItemDate("sub_category", name, Integer.parseInt(sub_key), Integer.parseInt(key), R.drawable.tool_icon_filter));
}
} catch (JSONException e) {
Log.e("JSONException", e.getMessage());
e.printStackTrace();
}
}
Spinner sp=(Spinner)findViewById(R.id.spinner1);
SpinnerAdapter adapter=new SpinnerAdapter((Activity) context, R.layout.sub_select_item, R.id.txt, list);
sp.setAdapter(adapter);
sp.setFocusable(true);
sp.setFocusableInTouchMode(true);
How can i solve this? Thanks.

How to pass data from bottom sheet to a fragment in android?

I am trying to pass values from a bottom sheet which contains some TextViews to a fragment.
My fragment contains an EditText field and a Floating action button.When the user clicks on the floating action button, the bottom sheet shows up which has a number of TextViews, when the user clicks on any of the textViews on the bottom sheet, the value or the string of that TextView should be displayed in the editText field of the fragment and the bottom sheet should be dismissed.
I have tried implementing the onClickListener inside the setOnShowListener method but it doesn't seem to work.
Here is my code:
Fragment_TextPropert1_EditText.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"
tools:context="com.example.sumitroy.TextProperty1_EditText"
>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="15dp"
android:padding="15dp"
android:layout_below="#+id/view"
android:layout_marginTop="10dp"
app:cardUseCompatPadding="true"
android:id="#+id/view2">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:hint="Enter Your Comments Here.."
android:id="#+id/userText"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</android.support.v7.widget.CardView>
<ImageButton
android:layout_width="65dp"
android:layout_height="65dp"
android:id="#+id/example_Ads"
android:background="#drawable/oval"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="20dp"
android:layout_marginRight="15dp"
android:src="#drawable/double_plus"
/>
</RelativeLayout>
TextProperty1_bottomsheet.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="15dp"
android:padding="15dp"
android:layout_marginTop="15dp"
app:cardUseCompatPadding="true"
android:id="#+id/view1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Comment1"
android:id="#+id/example_Ad1"
android:padding="10dp"
android:textStyle="bold"
android:textSize="14sp"
android:layout_below="#+id/view"
android:layout_alignParentStart="true" />
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="15dp"
android:padding="15dp"
android:layout_marginTop="10dp"
app:cardUseCompatPadding="true"
android:layout_below="#+id/view1"
android:id="#+id/view2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Comment 2"
android:id="#+id/example_Ad2"
android:padding="10dp"
android:textStyle="bold"
android:textSize="14sp"
android:layout_below="#+id/view"
/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="15dp"
android:padding="15dp"
android:layout_marginTop="15dp"
app:cardUseCompatPadding="true"
android:id="#+id/view3"
android:layout_below="#+id/view2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Comment 3 "
android:id="#+id/example_Ad3"
android:padding="10dp"
android:textStyle="bold"
android:textSize="14sp"
android:layout_below="#+id/view"
/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="15dp"
android:padding="15dp"
android:layout_marginTop="15dp"
app:cardUseCompatPadding="true"
android:id="#+id/view4"
android:layout_below="#+id/view3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Comment 4 "
android:id="#+id/example_Ad4"
android:padding="10dp"
android:textStyle="bold"
android:textSize="14sp"
android:layout_below="#+id/view"
/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:cardCornerRadius="15dp"
android:padding="15dp"
android:layout_marginTop="15dp"
app:cardUseCompatPadding="true"
android:id="#+id/view5"
android:layout_below="#+id/view4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Comment 5 "
android:id="#+id/example_Ad5"
android:padding="10dp"
android:textStyle="bold"
android:textSize="14sp"
android:layout_below="#+id/view"
/>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>
TextProperty1_EditText.java
import android.app.Activity;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomSheetBehavior;
import android.support.design.widget.BottomSheetDialog;
import android.support.v4.app.Fragment;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
/**
* A simple {#link Fragment} subclass.
*/
public class TextProperty1_EditText extends Fragment {
View bottomSheetView;
EditText editText1;
TextView t1;
BottomSheetDialog bottomSheetDialog;
BottomSheetBehavior bottomSheetBehavior;
ImageButton floatButton;
RelativeLayout backgroundLayout;
public TextProperty1_EditText() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
final View rootview= inflater.inflate(R.layout.fragment_text_property1__edit_text, container, false);
editText1=(EditText) rootview.findViewById(R.id.userText);
floatButton=(ImageButton)rootview.findViewById(R.id.example_Ads);
floatButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Toast.makeText(v.getContext(),"Floating Button Works",Toast.LENGTH_SHORT).show();
bottomSheetView=getActivity().getLayoutInflater().inflate(R.layout.textproperty1_bottomsheet,null);
bottomSheetDialog=new BottomSheetDialog(rootview.getContext());
bottomSheetDialog.setContentView(bottomSheetView);
bottomSheetBehavior=BottomSheetBehavior.from((View) bottomSheetView.getParent());
bottomSheetDialog.show();
bottomSheetDialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialog) {
t1=(TextView)bottomSheetView.findViewById(R.id.example_Ad1);
t1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String take1=t1.getText().toString();
//Toast.makeText(bottomSheetView.getContext(),"Floating Button Works",Toast.LENGTH_SHORT).show();
editText1.setText(take1);
bottomSheetDialog.dismiss();
}
});
}
});
bottomSheetDialog.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialog) {
}
});
bottomSheetDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
//Toast.makeText(bottomSheetView.getContext(),"Floating Button Works",Toast.LENGTH_SHORT).show();
return false;
}
});
}
});
return rootview;
}
}
As a simple solution you may use LocalBroadcastManager.
public static final String SOME_INTENT_FILTER_NAME = "SOME_INTENT_FILTER_NAME";
In your fragment:
private BroadcastReceiver someBroadcastReceiver = new BroadcastReceiver() {
#Override
public void onReceive(Context context, Intent intent) {
//TODO extract extras from intent
}
};
#Override
public void onResume() {
super.onResume();
LocalBroadcastManager.getInstance(getContext()).registerReceiver(someBroadcastReceiver,
new IntentFilter(SOME_INTENT_FILTER_NAME));
}
#Override
public void onPause() {
LocalBroadcastManager.getInstance(getContext()).unregisterReceiver(someBroadcastReceiver);
super.onPause();
}
In your bottomsheet:
Intent someIntent = new Intent(SOME_INTENT_FILTER_NAME);
//TODO put extras to your intent
LocalBroadcastManager.getInstance(context).sendBroadcast(someIntent);

code adds one item to listview and then stops

I have the following code that is a dynamic listview where you type in the textbox and click the add button and its added to the listview below. Listview is a custom listview with 2 textviews. Somehow the code adds the first item and then does not add the rest. The arraylist gets the item, i call adapter.notifyDataSetChanged(); and yet still the listview does not get updated. What am i doing wrong?
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Map;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ManageComplaintsActivity extends DashboardActivity {
EditText txtAddComplaint;
ListView lvComplaintsList;
Button btnAddComplaint;
private SimpleAdapter adapter;
private int count = 1;
private ArrayList<Map<String, String>> list = new ArrayList<Map<String, String>>();;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_manage_complaints);
setTitleFromActivityLabel (R.id.title_text);
txtAddComplaint= (EditText) findViewById(R.id.txtAddComplaint);
lvComplaintsList= (ListView) findViewById(R.id.lvComplaintsList);
btnAddComplaint = (Button)findViewById(R.id.btnAddComplaint);
String[] from = { "complaint", "complaintid" };
int[] to = { R.id.lblC, R.id.lblCID };
adapter = new SimpleAdapter (this, list, R.layout.activity_manage_complaints_row, from, to);
lvComplaintsList.setAdapter(adapter);
btnAddComplaint.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
HashMap<String, String> item = new HashMap<String, String>();
item.put("complaint", txtAddComplaint.getText().toString());
item.put("complaintid", Integer.toString(count));
list.add(item);
adapter.notifyDataSetChanged();
count++;
txtAddComplaint.setText("");
Toast.makeText(getApplicationContext(),Integer.toString(list.size()), Toast.LENGTH_SHORT).show();
}
});
lvComplaintsList.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),
((TextView)view.findViewById(R.id.lblC)).getText(), Toast.LENGTH_SHORT).show();
}
});
}
}
This is the 2 xml files
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="10dip"
android:shrinkColumns="1"
android:stretchColumns="1" >
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/txtAddComplaint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3" />
<Button
android:id="#+id/btnAddComplaint"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Add" />
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/txtComplaintLabel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="List of Complaints : "
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingBottom="20dp"/>
</TableRow>
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ListView
android:id="#+id/lvComplaintsList"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_span="2" />
</TableRow>
</TableLayout>
</ScrollView>
</LinearLayout>
This is the listviewrow xml
<?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" >
<TextView
android:id="#+id/lblC"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
/>
<TextView
android:id="#+id/lblCID"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
/>
</RelativeLayout>
As you can see in documentation to SimppleAdapter it is
An easy adapter to map static data to views defined in an XML file.
I believe that you can find any tricky way to add data dynamically. But I'm not sure that you have to do it like this. Try to use ArrayAdapter instead. By default it binds T.toString() values to single TextView, but you can override getView(int, View, ViewGroup) to return the type of view you want.
I changed the main xml file as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/LinearLayout02"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/txtAddComplaint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
</EditText>
<Button
android:id="#+id/btnAddComplaint"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add to Listview" >
</Button>
</LinearLayout>
<ListView
android:id="#+id/lvComplaintsList"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
I had used a table layout and the listview was in one of the rows.
Hope this helps someone .

Categories

Resources