i've developed an android messaging app but, i would like to change the whole layout. It even includes a alert dialog which also i want to change.. help please..
Your layout is generally in an xml file, e.g., main.xml, called with something like setContentView(R.layout.main). The main.xml file is usually in /res/layouts, and can be edited directly, independent of the Java code.
you can define custom layout in xml files and call the layout inflator service to inflate that xml file.
If you want to customize alertdialog, then do the following
make a customized xml file and declare a view.set the setContentView method and use that xml file.
Then inflate the layout using the LAYOUT_INFLATER_SERVICE, thenthere is a method Alertdialog.setview(view)..use that view in it.
Related
So guys my question is simple, i'm migrating my app layout to android tabview.
I had all the code of the switches and textviews inside my activity_main.xml layout file.
Now i made 3 layout fragments resource xml files and i migrated everything there, plus i included the layout app bar in the activity_amin file
I left all the java codes of the layout elements inside MainActivity.java without changing anything.
Now the app crashes becouse all the layout elements declared inside the fragment layout files return null.
Do i have to set the layout elemnts codes inside the frgament java associated files?
Thank you
You should definitely use the Fragment.java-classes for addressing the gui-components of a fragment.
If you need a refresh for how to work with fragments you can read thru the codelab here: https://codelabs.developers.google.com/codelabs/advanced-android-training-fragments/index.html?index=..%2F..index#0
Best
Sebi
That's simple you probably working inside onCreateView of the fragment
try to work inside the onActivityCreate, because onCreateView the view is not ready and every thing will return null, so if you work on :
Kotlin:
override fun onActivityCreated
Java :
#Override
public void onActivityCreated
that dosen't happen.
In my android app, I want to make a dialog box, the kind of one you get from alertbuilders, where you get access to set positive/neutral/negative button click, but I also want to use the .setcustomview() to load the content with my xml file.
Does android have a way to do this? I want to avoid making those buttons...
Thanks
I don't see a setcustomview() method for the AlertDialog.Builder class, but I do see a setView() method, if that's what you mean. :)
From my experience, using setView() will allow you to set your custom XML layout (though you will still need to inflate it first) without requiring you to recreate / add buttons to your layout. You just need to call the setXxxButton() methods before you build.
This may be a dumb question, so my apologies if so; I'm fairly new to Android.
But anyway - I have a working ViewStub, which is replaced by different layouts depending different situations. It's working fine with regards to showing the correct layout when I call the setLayoutResource() method, and then setVisibility to VISIBLE. However, I now need some of the content in this view that is being shows to be dynamic (i.e. I need to set it via code rather than just show a static layout).
Is this even possible? The setLayoutResource() method only takes a static layout-resource ID, but I need that layout XML file to be able to have it's TextViews contain non-static text that comes from some code that I have ready to utilize. How should this be approached if possible? I understand the concept of having a Java class, and inflating the XML to attach itself to it to update the fields, but I can't see how that relates to my code at hand, since it's simply a layout resource int I need to set for the setLayoutResource() method in ViewStub.
I can post existing code if needed, but I'm not sure it do much more than clutter up the post. For reference - All I have is a simple layout XML file with some TextViews, and then my main XML containing the ViewStub, which is part of a custom Dialog. The user is able to instantiate the Dialog and set the layout, which in turn sets the layout of the ViewStub. This is the layout in which I need the dynamic content to be used.
Any advice would be greatly appreciated. Thanks!
Turns out this wasn't too difficult to accomplish. I just needed to use the ID of the TextView layouts after inflating the ViewStub to get a copy of the actual TextViews, then I was easily able to set their text to whatever kind of dynamic/custom text I desired.
I also needed to comment out the code that shows it via the .VISIBLE call, and instead do the following (the .inflate() line of code accomplishes the same thing as setting it to VISIBLE):
View inflatedView = dialog.myStubView.inflate();
TextView myTextView = (TextView) inflatedView.findViewById(R.id.my_text_view);
myTextView.setText("Dynamic/Custom Text");
I'd like to create a custom view (I'll call it MyComplexView), for example a RelativeLayout with an Imageview, a TextView, and a Button.
I'd like to declare an xml with the layout and then create the class:
MyComplexView extends RelativeLayout{...}
But I don't know what I should override to indicate which layout should be inflated.
How can I do this? Thanks
Something like this:
add the constructors from the super class. (the one with just context is for creating the views programaticaly, the others are for when you add the view in XML.
create a method called init() for example and call it from each constructor.
inside the init method do:
LayoutInflater.from(context).inflate(R.layout.my_view_layout, this, true);
now in inflate the additional params actually mean:
true -> attach the layout to the root in your case relative layout (pro-tip: so inside the xml you can have just merge tags if your layout root is also relative layout and align them in code so the hierarchy is simpler) or any layout you like.
this -> the layout to attach the inflated view to in your case the relative layout you are extending.
it will automatically be attached to the root -> extends RelativeLayout.
then you can use findViewById like:
this.findViewById(R.id.myView);
I'm not 100% sure what your main goal is, so I try to be thorough:
If you want to include a complex layout in other layouts, then you can simply define my_complext_layout.xml, and in your other layouts put:
<include layout="#layout/my_complext_layout" />
If you need to run your own code, then you could simply make the root of this layout to be MyComplexView, and you can run code when the view is created.
If you have intended to let your code operate on the layout, then simply implement an OnGlobalLayoutListener and add it to your layout in your views constructor.
Implement a Constructor for the MyComplexView:
public MyComplexView(Context context, AttributeSet attrs){
LayoutInflater inflater = LayoutInflater.from(context);
inflater.inflate(R.layout.header_view, this, true);
mHeaderView = (TextView)findViewById(R.id.header);
if(mHeaderView != null)
mHeaderView.setText("Test");
}
See Custom Components in the developer docs. In particular the Compound Controls section.
Once you've made your java file, in order to refer to it in xml you'll have to use a fully qualified packagename i.e:
<com.yourpackage.YourCustomView
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
Creating a custom view usually is aimed to create a widget which doesn't exist yet. What you're trying to do is to have the same layout repeated at multiple places.
You have severall options to do that according to your context.
If the layout is to be placed in a lest, just create your layout in a separate file, and use it in a ListAdapter. Take a look at the ListView Tutorial for this.
If this layout is a generic layout to be embedded in multiple activities, try using a Fragment instead. Fragments are subparts of an activity, with their own views. Alternatively, you can just embed the layout in severall xml using the tag.
If really you want a custom class and single widget, then you need to extend the View class. Extending a layout means you wan't to organize child widgets differently (for example, organize them in circle). Extending a View, you can have exactly what you want (button, image, text) organized always in the same way. But I won't lie to you, this will mean lot of work.
I have read the android documentation about getLayoutInflator and I am still not understanding what it does. Can someone give me a use case for this method or may be during what time would you want to call getLayoutInflator?
XML Layouts in Android need to be Inflated (parsed into View objects) before they are used. getLayoutInflator() gets you an instance of the LayoutInflator that will allow you to manually inflate layouts for specific uses.
One example being in a Custom ArrayAdapter to populate a ListView with a Custom Layout.
You need to manually inflate and populate your desired Layout for each individual list item in the ArrayAdapter's overridden getView() method.
Use setContentView() when you're in an Activity. That method inflates the layout and displays the selected layout as the view for that Activity. But when you're NOT in an Activity and you need to work with a layout file, you have to inflate it to get access to the view objects in the XML.