I have a spinner within alert dialog. I wanted to reduce padding between spinner items and hence I implemented following:
spinner_row.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="30dp"
android:background="#fff" >
<TextView
android:id="#+id/tvCust"
android:layout_width="200dp"
android:layout_height="30dp"
android:gravity="left|center_vertical"
android:textColor="#000"
android:textSize="15sp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true" />
</RelativeLayout>
Activity code contains following:
spinner= (Spinner) dialog.findViewById(R.id.spinner);
String arr[] = { "1", "2", "3" };
ArrayAdapter<String> adapter = new ArrayAdapter<String>(
CameraActivity.this, R.layout.spinner_row, R.id.tvCust,arr);
spinner.setAdapter(adapter);
Now as you can see in below screenshot, radio button is getting displayed on spinner which is actually a part of spinner_row.xml. Note that textview width is 200dp while spinner is only 130dp long, so that radio button should not have been displayed on spinner. How can I remove it?
Also, when I click any of the spinner item, spinner pop-up doesn't get disappeared as expected.(note all 3 check-boxes are checked in spinner items list). setOnItemSelectedListener is not getting called on item click.
Any help appreciated.
Edit 1
As per farrukh's suggestion, I tried his code and following is the result.
I have this
and this
with these code of xml
xml for adapter named spinadapt.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="30dp"
android:background="#fff" >
<TextView
android:id="#+id/tvCust"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_toLeftOf="#+id/radioButton1"
android:gravity="left|center_vertical"
android:textColor="#000"
android:textSize="15sp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentRight="true" />
</RelativeLayout>
and main layout named 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">
<TextView
android:id="#+id/spinner1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:hint="Select item"
android:background="#drawable/spin"/>
</RelativeLayout>
and java code is class named MainActivity.java
public class MainActivity extends Activity
{
Spinner sp;
TextView tv;
String[] counting={"One","Two","Three","Four"};
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
sp=new Spinner(this);
tv=(TextView)findViewById(R.id.spinner1);
tv.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
sp.performClick();
}
});
sp.setAdapter(new Adapter(MainActivity.this, counting));
sp.setOnItemSelectedListener(new OnItemSelectedListener()
{
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
{
tv.setText(counting[arg2]);
}
#Override
public void onNothingSelected(AdapterView<?> arg0)
{
}
});
}
}
and adapter class named Adapter.java
public class Adapter extends BaseAdapter
{
LayoutInflater inflator;
String[] mCounting;
public Adapter( Context context ,String[] counting)
{
inflator = LayoutInflater.from(context);
mCounting=counting;
}
#Override
public int getCount()
{
return mCounting.length;
}
#Override
public Object getItem(int position)
{
return null;
}
#Override
public long getItemId(int position)
{
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
convertView = inflator.inflate(R.layout.spinadapt, null);
TextView tv = (TextView) convertView.findViewById(R.id.tvCust);
tv.setText(Integer.toString(position));
return convertView;
}
}
this is working perfect
hope this will help
Related
Main activity contains a spinner and a button. MainActivity.java code is as follows.
public class MainActivity extends AppCompatActivity implements LoadNew.VCallback {
public Spinner dropdown;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
dropdown = (Spinner) findViewById(R.id.spinner1);
//Create a list of items for the spinner.
String[] items = new String[]{"select topic", "topic1", "topic2"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_dropdown_item, items);
dropdown.setAdapter(adapter);
dropdown.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parentView, View selectedItemView, int position, long id) {
// TODO Auto-generated method stub
String sp1 = String.valueOf(dropdown.getSelectedItem());
if (sp1.contentEquals("select topic")) {
loadFun("");
}
if (sp1.contentEquals("topic1")) {
loadFun("topic1");
}
if (sp1.contentEquals("topic2")) {
loadFun("topic2");
}
}
#Override
public void onNothingSelected(AdapterView<?> parentView){
// TODO Auto-generated method stub
}
});
Button x = (Button) findViewById(R.id.full_screen);
x.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
loadFun("topic2", 0);
}
});
}
public void loadFun(String topicName, int i) {
LoadNew st = new LoadNew(this);
st.display(topicName, i);
}
}
The xml layout for main activity is as follows (activity_main.xml).
<?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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/btn_dropdown"
android:spinnerMode="dropdown" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/full_screen"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Screen"
style="?android:attr/borderlessButtonStyle"/>
</LinearLayout>
<LinearLayout
android:id="#+id/full_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</LinearLayout>
</RelativeLayout>
There is another java class LoadNew.java as follows.
public class LoadNew {
public interface VCallback {
//To access methods of MainActivity class
}
public Activity activity;
public LoadNew(Activity _activity) {
this.activity = _activity;
VCallback callerActivity = (VCallback) activity;
}
//Display PDF
public void display(String topicName, int i) {
LinearLayout fullScreen = (LinearLayout) this.activity.findViewById(R.id.full_view);
fullScreen.removeAllViews();//Clear field
LayoutInflater inflater = (LayoutInflater) this.activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.activity_load, null);
if(i ==0) {
this.activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
this.activity.setContentView(view);
}
TextView tv= (TextView) this.activity.findViewById(R.id.tv1);
tv.setText(topicName);
tv.setTextSize(100);
}
}
The activity_load.xml file is as follows.
<?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:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/tv1"
android:layout_below="#+id/bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
When an item is selected in the spinner, the corresponding text is displayed in the textview. Upon pressing the button, the textview appears in full screen mode.
Now I want to return to the previous view (by pressing back button or by single tab on screen), i.e., the view before the button was clicked. How can I achieve this? Thanks in advance for the patience and the help.
Simplest would be to have the spinner and the non-fullscreen TextView on one activity and open a second activity with the fullscreen mode on button click.
This is my first try to ask a question. I am a beginner in android.When I am using image OnClickListener() in a simple way it is working but when I use gridView which has one imageView and one TextView on Image Click I want to jump to a new activity.
What I do is just call gridView OnItemClickListener() and jump to another activity which is working as I show below
gridView = (GridView) findViewById(R.id.gridview1);
gridView.setAdapter(new Adapter1(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent sendID_intent = new Intent(Activtiy1.this, Activity2.class);
startActivity(sendID_intent);
}
});
wherein Adapter1 I am extends BaseAdapter where an image is load using Picasso library which is also working the only issue is with image click event which is not working properly.
public class Adapter1 extends BaseAdapter {
private Context mC1;
ArrayList<View> viewList = new ArrayList<View>();
public Adapter1(Context context) {
mC1 = context;
}
#Override
public int getCount() {
return normlSearchList.size();
}
#Override
public Object getItem(int position) {
return normlSearchList.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#SuppressLint("ViewHolder")
#Override
public View getView(final int position, View convertView,
ViewGroup parent) {
View view1=convertView;
if ((position + 1) > viewList.size()) {
LayoutInflater inflater = (LayoutInflater) mC1.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view1 = inflater.inflate(R.layout.view_grid,null);
ImageView imageView = (ImageView) view1.findViewById(R.id.iv_galleryb);
TextView textView = (TextView) view1.findViewById(R.id.tv_name);
textView.setText(normlSearchList.get(position).getPost_title());
final String txtID;
String img_url = normlSearchList.get(position).getImage();
Picasso.with(mContext)
.load(img_url)
.placeholder(R.drawable.it_logo_td)
.error(R.drawable.it_logo_td)
.noFade().resize(150, 150)
.centerCrop()
.into(imageView);
viewList.add(view1);
imageView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(mContext, Activity2.class);
mC1.startActivity(intent);
}
});
}
return view1;
}
}
My layout file simply contain relative layout which holds a linearLayout a TextView for display data not found message if no data is found and a gridView
<?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"
android:orientation="vertical"
android:layout_margin="5dp"
>
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:orientation="horizontal" >
...................
</LinearLayout>
<TextView
android:text=""
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView"
android:gravity="center_horizontal"
android:visibility="invisible"
/>
<GridView
android:id="#+id/gridview1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/layout1"
android:numColumns="3"
android:columnWidth="200dp"
android:gravity="center"
android:stretchMode="columnWidth"
/>
My viewgrid.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="100dp"
android:layout_height="80dp"
android:descendantFocusability="afterDescendants">
<ImageView
android:id="#+id/iv_galleryb"
android:layout_width="100dp"
android:layout_height="80dp"
android:clickable="true"
android:scaleType="fitXY"
/>
<TextView
android:id="#+id/tv_name"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/iv_galleryb"
/>
I hope I make my problem clear.
I'd avoid use clickable views inside of GridView, instead of, I'd use RecyclerView. Because GridView implements onItemClick, it can conflict with ImageView clickable.
Anyway try add android:focusable="true" in <ImageView />
I created the class below :
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
EditText edit = (EditText) view.findViewById(R.id.editText2);
edit.setText(parent.getItemAtPosition(pos).toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
It results an error when I try to change the value of edittext in the other layout. I put below code in my main activity
rootView = inflater.inflate(R.layout.food, container, false);
Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener());
After I change the layout of the main activity, then I set the setOnItemSelectedListener for the spinner in that layout.
this is my food.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="wrap_content"
android:background="#d0dae9"
android:orientation="vertical">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:spinnerMode="dropdown"
android:entries="#array/foodcategory"
android:layout_weight="1" />
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/spinner1"
android:entries="#array/foodcategory1"
android:layout_weight="1" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search"
android:id="#+id/button2"
android:layout_weight="1" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine"
android:ems="10"
android:id="#+id/editText2"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
</LinearLayout>
and below is the spinner item
<string-array name="foodcategory">
<item>Drinks</item>
<item>Meal</item>
<item>Meat</item>
<item>Snacks</item>
<item>Breads</item>
<item>Cakes</item>
<item>Fruits</item>
<item>Vegies</item>
<item>Other</item>
</string-array>
Please help me out..
update your CustomOnItemSelectedListener class:
public class CustomOnItemSelectedListener implements OnItemSelectedListener {
private final View rootView;
public CustomOnItemSelectedListener(View root) {
rootView = root;
}
public void onItemSelected(AdapterView<?> parent, View view, int pos,long id) {
EditText edit = (EditText) rootView.findViewById(R.id.editText2);
edit.setText(parent.getItemAtPosition(pos).toString());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
}
add rootView as argument to CustomOnItemSelectedListener constructor:
rootView = inflater.inflate(R.layout.food, container, false);
Spinner spinner = (Spinner) rootView.findViewById(R.id.spinner);
spinner.setOnItemSelectedListener(new CustomOnItemSelectedListener(rootView));
I'm trying to create an activity, RateCardActivity, which has a spinner in it. My layout file for RateCardActivity is rate_card. My RateCardActivity looks like the following.
public class RateCardActivity {
public void onCreate(Bundle bundle) {
super.onCreate(bundle);
setContentView(R.layout.rate_card);
Spinner spinner = (Spinner) findViewById(R.id.select_city);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(this,
R.array.select_city, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(adapter);
}
}
The layout file rate_card is:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:custom="http://schemas.android.com/apk/res/com.olacabs.customer"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"
android:gravity="center"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:text="#string/rate_card"
android:textColor="#color/white"
android:textSize="20dp"
custom:customFont="litera_bold.ttf" />
<Spinner
android:id="#+id/select_city"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The RateCardActivity is called from another activity using an intent (I'm sure there is nothing wrong with that part of the code as when I substitute RateCardActivity with another activity, the application works fine). When I try to open the RateCardActivity in the application in emulator, the application crashes and I got the message "The application has stopped unexpectedly. Please try again later."
I can't seem to understand what I'm doing wrong, and want to know how to correct this?
Improve :public class RateCardActivity extends Activity
and add RateCardActivity to AndroidManifiest.xml
Hi you can use spinner activity by this way, I gave a sample code for the help..
public class MyActivity extends Activity {
public static EditText edtsample;
public static EditText edtchannel;
public static EditText edtencoding;
private static Spinner samplespin;
private static Spinner channelspin;
private static Spinner encodingspin;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.settings);
edtsample = (EditText)findViewById(R.id.audvalue1);
edtchannel = (EditText)findViewById(R.id.chanvalue1);
edtencoding = (EditText)findViewById(R.id.encodingvalue1);
edtchannel.setFocusable(false);
edtchannel.setClickable(false);
edtencoding.setFocusable(false);
edtencoding.setClickable(false)
samplespin = (Spinner) findViewById(R.id.audspinner1);
samplespin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
edtsample.setText(parent.getItemAtPosition(position).toString());
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
edtsample.setText("");
}
});
channelspin = (Spinner) findViewById(R.id.chanspinner1);
channelspin.setOnItemSelectedListener(new OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
if(parent.getItemAtPosition(position).equals("CHANNEL_PHONE")){
edtchannel.setText(R.string.chan1);
System.out.println("VALUE OF " +
edtchannel.getEditableText().toString()) ;
}
if(parent.getItemAtPosition(position).equals("CHANNEL_CD")){
edtchannel.setText(R.string.chan2);
System.out.println("VALUE OF " +
edtchannel.getEditableText().toString()) ;
}
if(parent.getItemAtPosition(position).equals("CHANNEL_HD")){
edtchannel.setText(R.string.chan2);
System.out.println("VALUE OF " +
edtchannel.getEditableText().toString()) ;
}
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
edtchannel.setText("");
}
});
**And in XML part you do by this**
<Spinner
android:id="#+id/audspinner1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/audioText1"
android:spinnerMode= "dropdown"
android:entries="#array/sample_array" />
<EditText
android:id="#+id/audvalue1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/audspinner1"
android:hint="Enter Sampling Rate"
android:ems="10" >
friends,
i have created very simple custom listview adapter with ratingBar in it.
now i have noticed one thing i cannot rate those rating bars in listview because
when i click on listview that row particular row gets selected.
any one guide me how to select individual items in android listview?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/android:list"
/>
</LinearLayout>
and listview_item design
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="265dip"
android:orientation="vertical"
android:layout_height="wrap_content">
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text1"
android:textSize="25dip"
android:text="This is text1"/>
<TextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/text2"
android:text="This is text2"/>
<RatingBar android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/ratingBarStyleSmall"
android:id="#+id/star"
android:numStars="10"
android:stepSize="0.1"
android:isIndicator="true"
/>
</LinearLayout>
</LinearLayout>
any help would be appreciated.
Dear UMAR
In order to implement the listeners to the each element to the listview(Single row) use Custom adapter and in the getview(....) method of the custom adapter ,implements the listeners which u want.. sample code `public class FindFriendsListAdapter extends BaseAdapter {
private ArrayList<SingleElementDetails> allElementDetails;
private LayoutInflater mInflater;
private Context context;
private String userid1;
private int usersno1;
private DBAdapter db;
public FindFriendsListAdapter(Context context, ArrayList<SingleElementDetails> results,String userid,int usersno) {
this.context=context;
this.userid1=userid;
this.usersno1=usersno;
allElementDetails = results;
mInflater = LayoutInflater.from(context);
db=new DBAdapter(context);
db.open();
}
public int getCount() {
return allElementDetails.size();
}
public Object getItem(int position) {
return allElementDetails.get(position);
}
public long getItemId(int position) {
return position;
}
public View getView(final int position, View convertView, ViewGroup parent)
{
convertView = mInflater.inflate(R.layout.friendlisthelper, null);
ImageView imageview = (ImageView) convertView.findViewById(R.id.friendimageview);
TextView textview = (TextView) convertView.findViewById(R.id.friendtextview);
Button button=(Button)convertView.findViewById(R.id.friendbutton);
convertView.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//do what u want
}
});
button.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// do what u want
}
});
return convertView;
}
}
`
android:isIndicator="false" in order to rate it. and listview selection will be gone automatically and control will be transferred to that rating control.
Try this
ListView lv_party;
lv_party.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, final int position,long id)
{
// TODO Auto-generated method stub
view.findViewById(R.id.btn_del).setOnClickListener(new View.OnClickListener(){
//your code
}