Adding vertical scroll bar to an alert dialog - android

I am making an alert dialog in an application.Because of more text present in the alert dialog,i want to put vertical scroll bar in the alert dialog.I have tried many options,But nothing is working.Please tell me how to solve this problem.This is the code :
AlertDialog.Builder HelpOnButtonDialog ;
HelpOnButtonDialog = new AlertDialog.Builder(this);
TextView HelpOnButtonView = new TextView(this);
HelpOnButtonView.setSingleLine(false);
HelpOnButtonView.setTextColor(getResources().getColor(R.color.dark_green));
HelpOnButtonView.setText("hello");
Button HelpOnButton = new Button(this);
HelpOnButton.setHeight(20);
HelpOnButton.setWidth(20);
HelpOnButton.setText("Ok");
HelpOnButton.setOnClickListener(HelpOnButtonClickListener);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setLayoutParams( new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
linearLayout.setOrientation(1);
linearLayout.setBackgroundColor(getResources().getColor(R.color.black));
linearLayout.addView(HelpOnButtonView);
linearLayout.addView(HelpOnButton);
ScrollView sv = new ScrollView(this);
sv.pageScroll(0);
sv.setBackgroundColor(0);
sv.setScrollbarFadingEnabled(true);
sv.setVerticalFadingEdgeEnabled(false);
sv.addView(linearLayout);
alertHelp = HelpOnButtonDialog.create();
alertHelp.setView(sv);
alertHelp.show();

Create a custom dialog you can customize it as you wish. In the dialog box layout xml file
surround your RelativeLayout or LinearLayout with scrollable like below code
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="vertical"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:orientation="vertical"
android:layout_weight="1">
<!--Your other text views, buttons... etc surrounded by RelativeLayout
or LinearLayout goes here-->
</ScrollView>
When the content exceeds dlalog box size it will display the scroll bar.
Hope this will help you :)

Related

Android - Wrap buttons within view

I am trying to make buttons wrap in a LinearLayout in Android, but they are just continuing off to the right of the view (the word shown in the screenshot should be "HELLO", so I want the "O" to drop down to the next line).
I am adding the buttons programmatically, but even if I code them into the XML layout file, they still don't wrap. Here is the layout file with the LinearLayout container into which I am dynamically adding the buttons:
<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"
app:layout_constrainedWidth="true"
tools:context=".LetterTileView">
<LinearLayout
android:id="#+id/TilesContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constrainedWidth="true"
android:orientation="horizontal">
</LinearLayout>
And here is the code I am using to create and add the tile buttons:
Context context = this;
LinearLayout layout = (LinearLayout) findViewById(R.id.TilesContainer);
LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT );
params.setMargins(50, 50, 0, 0);
for (int i=0;i<wordLength;i++) {
Button tileButton = new Button(this);
tileButton.setLayoutParams(params);
tileButton.setText(wordStringtoLetters[i]);
tileButton.setId(i);
tileButton.setBackgroundResource(R.drawable.tile_button);
tileButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 36);
layout.addView(tileButton);
}
Any advice would be appreciated. Thanks!
I ended up using FlexboxLayout, which works great for this. Thanks to those who offered suggestions!
First of all, there is no need to use a ConstraintLayout you can use your LinearLayout as the parent layout.
Then for the purpose of displaying all buttons in one line, you have to set weight for the LinearLayout in XML and set weight for the views you add to it.
The xml file should look like:
<LinearLayout
android:id="#+id/TilesContainer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:weightSum="5"
app:layout_constrainedWidth="true"
android:orientation="horizontal">
</LinearLayout>
And in code you should set weight for each view you by adding ,1.0f to LayoutParam :
Context context = this;
LinearLayout layout = (LinearLayout) findViewById(R.id.TilesContainer);
LayoutParams params = new LayoutParams( LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT,1.0f );
params.setMargins(50, 50, 0, 0);
for (int i=0;i<wordLength;i++) {
Button tileButton = new Button(this);
tileButton.setLayoutParams(params);
tileButton.setText(wordStringtoLetters[i]);
tileButton.setId(i);
tileButton.setBackgroundResource(R.drawable.tile_button);
tileButton.setTextSize(TypedValue.COMPLEX_UNIT_SP, 36);
layout.addView(tileButton);
}

Dynamically added ListView shows only the first item

I'm trying to generate a dynamic screen that contains a "textview", "edittext","button" and "listview". I'm trying to add what is written in the edittext to my listview. I'm not getting any errors and i can see added items inside my ArrayList when i debug the code. But listview shows only the very first item added into the list.
LayoutParams layoutMatchParent = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LayoutParams layoutWrapContent = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
LayoutParams layoutMatchParentWidth = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
ScrollView scrollView = new ScrollView(this);
LinearLayout linearLayout = new LinearLayout(this);
linearLayout.setOrientation(LinearLayout.VERTICAL);
scrollView.addView(linearLayout);
setContentView(scrollView, layoutMatchParent);
LinearLayout vgMul=new LinearLayout(this);
vgMul.setOrientation(LinearLayout.VERTICAL);
TextView tvMul=new TextView(this);
tvMul.setText("Some Label");
tvMul.setId(1);
tvMul.setLayoutParams(layoutWrapContent);
vgMul.addView(tvMul);
EditText edtMul=new EditText(this);
edtMul.setId(2);
edtMul.setLayoutParams(layoutMatchParentWidth);
vgMul.addView(edtMul);
Button btnAddMul=new Button(this);
btnAddMul.setText("Add");
btnAddMul.setLayoutParams(layoutMatchParentWidth);
vgMul.addView(btnAddMul);
ListView lvMul =new ListView(this);
lvMul.setId(3);
lvListItems =new ArrayList<String>();
lvArrayAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, lvListItems);
lvMul.setAdapter(lvArrayAdapter);
lvMul.setScrollContainer(false);
vgMul.addView(lvMul);
btnAddMul.setOnClickListener(new View.OnClickListener() {
#Override public void onClick(View v) {
EditText edtMul=(EditText)findViewById(2);
lvListItems.add(edtMul.getText().toString());
lvArrayAdapter.notifyDataSetChanged();}
});
linearLayout.addView(vgMul);
Is this because of the scrollview? I'm adding my controls as children of a linear layout but i'm stuck...Any help will be appreciated...Thanks...
Remove your scroll view, a listview already implements a scrollview.
Alright, so I assume you have an activity, some part of that activity has a listview, you want to scroll on the listview and also scroll the whole activity.
You can do it by creating the listview in another Relative Layout (or Linear layout). Let me write an example for you in xml
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
//Your text views and other stuff goes here
</RelativeLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/SecondRelativeLayoutDetailedPage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:background="#color/holoActionBarColor">
<ListView
android:id="#+id/myRewardsActiveList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/myActiveRewardsHeadingLinLayout">
</ListView>
</RelativeLayout>
</RelativeLayout>
Does this make more sense now?

Linearlayout overlay another linearlayout

I have some problem with add linearlayout dynamically. It's add on the top of screen, overlay other linearlayout.
Here XML,code and results.
XML:
<TextView
android:id="#+id/top_km"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:background="#888"
android:gravity="top"
android:textColor="#fff"
android:textSize="30dip"
android:layout_centerHorizontal="true"/>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/top_km"
android:id="#id/textLayout">
</RelativeLayout>
Code:
myLayout = (RelativeLayout) page.findViewById(R.id.textLayout);
LinearLayout linLayout = new LinearLayout(this);
linLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myLayout.addView(linLayout);
LinearLayout hozLayout = new LinearLayout(this);
hozLayout.setOrientation(LinearLayout.HORIZONTAL);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
myLayout.addView(hozLayout);
Results:
enter link description here
Thanks
Don't use a RelativeLayout as your holder. Use a LinearLayout with orientation="vertical" instead.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/top_km"
android:orientation="vertical"
android:id="#id/textLayout" />
then in code
myLayout = (LinearLayout) page.findViewById(R.id.textLayout);
followed by
// rest of your code
It's because you use RealativeLayout for proper adding use
1. RelativeLayout.LayoutParams for st LayoutParams
2. In LayoutParams use field below
Example:
RelativeLayout rl=new RelativeLayout(this);
LinearLayout ll1=new LinearLayout(this);
TextView tx1=new TextView(this);
tx1.setText("Test1");
ll1.addView(tx1);
rl.addView(ll1);
LinearLayout ll2=new LinearLayout(this);
TextView tx2=new TextView(this);
tx2.setText("Test1");
ll2.addView(tx1);
rl.addView(ll2);
RelativeLayout.LayoutParams lp2=(LayoutParams) ll2.getLayoutParams();
And then use lp2.addRule
Here some help:
Parameters
verb One of the verbs defined by RelativeLayout, such as ALIGN_WITH_PARENT_LEFT.
anchor The id of another view to use as an anchor, or a boolean value(represented as TRUE) for true or 0 for false). For verbs that don't refer to another sibling (for example, ALIGN_WITH_PARENT_BOTTOM) just use -1.
Maybe it's easier for you to add it in the XML file with android:visibility="GONE" and then in the code just show it (View.VISIBLE) or hide it (View.GONE).

View Gravity not working

Im overlaying a linearlayout.The layout contains a button.The button should be top right of the linearlayout.But gravity doesnt seem to work.
CODE:
Inside the onCreate method of my service.
final WindowManager.LayoutParams params3 = new WindowManager.LayoutParams(
WindowManager.LayoutParams.TYPE_SYSTEM_ALERT,
WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT);
LinearLayout ll=new LinearLayout(this);
LinearLayout ll2=new LinearLayout(this);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
lp.gravity=Gravity.RIGHT;
lp.width=30;
lp.height=30;
b=new Button(this);
b.setBackgroundResource(R.drawable.x);
params3.gravity=Gravity.TOP;
params3.height=200;
params3.width=200;
ll.addView(b, lp);
wm.addView(ll, params3);
The linearlayout 200X200 is created and is on top.But the button isnt top right.I tried using
b.setWidth and b.setHeight. would not help.
LinearLayout by default is horizontal
You can't align horizontally in horizontal LinearLayout (e.g right, center_horizontal, left) and you can't align vertically in vertical LinearLayout (e.g top center_vertical, bottom) in vertical LinearLayout.
If you need to align it to the right you must either set your LinearLayout to be vertical or use a different ViewGroup, for example FrameLayout.
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinarLayout.VERTICAL);
Ant the Buttom will always be on top since it's a first item.
And why not doing it in xml? It will be much easier when less code.
Edit:
To put button to top right of the VideoView your layout will look like this.
<?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"
android:orientation="vertical" >
<VideoView
android:id="#+id/videoView1"
android:layout_width="200dp"
android:layout_height="200dp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:layout_alignTop="#+id/videoView1"
android:layout_alignRight="#+id/videoView1"
android:text="Button" />
</RelativeLayout>
Put this layout in a layout res folder of the project.
Project/res/layout/your_layout.xml
To attach a layout to Activity's window:
public final class YourActivity
extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.your_layout);
// Get VideoView
VideoView vv = (VideoView) findViewById(R.id.videoView1);
//get Button reference
View button = findViewById(R.id.button1);
}
}

How do I add elements dynamically to a view created with XML

I'm trying to add dynamic content to a view that has been created with XML.
I have a view "profile_list" that has a ScrollView that I like to add some elements to.
Here is some code of what I'm trying to do.
// Find the ScrollView
ScrollView sv = (ScrollView) this.findViewById(R.id.scrollView1);
// Create a LinearLayout element
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add text
tv = new TextView(this);
tv.setText("my text");
ll.addView(tv);
// Add the LinearLayout element to the ScrollView
sv.addView(ll);
// Display the view
setContentView(R.layout.profile_list);
The plan is to add a TableLayout and fill it dynamically and not just a dummy text but first I must get this to work.
Any help is welcome.
Kind Regards
Olle
I found the solution!
Stupid me I had left a element in my ScrollView in my XML-file!
Anyway her is a working example:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.profile_list, null);
// Find the ScrollView
ScrollView sv = (ScrollView) v.findViewById(R.id.scrollView1);
// Create a LinearLayout element
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
// Add text
TextView tv = new TextView(this);
tv.setText("my text");
ll.addView(tv);
// Add the LinearLayout element to the ScrollView
sv.addView(ll);
// Display the view
setContentView(v);
And the XML-file to match
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ScrollView
android:id="#+id/scrollView1"
android:clickable="true"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_marginBottom="50px"
android:layout_height="fill_parent">
</ScrollView>
</LinearLayout>
Hope this will help someone. Thanks for all help!
The way that you are adding things to the view is basically right - you just get the container object by its ID and then add them.
It looks like you are setting the content view to be the wrong view though. You should inflate the view, add the children and set it as the content view, or set the content view from a resource ID then do the manipulation. What you are doing is manipulating a view and then adding a different one. Try moving your setContentView(...) to the start of the block.

Categories

Resources