Using spinner in android fragment view and button click - android

I am a newbie programmer in android.I am trying to develop a simple paternity blood test.The logic is like this.I have three spinners and blood group A,B,AB and O will be listed into the spinner.The user have to chose blood type from A,B,AB or O for child,mother and father and then click submit button.The button will do some matching and produce a string result.I have tried several methods whichI found on internet. But still unable to use button click function.
Here is my code.Plz correct my mistake .Thanks.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/btn_paternity"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal"
android:layout_marginTop="52dp"
android:id="#+id/paternity_ans" />
<TextView
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:id="#+id/textView6"
android:text="Father"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:layout_width="100dp"
android:layout_above="#+id/childblds"
android:layout_centerHorizontal="true" />
<TextView
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:layout_marginTop="53dp"
android:id="#+id/textView5"
android:text="Child "
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:layout_width="100dp"
android:layout_marginRight="15dp"
android:layout_marginEnd="15dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/btn_paternity"
android:layout_toStartOf="#+id/btn_paternity" />
<Spinner
android:layout_width="100dp"
android:layout_height="wrap_content"
android:spinnerMode="dialog"
android:id="#+id/dadblds"
android:dropDownWidth="match_parent"
android:layout_toLeftOf="#+id/textView4"
android:layout_toStartOf="#+id/textView4"
android:layout_alignBottom="#+id/childblds"
android:layout_alignTop="#+id/childblds" />
<Spinner
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/childblds"
android:spinnerMode="dialog"
android:dropDownWidth="match_parent"
android:layout_marginTop="13dp"
android:layout_below="#+id/textView5"
android:layout_alignLeft="#+id/textView5"
android:layout_alignStart="#+id/textView5" />
<Spinner
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/momblds"
android:spinnerMode="dialog"
android:entries="#array/paternitybldtype"
android:dropDownWidth="match_parent"
android:layout_alignTop="#+id/dadblds"
android:layout_alignLeft="#+id/textView4"
android:layout_alignStart="#+id/textView4" />
<Button
android:text="Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btn_paternity"
android:layout_below="#+id/dadblds"
android:layout_centerHorizontal="true"
android:layout_marginTop="35dp" />
<TextView
android:layout_height="wrap_content"
android:id="#+id/textView4"
android:gravity="center_horizontal"
android:text="Mother"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:layout_width="100dp"
android:layout_marginLeft="9dp"
android:layout_marginStart="9dp"
android:layout_above="#+id/childblds"
android:layout_toRightOf="#+id/textView6"
android:layout_toEndOf="#+id/textView6" />
</RelativeLayout>
</LinearLayout>
Fragments code:
public class Paternitytest extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.paternitytestlo, container, false);
final Button setItem = (Button) view.findViewById(R.id.btn_paternity);
final TextView txt1 = (TextView) view.findViewById(R.id.paternity_ans);
setItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Some if else statement will be applied here by using String c, f and m
}
});
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Spinner childspinner = (Spinner) view.findViewById(R.id.childblds);
Spinner dadspinner = (Spinner) view.findViewById(R.id.dadblds);
Spinner momspinner = (Spinner) view.findViewById(R.id.momblds);
// Spinner Drop down elements
String[] categories = {"A", "B", "O", "AB",};
// Creating adapter for spinner
ArrayAdapter adapter = new ArrayAdapter(
getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, categories);
// Drop down layout style - list view with radio button
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
childspinner.setAdapter(adapter);
dadspinner.setAdapter(adapter);
momspinner.setAdapter(adapter);
// Spinner click listener
childspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String c = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
dadspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String f = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
momspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
String m = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}

I have made some changes in your code so try this.
public class Paternitytest extends Fragment {
private String childSpinnerString, momSpinnerString, dadspinnerString;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final View view = inflater.inflate(R.layout.paternitytestlo,
container, false);
final Button setItem = (Button) view.findViewById(R.id.btn_paternity);
final TextView txt1 = (TextView) view.findViewById(R.id.paternity_ans);
setItem.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
//Some if else statement will be applied here by using String c, f and m
Log.d("Blood groups- ", "Child - " + childSpinnerString + " Mom - " + momSpinnerString + " Dad - " + dadspinnerString);
}
});
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
Spinner childspinner = (Spinner) view.findViewById(R.id.childblds);
Spinner dadspinner = (Spinner) view.findViewById(R.id.dadblds);
Spinner momspinner = (Spinner) view.findViewById(R.id.momblds);
// Spinner Drop down elements
String[] categories = {"A", "B", "O", "AB",};
// Creating adapter for spinner
ArrayAdapter adapter = new ArrayAdapter(
getActivity().getApplicationContext(), android.R.layout.simple_list_item_1, categories);
// Drop down layout style - list view with radio button
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
childspinner.setAdapter(adapter);
dadspinner.setAdapter(adapter);
momspinner.setAdapter(adapter);
// Spinner click listener
childspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
childSpinnerString = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
dadspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
dadspinnerString = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
momspinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
momSpinnerString = parent.getItemAtPosition(position).toString();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}

Try onClickListener for your button:
btn_paternity.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String mom = momblds.getSelectedItem().toString();
}
});

Related

Multiple Custom Spinner always sets values to one spninner

I have two Spinner in my layout. First one to select vehicle, second one for choose color from the shorted list of that particular vehicle. eg.(selected Bajaj, second need to show list of color of that vehicle already saved)
Here I did getting all datas from database for vehicle and for color. Both values are in my hand. But if I select vehicle type in Spinner1, it was showing the color of that vehicles in same spinner instead of, showing in Spinner2. First I had same (Spinner getset class, adapter class,custom layout). Now I created all three as different. Even now also getting the same problem.
How to find, where I am doing wrong. Below is the code I am using.
public void setListData()// gets vehicles value from DB
{
List<VehicleGetSetter> contacts = vdb.getVehicleListToCustomer(vendor_id);
for (VehicleGetSetter cn : contacts) {
final BikeSpinner sched = new BikeSpinner();
sched.setBikeName("Select Bike");
CustomListBikeName.add(sched);
}}
private void setBikeAdapterValues() {// set Vehicle value in Spinner1
Resources res = getResources();
adapter = new BikeSpinnerAdapter(NewCustomer.this, R.layout.bike_spinner, CustomListBikeName,res);
mySpinner.setAdapter(adapter);
mySpinner.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
bike_no_from_adapter = ((TextView) v.findViewById(R.id.textbike_numbr)).getText().toString();
what_bike = ((TextView) v.findViewById(R.id.textbike)).getText().toString();
String OutputMsg = "Selected : \n\n"+bike_no_from_adapter;
if(position!=0){
setListBikeColor(bike_no_from_adapter);
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
//Toast.makeText(getApplicationContext(), "You Must Select Vehicle", Toast.LENGTH_LONG).show();
}
});
}
public void setListBikeColor(String bike_no_from_adapter){// get color of selected vehicles from DB
List<VehicleGetSetter> contacts1 = vdb.getVehicleColorListToCustomer(vendor_id,bike_no_from_adapter);
for (VehicleGetSetter cn1 : contacts1) {
final BikeSpinn sched1 = new BikeSpinn();
sched1.setBikeNumber(cn1.getColor());
CustomListBikeColor.add(sched1);
setBikeAdapterValuesColor();
}
}
private void setBikeAdapterValuesColor() {// trying to set value in second spinner, but setting in same vehicle spinner.
Resources res2 = getResources();
coloradapter = new BikeSpinnerColor(NewCustomer.this, R.layout.bike_spinner_color, CustomListBikeColor,res2);
cust_bike_color.setAdapter(coloradapter);
cust_bike_color.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
// Get selected row data to show on screen
bike_no_from_adapter = ((TextView) v.findViewById(R.id.textbike_numbr)).getText().toString();
what_bike = ((TextView) v.findViewById(R.id.textbike)).getText().toString();
#Override
public void onNothingSelected(AdapterView<?> parentView) {
//Toast.makeText(getApplicationContext(), "You Must Select Vehicle", Toast.LENGTH_LONG).show();
}
});
}
MainLayout which contain Spinner
<?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:background="#drawable/bg_blue"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/bike"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:textSize="22dp"
android:textColor="#000000"
android:text="Bike Details" />
<Spinner
android:id="#+id/cust_bike"
android:layout_width="120dp"
android:layout_height="40dp"
android:paddingBottom="1dp"
android:paddingLeft="5dp" />
<Spinner
android:id="#+id/cust_bike_color"
android:layout_width="120dp"
android:layout_height="40dp"
android:paddingBottom="1dp"
android:paddingLeft="5dp" />
<Spinner
android:id="#+id/cust_bike_number"
android:layout_width="120dp"
android:layout_height="40dp"
android:paddingBottom="1dp"
android:paddingLeft="5dp" />
</LinearLayout>
</LinearLayout>
Custom layout for spinners(Similar for both spinner(named differently))
<?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" >
<TextView
android:id="#+id/textbike"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:layout_marginRight="5dp"
android:textColor="#000000" />
<TextView
android:id="#+id/textbike_numbr"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="18dp"
android:textColor="#000000" />
</LinearLayout>
Custom ArrayAdapterClass(Having Two similar class for two Spinners)
public class BikeSpinnerAdapter extends ArrayAdapter<String>{
private Activity activity;
private ArrayList data;
public Resources res;
BikeSpinner tempValues=null;
LayoutInflater inflater;
public BikeSpinnerAdapter(NewCustomer activitySpinner,
int textViewResourceId,
ArrayList objects,
Resources resLocal
)
{
super(activitySpinner, textViewResourceId, objects);
activity = activitySpinner;
data = objects;
res = resLocal;
inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public View getDropDownView(int position, View convertView,ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
return getCustomView(position, convertView, parent);
}
public View getCustomView(int position, View convertView, ViewGroup parent) {
View row = inflater.inflate(R.layout.bike_spinner, parent, false);
tempValues = null;
tempValues = (BikeSpinner) data.get(position);
TextView label = (TextView)row.findViewById(R.id.textbike_numbr);
TextView sub = (TextView)row.findViewById(R.id.textbike);
label.setText(tempValues.getBikeName());
sub.setText(tempValues.getBikeNumber());
return row;
}
}
Like this I achieved with multiple Spinner related with one another
Seperate class to set Dropdown List Adapters
public void setListBikeName(String vr_id)
{ vendor_id=vr_id;
List<VehicleGetSetter> contacts = ((NewCustomer)context).vdb.getVehicleListToCustomer(vendor_id);
for (VehicleGetSetter cn : contacts) {
final BikeSpinner sched = new BikeSpinner();
if(cn.getModelYear().equals(""))
sched.setBikeName("Select Bike");
else
sched.setBikeName(cn.getModelYear());
((NewCustomer)context).CustomListBikeName.add(sched); /** Take Model Object in ArrayList */
}
}
public void setListBikeColor(String name){
((NewCustomer)context).CustomListBikeColor.clear();
List<VehicleGetSetter> contacts = ((NewCustomer)context).vdb.getVehicleColorListToCustomer(vendor_id,name);
for (VehicleGetSetter cn : contacts) {
final BikeSpinner sched = new BikeSpinner();
sched.setBikeName(cn.getColor());
((NewCustomer)context).CustomListBikeColor.add(sched); /** Take Model Object in ArrayList */
((NewCustomer)context).colorAdapterIntermediater();
}
}
public void setListBikeNumber(String name,String color){
((NewCustomer)context).CustomListBikeNumber.clear();
List<VehicleGetSetter> contacts = ((NewCustomer)context).vdb.getVehicleNumberListToCustomer(vendor_id,name,color);
for (VehicleGetSetter cn : contacts) {
final BikeSpinner sched = new BikeSpinner();
sched.setBikeName(cn.getNumber());
((NewCustomer)context).CustomListBikeNumber.add(sched); /** Take Model Object in ArrayList */
((NewCustomer)context).numberAdapterIntermediater();
}
}
public void setBikeAdapterValues(ArrayList<BikeSpinner> CustomListBikeName,BikeSpinnerAdapter adapter,
Spinner mySpinner,final String str,Resources res) {
res = context.getResources();
Spinner temp=mySpinner;
adapter = new BikeSpinnerAdapter((NewCustomer) context, R.layout.bike_spinner, CustomListBikeName,res);
temp.setAdapter(adapter);
temp.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View v, int position, long id) {
if(str.equals("name")){
bike_name_from_adapter = ((TextView) v.findViewById(R.id.textbike_numbr)).getText().toString();
setListBikeColor(bike_name_from_adapter);
}
else if(str.equals("color")){
bike_color_from_adapter = ((TextView) v.findViewById(R.id.textbike_numbr)).getText().toString();
setListBikeNumber(bike_name_from_adapter, bike_color_from_adapter);
}
else if(str.equals("number")){
bike_no_from_adapter = ((TextView) v.findViewById(R.id.textbike_numbr)).getText().toString();
if(bike_no_from_adapter.length()>=10){
((NewCustomer)context).last_layout.setVisibility(View.VISIBLE);
}
else
((NewCustomer)context).last_layout.setVisibility(View.GONE);
((NewCustomer)context).passBikeValueToCustomer(bike_name_from_adapter,bike_color_from_adapter,bike_no_from_adapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView) {
}
});
}
}
In the Main Class:
bikeadapterobj.setListBikeName(_id);
bikeadapterobj.setBikeAdapterValues(CustomListBikeName,adapter,mySpinner,"name",res);
public void colorAdapterIntermediater(){
bikeadapterobj.setBikeAdapterValues(CustomListBikeColor,coloradapter,cust_bike_color,"color",res);
}
public void numberAdapterIntermediater(){
bikeadapterobj.setBikeAdapterValues(CustomListBikeNumber,numberadapter,cust_bike_number,"number",res);
}
public void passBikeValueToCustomer(String name,String colr,String bkno){
bike_name=name;
bike_color=colr;
bike_reg_number=bkno;
Log.d("Bike Details : ", bike_name+" "+bike_color+" "+bike_reg_number);
}

Intent is not working in fragment

I have the below code :
public class HomeFragment extends Fragment {
private List<RowItem> rowItems;
private static Integer[] images = { R.drawable.red, R.drawable.spidy,
R.drawable.prisoners, R.drawable.red, R.drawable.spidy };
public HomeFragment() {
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container,
false);
ListView lv = (ListView) rootView.findViewById(R.id.myList);
rowItems = new ArrayList<RowItem>();
String[] titles = { "Movie1", "Movie2", "Movie3", "Movie4", "Movie5" };
String[] descriptions = { "First Movie", "Second movie", "Third Movie",
"Fourth Movie", "Fifth Movie" };
// Populate the List
for (int i = 0; i < titles.length; i++) {
RowItem item = new RowItem(images[i], titles[i], descriptions[i]);
rowItems.add(item);
}
// Set the adapter on the ListView
LazyAdapter adapter = new LazyAdapter(getActivity()
.getApplicationContext(), R.layout.list_row, rowItems);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(getActivity(), "test", Toast.LENGTH_LONG).show();
Intent myIntent = new Intent(getActivity(), News.class);
getActivity().startActivity(myIntent);
// startActivity(in);
}
});
lv.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
// TODO Auto-generated method stub
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
return rootView;
}
}
I have written Toast ans startIntent on item click, but it is not working. Neither toast is display nor new activity is starting. I have also tried onItemSelection method but it is not working. How can i used this in this Fragment class?
list_row.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#drawable/card_greenborder"
android:focusable="false"
android:focusableInTouchMode="false"
android:orientation="vertical"
android:padding="2dp" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="Dog Tag"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#040404"
android:textStyle="bold"
android:typeface="sans" />
<ImageView
android:id="#+id/list_image"
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_margin="2dp"
android:layout_weight="0.04"
android:scaleType="fitXY"
android:src="#drawable/dhoom" />
<TextView
android:id="#+id/description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:text="Create an NFC Pet Tag"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/secondary_text_dark"
android:textStyle="bold" />
<Button
android:id="#+id/btnregister"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Register" />
</LinearLayout>
First don't need to be implement OnItemSelectedListener for ListView. Just remove it.
Second make
android:focusable = "false"
android:focusableInTouchMode = "false"
for your UIs in custom inflated views in listview

onItemClick and Toast don't work in a listfragment

i tried to do a simple Toast into my onitemclickmethod but nothing it's happening and no error just nothing happen when i press an item of the list
My listfragment :
public class F1_fr extends ListFragment {
View rootview;
TextView textView1;
ArrayAdapter<String> aa;
ArrayList<String> arrayList = new ArrayList<String>();
SQLiteDatabase db;
ListView listView;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
rootview=inflater.inflate(R.layout.f1_lay,container,false);
textView1=(TextView)rootview.findViewById(R.id.textView1);
db = getActivity().openOrCreateDatabase("testDB2", Context.MODE_PRIVATE, null);
db.execSQL("CREATE TABLE IF NOT EXISTS test2(mac VARCHAR,mdp VARCHAR,obj VARCHAR);");
aa = new ArrayAdapter<String>(getActivity(),
android.R.layout.simple_list_item_1, arrayList);
setListAdapter(aa);
((ListView) rootview.findViewById(android.R.id.list)).setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getActivity(),"lol",Toast.LENGTH_LONG).show();
}
});
Cursor cursor = db.rawQuery("SELECT * FROM test2", null);
// Toast.makeText(myContext, ""+cursor.getCount(), Toast.LENGTH_LONG).show();
if(cursor.moveToFirst())
{
do {
arrayList.add(cursor.getString(2));
} while (cursor.moveToNext());
}
rootview.findViewById(R.id.semi_transparent).
setOnClickListener(new View.OnClickListener() {
#Override
public void onClick (View v){
Intent intent = new Intent(getActivity(), ajout.class);
startActivityForResult(intent, 2);
}
}
);
return rootview;
}
and my layout :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#id/android:list"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true" />
<TextView
android:id="#android:id/empty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:textSize="30sp"
android:text="" />
<com.getbase.floatingactionbutton.AddFloatingActionButton
android:id="#+id/semi_transparent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
fab:fab_plusIconColor="#color/primaryColorDark"
fab:fab_colorNormal="#color/primaryColor"
fab:fab_colorPressed="#color/AccentColor"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
So i'am just asking your help because i have absolutly no error nothing in the logcat just nothing is happening
You've implemented ListFragment so directly #Override onListItemClick(...)
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
// do something
Toast.makeText(getActivity(), "Lol", Toast.LENGTH_SHORT).show();
}
Take a look at this
I am not sure but try to put below line in RelativeLayout.
android:descendantFocusability="blocksDescendants"
Hope it will work.!!!
you have to override onListItemClick() of listFragment you dont call listview.setOnItemClick();

How to create alertdialog in spinner

I want to create alertDialog in spinner selected item, and add an EditText inside of alertDialog.
Thanks
sh = (Spinner) view.findViewById(R.id.shield);
ArrayAdapter<CharSequence> adaptera = ArrayAdapter.createFromResource(getActivity(),
R.array.shield, android.R.layout.simple_spinner_item);
adaptera.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
sh.setAdapter(adaptera);
sh.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
sa = (String) sh.getSelectedItem();
AlertDialog.Builder alertDialogBuildera = new AlertDialog.Builder(getActivity());
alertDialogBuildera.setTitle("Your Title");
alertDialogBuildera
.setMessage("Click yes to exit!")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
double sl = Double.valueOf(tc.getText().toString());
getActivity().finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alertDialoga = alertDialogBuildera.create();
if (sa.trim().equals("Lead")) {
alertDialoga.show();
} else
if (sa.trim().equals("Steel")) {
alertDialoga.show();
}
}
});
it's doesn't work.
if I selected item, the alertdialog automaticaly shown.
Please check the below link.This will gives you detailed explanation on how to customize spinner. hope this will solves your problem.
http://mrbool.com/how-to-customize-spinner-in-android/28286
In that example you should replace imageview with edittext
Please use the below code i replaced imageview with edittext its working fine
main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="Spinner Customization"
android:textSize="30px" />
<Spinner
android:id="#+id/spinner_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100px"
android:drawSelectorOnTop="true" />
custom_spinner.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="3px" >
<EditText
android:id="#+id/left_content"
android:layout_width="wrap_content"
android:layout_height="80px" />
<TextView
android:id="#+id/text_main_seen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5px"
android:layout_marginTop="2px"
android:layout_toRightOf="#+id/left_content"
android:padding="3px"
android:text="JMD Group"
android:textColor="#0022ee"
android:textSize="22px"
android:textStyle="bold" />
<TextView
android:id="#+id/sub_text_seen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_main_seen"
android:layout_marginLeft="5dip"
android:layout_toRightOf="#+id/left_content"
android:padding="2px"
android:text="beyond the expectations..."
android:textColor="#777777" />
</RelativeLayout>
MainActivity
public class MainActivity extends Activity {
String[] spinnerValues = { "Blur", "NFS", "Burnout", "GTA IV", "Racing", };
String[] spinnerSubs = { "Ultimate Game", "Need for Speed",
"Ulimate Racing", "Rockstar Games", "Thunder Bolt" };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
mySpinner.setAdapter(new MyAdapter(this, R.layout.custom_spinner,
spinnerValues));
}
public class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
super(ctx, txtViewResourceId, objects);
}
#Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomView(position, cnvtView, prnt);
}
#Override
public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View mySpinner = inflater.inflate(R.layout.custom_spinner, parent,
false);
TextView main_text = (TextView) mySpinner
.findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
TextView subSpinner = (TextView) mySpinner
.findViewById(R.id.sub_text_seen);
subSpinner.setText(spinnerSubs[position]);
EditText left_content = (EditText) mySpinner
.findViewById(R.id.left_content);
left_content.setHint("This is EditText:"+(position+1));
return mySpinner;
}
}
}
Thanks

How do calculate multiple spinner value with edit text?

Im new in android programing, I want make calculating with multiple spinner that have value string and edit text. and proccess with button click and show on text view. please healp me to fix so i can learn next subject.
THx
My activity .java code:
public class MainActivity extends Activity {
private EditText jumlah;
String[] spinnerValues = { "Bakso", "Es Buah" };
String[] spinnerSubs = {"10000", "8000" };
int total_images[] = { R.drawable.bakso, R.drawable.es_buah };
private Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner mySpinner = (Spinner) findViewById(R.id.spinner_show);
mySpinner.setAdapter(new MyAdapter(this, R.layout.menu_resto,
spinnerValues));
jumlah = (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
initButton();
}
private void initButton() {
button1.setOnClickListener(new OnClickListener() {
// this one performs an action when our button is clicked. it performs whatever is below
#Override
public void onClick(View v) {
// String strA = i want call the spinersubs value that chsoed. how ?
String strB = jumlah.getText().toString();
Double dblAnswer = doCalc(strA, strB);
TextView lblAnswer = (TextView) findViewById(R.id.lblAnswer);
// the disadvantage is that we can't do anything to it outside of this curly
// in general it's wasteful to use fields when you can suffice with local variable
String answer = String.valueOf(dblAnswer);
// we get our answer and turn it to a string.
lblAnswer.setText(answer);
// finally we set our result to the textView.
}
});
}
public double doCalc(String a, String b) {
double dblA = Double.parseDouble(a);
double dblB = Double.parseDouble(b);
return dblA * dblB;
}
class MyAdapter extends ArrayAdapter<String> {
public MyAdapter(Context ctx, int txtViewResourceId, String[] objects) {
super(ctx, txtViewResourceId, objects);
}
#Override
public View getDropDownView(int position, View cnvtView, ViewGroup prnt) {
return getCustomView(position, cnvtView, prnt);
}
#Override
public View getView(int pos, View cnvtView, ViewGroup prnt) {
return getCustomView(pos, cnvtView, prnt);
}
public View getCustomView(int position, View convertView,
ViewGroup parent) {
LayoutInflater inflater = getLayoutInflater();
View mySpinner = inflater.inflate(R.layout.menu_resto, parent,
false);
TextView main_text = (TextView) mySpinner
.findViewById(R.id.text_main_seen);
main_text.setText(spinnerValues[position]);
TextView subSpinner = (TextView) mySpinner
.findViewById(R.id.sub_text_seen);
subSpinner.setText(spinnerSubs[position]);
ImageView left_icon = (ImageView) mySpinner
.findViewById(R.id.left_pic);
left_icon.setImageResource(total_images[position]);
return mySpinner;
}
Here my XML
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="X - Resto Menu"
android:textSize="30px" />
<Spinner
android:id="#+id/spinner_show"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="100px"
android:drawSelectorOnTop="true" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner_show"
android:layout_centerHorizontal="true"
android:layout_marginTop="46dp"
android:ems="10" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner_show"
android:layout_centerHorizontal="true"
android:layout_marginTop="23dp"
android:text="Jumlah Pesanan" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:onClick="#string/hitung"
android:text="#string/pesan" />
<TextView
android:id="#+id/lblAnswer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:text="" />
You need to implement CustomOnItemSelectedListener for Spinner like
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
Toast.makeText(parent.getContext(),
"OnItemSelectedListener : " + parent.getItemAtPosition(pos).toString(),
Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
and then set this Listener to your Spinner like
mySpinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
and in Button click you'll get a Spinner selected value like
btnSubmit.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MyAndroidAppActivity.this,
"OnClickListener : " +
"\nSpinner : "+ String.valueOf(mySpinner.getSelectedItem())
,Toast.LENGTH_SHORT).show();
}
});
Go to this for Tutorial

Categories

Resources