How to reduce height of list item in dialog box? - android

I have list in AlertDialog. I used setItems to show array for each list item. But, the height of each item in the list is too big. Is there anyway to reduce height of the list item in dialog box?

you must create XML layout lets called row .xml contain list component,
then you will inflater the row layout ,
after you can change list height inside dialog from xml layout as you wish .
you can try this :
Dialog dialog = new Dialog(context); LayoutInflater inflater =
context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate
(R.layout.row, null, false); dialog .setContentView(v);
dialog .show();
then your xml layout :
<TexView
android:id="#+id/YOUR LIST"
android:layout_width="fill_parent"
android:layout_height="your height" >
</TextView>
hope this help

Related

Customize spinner dialog: popupbackground setting for dialog

I have a spinner as shown below. While I can style the row("raspberrypi-0"), I am unable to change anything for the rest of the dialog.
I would like to change the popup background color. If possible the color of "Select a device for the sensor" should also be changed. Below is my spinner's xml defined inside a linear layout(not shown).
<fr.ganfra.materialspinner.MaterialSpinner
android:id="#+id/attached_device_value_dropdown"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:spinnerMode="dialog"
app:ms_alignLabels="false"
app:ms_arrowSize="#dimen/spinner_arrow_size"
app:ms_hint="#string/attached_device_select"
app:ms_enableErrorLabel="true"
app:ms_multiline="true"
app:ms_floatingLabelColor="#color/black"
app:ms_hintColor="#color/light_grey"
app:ms_baseColor="#color/black"
app:ms_highlightColor="#color/black"
app:ms_arrowColor="#color/black"/>
A similar setting: popupBackground exist when one sets spinner mode to "dropdown". I am looking for something similar to this but for dialog.
In Java, this is how I set my spinner:
CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(parentActivity, R.layout.spinner_dropdown_item, devices);
customArrayAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
registeredDevicesDropdown.setAdapter(customArrayAdapter);
where spinner_dropdown_item is just a text view.
Also MaterialSpinner just extends Spinner, so most of the things should apply. Here is more info about it on github.
I solved it by hacking the adapter code for the MaterialSpinner. In your overridden getCustomView function, set the color for background and text.
public View getCustomView(int position, View convertView, ViewGroup parent, boolean isDropDownView) {
//TODO: use convert view.
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final int resid = isDropDownView ? android.R.layout.simple_spinner_dropdown_item : android.R.layout.simple_spinner_item;
TextView row = (TextView) inflater.inflate(resid, parent, false);
row.setText(devices.get(position).getDeviceLabel());
//Hack: Cannot set the dialogs color from XML and text color from XML
row.setTextColor(ContextCompat.getColor(parentActivity, R.color.black)); //set Text Color for the row content
parent.setBackgroundColor(ContextCompat.getColor(parentActivity, R.color.colorSecondary)); //set backgraound color ofr skeleton.
return row;
}

Android :How to Spinner Look like textView and Onclick on spinner open dialog with custom listview

Please anyone can tell me how to design this layout with spinner
this is my 1st screen with spinner
this is my screen which open the dialog box with custon listview onclick of select city spinner
and onselecting Dialog item it display in 1st screen which is functionality of spinner ..
Please anyone can suggest me how to do this ...
Thanks in advance..
For a quick fix:
Create a custom relative layout and put a spinner to the place where writes pune on first screen. Make spinners background transparent or the same color as relative layout.
Set spinners mode dialogMode from xml:
android:spinnerMode="dialog"
Set a new onClickListener to your relative layout and put in:
mySpinner.performClick();
Finally in your spinner adapter define your custom spinner row layout like in second screen.
you can follow below steps.
1) Have a TextView instead of Spinner, RegisterOnclick Listener on it.
2) on onClick event, start a dialog with list view. Tutorial
3) get Item Selected from Listview/Dialog, set it again in listview.
Specify your layout with text view in spinner_layout.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" >
<TextView
android:id="#+id/id_of_your_textView"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
In Adapter class do:
//members
protected Context context;
protected LayoutInflater inflater;
//constructor
public CustomArrayAdapter(Context context) {
super(context, resource);
this.context = context;
inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
//override method getView
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View element = inflater.inflate(R.layout.spinner_layout, parent, false);
TextView textView = (TextView)element.findViewById(R.id.id_of_your_textView);
textView.setText("blah-blah");
return element;
}
It should be enough.
If your spinner items should show complex item, do not forget to override
getDropDownView(int position, View convertView, ViewGroup parent) method.

Button inside AlertDialog Android

I have a AlertDialog.Builder in my Fragment Page and it shows three EditText (in a xml file).
Now I'd like to add a button at the top of the AlertDialog and on its click change the text of two of them.
It is like:
Button : Get coordinates
1st EdiText : latitude
2st EdiText : longitude
If you want a button added in xml file to be displayed in AlertDialog, just inflate the layout
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setCancelable(false);
builder.setView(layout);
where "layout" is inflated view
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.activity_filename,null); // specify your xml layout here
then use "layout" to get id of the button
Button btn=(Button)layout.findViewById(R.id.getCoordBtn);
add click listener to it and perform whatever you want

Add a layout inside the list view

I wanted to display a web view in between in the list view and bottom of the list view.
I was able to add footer to the list view to display the web view in the bottom, but I don't know how to display in the middle
Code where I have added the footer:
ListView list = (ListView)view.findViewById(android.R.id.list);
WebView web;
LayoutInflater inflaterOf = getActivity().getLayoutInflater();
LinearLayout listFooterView =
(LinearLayout)inflaterOf.inflate(R.layout.list_footer, null);
web = (WebView)listFooterView.findViewById(R.id.webview_footer);
web.getSettings().setJavaScriptEnabled(true);
web.loadUrl("file:///android_asset/news_footer.html");
list.addFooterView(listFooterView);
Please let me know how to insert the layout with web view in-between the list view
You should create a custom object and in your ArrayAdapter of listview, change the layout to inflate when getCount is equal to list.size()/2.
That's the way I'll handle this.

How to inflate one view with a layout

I have a layout defined in XML. It contains also:
<RelativeLayout
android:id="#+id/item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
I would like to inflate this RelativeView with other XML layout file. I may use different layouts depending on a situation. How should I do it? I was trying different variations of
RelativeLayout item = (RelativeLayout) findViewById(R.id.item);
item.inflate(...)
But none of them worked fine.
I'm not sure I have followed your question- are you trying to attach a child view to the RelativeLayout? If so you want to do something along the lines of:
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
item.addView(child);
You inflate an XML resource. See the LayoutInflater doc .
If your layout is in a mylayout.xml, you would do something like:
View view;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
RelativeLayout item = (RelativeLayout) view.findViewById(R.id.item);
Though late answer,
but would like to add that one way to get this
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(R.layout.mylayout, item );
where item is the parent layout where you want to add a child layout.
It's helpful to add to this, even though it's an old post, that if the child view that is being inflated from xml is to be added to a viewgroup layout, you need to call inflate with a clue of what type of viewgroup it is going to be added to. Like:
View child = getLayoutInflater().inflate(R.layout.child, item, false);
The inflate method is quite overloaded and describes this part of the usage in the docs. I had a problem where a single view inflated from xml wasn't aligning in the parent properly until I made this type of change.
Even simpler way is to use
View child = View.inflate(context, R.layout.child, null)
item.addChild(child) //attach to your item
Try this code :
If you just want to inflate your layout :
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,null); // Code for inflating xml layout
RelativeLayout item = view.findViewById(R.id.item);
If you want to inflate your layout in container(parent layout) :
LinearLayout parent = findViewById(R.id.container); //parent layout.
View view = LayoutInflater.from(context).inflate(R.layout.your_xml_layout,parent,false);
RelativeLayout item = view.findViewById(R.id.item); //initialize layout & By this you can also perform any event.
parent.addView(view); //adding your inflated layout in parent layout.
layout inflation
View view = null;
LayoutInflater inflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.mylayout, null);
main.addView(view);
If you're not in an activity you can use the static from() method from the LayoutInflater class to get a LayoutInflater, or request the service from the context method getSystemService() too :
LayoutInflater i;
Context x; //Assuming here that x is a valid context, not null
i = (LayoutInflater) x.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//OR
i = LayoutInflater.from(x);
(I know it's almost 4 years ago but still worth mentioning)
AttachToRoot Set to True
Just think we specified a button in an XML layout file with its layout width and layout height set to match_parent.
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/custom_button">
</Button>
On This Buttons Click Event We Can Set Following Code to Inflate Layout on This Activity.
LayoutInflater inflater = LayoutInflater.from(getContext());
inflater.inflate(R.layout.yourlayoutname, this);
Hope this solution works for you.!
If you want to add a single view multiple time then you have to use
layoutInflaterForButton = getActivity().getLayoutInflater();
for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);
btnContainer.addView(btnView);
}
If you do like
layoutInflaterForButton = getActivity().getLayoutInflater();
FrameLayout btnView = (FrameLayout) layoutInflaterForButton.inflate(R.layout.poll_button, null);
and
for (int noOfButton = 0; noOfButton < 5; noOfButton++) {
btnContainer.addView(btnView);
}
then it will throw exception of all ready added view.
If you are you trying to attach a child view to the RelativeLayout? you can do by following
RelativeLayout item = (RelativeLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, item, true);
I had the hardest time with this error, because of my unique circumstances, but finally found a solution.
My situation: I am using a separate view (XML) which holds a WebView, then opens in an AlertDialog when I click a button in my main activity view. But somehow or another the WebView belonged to the main activity view (probably because I pull the resource from here), so right before I assigned it to my AlertDialog (as a view), I had to get the parent of my WebView, put it into a ViewGroup, then remove all the views on that ViewGroup. This worked, and my error went away.
// set up Alert Dialog box
AlertDialog.Builder alert = new AlertDialog.Builder(this);
// inflate other xml where WebView is
LayoutInflater layoutInflater = (LayoutInflater)this.getSystemService
(Context.LAYOUT_INFLATER_SERVICE);
View v = layoutInflater.inflate(R.layout.your_webview_layout, null);
final WebView webView = (WebView) v.findViewById(R.id.your_webview_id);
// more code...
.... later on after I loaded my WebView ....
// first, remove the parent of WebView from it's old parent so can be assigned a new one.
ViewGroup vg = (ViewGroup) webView.getParent();
vg.removeAllViews();
// put WebView in Dialog box
alert.setView(webView);
alert.show();
With Kotlin, you can use:
val content = LayoutInflater.from(context).inflate(R.layout.[custom_layout_name], null)
[your_main_layout].apply {
//..
addView(content)
}
When add a layout to an Activity in Kotlin, see these steps:
Add just in Activity - One layout as a parent
Xml file of new
Layout Give the value of R.
val parent: LinearLayout =findViewById(R.id.stars)
val view =
LayoutInflater.from(applicationContext).inflate(R.layout.another, parent,false)
Where parent is not necessary, can be null But warning message will be appear
view.findViewById<ImageView>(R.id.ivTimer).setImageResource(R.drawable.t2)
Any view must be set value in this way, finally add
parent.apply { addView(view)}
I had used below snippet of code for this and it worked for me.
LinearLayout linearLayout = (LinearLayout)findViewById(R.id.item);
View child = getLayoutInflater().inflate(R.layout.child, null);
linearLayout.addView(child);

Categories

Resources