Android: How to modify the layout of a class from another class? - java

I have an app with multiple activities and I would like to modify some of its layout properties to recycle them and avoid creating more activities. I tried with LayoutInflater, but when I modify the layout, the methods atached to that layout won´t work. Before that, I had used LayoutInflater to change the layout of an AlertDialog and it worked fine.
Another thing I could do is send parameters with an Intent and retrieve them in the class of the layout I want to change, but like I said, I have a lot of activities and I think there has to be an easier solution.
I include here my try with LayoutInflater ;act2 is the class with the layout I want to change:
LayoutInflater Inflater = act_main.getLayoutInflater();
lay = Inflater.inflate(R.layout.act2, null);
ImageButton restart = (ImageButton)lay.findViewById(R.id.restart);
restart.setVisibility(View.INVISIBLE);
act_main.setContentView(lay);
PD: Sorry if my English is not that good.

Related

Change textview of my listview

I have been trying for days to find a solution to my problem, but now I've decided to try to ask you. I'm a noob for programming Android, so please forgive me.
I have a main Activity with a Listview in it.
I am using a Simplecursoradapter to feed it with information from my database and a customized layout I made as an XML.
my problem is, I want to change some of the terms or the units that I used in this custom layout, which is not feed through the database.
But if I use settext to the TextViews in there it wont run, the app will crash, because my setContentView is set to another layout for this class, I guess. I have been looking at inflaters, trying to see if I could change the XML programmable, use a string from String.xml and change that. But as far as I can see these are not an option. Later I found this code on StackOverflow
Activity activity = (Activity)getContext();
TextView t = (TextView)activity.findViewById(R.id.txtDisponible);
t.setText("E-ticket validado");
But I can't get this to work because the first line isn't working for me. The Activity won't give me the getcontext method. And even if I did get this to work, I wouldn't know if this would work. Could you guide me in which direction I need to go? As of now, I don't have a class for my ListView, is that the way to go. I want to keep this as simple as I can.
Thank you for your help.
EDIT 1:
Okay i tried to put this in my code:
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.item_list, null);
TextView t = (TextView) view.findViewById(R.id.kmListview);
t.setText("bok");
The app doesn't crash, but it doesn't change the textview either.
Activity extends ContextWrapper class and you should use this or getBaseContext() because there is no method like getContext() which extends ContextWrapper class.
So use this as Context or getBaseContext() in your Actitiy and I think your problem will be resolved.

Why difference LayoutInflater cause difference SearchView Icon

Hello every Android geek, i has using LayoutInflater to customer My Android ActionBar.
but i found that difference LayoutInflater will cause generate difference SearchView Icon
This is my Difference LayoutInflater and capture image
LayoutInflater mInflater1 =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
//You can change mInflater1 =LayoutInflater.from(this); is a same result
LayoutInflater mInflater2 =LayoutInflater.from(getApplicationContext());
My Question is Why difference LayoutInflater cause difference SearchView Icon ?
The problem is that in the second example (which I believe correlates to the first picture), you are using an Application Context to inflate Views instead of an Activity Context.
The framework does a lot of work to ensure that an Activity's Context is correctly configured with the correct screen density, theme, and other such information. An Application is not expected to ever need to worry about displaying content, so it is not configured to do so.
Do not use an Application context to inflate Views or obtain resources. Use an Activity context.
Well its that the context determines from where(theme) the icons are created. I guess your OS and the app has different themes
Read more here http://developer.android.com/reference/android/view/LayoutInflater.html
The Context in which this LayoutInflater will create its Views; most importantly, this supplies the theme from which the default values for their attributes are retrieved.

Programmatically access IntelliJ UI Designer created objects?

I have created a TextView in the UI designer, but I can't figure out how I should access it from the code. I have tried Go To Declaration but that just brings me to the XML file where the TextView is 'made'. Does anyone know how to do this? Help is very much appreciated!
This is independent of the IDE. First you need to "find" the TextView, then you can modify its properties:
TextView myTextView = (TextView) findViewById(R.id.yourid); // The ID is declared in the XML file as android:id atrribute.
myTextView.setText("New Text");
What do you mean by "access it from the code"? If you're talking about navigating from where it's referenced in the code to viewing it in the UI designer, newer versions of Intellij with Android support enabled put tabs at the bottom of the editor when you're editing XML files to let you switch between a text representation and a visual representation of layout files.
If you're talking about how to instantiate the view in code, post some samples of what you've been trying (the most common way is to use a LayoutInflater).
Edit:
Changing the actual text that's displayed in the TextView isn't an IDE-specific issue. You have two ways to do this (well, three if you count the visual and text views of the XML file as separate methods). You can set the text either in the XML file by setting the android:text attribute on the TextView widget, or in the code by calling setText(). Whichever way you decide to do it, you should consider not referring to your text as a raw string but as a String resource as described here.
Edit 2:
OK, you're looking for instructions on how to inflate the view in the first place to get access to it. This is what I answered initially, but here's a little more code. In your Activity (you do have an Activity set up, right?):
LayoutInflater inflater = (LayoutInflater)getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.<your layout ID>, null);
RelativeLayout item = (RelativeLayout) view.findViewById(R.id.<your TextView's id>);

How to create DialogFragment layout programmatically

I need to programmatically create a DialogFragment layout, but I don't know how to proceed. I cannot use xml layout because the DialogFragment will be a part of closed-source JAR file.
Normal Dialogs accept an Activity in their constructor, so it is then possible to instantiate a new Layout like this: layout = new LinearLayout(this);. DialogFragments, on the other hand, do not usually take the Activity as a parameter, so I don't know how to perform this first step.
How should I create the layout?
Is it OK to ask for an Activity in the constructor?
Is creating a layout of DialogFragment any different from creating a layout of normal Dialog?
I will be grateful for any other advice regarding manual DialogFragment design.
How should I create the layout?
How ever you want it to look. If you can't/don't want to use a layout file that you inflate and return from the onCreateView method you'll have to build the entire view hierarchy of your new dialog in code.
Is it OK to ask for an Activity in the constructor?
There is no need for this, the Fragment will get a reference to an Activity, you'll have a reference to that context by using getActivity().
Is creating a layout of DialogFragment any different from creating a
layout of normal Dialog? I will be grateful for any other advice
regarding manual DialogFragment design.
No, it's not different. The documentation for the DialogFragment has a great example on how to build a custom DialogFragment, you should check it out.

Create a composite view in Android

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.

Categories