spinner not displaying when clicked - java

hi currently im encountering a problem where the spinner is not showing up when clicked. when i run theres not any error so i dont know whats wrong . sorry im still new to android and java pls help....
public class SpinnerInfo extends MainProduct {
Spinner spinners;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
spinners = (Spinner) findViewById(R.id.spinner);
final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.type_arrays, R.layout.support_simple_spinner_dropdown_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_item);
spinners.setAdapter(adapter);
spinners.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
String [] dataArray = getResources().getStringArray(R.array.type_arrays);
String type = dataArray[position];}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}}
my xml
<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:background="#android:color/darker_gray"
android:orientation="vertical"
android:elevation="1dp"
android:weightSum="1">
<TextView
android:text="Product List"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:textSize="24sp"
android:textStyle="normal|italic"
android:textAlignment="center"
android:fontFamily="casual"
android:textColor="#android:color/background_dark"
android:layout_marginTop="10dp"/>
<TextView
android:text="Select Type:"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:id="#+id/textView2"
android:textAlignment="textStart"
android:textStyle="normal|bold" />
<Spinner
android:layout_width="126dp"
android:layout_height="wrap_content"
android:id="#+id/spinner"
/>
<ListView
android:id="#+id/listview_product"
android:layout_width="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:divider="#android:color/darker_gray"
android:dividerHeight="8dp"
android:background="#android:color/white"
android:layout_height="match_parent">
</ListView>

final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.array.type_arrays, R.layout.support_simple_spinner_dropdown_item);
to
final ArrayAdapter adapter = ArrayAdapter.createFromResource(this, R.layout.support_simple_spinner_dropdown_item, R.array.type_arrays);

By the way you are accessing to the string resources when you can do this parent.getItemAtPosition(position)

Related

How to set text to value of listview items after time intervals

I am developing an android app where a user will input text to an EditText, then it will be added to a ListView.
But once a button with onClick value "start" is clicked, it will display each item of the ListView in a TextView with id displayText, in the order of each item and it will loop(Repeat) once it is exhausted.
Here is my Code
Math.java
public class Math extends AppCompatActivity {
private ArrayList<String> items;
private ArrayAdapter<String> itemsAdapter;
private ListView lvItems;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_math);
lvItems = (ListView) findViewById(R.id.lvItems);
items = new ArrayList<String>();
itemsAdapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, items);
lvItems.setAdapter(itemsAdapter);
}
public void AddItem(View v) {
EditText etNewItem = (EditText) findViewById(R.id.etNewItem);
String itemText = etNewItem.getText().toString();
itemsAdapter.add(itemText);
etNewItem.setText("");
}
public void start(View v){
RelativeLayout lc = (RelativeLayout)findViewById(R.id.contentContainer);
lc.setVisibility(View.GONE);
RelativeLayout tc = (RelativeLayout)findViewById(R.id.tvContainer);
tc.setVisibility(View.VISIBLE);
}
}
activity_math.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=".Math">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/tvContainer"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:textSize="30sp"
android:id="#+id/displayText"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/contentContainer">
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/lvItems"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/btnAddItem" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/etNewItem"
android:layout_alignTop="#+id/btnAddItem"
android:hint="Enter a new item"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_toLeftOf="#+id/btnAddItem"
android:layout_toStartOf="#+id/btnAddItem"
android:layout_alignParentBottom="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add Item"
android:id="#+id/btnAddItem"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:onClick="AddItem"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#id/btnAddItem"
android:onClick="start"/>
</RelativeLayout>
Okay, i think the issue is that you haven't given the TextView any thing to display. you have to convert the arrayList to a String array and use setText to display the Value.
add this part to the "start" method
String ls =System.getProperty("line.separator");// i used this to seperate the array items
String joint = TextUtils.join(ls, lvitems);
result.setText(joint);
That should work

Use a Spinner to change a TextView Android

I'm trying to use a spinner selection to change a TextView. Where I think my error is, is that "onItemSelected" "is never used". I'm very new to android/java so i'm having a hard time figuring out why this is happening.
public class activity_game extends AppCompatActivity {
public String myString1 = "Counter will increase every 3 seconds";
public TextView myCounterText;
private Spinner mySpinner;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
Spinner dropdown = (Spinner)findViewById(R.id.spinner);
String[] items = new String[]{"Select your difficulty!", "Easy", "Medium", "Hard"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
}
public void onItemSelected(AdapterView<?> parent, View arg1, int pos, long arg3) {
TextView myCounterText = (TextView)findViewById(R.id.myCounter);
Spinner mySpinner = (Spinner)findViewById(R.id.spinner);
if (mySpinner.getSelectedItem().equals("Easy")){
myCounterText.setText("myString1");
}
}
public void toActivityPlay (View view) {
Intent toActivityPlay = new Intent(this, activity_play.class);
startActivity(toActivityPlay);
}
}
Thanks in advance.
Edit: Here is my XML (with unnecessary stuff edited out so that I can post without having "mostly code".
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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"
android:background="#color/background"
tools:context="com.example.newpc.fizzbuzz.activity_game">
<TextView
android:id="#+id/counterText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="#string/counter_text"
android:textAlignment="center"
android:textColor="#color/text_shadow"
android:textStyle="bold"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.503"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.229"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Button
android:id="#+id/startButton"
android:layout_width="239dp"
android:layout_height="73dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#color/FizzBuzz_yellow"
android:text="#string/start_game"
android:textAllCaps="false"
android:textColor="#fff"
android:textSize="36sp"
android:textStyle="bold"
android:shadowColor="#color/text_shadow"
android:shadowDx="2"
android:shadowDy="2"
android:shadowRadius="3"
android:onClick="toActivityPlay"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.863"
app:layout_constraintHorizontal_bias="0.531"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp" />
<Spinner
android:id="#+id/spinner"
android:layout_width="240dp"
android:layout_height="28dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:background="#ffffff"
android:dropDownWidth="match_parent"
android:entries="#+id/difficulty"
android:textAlignment="center"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.137" />
</android.support.constraint.ConstraintLayout>
try this use setOnItemSelectedListener of spinner
Spinner dropdown = (Spinner)findViewById(R.id.spinner);
String[] items = new String[]{"Select your difficulty!", "Easy", "Medium", "Hard"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
dropdown.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
String text=spin.getSelectedItem().toString();
if (text.equals("Easy")){
myCounterText.setText("myString1");
}
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});

How to design a list of spinner?

I want to have a spinner which will contain a list of items.
The design I want is as follow:
I tried to put background for the spinner so now it only shows a background. I want white layout inside and with separator.
I tried to create a layout for the item and apply to the spinner but it gives error :
Process: com.kiranaapp, PID: 16697
java.lang.IllegalStateException: ArrayAdapter requires the resource ID to be a TextView
My design now looks like this:
And onclick of spinner I get layout like this:
But I want the list to be shown onClick of TextInputLayout.
Here is Xml file:
<?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"
android:background="#drawable/bg"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.kiranacustomerapp.Activities.SearchActivity"
tools:showIn="#layout/activity_search">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:paddingTop="05dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/bg">
<Spinner
android:id="#+id/items_spinner"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#ffffff"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp" />
</LinearLayout>
<android.support.design.widget.TextInputEditText
android:layout_width="240dp"
android:layout_height="45dp"
android:id="#+id/editTextItemName"
android:layout_gravity="center_horizontal"
android:hint="#string/item_name"
android:textSize="15sp"
android:padding="10dp" >
</android.support.design.widget.TextInputEditText>
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_unit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="05dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<android.support.design.widget.TextInputEditText
android:layout_width="240dp"
android:layout_height="45dp"
android:id="#+id/editTextItemUnit"
android:layout_gravity="center_horizontal"
android:hint="#string/unit"
android:textSize="15sp"
android:padding="10dp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TextInputLayout
android:id="#+id/input_layout_item_quantity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:paddingTop="05dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp">
<android.support.design.widget.TextInputEditText
android:layout_width="240dp"
android:layout_height="45dp"
android:id="#+id/editTextItemQuantity"
android:layout_gravity="center_horizontal"
android:hint="#string/quantity"
android:textSize="14sp"
android:padding="10dp" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button
android:layout_width="100dp"
android:layout_height="30dp"
android:text="Select"
style="?android:attr/borderlessButtonStyle"
android:id="#+id/buttonSelect"
android:layout_gravity="center_horizontal"
android:layout_marginTop="100dp"
android:layout_marginBottom="50dp"
android:background="#drawable/btn_hlf_blue"
android:textColor="#android:color/white"
android:textSize="12sp" />
</LinearLayout>
</RelativeLayout>
Activity:
public class SearchActivity extends AppCompatActivity implements AdapterView.OnItemSelectedListener {
private TextInputEditText edt_Item_Name,edt_Item_Unit,edt_Item_quantity;
private TextInputLayout textInput_Item_name,textInput_Item_Unit,textInput_Item_quantity;
private Spinner spinner;
private Button btnSelect;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_search);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationIcon(R.drawable.ic_back);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
finish();
}
});
setUpUI();
}
public void setUpUI(){
edt_Item_Name = (TextInputEditText) findViewById(R.id.editTextItemName);
edt_Item_quantity = (TextInputEditText)findViewById(R.id.editTextItemQuantity);
edt_Item_Unit = (TextInputEditText)findViewById(R.id.editTextItemUnit);
textInput_Item_name = (TextInputLayout)findViewById(R.id.input_layout_item_name);
textInput_Item_quantity = (TextInputLayout)findViewById(R.id.input_layout_item_quantity);
textInput_Item_Unit = (TextInputLayout)findViewById(R.id.input_layout_item_unit);
spinner = (Spinner)findViewById(R.id.items_spinner);
btnSelect = (Button)findViewById(R.id.buttonSelect);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.items_array, R.layout.order_item_layout);
// Specify the layout to use when the list of choices appears
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// Apply the adapter to the spinner
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(this);
textInput_Item_name.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
spinner.setVisibility(View.VISIBLE);
edt_Item_Unit.setVisibility(View.GONE);
edt_Item_quantity.setVisibility(View.GONE);
btnSelect.setVisibility(View.GONE);
textInput_Item_name.setBackgroundResource(R.drawable.purple_bg);
}
});
}
public void onItemSelected(AdapterView<?> parent, View view,
int pos, long id) {
// An item was selected. You can retrieve the selected item using
// parent.getItemAtPosition(pos)
}
public void onNothingSelected(AdapterView<?> parent) {
// Another interface callback
}
}
How can I achieve this design? Can anyone help with this please? Thank you..
Use ListView or RecyclerView and put a spinner in a layout that will be use in your viewholder.class associated with getView method in adapter class, after-that set your data using ArrayList/Hashmap with the help of constructor of Adapter class.
Follow this
package com.example.spinner;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;
import android.widget.AdapterView.OnItemSelectedListener;
class AndroidSpinnerExampleActivity extends Activity implements OnItemSelectedListener{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Spinner element
Spinner spinner = (Spinner) findViewById(R.id.spinner);
// Spinner click listener
spinner.setOnItemSelectedListener(this);
// Spinner Drop down elements
List<String> categories = new ArrayList<String>();
categories.add("Automobile");
categories.add("Business Services");
categories.add("Computers");
categories.add("Education");
categories.add("Personal");
categories.add("Travel");
// Creating adapter for spinner
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, categories);
// Drop down layout style - list view with radio button
dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
// attaching data adapter to spinner
spinner.setAdapter(dataAdapter);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
// On selecting a spinner item
String item = parent.getItemAtPosition(position).toString();
// Showing selected spinner item
Toast.makeText(parent.getContext(), "Selected: " + item, Toast.LENGTH_LONG).show();
}
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
Use this layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dip"
android:text="Category:"
android:layout_marginBottom="5dp"/>
<Spinner
android:id="#+id/spinner"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:prompt="#string/spinner_title"/>
</LinearLayout>
Draw your layout somthing like this:
<RelativeLayout
android:id="#+id/spinner_layout"
android:layout_width="match_parent"
android:layout_height="#dimen/dp_30"
android:layout_margin="#dimen/dp_20"
android:background="#color/fragment_bg"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_city"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="#dimen/dp_20"
android:hint="#string/select_city"
android:singleLine="tru
android:textSize="#dimen/sp_15"/>
<Spinner
android:id="#+id/spinner_city"
android:layout_width="match_parent"
android:layout_height="#dimen/dp_30"
android:layout_centerVertical="true"
android:entries="#array/StateName"
android:gravity="center"
android:padding="#dimen/sp_15"
app:binding="#{location.locations}"/>
</RelativeLayout>
and use your textinput layout instead of textview
Don't use Edittext use Textview to set clicked item text. "if you use edittext, then you have to tap two times to get call in onClickListener method because on first time of click it set to get focus and open soft keyboard"

using setOnItemClickListener and new intent not working

I have a grid view with images in it. Once one is clicked it should go to PlayVideoActivity but onItemClick never get called.
I followed in the debugger and tried clicking the image but it never fired onItemClick.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_videos);
gridView = (GridView) findViewById(R.id.gridView);
customGridAdapter = new GridViewAdapter(this, R.layout.row_grid,
getData());
gridView.setAdapter(customGridAdapter);
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int i, long l) {
Toast.makeText(videoChannelActivity.this, i + "#Selected",
Toast.LENGTH_SHORT).show();
Intent myIntent = new Intent(videoChannelActivity.this, PlayVideoActivity.class);
startActivity(myIntent);
}
});
}
row_grid.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginTop="5dp"
android:background="#drawable/grid_color_selector"
android:clickable="true"
android:focusable="true"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp" >
<ImageView
android:id="#+id/image"
android:layout_width="280dp"
android:layout_height="158dp"
android:contentDescription="#string/desc"
android:scaleType="centerInside" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:gravity="center"
android:textSize="12sp" >
</TextView>
Add android:descendantFocusability="blocksDescendants" attribute in parent layout of row_grid.xml
You should remove
android:clickable="true"
android:focusable="true"

android listview is iterating through the footer menus too

This is my activity code:
#Override
public void onCreate(Bundle icicle)
{
super.onCreate(icicle);
String[] names = new String[] { "Investor Relations", "Social Sharing", "Rate this app", "About Rathbones",
"Disclaimer", "Security", "Credits"};
// Create an ArrayAdapter, that will actually make the Strings above
// appear in the ListView
this.setListAdapter(new ArrayAdapter<String>(this, R.layout.moremenulist,
R.id.label, names));
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
// Get the item that was clicked
Object o = this.getListAdapter().getItem(position);
String keyword = o.toString();
Toast.makeText(this, "You selected: " + keyword, Toast.LENGTH_LONG).show();
}
And here is the xml file for this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="#+id/LinearLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:layout_alignParentLeft="true" android:layout_alignParentTop="true" android:layout_width="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:layout_height="wrap_content" android:id="#+id/label"></TextView>
<LinearLayout android:id="#+id/linearLayout2" android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignParentRight="true" android:layout_alignParentBottom="true">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton android:layout_width="wrap_content" android:id="#+id/imgbtn1" android:layout_alignParentLeft="true" android:layout_alignBottom="#+id/imgbtn3" android:layout_height="wrap_content" android:src="#drawable/topnews" android:visibility="visible"></ImageButton>
<ImageButton android:layout_width="wrap_content" android:layout_alignParentRight="true" android:id="#+id/imgbtn5" android:layout_alignBottom="#+id/imgbtn4" android:layout_height="wrap_content" android:src="#drawable/more"></ImageButton>
<ImageButton android:layout_width="wrap_content" android:id="#+id/imageButton1" android:layout_height="wrap_content" android:src="#drawable/contact_us" android:layout_alignParentTop="true" android:layout_toRightOf="#+id/imgbtn1"></ImageButton>
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
Now, when the activity is loading it is appending the menu with each item in the list. I just want it to appear only in bottom not with each and every list item.
any suggestions?
Edit-1 :
I tried this but getting an error of nullpointer exception
View footerView =
((LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer, null, false);
ListView lv = new ListView(this);
lv.addFooterView(footerView);
What do you mean by menu? Do you mean your ImageButtons?
If that's the case then remove those create another layout for those ImageButtons and add them using ListView.addFooterView() instead.
You could create e.g. list_footer_layout.xml, which contains your ImageButtons. Then:
public void onCreate(Bundle bundle) {
...
ListView lv = getListView(); // If your activity is ListActivity
View foot = getLayoutInflater().inflate(R.layout.list_footer_layout, null);
lv.addFooterView(foot);
}

Categories

Resources