Change Visibilty of imageviews in alertdialog-Android - android

I'm trying to build an custom alertdialog in which there are 3 ImageViews .I want to hide and show these ImageViews dynamically.Here is my showroute() in HomeActivity class in which i'm creating the custom dialog.
AlertDialog.Builder alertDialog = new AlertDialog.Builder(HomeActivity.this);
alertDialog.setView(getLayoutInflater().inflate(R.layout.routedialog, null));
alertDialog.setTitle("Home Activity");
alertDialog.setIcon(R.drawable.logo);
alertDialog.setMessage(Html.fromHtml("<b>Route Details</b>"));
ImageView iv1,iv2,iv3;
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View convertView = (LinearLayout) inflater.inflate(R.layout.routedialog, null);
iv1=(ImageView) convertView.findViewById(R.id.iv1);
iv2=(ImageView) convertView.findViewById(R.id.iv2);
iv3=(ImageView) convertView.findViewById(R.id.iv3);
iv1.setVisibility(View.GONE);
iv2.setVisibility(View.GONE);
iv3.setVisibility(View.GONE);
convertView.findViewById(R.id.ll).invalidate();
and here is my routedialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dp" >
<ImageView
android:id="#+id/iv1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/ivred" />
<ImageView
android:id="#+id/iv2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/ivblue" />
<ImageView
android:id="#+id/iv3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/ivgreen" />
</LinearLayout>
I want these ImageViews to be hidden when the dialog manager pops up and has to be made visible dynamically after checking certain conditions.Please Help.

You are inflating the layout again
LayoutInflater inflater = getLayoutInflater();
View convertView = (LinearLayout) inflater.inflate(R.layout.routedialog, null);
alertDialog.setView(convertView);
Now initialize views and set the visibility for the views. Also check whether you want GONE or INVISIBLE # http://developer.android.com/reference/android/view/View.html#attr_android:visibility depending on your need
Also i do not see anywhere iv4,iv5,iv6,iv7,iv8 in your custom dialog layout.
This
iv4=(ImageView) convertView.findViewById(R.id.iv4);
iv5=(ImageView) convertView.findViewById(R.id.iv5);
iv6=(ImageView) convertView.findViewById(R.id.iv6);
iv7=(ImageView) convertView.findViewById(R.id.iv7);
iv8=(ImageView) convertView.findViewById(R.id.iv8);
will give NullPointerException.
Edit :
AlertDialog.Builder alertDialog = new AlertDialog.Builder(HomeActivity.this);
LayoutInflater inflater = getLayoutInflater();
View convertView = (View) inflater.inflate(R.layout.routedialog, null);
alertDialog.setView(convertView);
alertDialog.setTitle("Home Activity");
alertDialog.setIcon(R.drawable.ic_launcher);
alertDialog.setMessage(Html.fromHtml("<b>Route Details</b>"));
ImageView iv1,iv2,iv3;
iv1=(ImageView) convertView.findViewById(R.id.iv1);
iv2=(ImageView) convertView.findViewById(R.id.iv2);
iv3=(ImageView) convertView.findViewById(R.id.iv3);
iv1.setVisibility(View.GONE); // GONE oR INVISIBLE according to what you want
iv2.setVisibility(View.GONE);
iv3.setVisibility(View.GONE);
alertDialog.show();
Snap when imageview is visible

Related

text isn't showing after using setText() to TextView() in inflated layout

Yes, I know that similar questions were asked before but I tried many of those. So I have a problem with setting text in TextView in the inflated layout. I tried setContentView() then it works but then activity_main.xml with the menu isn't working. So I tried to inflate layout with TextView that I need. When I try to setText() it just doesn't displaying it. Here is my code:
public void bodAlg() {
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.layout1, null);
EditText editText = (EditText) vi.findViewById(R.id.bod_servers);
String[] serversArray = editText.getText().toString().split(", ");
TextView textView = (TextView) vi.findViewById(R.id.bod_serv_array);
textView.setText(Arrays.toString(serversArray));
}
And here is xml:
<?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" >
<RelativeLayout
android:id="#+id/rl_01"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/ll_01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/bod_servers"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Cray CS-Storm, Vulcan – Blue Gene/Q, Blue Gene/Q, Stampede – PowerEdge C8220, Piz Daint – Cray XC30"
android:textColor="#color/common_google_signin_btn_text_dark_focused"
android:textSize="13sp"/>
<TextView
android:id="#+id/bod_serv_array"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textSize="15sp"
android:textColor="#color/common_google_signin_btn_text_dark_focused"
android:paddingBottom="5dp"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
Thank you for answer!
Thanks for comments! Problem is solved!
public void bodAlg() {
LayoutInflater inflater = (LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.layout1, null);
EditText editText = (EditText) vi.findViewById(R.id.bod_servers);
String[] serversArray = editText.getText().toString().split(", ");
TextView textView = (TextView) vi.findViewById(R.id.bod_serv_array);
textView.setText(Arrays.toString(serversArray));
// solution
CoordinatorLayout mainL = (CoordinatorLayout) findViewById(R.id.main_view);
mainL.removeAllViews(); // remove previous view, add 2nd layout
mainL.addView(vi);
}

custom dialog only display a small white box

I'm trying to display a custom Dialog. I replicated a class which perfectly works and used it for this dialog but it don't display anything but a little box in the middle of the screen.
I can't figure out what's wrong....
Dialog onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
LayoutInflater inflater = (LayoutInflater) context.getSystemService( Context.LAYOUT_INFLATER_SERVICE );
final View v = inflater.inflate(R.layout.fleet_select_dialog, null);
WindowManager windowManager = (WindowManager) context
.getSystemService(Context.WINDOW_SERVICE);
Display display = windowManager.getDefaultDisplay();
int width = (display.getWidth() );
int height = (display.getHeight() );
getWindow().setLayout(ViewGroup.LayoutParams.MATCH_PARENT,(width/3)*2 );
}
xml layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="true"
android:focusableInTouchMode="true"
android:id="#+id/fleet_select_dialog">
<EditText
android:drawableLeft="#android:drawable/ic_menu_search"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:id="#+id/edit_fence2"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:layout_marginTop="15dip"
android:textColor="#color/themeapp"
android:background="#drawable/layout_corner_white"
android:singleLine="true"
android:inputType="textCapWords"/>
<ExpandableListView
android:layout_below="#+id/edit_fence2"
android:layout_marginTop="5dip"
android:id="#+id/lvExp"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:cacheColorHint="#00000000"
android:divider="#color/themeapp"
android:dividerHeight="1dp"
android:layout_marginBottom="30dip"
android:layout_above="#+id/footerview" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="40dip"
android:id="#+id/footerview"
android:layout_alignParentBottom="true">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Confirm"
android:paddingBottom="10dip"
android:id="#+id/button_view"
android:textColor="#color/themeapp"
android:textSize="24dip"
android:background="#ffffffff"
android:paddingLeft="20dip"
android:paddingRight="8dip"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:id="#+id/waiting"
android:layout_height="fill_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:text="Loading..."
android:id="#+id/textView16" />
</RelativeLayout>
</RelativeLayout>
the dialog is used like this:
fleetSelectDialog = new FleetSelectDialog(context);
fleetSelectDialog.show();
what am I doing wrong??
Try setting the windowLayout of the dialog before you set the content view with the inflater.
You can create your view directly from the Layout Inflater, you only need to use the name of your layout XML file and the ID of the layout in file.
Use the following code to set the layout of your AlertDialog
LayoutInflater inflater = getLayoutInflater(); View dialoglayout = inflater.inflate(R.layout. fleet_select_dialog, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setView(dialoglayout);
builder.show();
I accidently deleted one important line:
this.setContentView(v);
final Dialog dialog = new Dialog(mContext);
// hide to default title for Dialog
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
// inflate the layout dialog_layout.xml and set it
// as contentView
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.customize_dialog, null, false);
dialog.setCanceledOnTouchOutside(false);
dialog.setContentView(view);
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(0));
dialog.show();
Enjoy full code.
Cheers !

How to get views from AlertDialog?

I build an alertDialog in this way:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/title1"
android:textSize="20sp"
android:textColor="#color/white" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/chars1" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/RelativeLayout1"
android:background="#drawable/body_white">
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/Layout1"
android:layout_centerHorizontal="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/img1"
android:id="#+id/img1"
android:layout_alignParentTop="true"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/img2"
android:background="#drawable/img2"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/img1"
android:layout_toEndOf="#+id/img1" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
I want to do this thing:
img1 = (ImageView) findViewById(R.id.img1);
But doing this I receive a NullPointerException.
I used this:
(EditText)((Dialog) dialog).findViewById(R.id.username);
in a early application where I take value after click.
In this case I don't have an object DialogInterface dialog. So I can't use it. How can I fill img1?
I want so set manually padding but I want to do this in JAVA and not in XML.
Thanks
Edit:
This is method that launch alertDialog:
public void createDialog() {
alertDialog = new AlertDialog.Builder(Instructions.this);
LayoutInflater inflater = this.getLayoutInflater();
alertDialog.setView(inflater.inflate(R.layout.alert_dialog_layout, null));
n1Dialog = (ImageView) findViewById(R.id.img1);
n1Dialog.setPadding(0,0,0,0);
alertDialog.show();
}
I have a simple button that when in onClick() method have
createDialog();
Row with setPadding (that is only a proof, it isn't what I want to do) was mistaken because is NullPointerException
do like this:
alertDialog = new AlertDialog.Builder(Instructions.this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView=inflater.inflate(R.layout.alert_dialog_layout, null);
alertDialog.setView(dialogView);
n1Dialog = (ImageView) dialogView.findViewById(R.id.img1);
n1Dialog.setPadding(0,0,0,0);
alertDialog.show();
This is how you should inflate a custom view for a AlertDialog:
Builder myCustomDialog = new AlertDialog.Builder(yourContext);
LayoutInflater factory = LayoutInflater.from(context);
final View yourInflatedView = factory.inflate(R.layout.your_xml_layout, null);
//Here you get your imageView (or TextView, EditText,...)
ImageView img1 = (ImageView) yourInflatedView.findViewById(R.id.img1);
// Do stuff with your imageview...
myCustomDialog.setView(yourInflatedView);
myCustomDialog.show();
You can create the dialog in the following way also:
public void displayPopup(Context ctx) {
Dialog cd_display = new Dialog(ctx);
cd_display.requestWindowFeature(Window.FEATURE_NO_TITLE);
cd_display.setContentView(R.layout.alert_dialog_layout);
ImageView n1Dialog = (ImageView) cd_display.findViewById(R.id.img1);
cd_display.show();
}

Android ImageView dynamic width and height

I have a horizontal list of images loaded from a url. So i have an ImageLoader, then an Adapter then my Activity. My xml layouts looks like this:
mainlayout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<com.devsmart.android.ui.HorizontalListView
android:id="#+id/hlist_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:overScrollMode="always" >
</com.devsmart.android.ui.HorizontalListView>
</LinearLayout>
Then my list.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:translationY="200dp" > <!-- make this dynamic -->
<FrameLayout
android:id="#+id/mainlayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content" >
<ImageView
android:id="#+id/prof_pic"
android:clickable="true"
android:adjustViewBounds="true"
android:layout_width="360dp"
android:layout_height="360dp" />
<ProgressBar
android:id="#+id/img_progress"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
<TextView
android:id="#+id/sname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textStyle="bold" />
<TextView
android:id="#+id/sdesc"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
With this layout, the result is Figure A. The gray broken line is the imageView, we can't actually see it but only those images(green box). What i want to achieve is the one in Figure B.
So, to somewhat solve this i tried adding in my imageView
<ImageView
android:id="#+id/prof_pic"
android:clickable="true"
android:adjustViewBounds="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxWidth="360dp"
android:maxHeight="360dp"
android:minHeight="240dp"
android:minWidth="240dp"
/>
But what happened is that, by the time i open the application, my image list looks like Figure A. But when a scroll along the images, it adjusted to somewhat look like Figure B. But when i scroll back to the first image, only the first have of the image will only be show or sometimes, my image list starts at my second image. This is really crazy.
Is there any way where i can achieve Figure B without destroying my list of images or at the start of the app, it already displays like Figure B?
EDIT:
This is how my Adapter looks like:
public View getView(int position, View convertView, ViewGroup parent){
ViewHolder holder = null;
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
if(convertView == null){
convertView = inflater.inflate(R.layout.list_layout, null);
holder = new ViewHolder();
holder.txtShopName = (TextView) convertView.findViewById(R.id.shop_name);
holder.txtDesc = (TextView) convertView.findViewById(R.id.desc);
holder.imageView = (ImageView) convertView.findViewById(R.id.prof_pic);
holder.progBar = (ProgressBar) convertView.findViewById(R.id.img_progress);
convertView.setTag(holder);
}else{
holder = (ViewHolder) convertView.getTag();
}
imageLoader=new ImageLoader(context.getApplicationContext(), holder.progBar);
SRowItem sItem = (SRowItem) getItem(position);
holder.txtName.setText(sItem.getShopName());
holder.txtDesc.setText(sItem.getShopDesc());
imageLoader.DisplayImage(sItem.getImageUrl(), holder.imageView);
return convertView;
}
Try something like this :
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(width, height);
ImageView imageView = (ImageView) LayoutInflater.from(context).inflate(R.layout.image, null);
imageView.setLayoutParams(params);

Trying to generate dialog with android

Anyone know why my button isn't showing up in the dialog window?
Dialog d = new Dialog(AddContact.this);
LayoutInflater li = (LayoutInflater) getSystemService(Service.LAYOUT_INFLATER_SERVICE);
ViewGroup contentView = (ViewGroup) li.inflate(R.layout.dialog,null);
d.setContentView(contentView);
d.setTitle("Please correct these errors:");
TextView error = (TextView) contentView.findViewById(R.id.textView1);
Button closer = (Button) contentView.findViewById(R.id.button1);
closer.setText("Close");
error.setText(errorMessage);
d.show();
This my dialog.xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="vertical" android:padding="5dp">
<TextView android:text="TextView" android:id="#+id/textView1"
android:layout_height="fill_parent" android:layout_width="fill_parent" />
<Button android:text="Button" android:id="#+id/button1"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:gravity="center" />
</LinearLayout>
What do I need to do so my button shows in the dialog window?
Your dialog is not visible when you use findViewById(), which means that the view with that id is not yet in your view hierachy and can not be found. This results in error beeing null, which will throw a NullPointerException in the following line.
You can solve this by inflating the layout seperately and use findViewById() on the inflated view.
Dialog d = new Dialog(AddContact.this);
LayoutInflater li = (LayoutInflater) getSystemService(Service.LAYOUT_INFLATER_SERVICE);
ViewGroup contentView = (ViewGroup) li.inflate(R.layout.dialog, null);
d.setContentView(contentView);
d.setTitle("Please correct these errors:");
error = (TextView) contentView.findViewById(R.id.textView1);
error.setText(errorMessage);
d.show();

Categories

Resources