I am new to android app developing as i was creating spinner i noticed a extra space / padding vertically to drop down list of the spinner at the start and end of the drop down.
MainActivity.java:
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner sr = (Spinner) findViewById(R.id.spinner);
String[] days = getResources().getStringArray(R.array.days);
ArrayAdapter<String> ar = new ArrayAdapter<>(this, R.layout.single_row, days);
sr.setAdapter(ar);
}
}
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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#666666"
tools:context="com.xxxxx.defaultspinner.MainActivity">
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:spinnerMode="dialog"
android:background="#898989">
</Spinner>
</RelativeLayout>
single_row.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#FFFFFF"
android:textSize="26sp"
android:background="#214161">
</TextView>
I set background of all the views to different color so that i can identify the source of the extra space / padding. But the the extra space / padding has white background which none of the view has.
Note this is not because of the spinnerMode="dialog" option. this behavior is also happens when spinnerMode="dropdown". How can i remove this space ? or i am doing something wrong ?
You just need to override the getDropDownView method in the adapter.
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
parent.setPadding(0, 0, 0, 0);
return convertView;
}
try adding this to your TextView
android:includeFontPadding="false"
it will remove the TextView extra top and bottom padding
Related
I've wrote a small application, which shows several android cards. But I'd like to be able to set a colour and title to the top of the card like in the image below, so far I haven't found any information online how to do this. So some help would be fantastic :-)
(My code so far does not accomplish the above, so far my code just produces regular all white cardviews)
My code so far is below:
CardAdapter.java
public class CardAdapter extends RecyclerView.Adapter<CardAdapter.ViewHolder> {
public List<TTItem> posts = new ArrayList<>();
public void addItems(List<TTItem> items) {
posts.addAll(items);
notifyDataSetChanged();
}
public void clear() {
posts.clear();
notifyDataSetChanged();
}
#Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
Context context = parent.getContext();
View view = View.inflate(context, R.layout.item_cardview, null);
return new ViewHolder(view);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.mTextView.setText(posts.get(position).title);
Picasso.with(holder.mImageView.getContext()).load(posts.get(position).images[0]).into(holder.mImageView);
}
#Override
public int getItemCount() {
return posts.size();
}
static class ViewHolder extends RecyclerView.ViewHolder {
public TextView mTextView;
public ImageView mImageView;
public ViewHolder(View view) {
super(view);
mTextView = (TextView) view.findViewById(R.id.textview);
mImageView = (ImageView) view.findViewById(R.id.imageView);
}
}
}
MainActivity.java
public class MainActivity extends ActionBarActivity implements SwipeRefreshLayout.OnRefreshListener {
#InjectView(R.id.mainView)
RecyclerView mRecyclerView;
#InjectView(R.id.refreshContainer)
SwipeRefreshLayout refreshLayout;
private LinearLayoutManager mLayoutManager;
private CardAdapter mAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.inject(this);
mRecyclerView.setHasFixedSize(true);
refreshLayout.setOnRefreshListener(this);
TypedValue tv = new TypedValue();
int actionBarHeight = 0;
if (getTheme().resolveAttribute(R.attr.actionBarSize, tv, true)) {
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data, getResources().getDisplayMetrics());
}
refreshLayout.setProgressViewEndTarget(true, actionBarHeight);
mAdapter = new CardAdapter();
mRecyclerView.setAdapter(mAdapter);
// use a linear layout manager
mLayoutManager = new GridLayoutManager(this, 1);
mRecyclerView.setLayoutManager(mLayoutManager);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return super.onOptionsItemSelected(item);
}
#Override
public void onRefresh() {
mAdapter.clear();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
public void run() {
refreshLayout.setRefreshing(false);
}
}, 2500);
}
}
Activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v4.widget.SwipeRefreshLayout
android:id="#+id/refreshContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/mainView"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
item_cardview.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="200dp"
android:layout_height="200dp"
android:padding="10dp"
card_view:cardCornerRadius="2dp"
card_view:contentPadding= "5dp"
card_view:cardUseCompatPadding="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="150dp"
android:scaleType="centerCrop"/>
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center"/>
</LinearLayout>
</android.support.v7.widget.CardView>
The only change I've made from yours is, card_view:cardCornerRadius="8dp"> and removed the imageview(as no longer needed)
Screenshot of flashcard not filling to half of card:
I believe this is (almost) exact layout of what you want. It is pretty self-explanatory but feel free to ask if something's not clear.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
app:cardElevation="8dp"
card_view:cardCornerRadius="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout <!-- This is the specific part you asked to color -->
android:id="#+id/heading_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/teal_500"
android:padding="36dp"
android:orientation="vertical">
<TextView
android:id="#+id/tv_heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="22 mins to Ancona"
android:textColor="#color/white"
android:textStyle="bold"
android:textSize="36sp" />
<TextView
android:id="#+id/tv_subheading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_below="#+id/tv_heading"
android:text="Light traffic on ss16"
android:textColor="#color/teal_200"
android:textSize="24sp" />
</LinearLayout>
<ImageView
android:id="#+id/iv_map"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:contentDescription="Assigned delivery boy"
android:scaleType="fitXY"
android:src="#drawable/bg_map" />
<TextView
android:id="#+id/tv_footer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:layout_below="#+id/tv_heading"
android:text="It is just an example!"
android:textColor="#color/grey_500"
android:textStyle="bold"
android:textSize="24sp" />
</LinearLayout>
</android.support.v7.widget.CardView>
I have replaced your mapFragment (presumably) with imageView to reduce complications.
Update: As the question now addresses the infamous "round corner" problem, this is actually by design. Yes, it is a big flaw. But the solution (as given in docs Here) would be to use card_view:cardPreventCornerOverlap="false" attribute (which I don't think does anything good because it just makes card square again).
See these questions for a good reference to this problem:
Appcompat CardView and Picasso no rounded Corners
Make ImageView fit width of CardView
From my understanding, you can change the colour of a CardView in it's entirety but not parts of it. I'm not sure how that would even work.
What you can do, is nest a TextView with the title within the CardView, then colour the background of the TextView to the colour you would like. Use appropriate margins/padding for a uniform look. Add a background to your TextView and see what you get.
Your TextView is already using the match_parent parameter on your android:layout_width="" so you would only need to add the background like so:
<TextView
android:id="#+id/textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textStyle="bold"
android:layout_gravity="center"
android:background="#FF4444"/>
To change the entire CardView colour like I mentioned at the beginning you can do it the same way or programmatically like so:
cardView.setCardBackgroundColor(COLOURHERE);
I've tried a lot of other answers on SO, none of them seem to work for me. I'm not sure what I did wrong.
Here's my xml:
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/product_category_spinner"
android:theme="#style/AppartmentBlue" />
And here's my Java (it's a DialogFragment extending class):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_product_dialog, container, false);
Spinner productCategory = (Spinner) v.findViewById(R.id.product_category_spinner);
ArrayAdapter<String> spinnerAdapter =
new ArrayAdapter<String>(getActivity().getApplicationContext(),
android.R.layout.simple_spinner_dropdown_item,
getResources().getStringArray(R.array.category_array));
spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
productCategory.setAdapter(spinnerAdapter);
// setCancelable(false);
return v;
}
The thing is, the text is white on very bright gray. It came that way by default, if it came in black foreground I wouldn't need to touch it. Why's it doing this? My theme extends from Theme.Holo.Light.DarkActionBar.
I don't wanna set it at runtime, I'm gonna have a lot of spinners and I want it to be global across the app, with the option for me to add later themes.
I hope this helps:
My Spinner:
<Spinner
android:id="#+id/spinner_cat"
android:paddingLeft="0dp"
android:paddingRight="0dp"
android:paddingBottom="0dp"
android:paddingTop="0dp"
android:layout_marginStart="#dimen/left_margin_large"
android:layout_marginLeft="#dimen/left_margin_large"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"/>
Setting up the adapters:
private final int _singleItemRes = R.layout.simple_spinner_item;
private final int _dropdownRes = R.layout.simple_spinner_dropdown_item;
ArrayAdapter<String> adapter = new ArrayAdapter<String>(_handler.getContext(), _singleItemRes, getTitles());
adapter.setDropDownViewResource(_dropdownRes);
simple_spinner_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#color/actionbar_text"
android:textSize="20sp"
android:textStyle="normal"/>
simple_spinner_dropdown_item.xml:
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/text1"
style="?android:attr/spinnerDropDownItemStyle"
android:layout_width="match_parent"
android:layout_height="48dp"
android:background="#color/primary_dark"
android:ellipsize="marquee"
android:layout_centerVertical="true"
android:singleLine="true"
android:paddingRight="#dimen/left_margin_small"
android:paddingLeft="#dimen/left_margin_small"
android:textColor="#color/actionbar_text"/>
Let me know if I am missing anything from the answer and I will add it in.
I currently wanted to follow this layout but not sure how to control the buttons below. My problem is that the image stretched itself whatever the listview's width is. I wanted to follow the layout below. I am using Android's back button.
I am not quite familiar in programmatically controlling the position of the image and it's default size. The button is inside addFooterView(btnLoadMore);
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.load_main_groups_activty, container, false);
// Getting listview from xml
ListView lv = (ListView) rootView.findViewById(android.R.id.list);
// Creating a button - Load More
Button btnLoadMore = new Button(mContext);
btnLoadMore.setBackgroundResource(R.drawable.navigation_back);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
btnLoadMore.setLayoutParams(params2);
// Adding button to listview at footer
lv.addFooterView(btnLoadMore);
return rootView;
}
load_main_groups_activty
<?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"
android:orientation="vertical"
android:background="#color/white"
>
<!-- Main ListView
Always give id value as list(#android:id/list)
-->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
>
</ListView>
<FrameLayout
android:id="#+id/contentFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</RelativeLayout>
Try to implement this:
<?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"
android:orientation="vertical"
android:background="#color/white"
>
<!-- Main ListView
Always give id value as list(#android:id/list)
-->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:listSelector="#drawable/list_selector">
</ListView>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:background="#null"
android:src="#drawable/navigation_back" />
</RelativeLayout>
I am sure it'll help- you. Thanks
To keep the image from stretching, you can create a LinearLayout and place the button inside it. By setting the gravity attribute, you can position the button in the center, left or right of the ListView footer. After adding the button to the LinearLayout, the LinearLayout can be added to ListView footer.
See if the following code gets you the layout you want:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.load_main_groups_activty, container, false);
// Getting listview from xml
ListView lv = (ListView) rootView.findViewById(android.R.id.list);
// Creating a button - Load More
Button btnLoadMore = new Button(mContext);
btnLoadMore.setBackgroundResource(R.drawable.navigation_back);
LinearLayout llFooter = new LinearLayout(mContext);
LinearLayout.LayoutParams paramsBtn = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsBtn.gravity = Gravity.LEFT;
llFooter.addView(btnLoadMore, paramsBtn);
//RelativeLayout.LayoutParams params2 = new
//RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
//RelativeLayout.LayoutParams.MATCH_PARENT);
//llFooter.setLayoutParams(params2);
// Adding button to listview at footer
lv.addFooterView(llFooter);
return rootView;
}
I have listview that i want to fill the whole screen,There are four items in listview. It leaves empty space below after four items are filled.You can see in the screenshot. It leaves the blank space. I want whole screen to be covered.
I would like to have like this:
Here is the source Code
MainActivity.java.
public class MainActivity extends Activity {
ListView resultPane;
List<Taskinfo> list;
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultPane = (ListView) findViewById(R.id.mylist);
list = new ArrayList<Taskinfo>();
Resources res = getResources(); // resource handle
Drawable drawable = res.getDrawable(R.drawable.browse_home);
list.add(new Taskinfo("Browse", drawable));
drawable = res.getDrawable(R.drawable.jewelry);
list.add(new Taskinfo("Whats New", drawable));
drawable = res.getDrawable(R.drawable.show);
list.add(new Taskinfo("Upcoming Show", drawable));
drawable = res.getDrawable(R.drawable.contact);
list.add(new Taskinfo("Contact Us", drawable));
adapter = new CustomAdapter(this, list);
resultPane.setAdapter(adapter);
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private Context context;
private List<Taskinfo> list;
public CustomAdapter(Context context, List<Taskinfo> list) {
this.context = context;
this.list = list;
}
public View getView(int index, View view, final ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.single_list_item, parent, false);
}
Taskinfo t = list.get(index);
RelativeLayout l = (RelativeLayout) view
.findViewById(R.id.testrelative);
l.setBackgroundDrawable(t.getImage());
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText(t.getName());
return view;
}
}
single_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/testrelative"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/browse_home"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
</LinearLayout>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:text=""
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>
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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45sp"
android:id="#+id/llayout"
android:background="#drawable/navbar"
android:padding="3sp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Golden Stone"
android:textColor="#color/white"
android:textSize="20sp" >
</TextView>
</LinearLayout>
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_below="#id/llayout"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
Using ListView in such case is strange and unnecessary. Think about your constraints (like: what if items overflow?) and just use a proper layout manager. Like a vertical LinearLayout with the last item having a non-zero layout weight.
ListViews make sense only if you need an abstraction that generates list items on-the-fly.
You would want to set a background on the ListView. Currently your view should be going to the bottom, but the background of the ListView is transparent, so setting it white should achieve what you're asking.
<ListView ...
android:background="#android:color/white"
... />
if there are so few items, you can use the LinearLayout instead, and give weights for each of its items.
However, do note that android supports many devices and screens, so you might want to have a limitation of how small each row would be.
anyway, in case you wish to make each row the fitting height, you can check its size and then divide by the number of items.
in order to get the size of the listView , you can use this small snippet i've made .
I have layout with controls:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/contact_phones_layout_row"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/contact_phone"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:inputType="phone" />
<Spinner
android:id="#+id/contact_phone_type"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
And I want to inflate it into another layout on the fragment. Quantity of these controls depends on values in a array. Array contains controls objects. So every time onCreateView rises I filled the layout from the array:
private void addLine(PhoneLine line) {
LinearLayout layout = (LinearLayout) mLayout.findViewById(R.id.contact_phones_layout);
View view = line.getParent();
((ViewGroup) view.getParent()).removeView(view);
layout.addView(view, layout.getChildCount() - 1);
setButtonVisible(false);
}
if line is null controls are created this way:
private void addLine() {
LinearLayout layout = (LinearLayout) mLayout.findViewById(R.id.contact_phones_layout);
View view = inflater.inflate(R.layout.contact_phone_line, layout, false);
EditText phoneEditText = (EditText) view.findViewById(R.id.contact_phone);
Spinner phoneType = (Spinner) view.findViewById(R.id.contact_phone_type);
phoneLines.add(new PhoneLine(phoneEditText, phoneType, view));
layout.addView(view, layout.getChildCount() - 1);
}
But after that I get same values in all EditText/Spinner controls, values equal to last element in the array. What can be wrong? May be there is more pure way to add controls dynamically?
I fixed it by adding controls when onResume rises, not onCreateView. But I still don't understand why onCreateView changes all values.