Avoid showing selected text in Spinner - android

The default behavior of the android spinner is to initially display the first item in the top of the dropdown portion of the spinner. As the user selects other items/text in the dropdown, it will update the text at the top with the selected item.
I don't want any text to show up at all in the top part of the spinner. Without having an empty sting as the first item, because that leaves a gap. Does anyone know how to do that? This is what I have so far
styles.xml
<resources>
<style parent="#android:style/Widget.TextView.PopupMenu" name="SpinnerAsEditText">
<item name="android:background">#drawable/filter_arrow</item>
</style>
</resources>
this is my layout: main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:layout_height="match_parent">
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:entries="#array/list"
style="#style/SpinnerAsEditText"/>
</RelativeLayout>
This is a visual representation of my problem - if you click on this image/gif, it should animate my problem.

You can use popUpWindow to show dropDownList.
<ImageView
android:id="+#id/spinnerImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showSpinner" />
in java
findViewById(R.id.spinnerImage).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mSortPopupWindow = popupWindow(spinnerItemList);
mSortPopupWindow.showAsDropDown(v, -5, 0); // This is impotent to where to show drop down
}
});
public PopupWindow popupWindow(String[] stringArray) {
PopupWindow popupWindow = new PopupWindow(mContext);
ListView listView = new ListView(mContext);
listView.setAdapter(dropDownAdapter(stringArray));
listView.setOnItemClickListener( AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(mContext, "Selected" + stringArray[position], Toast.LENGTH_SHORT).show();
popupWindow.dismiss();
}
});
popupWindow.setFocusable(true);
popupWindow.setWidth(250);//Or you can set wrap_content
popupWindow.setHeight(WindowManager.LayoutParams.WRAP_CONTENT);
popupWindow.setContentView(listView);
return popupWindow;
}

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:layout_height="match_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/list"
style="#style/SpinnerAsEditText"/>
</RelativeLayout>
<?xml version="1.0" encoding="utf-8"?>
<resources>
<array name="list">
<item></item>
<item>second</item>
<item>third</item>
<item>fourth</item>
<item>fifth</item>
</array>
</resources>

You could use an image which when clicked on shows the spinner. Initially the spinner would be hidden.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:layout_height="match_parent">
<ImageView
android:id="+#id/spinnerImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="showSpinner" />
<Spinner
android:id="+#id/spinner"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:entries="#array/list"
style="#style/SpinnerAsEditText"
android:visibility="hidden"/>
and in Java:
void showSpinner() {
findViewById(R.id.spinnerImage).setVisibility(View.GONE);
findViewById(R.id.spinner).setVisibility(View.VISIBLE);
}

Related

Android - Listview items change background when selected (checkable)

How can I make a ListView item changed its background when pressed, like it was checked but no checkbox, I know it has a checkListener or something but i don't know how to apply. Remain its color background after pressed and changed back to its first background when pressed again.
Your listItem.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:attr/activatedBackgroundIndicator"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_list_item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
</LinearLayout>
Your selector: listview_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android"
android:exitFadeDuration="#android:integer/config_mediumAnimTime">
<item android:drawable="#drawable/list_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/list_selected" android:state_activated="true"/>
<item android:drawable="#drawable/list_default"/>
</selector>
Your Layout with listivew:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listViewMainFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:listSelector="#drawable/listview_selector.xml"
android:choiceMode="singleChoice">
</ListView>
</LinearLayout>
1) Create boolean field in your Model-Class.
2) Define root Layout of item (create id and find view in ListView Adapter's getView() method).
3) Add OnClickListener for root Layout, if clicked new boolean = true.
4) If new boolean of your Model-Class item is true, set root Layout bg_color_1, else set bg_color_2.
Try this
listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
view.setBackgroundColor(Color.GREEN);
}
});

remove black border from inflated layout

I am using PopupWindow, which is working fine, but the problem is after inflating the layout view that is shown with black border.
final PopupWindow popupWindow = new PopupWindow(context);
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.pop_up_window_menu_layout, null);
popupWindow.setContentView(view);
pop_up_window_menu_layout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/solid_white"
android:orientation="vertical" >
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:gravity="center_vertical"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:text="SAVE"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/black"
android:textSize="#dimen/txt_size_sliding_list" />
I've tried to remove border by using popupWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)), but problem is still same.
Can anyone tell me how to remove unwanted black border?
I don't see any border around my popup windows, but I do set them up slightly different than you. Hopefully this points you in the right direction.
popup_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:background="#f5f5f5"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/btn_close"
android:text="Close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:id="#+id/main_view"
tools:context=".MainActivity">
<Button
android:id="#+id/btn_popup"
android:text="Popup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt_text"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
MainActivity.java
public class MainActivity extends ActionBarActivity implements View.OnClickListener {
View popupView;
Button btnPopup;
Button btnClose;
PopupWindow popupWindow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
popupView = getLayoutInflater().inflate(R.layout.popup_view, null);
btnClose = (Button) popupView.findViewById(R.id.btn_close);
btnClose.setOnClickListener(this);
popupWindow = new PopupWindow(popupView,
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
btnPopup = (Button) findViewById(R.id.btn_popup);
btnPopup.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if (v.getId() == R.id.btn_popup) {
popupWindow.showAtLocation(findViewById(R.id.main_view), Gravity.NO_GRAVITY, 100, 200);
//popupWindow.showAsDropDown(btnPopup, 10, 10);
} else if (v.getId() == R.id.btn_close) {
popupWindow.dismiss();
}
}
}
If you getting a black border this means that the default background is set.
this is how you remove it :
popup.setBackgroundDrawable(null);
Try this
Just try this and let me know..
Be sure to create your Pop-up window referencing your custom theme:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="DialogTheme" parent="android:Theme.Dialog">
<!-- Fill the screen -->
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<!-- No backgrounds and No titles , and no Window Floating -->
<item name="android:windowBackground">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<!-- Set background what you want-->
<item name="android:background">#ff0000</item>
</style>
</resources>
This is because you are sending context inside popup window constructor. -
final PopupWindow popupWindow = new PopupWindow(context);
Change this by popup window view -
View popupView = getLayoutInflater().inflate(R.layout.popup_view, null);
PopupWindow popupWindow = new Pop

How to set ListView selected item alternet text color in android

I have a custom listview with a imageview and an textview. I want when user select a item the textview color should change and all the other textview should remain default.
here are my xmls
ListView.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/listViewBell"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#00000000"
android:choiceMode="singleChoice"
android:divider="#b5b5b5"
android:dividerHeight="1dp"
android:listSelector="#drawable/list_selector" />
</LinearLayout>
ListViewItems.xml
<?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="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<TextView
android:id="#+id/txt_bell_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/rihanna_love_the_way_lie"
android:textColor="#color/black"
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
<ImageView
android:id="#+id/list_bell_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:src="#drawable/bronze_bell" />
</RelativeLayout>
Create following button_text.xml in drawable
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:color="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
Change your text view's textColor to:
<TextView
android:id="#+id/txt_bell_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="#string/rihanna_love_the_way_lie"
android:textColor="#color/button_text" //point to that xml
android:textSize="15sp"
android:textStyle="bold"
android:typeface="sans" />
You can read about color state list here
Like others have said, just change the color in your onItemClick method. Additionally:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
setItemNormal();
View rowView = view;
setItemSelected(rowView);
}
public void setItemSelected(View view){
View rowView = view;
TextView tv = (TextView)rowView.findViewById(android.R.id.text1);
tv.setTextColor(Color.BLUE);
}
public void setItemNormal()
{
for (int i=0; i< mDrawerList.getChildCount(); i++)
{
View v = mDrawerList.getChildAt(i);
//TextView txtview = ((TextView) v.findViewById(R.id.menurow_title));
TextView txtview = ((TextView)v.findViewById(android.R.id.text1));
txtview.setTextColor(Color.BLACK);
}
}
by doing this we set all items to normal before we change it on each item press.
For that you've to set OnClickListener for that particular ImageView and then change TextView's color on click of ImageView in Adapter Class.
Alternatively,
mList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int pos, long id) {
TextView tv= view.findViewById(resID);
tv.setTextColor(color)
}
});
Implement setOnItemClickListener for the listView
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int position,
long arg3) {
RelativeLayout relativeLayout = (RelativeLayout) view.getParent();
TextView textView = (TextView) relativeLayout.getChildAt(0);
ImageView imaegView = (ImageView) relativeLayout.getChildAt(1);
textView.setTextColor(Color.RED);
// Perform whatever action for your textView and imageView
}
});
after many attempts set color in navigation view, I found out the simplest way to set text color in listview, maybe it will help to someone
res/color/listview_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<!-- This is used when the navigation item is selected -->
<item android:state_enabled="true"
android:state_activated="true" android:color="#color/colorAccent" />
<!-- This is the default text color -->
<item
android:color="#android:color/black" />
</selector>
and set in TextView as android:textColor:
<TextView
android:id="#+id/tvItem"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_vertical_extra_margin"
android:textColor="#color/listview_selector"
android:textSize="#dimen/text_size_normal"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
tools:text="Row item text" />
the trouble was in the right state, use android:state_activated in navigation view

Button under ListView in AlertDialog is hidden

I want to show a list with a button under it in an AlertDialog. The Button is to close the Dialog itself. I extended AlertDialog and added some LayoutParams to the Window to extend the Dialog of the whole screen width:
//Grab the window of the dialog, and change the width
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
Window window = getWindow();
lp.copyFrom(window.getAttributes());
//This makes the dialog take up the full width
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
window.setAttributes(lp);
ok. But if the list is long (display is full of it), I can scroll the ListView but the button is not shown under it. Here's the ContentView of the Dialog:
<?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">
<ListView
android:id="#+id/choose_equipment_list"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:layout_below="#+id/choose_equipment_list"
android:id="#+id/btn_choose_equipment_close"
style="#style/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close" />
</RelativeLayout>
What can I do to show the button ALWAYS under the list, no matter how long it is? (I read a lot warnings to put a ListView inside a ScrollView, tried it anyway and failed...)
Try it:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ListView
android:id="#+id/choose_equipment_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
<Button
android:id="#+id/btn_choose_equipment_close"
style="#style/custom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="close" />
</LinearLayout>
If not work, remove the style="#style/custom_button" line. Some style configuration could change the view style and hide the button.
I want to show a list with a button under it in an AlertDialog.
So What you have to do is :
Remove this from from your Button:
android:layout_below="#+id/choose_equipment_list"
Then put this in Button:
android:layout_alignParentBottom="true"
And Put this in ListView:
android:layout_above="#+id/btn_choose_equipment_close"
You are Done.
Hmm. Maybe try to use Fragments. I coded sample for you. Tell me if it is what you are looking for:)
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_activity);
findViewById(R.id.open_dialog_button).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SampleDialogFragment dialogFragment = new SampleDialogFragment();
dialogFragment.show(getSupportFragmentManager(), "dialogFragment");
}
});
}
}
main_activity.xml
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<Button
android:id="#+id/open_dialog_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Open dialog"
android:layout_gravity="center" />
</FrameLayout>
SampleDialogFragment.java
public class SampleDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle("Timer");
alert.setPositiveButton("OK", null);
View view = View.inflate(getActivity(), R.layout.dialog, null);
String[] array = {"Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello", "Hello"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(), android.R.layout.simple_list_item_1, array);
((ListView) view.findViewById(R.id.listView)).setAdapter(adapter);
alert.setView(view);
return alert.create();
}
}
dialog.xml
<ListView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/listView"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
I think it could be possible to get what you need declaring in the XML your button first located in the bottom and then the listview above it with height set to fill_parent
This is should be what you want...
<?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">
<ListView
android:id="#+id/choose_equipment_list"
android:layout_above="#+id/btn_choose_equipment_close"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btn_choose_equipment_close"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="close" />
</RelativeLayout>

Adding a vertical scrollbar to an AlertDialog in Android?

I would like to add a vertical scrollbar to an AlertDialog because my text is too long to display on 1 screen:
I have tried to use :
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
but the scrollbars don't even display ?
Here's the xml layout file I'm using:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:id="#+id/instructions_view" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A LONG TEXT 1"/>
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A LONG TEXT 2"/>
</LinearLayout>
I call the AlertsDialog with :
public void onClick(View v) {
switch(v.getId()){
case R.id.Button_Instructions:
InstructionsDialog();
break;
case R.id.Button_Exit:
ExitDialog();
break;
}
}
public void InstructionsDialog(){
AlertDialog.Builder ad = new AlertDialog.Builder(this);
ad.setIcon(R.drawable.icon);
ad.setTitle("Instructions ...");
ad.setView(LayoutInflater.from(this).inflate(R.layout.instructions_dialog,null));
ad.setPositiveButton("OK",
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int arg1) {
// OK, go back to Main menu
}
}
);
ad.setOnCancelListener(new DialogInterface.OnCancelListener(){
public void onCancel(DialogInterface dialog) {
// OK, go back to Main menu
}}
);
ad.show();
}
I found the answer now=> IT WORKS NOW WITH THIS :
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:id="#+id/instructions_view" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A LONG TEXT 1"/>
<TextView
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="A LONG TEXT 2"/>
</LinearLayout>
</ScrollView>
In order for a view to scrollable, it must be nested inside of a ScrollView container:
<ScrollView>
<LinearLayout android:orientation="vertical"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true">
<TextView />
<Button />
</LinearLayout>
</ScrollView>
Note that a ScrollView container can only have one child layout view. It is not possible, for example, to place a TextView and Button in a ScrollView without the LinearLayout.
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("YOUR_TITLE")
.setMessage("YOUR_MSG")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
})
.setIcon(android.R.drawable.ic_dialog_info)
.show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setMaxLines(5);
textView.setScroller(new Scroller(this));
textView.setVerticalScrollBarEnabled(true);
textView.setMovementMethod(new ScrollingMovementMethod());
All code found here is deprecated.
You should now use (in the layout file for your dialog)
NestedScrollView
<?xml version="1.0" encoding="utf-8"?>
<androidx.core.widget.NestedScrollView 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:fillViewport="true"
android:scrollbars="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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:padding="16dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.core.widget.NestedScrollView>
Besides, it is no more recommanded to use AlertDialog alone. You have to implement your own DialogFragment by extending DialogFragment.
See : https://developer.android.com/guide/topics/ui/dialogs
Then you can call your dialog fragment :
MyDialogFragment dialogFragment = MyDialogFragment.newInstance();
dialogFragment.show(fragment.getFragmentManager(), MyDialogFragment.TAG);
Using this it works :
.setScrollable(true)
Use this in your AlertDialogBox or MaterialStyledDialog.
private void gettermsandconditions() {
final MaterialStyledDialog dialogHeader_1 =
new MaterialStyledDialog.Builder(this)
.setTitle("Terms and Conditions !")
// .setDescription("What can we improve? Your feedback is always welcome.")
.setDescription(R.string.Terms_and_condition)
.setIcon(R.drawable.bill_icon)
.setStyle(Style.HEADER_WITH_ICON)
.setHeaderColor(R.color.colorPrimary)
.withDarkerOverlay(true)
.setScrollable(true)
.onPositive(new MaterialDialog.SingleButtonCallback() {
#Override
public void onClick(#NonNull MaterialDialog dialog, #NonNull DialogAction which) {
// startActivity(new Intent(Intent.ACTION_VIEW, Uri.parse("https://play.google.com/store/apps/details?id=" + context.getPackageName())));
}
})
.setNegativeText("Ok")
.build();
//.setStyle(Style.HEADER_WITH_TITLE)
dialogHeader_1.show();
}
On Android Q the scrollbars can be made always visible.
After creating and showing the dialog, find the TextView holding the message and modify it like this. (BTW the call to setMaxLines(5) is just to limit the number of visible lines to demonstrate the scrolling. Set it as you will, or remove it):
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setMaxLines(5);
textView.setVerticalScrollBarEnabled(true);
textView.setMovementMethod(new ScrollingMovementMethod());
textView.setScrollBarStyle(View.SCROLLBARS_INSIDE_INSET);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
textView.setScrollbarFadingEnabled(false);
textView.setVerticalScrollbarTrackDrawable(m_mainActivity.getResources().getDrawable(R.drawable.scrollbar_track_vertical));
textView.setVerticalScrollbarThumbDrawable(m_mainActivity.getResources().getDrawable(R.drawable.scrollbar_thumb_vertical));
}
Two drawables are needed.
For app/src/main/res/drawable/scrollbar_thumb_vertical.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="0"
android:endColor="#D8D8D8"
android:startColor="#F7F7F7" />
<corners android:radius="6dp" />
</shape>
And for app/src/main/res/drawable/scrollbar_thumb_vertical.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient
android:angle="0"
android:endColor="#757575"
android:startColor="#A0A0A0" />
<corners android:radius="6dp" />
</shape>

Categories

Resources