Dialog Java Code:
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom_dialog);
dialog.setTitle("Title...");
ScrollView scroll = new ScrollView(context);
scroll.setBackgroundColor(android.R.color.transparent);
scroll.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.icon);
dialog.show();
XML Code Here:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:scrollbarAlwaysDrawVerticalTrack="true">
>
<ImageView android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
I know that if i have only text on alert dialog it's automatically scrolling, but with an imageview automatically scrolling doesn't work.
How can i fix this problem?
Put your layout inside a ScrollView:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/layout_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="10dp"
android:scrollbarAlwaysDrawVerticalTrack="true" >
<ImageView android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp" />
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF" />
</LinearLayout>
<ScrollView>
I see that your LinearLayout has a horizontal orientation. So if this the case and you want to scroll horizontaly, you should replace ScrollView by HorizontalScrollView and set the orientation to horizontal.
Let me know if this works.
Related
I am trying to resize the inflated view but not working
I am adding this view to AlertDialog
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.popup_util, null,false);
view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 200)); //Set params here
......
dialog.setView(view);
popup_util
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/setting_password_popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtPopUpTitle"
android:padding="10dp"
android:layout_centerHorizontal="true"
android:textColor="#000000" android:textSize="24dp" android:background="#color/colorAccent"/>
<TextView android:padding="10dp" android:layout_width="match_parent" android:layout_below="#+id/txtPopUpTitle" android:id="#+id/txtPopUpMessage" android:layout_height="wrap_content" android:textSize="22dp"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_horizontal|bottom">
<Button android:id="#+id/btnPopUpCancel"
android:layout_width="match_parent" android:layout_height="50dp"
android:layout_weight="1"
android:textSize="24dp"
android:visibility="gone"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:text="#string/btn_cancel_text" />
<Button android:id="#+id/btnPopUpOk"
android:layout_width="match_parent" android:layout_height="50dp"
android:textColor="#color/colorAccent"
android:textSize="24dp"
android:layout_centerHorizontal="true"
android:text="#string/btn_ok_text"
android:background="#android:color/transparent"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
But it is not working.. What i m missing ?
u cane try this code:
View view = inflater.inflate(R.layout.active_slide, this);
view.setMinimumWidth(200);
You can use this code its works good in my case
this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));
NOTE: Be sure to use the parent Layout's LayoutParams. Mine is LinearLayout.LayoutParams!
You can do that by setting the width and height to the window of the dialog itself.
alertDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, 200);
You must do that after showing the dialog using dialog.show()
I m noob to android, I have a simple Question.
i have TextView in a RelativeLayout, i want to add a button dynamically just below that TextView which is present in a RelativeLayout,
how can i do it ?
thanks !
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:background="#mipmap/background"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container">
<android.support.v4.view.ViewPager
android:id="#+id/screen3"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
>
</android.support.v4.view.ViewPager>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:paddingTop="30dp"
android:id="#+id/act3_txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="35sp"
android:textAlignment="center"
android:textColor="#FFFFFF"
/>
<ImageView
android:paddingTop="30dp"
android:id="#+id/cam_images"
android:layout_height="250sp"
android:layout_width="wrap_content"
android:layout_below="#+id/act3_txt1" />
<TextView
android:paddingTop="30dp"
android:id="#+id/act3_txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cam_images"
android:textSize="35sp"
android:textAlignment="center"
android:layout_centerInParent="true"
android:textColor="#FFFFFF"
android:paddingBottom="20dp"/>
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
1) create a button like this inside onCreate().
Button myButton = new Button(this);
myButton.setText("Press Me");
2) then add it into your layout. before it you have to add id for your layout in which you want to show the button.
RelativeLayout = (RelativeLayout) findViewById(R.id.relativeLayoutID);
layout.addView(myButton);
Inflate the relative layout and the existing textview first
relativeLayout= (RelativeLayout) findViewById(R.id.relativeLayout);
textView= (TextView) findViewById(R.id.tittle);
Prepare layout params and addRule method to put the button below the textview
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.BELOW, R.id.tittle);
Button button=new Button(this);
button.setLayoutParams(params);
button.setText("Click Me!");
relativeLayout.addView(button,1);
I build a custom dialog to pop up an image with close button like this:
As you can see the dialog is not fitting the dialog layout and there's this white borer on the top and on the right, so how can i get rid the white border thing and make my custom dialog fit?
<?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="wrap_content"
android:layout_gravity="center_vertical"
android:gravity="center"
android:orientation="vertical" >
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" >
<LinearLayout
android:id="#+id/linearMain"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="vertical"
android:paddingBottom="-50dp" >
<ImageView
android:id="#+id/imgMain"
android:layout_width="300dp"
android:layout_height="350dp"
android:gravity="center"
android:background="#drawable/eairh_dialog"
/>
<!---add your views here-->
</LinearLayout>
<ImageView
android:id="#+id/imgClose"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top|right"
android:clickable="true"
android:src="#drawable/close_button" />
</FrameLayout>
</LinearLayout>
getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Use Above Code in your Dialog class to make Dialog TransParent
Remove margin Top and right on LinearLayout:
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
To make the Dialog bigger, you can set those parameters to MATCH_PARENT.
int screenWidth = 0, screenHeight = 0 ;
Display display = thid.getWindowManager().getDefaultDisplay();
screenWidth = display.getWidth();
screenHeight = display.getHeight();
Dialog dialog = new Dialog(getActivity(), android.R.style.Theme_DeviceDefault);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.cart_empty);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = screenWidth;
lp.height = screenHeight;
dialog.getWindow().setAttributes(lp);
dialog.show();
I am designing a pop up AlertDialog that sets the view of of the alert dialog to the following XML file. For some reason the file will not add margins and I cannot figure out what I am doing wrong. Margins and Padding do not work in the activity_priority_level_values LinearLayout, the LinearLayout that holds all of the TextViews, or any of the TextViews. How can I add margins (or padding) for the dialogs borders and margins between all of the text views?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/activity_priority_level_values"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/large_space">
<TextView android:id="#+id/level_1_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_1_header"/>
<TextView android:id="#+id/level_1_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_1_description"/>
<TextView android:id="#+id/level_2_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_2_header"/>
<TextView android:id="#+id/level_2_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/mid_space"
android:text="#string/level_2_description"/>
<TextView android:id="#+id/level_3_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_3_header"/>
<TextView android:id="#+id/level_3_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/mid_space"
android:text="#string/level_3_description"/>
<TextView android:id="#+id/level_4_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_4_header"/>
<TextView android:id="#+id/level_4_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="#dimen/mid_space"
android:text="#string/level_4_description"/>
<TextView android:id="#+id/level_5_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_5_header"/>
<TextView android:id="#+id/level_5_decription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/level_5_description"/>
</LinearLayout>
</LinearLayout>
Activity
LinearLayout hiddenLayout = (LinearLayout)findViewById(R.id.activity_priority_level_values);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
if(hiddenLayout == null){
LinearLayout myLayout = (LinearLayout)findViewById(R.id.activity_law_priority);
View hiddenInfo = getLayoutInflater().inflate(R.layout.activity_priority_level_values, null);
builder.setView(hiddenInfo);
}
builder.create();
builder.show();
The problem is the inflating of your view.
LinearLayout myLayout = (LinearLayout)findViewById(R.id.activity_law_priority);
View hiddenInfo = getLayoutInflater().inflate(R.layout.activity_priority_level_values, null);
You should never inflate with null. Instead change the code to:
LinearLayout myLayout = (LinearLayout)findViewById(R.id.activity_law_priority);
View hiddenInfo = getLayoutInflater().inflate(R.layout.activity_priority_level_values, myLayout, false);
If you are inflating with null the layout params will be ignored.
Consider the widget hello:
When clicked, it opens the area at the bottom with additional widgets (text, buttons, etcetera):
What is this kind of expand/collapse widget called?
One way to do this is to build your whole layout with the text and the buttons, but hide them initially by setting their visibility to gone or invisible. Then in code in the onClick listener of the "hello_text" text view, you can change their visibility to visible.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/hello_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello_text" />
<TextView
android:id="#+id/the_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/my_text"
android:visibility="invisible" />
<LinearLayout android:id="#+id/btn_holder"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:visibility="invisible">
<Button android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text1" />
<Button android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_text2" />
</LinearLayout>
</LinearLayout>
Now in your activity or fragment you can do:
TextView the_text_view = (TextView) v.findViewById(R.id.the_text);
LinearLayout ll_btn_holder = (LinearLayout) v.findViewById(R.id.btn_holder);
TextView hello_text_view = (TextView) v.findViewById(R.id.hello_text);
hello_text_view.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
the_text_view.setVisibility(View.VISIBLE);
ll_btn_holder.setVisibility(View.VISIBLE);
}
});