setVisibility(View.INVISIBLE) causes exception, why? - android

why my setVisibility(View.INVISIBLE) causes exception while
setVisibility for TextView works. I also tried set it for ImageView and it also does not work - I am getting exception too
public void alertdiag() {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(this);
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.alertdiag_layout, null);
dialogBuilder.setView(dialogView).show();
View Divider1 = (View) dialogView.findViewById(R.style.Divider1);
Divider1.setVisibility(View.INVISIBLE); //causes java.lang.IllegalStateException: Could not execute method of the activity"
TextView HELP0 = (TextView) dialogView.findViewById(R.id.HELP0);
HELP0.setVisibility(View.INVISIBLE); // this works
}
alertdiag_layout.xml:
<View style="#style/Divider1"
android:layout_below="#+id/HELP3"
android:background="#000000"
android:layout_alignRight="#+id/HELP_FIX_LINE"
android:layout_alignLeft="#+id/HELP3"
/>
<TextView
android:id="#+id/HELP0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/HELP_FIX_LINE"
android:textStyle="bold"
android:typeface="normal"
/>
styles.xml:
<style name="Divider1">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item>
</style>

You are trying to set the Visibility to a
Style (R.style.Divider1)
Add an Id to your View and change the R.style.Divider1 to R.id.yourNewId
<View style="#style/Divider1" android:id="#+id/yourNewId"
android:layout_below="#id/HELP3" android:background="#000000"
android:layout_alignRight="#id/HELP_FIX_LINE"
android:layout_alignLeft="#id/HELP3" />

Add id attribute to your view
<View style="#style/Divider1"
android:id="#+id/my_view"
android:layout_below="#id/HELP3"
android:background="#000000"
android:layout_alignRight="#id/HELP_FIX_LINE"
android:layout_alignLeft="#id/HELP3"
/>
NOTE: I switched #+id/HELP3 and #+id/HELP_FIX_LINE with #id/HELP3 and #id/HELP_FIX_LINE respectively because #+id/ is only used when you are giving a view an id. It is not used to refer other views.
Now, Change this line of code
View Divider1 = (View) dialogView.findViewById(R.style.Divider1);
with
View Divider1 = (View) dialogView.findViewById(R.id.my_view);
It will find the view with the given id, instead of style.
To make the view invisible you need to set
Divider1.setVisibility(View.INVISIBLE);
setVisibility must be used on the view, not on the style.

Related

Change Title header of AlertDialog

I am trying to change the title header of Alertdialog box but the output is not what I exactly wanted. I am creating the following style in styles.xml:
<style name="question_dialog"
parent="#android:style/Theme.Holo.Dialog">
<item name="android:windowTitleStyle">#style/question_dialog_title</item>
</style>
<style name="question_dialog_title" parent="android:Widget.TextView">
<item name="android:background">#5cc5cc</item>
<item name="android:textSize">21sp</item>
<item name="android:textColor">#ffffff</item>
</style>
The java code is as follows :
new AlertDialog.Builder(this,
R.style.question_dialog).setTitle("Assam Quiz" ).
setMessage("Hello world Hello world").
setPositiveButton("OK", (dialog, which) - >
{dialog.dismisd();
}).show();
}
The AlertDialog image is attached.
out of the style - I think your dialog has some header layout set to it that prevents the title to be at the top, but if that is the case or not - you can easily set a custom title for the dialog header giving it the header layout only, and so you will have the full control for the dialog header:
// creating the Dialog
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// getting dialog context
Context mContext = builder.getContext();
// building the inflater
LayoutInflater mLayoutInflater = LayoutInflater.from(mContext);
// inflate the dialog header layout
View mView = mLayoutInflater.inflate(R.layout.simple_dialog_header, null);
// get the TextView for the header (contained in the header layout)
TextView mTextView = (TextView) mView.findViewById(R.id.title_text);
// set the text for that TextView
mTextView.setText(message);
// set the custom header view for the dialog
builder.setCustomTitle(mView);
/*
here you can set positive , negative ,neutral buttons
or set the dialog message or any attribute you want
*/
// finally, show the dialog
builder.show();
and for the header layout (R.layout.simple_dialog_header):
<?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:background="#color/primary"
android:orientation="vertical"
android:paddingBottom="15dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="15dp">
<TextView
android:id="#+id/title_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:maxLines="1"
android:textAlignment="center"
android:textColor="#color/primary"
android:textSize="16sp"
android:textStyle="bold" />
</LinearLayout>

Change view background color into a listview

I'm trying to change background color on a view component, but not success
public View getView(int position, final View convertView, ViewGroup parent) {
View view = convertView;
try {
if (view == null) {
LayoutInflater vi = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = vi.inflate(R.layout.listview_accounts, null); // --CloneChangeRequired(list_item)
}
final Account listItem = (Account) mList.get(position); // --CloneChangeRequired
if (listItem != null) {
int color = listItem.getColor();
View vColor = (View) view
.findViewById(R.id.lv_account_view_color);
vColor.setBackgroundColor(color);
}
}
} catch (Exception e) {
}
return view;
}
I can set some text in textview, but set color not working.
Can anybody helps me how to set the color? Thanks
The example color used is: -16711717
edit
Listview Item layout:
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<View
android:id="#+id/lv_account_view_color"
android:layout_width="#dimen/activity_horizontal_margin"
android:layout_height="wrap_content"
android:background="#167117" />
<TextView
android:id="#+id/lv_account_tv_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_horizontal_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_horizontal_margin"
android:textColor="#color/black" />
</LinearLayout>
background cannot be setted in xml, it's a dynamic color
Try this: vColor.setBackgroundColor(Color.parse("#yourcolorcode"));
You can directly set the background color of the component in the listview_accounts.xml file of your project. For example
<component>
android:background="#color/color_name"
</component>
You have to make a color.xml file in your values folder(maybe already present) and add the color value ex:-16711717.
try this and let me know
<View
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="#167117" />
you can find solution on This link
Just you have to change one thing. In delete.onclick method replace list.removeView(customView); with Customview.setbackground(Color.red); etc. This may help you. Best of luck.

Finding a textView from XML of a Custom View for a AlertDialog Fragment

I have a custom class that extends a dialog fragment that I will later be putting in an activity, however no matter what I do I cannot find the Textview associated with the XML used to create the custom AlertDialog.
EDIT: Mike was right, the onCreateDialog(Bundle ...) is not called before I try to access the textViews, thus leading to the null pointer exception, I cannot, however for the life of me figure out how to create the dialog then try and access the fields, I have attempted to place the code that finds the views in the onAttach(...) method of a fragment because it is the earliest in the lifecycle, however the code the always calls the getter (to get the textView instance in the fragments first and not onCreateDialog or onAttach, is there anyway to ENSURE that the fragment's onCreateDialog or onAttach is called before the getters?
This is in the activity that that will host the fragment
win_frag is an already instantiated instance of the custom fragment, appendStreak is the getter to attempt to get the textView instances from the fragment.
mainPanel.getThread().onPause();
win_frag.show(fragmentManager, "winning");
// Set the text in the views accordingly by calling a method from
// the fragment
Log.d(TAG, "Attempting to append to the fragment textView");
win_frag.appendStreak(pongApplication.getStreak());
win_frag.appendBonus(pongApplication.getBonus());
win_frag.appendTotal(total);
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder build = new AlertDialog.Builder(getActivity());
// Get the layout inflater (needed to put in custom XML)
LayoutInflater inflater = getActivity().getLayoutInflater();
View inflatedView = inflater.inflate(R.layout.win_screen, null, false);
build.setView(inflatedView);
streakView= (TextView)inflatedView.findViewById(R.id.streak_box);
bonusView= (TextView)inflatedView.findViewById(R.id.bonus_box);
totalView= (TextView)inflatedView.findViewById(R.id.total_box);
}
win_screen.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"
android:background="#drawable/metal_background">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:height="50dp"
android:textColor="#android:color/white"
android:text="#string/win_title"
android:textSize="30sp"
android:typeface="monospace"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:gravity="left|center"
android:text="#string/streak_title"
android:textSize="20sp"
android:textColor="#ffffff"
android:id="#+id/streak_box"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:gravity="left|center"
android:text="#string/bonus_title"
android:textSize="20sp"
android:textColor="#ffffff"
android:id="#+id/bonus_box"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:height="50dp"
android:gravity="left|center"
android:text="#string/total_title"
android:textSize="20sp"
android:textColor="#ffffff"
android:id="#+id/total_box"
/>
</LinearLayout>
Try this
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder build = new AlertDialog.Builder(getActivity());
// Get the layout inflater (needed to put in custom XML)
LayoutInflater inflater = getActivity().getLayoutInflater();
View inflatedView = inflater.inflate(R.layout.win_screen, null);
build.setView(inflatedView);
streakView= (TextView) inflatedView.findViewById(R.id.streak_box);
bonusView= (TextView) inflatedView.findViewById(R.id.bonus_box);
totalView= (TextView) inflatedView.findViewById(R.id.total_box);
return builder.create();
}
try changing
inflater.inflate(R.layout.win_screen, null, false);
to
inflater.inflate(R.layout.win_screen, null);

Inflate layout and edit it in code

I have an android XML file with the following content:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relLay"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/ivQuit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/imgquit"
android:background="#null"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageButton
android:id="#+id/btnYes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/btnyes"
android:background="#null"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
<ImageButton
android:id="#+id/btnNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/btnno"
android:background="#null"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</RelativeLayout>
Now I want to inflate it and then edit the ImageButtons via code. I do this as followed:
Inflate XML:
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.quitgame, null);
Try editing an ImageButton:
ImageView imgView = (ImageView)findViewById(R.id.ivQuit);
ImageButton imgBtnYes = (ImageButton)findViewById(R.id.btnYes);
ImageButton imgBtnNo = (ImageButton)findViewById(R.id.btnNo);
RelativeLayout.LayoutParams relParams = (RelativeLayout.LayoutParams)imgBtnYes.getLayoutParams();
relParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
imgBtnYes.setLayoutParams(relParams);
However as long as the three code lines with the LayoutParams are existent the PopupWindow I create with the inflated view does not appear. It seems that the error is hidden in this line
RelativeLayout.LayoutParams relParams = (RelativeLayout.LayoutParams)imgBtnYes.getLayoutParams();
but I don't understand why.
Can anybody tell me why the PopupWindows does not appear because of this code line(s)?
Is it because of the PopupWindow itself or because of the LayoutInflater?
You have this
LayoutInflater inflater = (LayoutInflater) MainActivity.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.quitgame, null);
Use the View object to initialize the views.
ImageView imgView = (ImageView)layout.findViewById(R.id.ivQuit);
ImageButton imgBtnYes = (ImageButton)layout.findViewById(R.id.btnYes);
ImageButton imgBtnNo = (ImageButton)layout.findViewById(R.id.btnNo);
You can findViewById of the current view hierachy set to the activity. In your case you are inflating your layout. So use the inlfated view object to initialize your views.
You are inflating the layout with root set to null, so your layout variable (returned by the inflate method) holds the root of the inflated layout (in this case, your RelativeLayout). I didnt see the code where you are attaching the inflated layout to your activity's content (maybe the code you posted is not complete?).
Given this, your findViewById calls should return nulls.
Moreover, as far as I know and understand, unless the view is attached to a view that is already somewhere in the activity, the LayoutParams are not going to be correct as no dimensions are set (even in case they are invoked on a view that isn't null, obviously).

how to add another view using layout inflatter

I created mainLayout with two buttons
add: to add other layout
remove : to remove other layout.
<Button
android:id="#+id/btnAdd"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Add View"
android:onClick="addView" />
<Button
android:id="#+id/btnRemove"
android:textStyle="bold"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Remove View"
android:onClick="removeView" />
now i wrote following code to add the view
when I click on addView button
LayoutInflater inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(R.layout.other_layout,null);
mainLayout.addView(view);
the view is added below the main layout.
But I want the view to add in middle of the screen not at the bottom of screen.
How can I do that?
Modify this line and put ur parent view.
view=inflater.inflate(R.layout.other_layout,PARENT_VIEW_OBJECT);
It should work
get the parentLayout like this
parentLayout=(YourParentLayout)view.findViewById(R.id.parentLayout);
then
parentLayout.addView(yourview);
You can check below code, its working fine for me
private View view = null ;
Button addbutton = null;
ViewGroup parent = null;
LayoutInflater inflater = null;
inflater= (LayoutInflater)this.getSystemService(LAYOUT_INFLATER_SERVICE);
addbutton = (Button)findViewById(R.id.btnAdd);
parent = (ViewGroup) findViewById(R.id.mainxml);
addbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
view = inflater.inflate(R.layout.other, null);
parent.addView(view);
}
});
I would put an empty LinearLayout placeholder at the top of your main layout where you want your view to be and add the view to that instead of the main layout.
You can adjust the placeholder first and modify it's location and looks.
<LinearLayout
android:id="#+id/placeholder"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
..../>
<Button
..../>
I would look into using a ViewStub it holds a place until .inflate() is called. That way the layout isn't taking resources until it's needed.

Categories

Resources