Hello guys so I want to add a border around my spinner. just a rectangular box would work. I have already added the border. The problem is that the border keeps the size of the biggest object in the list not the selected object.
Just if it is not clear, My problem is that I have a border(rectangular box) around the spinner which is great, but the size of of the box is the problem. the size of the box is determined by the largest object in the list. Instead, I want the size of the box to be determined by the selected object from the spinner. So if I choose test the box should be small but if I choose "ajidhfkljhfiidsff1233" the box should become bigger.
I already added a border but it is around the spinner
xml of spinner:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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">
<Spinner
android:id="#+id/spinner_for_reason"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/color_for_botton_off"
android:gravity="center_horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/test"
</androidx.constraintlayout.widget.ConstraintLayout>
xml of color_for_botton_off:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#color/white" />
<stroke
android:width="1dp"
android:color="#607D8B" />
<corners android:radius="5dp" />
</shape>
xml spinner_xml_to_design:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:textColor="#color/fav"
android:layout_gravity="center_vertical"
android:paddingLeft="10dp"
android:paddingRight="10dp"/>
Main.java:
options_list = new ArrayList<String>();
ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), R.layout.spinner_xml_to_design, options_list);
dataAdapter.setDropDownViewResource(R.layout.spinner_xml_to_design);
spinner_for_reason.setAdapter(dataAdapter);
Thank you in advance guys any help would be appreciated. :) it is my 2 nd post of overflow
Related
I want to achieve the background effect of the TextView at the bottom of the below image.
sorry for my poor english...
You can create your background drawable
your_drawable.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="oval">
<gradient android:angle="0"
android:endColor="#fff"
android:startColor="#c86e6e6e"
android:gradientRadius="100dp"
android:type="radial"/>
</shape>
And add to textview:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEST"
android:textColor="#fff"
android:background="#drawable/your_drawable"
android:padding="90dp"
android:textSize="30sp"/>
You have to use gradient scale. go Check documentation for more details
I can not draw the shadow with setElevation.I do not want to use a drawing to do this. Here is the code:
This is the parent.
Page.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/background">
<LinearLayout
android:id="#+id/pp_cont_new_list"
android:background="#color/background"
android:layout_width="match_parent"
android:layout_height="150dp"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingLeft="30dp"
android:paddingRight="30dp"
android:paddingTop="5dp">
</LinearLayout>
</LinearLayout>
Here I build a simple layout.
PersonalPage.Java
final LinearLayout ROOT = (LinearLayout) findViewById( R.Id.pp_cont_new_list);
final LinearLayout LL = new LinearLayout(context); //Container
LL.setLayoutParams (new LinearLayout.LayoutParams(
Generic.PARAM_MP, //eq. match_parent
(int) context.getResources ().getDimension (R.dimen.bt_preview_list_height)
));
LL.setBackgroundResource (R.drawable.background_new_list);
LL.setElevation (5f);
LL.setClickable (true);
ROOT.addView( LL );
/*
* other code inner the container
*/
this background
background_new_list.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list
xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape android:shape="rectangle">
<corners android:radius="10dp"/>
<solid android:color="#color/background"/>
</shape>
</item>
<item>
<ripple android:color="#color/textColorAlpha"/>
</item>
</layer-list>
I would like an explanation of my error.
And I would also like to know what to use for pre-LOLLIPOP.
set elevation just doesnot work for pre-lolipop
You can create a custom button image that has a shadow and set it as button background..or any view's background to give shadow effect
use ViewCompat.setElevation(your_view(in your case LL), elevation_amount);
but for devices pre-Lollipop this will not have effect,
for pre-Lollipop cases you might use:
android:background="#android:drawable/dialog_holo_light_frame"
This is working if you don't mind doing things in xml. I tried it out. I did not have background color value that you are using so I used a different color for parent layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#E8E8E8"
android:paddingBottom="20dp"
android:clipToPadding="false"
android:translationZ="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/background_new_list"
android:clipToPadding="false"
android:elevation="25dp"
android:orientation="vertical"
/>
Note: I am using a higher elevation value just to make it easier to check if it's working. you can adjust it to what you need.
Hello I'm using badge counter in navigation menu, but. I can't move to center vertically. I tried margin, height:match_parent but width shape not success.
( https://stackoverflow.com/a/33607630/4933464 )
Here my codes ( I use with shape ):
#drawable/menu_counter.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<solid android:color="#ec443e" />
<padding
android:left="5dp"
android:right="5dp"
android:top="3dp"
android:bottom="3dp"/>
<corners android:radius="5dp" />
</shape>
#layout/menu_counter.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:background="#drawable/menu_counter"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#ffffff" />
Thanks.
Wrap it with a relative layout and add
android:layout_centerVertical="true"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_centerVertical="true"
android:background="#drawable/menu_counter"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#ffffff" />
</RelativeLayout>
Original DrawerLayout doesn't support badges. But there is a many useful library
MaterialDrawer : https://github.com/mikepenz/MaterialDrawer
Screenshots aviable on github page.
I am making an app and I have an activity with a searchview. My point is that I want to change the background color of the text field (the black rectangle in screnn shot).
My drawable:
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle"
android:padding="10dp">
<solid android:color="#FFFFFF"/>
<corners
android:bottomRightRadius="10dp"
android:bottomLeftRadius="10dp"
android:topLeftRadius="10dp"
android:topRightRadius="10dp"/>
</shape>
My layout:
<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:fitsSystemWindows="true"
tools:context=".SearchActivity" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="160sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:background="#drawable/search_background"
android:orientation="vertical" >
<SearchView
android:id="#+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20sp"
android:layout_marginRight="20sp"
android:layout_marginTop="60sp"
android:actionViewClass="android.widget.SearchView"
android:background="#drawable/searchview"
android:iconifiedByDefault="false"
android:paddingLeft="10sp"
android:paddingTop="5sp"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:showAsAction="collapseActionView"
android:focusable="false" >
</SearchView>
</LinearLayout>
Screenshot: http://imageshack.com/a/img829/3203/pd85.png
Can anybody help me with that?
thanks
After a little detective work you'll see that if you look at the source for the SearchView widget, you will see it extends LinearLayout with some added functionality. Looking at the line #242 it contains a widget (specifically a SearchAutoComplete which extends the AutoCompleteTextView which in the end extends EditText.
With that said, you'll need to modify the background attribute of that child element, with something on the lines of this:
SearchView c = findViewById(R.id.searchView);
EditText e = (EditText)c.findViewById(c.getContext().getResources().getIdentifier("android:id/search_src_text", null, null));
e.setBackgroundColor(Color.BLACK); //←If you just want a color
e.setBackground(getResources().getDrawable(R.drawable.YOUR_DRAWABLE));
//↑ If you want a drawable ↑
Useful links:
Source for SearchView
Works for me:
mSearchView.findViewById(android.support.v7.appcompat.R.id.search_plate).setBackground...
Also for voice button:
mSearchView.findViewById(android.support.v7.appcompat.R.id.search_voice_btn).setBackground...
im quite new to android, so i apologise if this is a noob-ish question (:
i designed my list following the example found here: http://android-er.blogspot.com/2010/06/custom-arrayadapter-with-with-different.html
but what i would like to know is how would i go about adding setContentView(R.layout.main) so that i can display other (xml) elements along with the list?
thanks for any advice (:
You just edit your main.xml to look whatever you want it to look, eg:
<?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">
<View android:id="#+id/emptyview"
android:layout_height="30dp"
android:layout_width="fill_parent"
/>
<ListView android:id="#+id/listview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="true"
android:stackFromBottom="true"
android:layout_below="#id/emptyview"
android:headerDividersEnabled="true"
/>
</RelativeLayout>
Change your activity to extend only Activity, not ListActivity. Finally, in your activity you can then fetch your list view with:
ListView list = (ListView) findViewById(R.id.listview);
and do whatever you need to do with the list.
You create your main.xml
Add to it a ListView
<ListView android:layout_width="match_parent"
android:layout_height="match_parent" android:id="#+id/myListView"
android:divider="#ffa500" android:dividerHeight="1px"
android:background="#drawable/somedrawable_xml"
android:choiceMode="singleChoice"></ListView>
somedrawable_xml.xml could be any drawable example:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#00ffa500" />
<stroke android:width="2dp" android:color="#ffffa500" />
<padding android:left="1dp" android:top="1dp" android:right="1dp"
android:bottom="1dp" />
Add a layout xmlFile myLayout.xml example: (I added imageView for demonstration) anyway what is important is the id of the textview
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/dasdasd">
<TextView android:text="TextView" android:layout_height="wrap_content"
android:id="#+id/thisIsTheTextView" android:layout_width="wrap_content" android:textAppearance="?
android:attr/textAppearanceLarge"></TextView>
<ImageView android:src="#drawable/icon" android:id="#+id/imageView1" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true"></ImageView>
finally in your Activity
ArrayAdapter myAD=new ArrayAdapter(mContext,R.layout.myLayout,R.id.thisIsTheTextView,new String[] {"item1", "item2", "item3", "item4", "item5"});
myListView.setAdapter(myAD);