I'm trying to manipulate components not on an Activity's own layout view. How would I reference them?
I have the resource ID but when I try to reference them using:
DatePicker date = ((DatePicker) findViewById(resID));
.. I get a NullPointerException.
If you want to get views in different layout xml file, the best practice is to use a LayoutInflater to inflate(load) these files. You can do something like this:
LayoutInflater inflater;
View firstView;
View secondView;
Button aboutFirst;
Button aboutSecond;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);//initialize the layout inflater
firstView = inflater.inflate(R.layout.firstview, null); // inflate firstview.xml layout file
secondView = inflater.inflate(R.layout.secondview, null);//inflate secondview.xml layout file
aboutFirst = (Button) firstView.findViewById(R.id.button1);//get button in firstview.xml
aboutSecond = (Button) secondView.findViewById(R.id.button1);//get button in secondview.xml
}
The only solution that I found to this was to change the current content view to the one that holds the View you're trying to find:
setContentView(R.layout.second_layout);
DatePicker date = ((DatePicker)findViewById(resID));
setContentView(R.layout.main);
then changing back to the original layout. I'm still looking for a better solution, but this is what I've got currently.
Related
I have an Activity and its layout. Now I need to add a LinearLayout from another layout, menu_layout.xml.
LayoutInflater inflater;
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout layout = (LinearLayout) inflater.inflate(R.layout.menu_layout, null);
After this, findViewById() returns null. Is there is any solution for that?
Note: I can't put both XML in one place, and using <include> also is not working.
Explanation
When you inflate a layout, the layout is not in the UI yet, meaning the user will not be able to see it until it's been added. To do this, you must get a hold of a ViewGroup (LinearLayout,RelativeLayout,etc) and add the inflated View to it. Once they're added, you can work on them as you would with any other views including the findViewById method, adding listeners, changing properties, etc
Code
//Inside onCreate for example
setContentView(R.layout.main); //Sets the content of your activity
View otherLayout = LayoutInflater.from(this).inflate(R.layout.other,null);
//You can access them here, before adding `otherLayout` to your activity
TextView example = (TextView) otherLayout.findViewById(R.id.exampleTextView);
//This container needs to be inside main.xml
LinearLayout container = (LinearLayout)findViewById(R.id.container);
//Add the inflated view to the container
container.addView(otherLayout);
//Or access them once they're added
TextView example2 = (TextView) findViewById(R.id.exampleTextView);
//For example, adding a listener to the new layout
otherLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Your thing
}
});
Assuming
main.xml contains a LinearLayout with the id container
other.xml is a layout file in your project
other.xml contains a TextView with the id exampleTextView
Try using
layoutObject.findViewById();
i have an activity that displays two buttons , on click of call button i want to show another activity as shown in image.
Please help .How to achieve this
One way to do it is to have a custom layout which you add to each activity with a viewgroup as such:
private View view;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewGroup parent = (ViewGroup) findViewById(R.id.linearLayout1);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.custom_layout,
null);
parent.addView(view);
Then you just need to implement your listeners and whatnot further down :)
You need to create xml file of these two buttons and add that new xml into old one by using Layout Inflator. for e.g
1) your new xml
LayoutInflator buttons = LayoutInflater.from(getBaseContext()).inflate(R.layout.buttons,
currentxml, false);
2) your old xml parent referenced through id -->
RelativeLayout relativeLayout = (RelatveLayout) findViewById(R.id.oldxml);
3) now add..
relativeLayout.addView(buttons);
You can Use Pop up By Adding this in your function. Popuser is a xml file which you want to inflate.
public void popupshow(){
try{
LayoutInflater inflater=(LayoutInflater)SRSDMain.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
Display display=getWindowManager().getDefaultDisplay();
int width=display.getWidth()/2;
int height=display.getHeight()/2;
View pop=inflater.inflate(R.layout.popupuser,null,false);
pop.measure(View.MeasureSpec.UNSPECIFIED,View.MeasureSpec.UNSPECIFIED);
height=pop.getMeasuredHeight();
width=pop.getMeasuredWidth()+50;
pu=new PopupWindow(pop,width,height,true);
pu.showAtLocation(findViewById(R.id.ll3),Gravity.CENTER,1,1);
Button btn=(Button)pu.getContentView().findViewById(R.id.button);
btn.getBackground().setColorFilter(new LightingColorFilter(0xFF505450,0xFF101010));
btn.setTextColor(Color.WHITE);
btn.setTypeface(null,Typeface.BOLD);
btn.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
//anything you want to do
pu.dismiss();
}
});
}
catch(Exception ex){
//Catch The Exception
}
}
}
I need to get the EditText that's defined in an xml layout which is dynamically loaded as a view in a preference dialog i.e. :
public class ReportBugPreference extends EditTextPreference {
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
super.onPrepareDialogBuilder(builder);
builder.setView(LayoutInflater.from(ctx).inflate(R.layout.preference_report_bug_layout,null));
EditText edttxtBugDesc = (EditText) findViewById(R.id.bug_description_edittext); // NOT WORKING
}
}
EDIT : SOLUTION by jjnFord
#Override
protected void onPrepareDialogBuilder(AlertDialog.Builder builder) {
super.onPrepareDialogBuilder(builder);
View viewBugReport = LayoutInflater.from(ctx).inflate(R.layout.preference_report_bug,null);
EditText edttxtBugDesc = (EditText) viewBugReport.findViewById(R.id.bug_description_edittext);
builder.setView(viewBugReport);
}
Since you are extending EditTextPreference you can just use the getEditText() method to grab the default text view. However, since you are setting your own layout this probably won't do what you are looking for.
In your case you should Inflate your XML layout into a View object, then find the editText in the view - then you can pass your view to the builder. Haven't tried this, but just looking at your code I would think this is possible.
Something like this:
View view = (View) LayoutInflater.from(ctx).inflate(R.layout.preference_report_bug_layout, null);
EditText editText = view.findViewById(R.id.bug_description_edittext);
builder.setView(view);
LayoutInflater is needed to create (or fill) View based on XML file in
runtime. For example if you need to generate views dynamically for
your ListView items.
What is the layout inflater in an Android application?
Create your LayoutInflater:
LayoutInflater inflater = getActivity().getLayoutInflater();
Create your view by inflater refered to your_xml_file:
View view= inflater.inflate(R.layout.your_xml_file, null);
Find your object in your layout by id.
TextView textView =
(TextView)view.findViewById(R.id.text_view_id_in_your_xml_file);
Use your object: i.e.
textView.setText("Hello!");
I have an activity with a LinearLayout which I want to add multiple buttons to. These buttons are Custom Views which I inflate. I need to add these custom buttons to my Linear Layout in code. All of this works.
What I cant get to work is to use findViewById in my custom button after I have inflated it so I can set its properties. findViewById returns null.
Activity Code:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.levelbrowser);
LinearLayout uttonLayout = (LinearLayout)findViewById(R.id.ButtonLayout);
LevelBrowserButton button1 = new LevelBrowserButton(this, "Level 1");
ButtonLayout.addView(button1);
}
Custom Button Code:
public LevelBrowserButton(Context context, String name)
{
super(context);
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.levelbrowser_button, this);
TextView nameTextView = (TextView) layout.findViewById(R.id.NameTextView);
// nameTextView = null!
NameTextView.setText("test"); // This will throw an exception
}
I have read through many posts on the internet and still cant get this to work. I have also tried "Clean..." in Eclipse on my project.
I have set up a ViewFlipper in my Android application that will host a series of windows to display messages. Each window should correspond to a different set of messages, similar to multiple open chats. For each window I am using the same window.xml view to bring the view onto the screen, it also has the variable for the EditText need to edit.
For reference I am creating and adding children as follows:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
detector = new GestureDetector(this,this);
setContentView(R.layout.viewflip);
flipper = (ViewFlipper) findViewById(R.id.viewflip);
addChild(flipper);
addChild(flipper);
}
private void addChild(ViewFlipper flip){
int index=0;
View view = getView();
if(flip.getChildCount()==0){
flip.addView(view,index);
}
else{
flip.addView(view,flip.getChildCount());
}
}
private View getView(){
LayoutInflater inflater = this.getLayoutInflater();
View view = inflater.inflate(R.layout.window, null);
return view;
}
As you can see, I am basically duplicating the view (I'm not sure this is the proper approach to my design). So if I were to do something in the onCreate function after adding the children, such as
EditText messageHistoryText = (EditText) findViewById(R.id.messageHistory);
messageHistoryText.append("Testing :\n");
I see the text on both windows.
I thought that something like this would be better:
View v1 = flipper.getChildAt(1);
EditText messageHistoryText2 = (EditText) v1.findViewById(R.id.messageHistory);
messageHistoryText2.append("Testing2 :\n");
but when I use that one, I don't see anything at all. Perhaps there is a mistake on adding the children. Perhaps I can't use the same view, or perhaps I am selectively changing an EditText in an incorrect way.
Tips?
Try the following,
EditText messageHistoryText = flip.getChildAt(j).findViewById(R.id.messageHistory);
where j represents the child position in ViewFlipper.