I have created a dropdown spinner with a background custom image. I have placed the spinner in a linear layout and I have aligned items using weight.
Here is my code -
XML -
<LinearLayout
android:id="#+id/csbar"
android:windowSoftInputMode="adjustPan"
android:paddingTop="8dp"
android:paddingBottom="8dp"
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:background="#101010"
android:layout_height="wrap_content">
<ImageButton
android:id="#+id/run"
android:layout_width="0dp"
android:layout_height="#dimen/height"
android:layout_weight="1"
android:background="#null"
android:contentDescription="#string/nul"
android:src="#drawable/ic_action_play" />
<ImageButton
android:id="#+id/save"
android:layout_width="0dp"
android:layout_height="#dimen/height"
android:layout_weight="1"
android:background="#null"
android:contentDescription="#string/nul"
android:src="#drawable/ic_action_save" />
<Spinner
android:id="#+id/more"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#drawable/ic_action_sort_by_size"
android:layout_height="#dimen/height"/>
</LinearLayout>
Java -
int firstC = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
/////I have removed rest of the code to make it more legible/////
Spinner more = (Spinner) rootView.findViewById(R.id.more);
ArrayAdapter<CharSequence> adapter = ArrayAdapter.createFromResource(getActivity(),
R.array.more, android.R.layout.simple_spinner_item);
adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
more.setAdapter(adapter);
more.setOnItemSelectedListener(this);
}
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position,
long id) {
((TextView)view).setText(null);
}
Here is the output -
As you can see the sort by size icon (icon on right) appears stretched.
How I want it to look like -
What is wrong with my code? Please help.
The background image takes all the given space - 1/3 of the the LinearLayout width. If you don’t want it to stretch, you could create a drawable layout, e.g. drawable/my_sort_icon:
<?xml version="1.0" encoding="utf-8"?>
<bitmap xmlns:android="http://schemas.android.com/apk/res/android"
android:src="#drawable/ic_action_sort_by_size"
android:tileMode="disabled"
android:gravity="center" />
and use it as a background image in your Spinner:
<Spinner
android:id="#+id/more"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#drawable/my_sort_icon"
android:layout_height="#dimen/height"/>
Related
I have a RecyclerView with a fixed Dimension-Ratio
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
...>
<androidx.recyclerview.widget.RecyclerView
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constraintDimensionRatio="35:16"
... />
</androidx.constraintlayout.widget.ConstraintLayout>
I Need to show list of ImageView inside the RecyclerView, but with a fixed number of images shown on the screen, and the rest of the images need to scroll to be shown, similar to the image
I Create this layout with ImageView inside it:
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginStart="4dp"
android:layout_marginEnd="4dp"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/image2"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
The problem here
I try a lot, but the image doesn't show without setting the layout_width and layout_hieght with a fixed number.
and What i want is to set ImageView hight-width dynamicly with RecyclerView Hight and Screen Width.
The solution is super easy:
pass DisplayMetrics In The Adapter Constructor for getting screen width.
DisplayMetrics displayMetrics;
public SearchSliderOneAdapter(DisplayMetrics displayMetrics) {
this.displayMetrics = displayMetrics;
}
and calculate the width of ImageView = ScreenWidth\numberOfImage inside onCreateViewHolder function of the adapter.
public SearchSliderOneViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View inflate = LayoutInflater.from(parent.getContext()).inflate(R.layout.view_image_slider, parent, false);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(
displayMetrics.widthPixels / numberOfImage,
GridLayout.LayoutParams.MATCH_PARENT, 1);
inflate.setLayoutParams(params);
return new SearchSliderOneViewHolder(inflate, listener);
}
I want to update the style of the selected item inside a ViewPager.
How can I do this?
This is the UI design. As you can see, the "November" tab is selected, so the corresponding bar is highlighted in yellow.
The way I've implemented this is a ViewPager with a custom item (white, not highlighted):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.cardview.widget.CardView
android:layout_width="142dp"
android:layout_height="275dp"
android:layout_margin="0dp"
app:cardElevation="0dp"
app:cardBackgroundColor="#android:color/transparent">
<TextView
android:id="#+id/monthly_spend_month"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/December"
android:textColor="#color/aluminum"
android:textSize="20sp"
android:textStyle="normal"
android:layout_marginStart="22dp"
android:layout_marginTop="34dp"/>
<RelativeLayout
android:id="#+id/monthly_spend_card"
android:layout_marginTop="120dp"
android:layout_marginStart="22dp"
android:layout_width="120dp"
android:layout_height="155dp"
android:background="#drawable/monthlyspend_card_bg_inactive">
<TextView
android:id="#+id/monthly_spend_text"
android:textColor="#color/almost"
android:layout_marginTop="50dp"
android:layout_marginStart="22dp"
android:text="Total
spent"
android:textSize="#dimen/balanceDescTextSize"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/monthly_spend_amount"
android:textColor="#color/almost"
android:layout_marginTop="10dp"
android:layout_marginStart="22dp"
android:layout_below="#id/monthly_spend_text"
android:text="$ 254.98"
android:textSize="#dimen/balanceDescTextSize"
android:textStyle="bold"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
I plan on doing the highlighting programmatically.
Here's some adapter code:
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.monthlyspend_item, container, false);
TextView monthlySpendMonth, monthlySpendAmount;
monthlySpendCard = view.findViewById(R.id.monthly_spend_card);
monthlySpendMonth = view.findViewById(R.id.monthly_spend_month);
monthlySpendAmount = view.findViewById(R.id.monthly_spend_amount);
monthlySpendMonth.setText(monthlySpendings.get(position).getMonth());
monthlySpendAmount.setText(monthlySpendings.get(position).getAmountSpent());
container.addView(view, 0);
return view;
}
Any help would be appreciated!
I thing you are missing click listener :
view.setOnClickListener(new OnClickListener(){
public void onClick(View v){
// do you stuff here
}
});
((ViewPager)container).addView(view, 0)
Also inside instantiateItem() make int position as final.
you should change your model (in your case "monthlyspendings") at the selected item position.
then notify the adapter
adapter.notifyDataSetChanged()
and finally, custom selected view according to the model. for exapmle :
if(monthlySpendings.get(position).isSelected) monthlySpendCard.setBackgroundColor(Color.parseColor("#fcd14b"));
I am adding dynamic imagebuttons to a layout in a Fragment using recyclerview with gridlayoutmanager. The buttons are added as a user performs an action, so they are not all created during onCreateView().
I seem to have to initialize my recycler view and adapter onCreateView() with a an empty imagebutton, when my app starts up. And so I do that, but it creates a little grey square like the image below.
and then when the user performs an action and the real button I want is created, the square is still present over my image button that was just created like you can see in this new image below.
DOES ANYONE KNOW HOW I CAN GET THE INITIAL EMPTY GREY IMAGE BUTTON TO NOT BE THERE AT STARTUP OR OVER MY "DriveDroid" IMAGE BUTTON?
I have tried setting the initial background of my image button to transparent (using background color = #00000000), but this seems to make a transparent image button over my DriveDroid button, so that my onClick listener for DriveDroid no longer works.
Here is how I initialize my recycler view:
public class MyFragment extends Fragment {
private GridLayoutManager lLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment, container, false);
// Create an empty list to initialize the adapter (or else get nullPointerException error)
List<ItemObject> myList = new ArrayList<ItemObject>();
lLayout = new GridLayoutManager(getActivity(), 4, GridLayoutManager.HORIZONTAL, false);
RecyclerView rView = (RecyclerView)view.findViewById(R.id.recycler_view);
rView.setHasFixedSize(true);
rView.setLayoutManager(lLayout);
RecyclerViewAdapter rcAdapter = new RecyclerViewAdapter(getActivity(),myList);
rView.setAdapter(rcAdapter);
return view;
}
And here is my layout my_fragment
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_margin="5dp"
android:id="#+id/my_fragment"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="horizontal" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/new_app_button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/new_app_name"
android:layout_below="#+id/new_app_button"
android:layout_alignStart="#+id/new_app_button"
android:layout_alignLeft="#+id/new_app_button"
android:gravity="center"
/>
</RelativeLayout>
HERE IS HOW I CHANGED MY LAYOUT TO GET IT TO WORK, IF IT HELPS ANYONE ELSE!
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:layout_marginTop="15dp"
android:id="#+id/my_fragment"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scrollbars="horizontal" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/new_app_button"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/new_app_name"
android:gravity="center"
android:layout_below="#+id/new_app_button"
/>
</RelativeLayout>
</LinearLayout>
As mentioned in the comment, that your controls was overlapping each others, you should wrap them inside linear layout or add properties to make them positioned relative to each others.
Regarding the grey button this should be related to "new_app_button"
i'm developing an android project which it has a viewpager. i wrote an adapter for my viewpager and my adapter consist of s scrollview and some views inside that.
i want when user click on the scrollview, something happen (i wrote this part too and test it and it works).
i implement onClickListener for my scrollview but it's not triggered when user click on that.
i have already read this but it's not work for me.
my code for onClickListener is here
rootView = inflater.inflate(R.layout.level_selector_list_view_adapter, container, false);
ScrollView scroll = (ScrollView) rootView.findViewById(R.id.level_selector_view_scroll_view);
scroll.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View arg0)
{
Log.e(MainActivity.WATERMELON_TAG, "Click!!!");
}
});
and my layout code is this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="#dimen/level_selector_values_main_page_view_pager_width"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ScrollView
android:id="#+id/level_selector_view_scroll_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
tools:ignore="UselessParent" >
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
tools:ignore="UselessParent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:clickable="false"
android:orientation="vertical" >
<ImageView
android:id="#+id/level_selector_view_pager_adapter_level_logo"
android:layout_width="#dimen/level_selector_values_view_pager_adapter_level_logo_width"
android:layout_height="#dimen/level_selector_values_view_pager_adapter_level_logo_height"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/level_selector_values_view_pager_adapter_level_logo_top_margin"
android:clickable="false"
android:scaleType="fitXY"
tools:ignore="ContentDescription" />
<TextView
android:id="#+id/level_selector_view_pager_adapter_level_name"
android:layout_width="#dimen/level_selector_values_view_pager_adapter_level_name_width"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/level_selector_values_view_pager_adapter_level_name_top_margin"
android:clickable="false"
android:gravity="center"
android:textColor="#color/level_selector_values_level_name_font_color"
android:textSize="#dimen/level_selector_values_level_name_font_size" />
<TextView
android:id="#+id/level_selector_view_pager_adapter_level_score"
android:layout_width="#dimen/level_selector_values_view_pager_adapter_level_score_width"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="#dimen/level_selector_values_view_pager_adapter_level_score_top_margin"
android:clickable="false"
android:gravity="center"
android:textColor="#color/level_selector_values_level_name_font_color"
android:textSize="#dimen/level_selector_values_font_size" />
</LinearLayout>
<ImageView
android:id="#+id/level_selector_view_pager_adapter_lock"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:alpha="0.75"
android:clickable="false"
android:scaleType="fitXY"
android:src="#drawable/lock"
tools:ignore="ContentDescription" />
</FrameLayout>
</ScrollView>
The child Views of your ScrollView are consuming the click events you do on the ScrollView.
The solution to this problem is to set the onClickLIstener to the immediate child of the ScrollView, the FrameLayout in your case.
Don't forget to set the layout to "clickable".
Furthermore, I recommend not to use the FrameLayout at all, since it is just a useless container. Instead, just let the LinearLayout that is currently the child of the FrameLayout be the next child to the ScrollView.
try:
public class ClassName implements OnClickListener {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.fragment_child, container, false);
ScrollView scrollView= (ScrollView) root.findViewById(R.id.my_ScrollView);
scrollView.setOnClickListener(this);
return root;
}
#Override
public void onClick(View v) {
Log.e(MainActivity.WATERMELON_TAG, "Click!!!");
}
}
I have SupportMapFragment and I need to add custom controls into it for changing a map type. Calling getView(), I get NoSaveStateFramelayout and I don't think it is a good idea to add it directly into it or its children.
What is the best way how can I can add a button over my map for changing the map type?
I have decided to override onCreateView and encapsulate the map in the code.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
View mapView = super.onCreateView(inflater, viewGroup, bundle);
RelativeLayout view = new RelativeLayout(getActivity());
view.addView(mapView, new RelativeLayout.LayoutParams(-1, -1));
// working with view
return view;
}
And it works as I needed.
Try to put your map inside of a Frame Layout with Relative layout together when the map and the relative layout match_parent for the width and the height. In the relative layout place all your desired extended controls. That way the will sit on top on the map.
some thing like that:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/title_gray_background" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="10dp"
android:text="#string/tasks"
android:paddingRight="3dp"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#color/my_even_darker_gray" />
<Button
android:id="#+id/bLocateMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
android:layout_marginRight="3dp"
android:background="#drawable/locate_me_button_selector"
android:clickable="true"
android:onClick="locateMeButtonOnClick"
android:text="#string/locate_me"
android:textColor="#color/my_white" />
</RelativeLayout>
<fragment
xmlns:map="http://schemas.android.com/apk/res-auto"
android:id="#+id/map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>
The view returned from onCreateView of MapFragment is a FrameLayout. So let use it, no need to add another layout (we will reduce view hierarchy).
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup viewGroup, Bundle bundle) {
FrameLayout mapView = (FrameLayout) super.onCreateView(inflater, viewGroup, bundle);
mapView.add(someView);
return view;
}
Use this code to add you layout above of the map
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mapView = super.onCreateView(inflater, container, savedInstanceState);
#SuppressLint("InflateParams")
FrameLayout view = (FrameLayout) inflater.inflate(R.layout.fragment_map, null);
view.addView(mapView, 0, new RelativeLayout.LayoutParams(-1, -1));
return view;
}
I did the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<!--suppress AndroidDomInspection -->
<fragment
android:id="#+id/map"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
>
<ImageButton
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="10sp"
android:layout_marginTop="10sp"
android:src="#android:drawable/ic_menu_mylocation"
/>
</RelativeLayout>