I have xml layout which includes a avatar picture a name and a textbox. These are all in one xml file. I would like to add instances these programmically to a linear layout that is nested in a scoll view. Here is my code.
View v = getLayoutInflater().inflate(R.layout.include_message, null);
LinearLayout stallWall = (LinearLayout) v.findViewById(R.id.stallMessages);
R.layout.include_message = My xml
R.id.stallMessages = Linear Layout
I get no errors and i see no items being added.
I would like to read a array and put a include_message in for each message.
First, I would not recommend using x = y = z = a and so on.
It may be working but it's not easy to read and understand.
as codeMagic said, you should use addView() method to achieve this.
here's an example http://androidexample.com/Dynamically_Create_View_Elements__-_Android_Example/index.php?view=article_discription&aid=115&aaid=137
If you are trying to use a layout defined in an xml file, you can user LayoutInflater like this:
View view = getLayoutInflater().inflate(R.layout.yourfile, null);
layout.addView(view);
Related
Can I work somehow with layouts from resources folder in java?
I have a layout in the resorces folder which contains a RelativeLayout which contains a LinearLayout, and any TextViews. And I want to create something like a class instance, and change in example the RelativeLayout background and the texts in the TextViews.
Because then I want to convert this to a Bitmap (for which I have already written the function).
And I don't want it to show up. All this in a static function.
It's possible? How do I create a something like variable/class?
Sorry for any spelling or composition errors I am not a native speaker.
fun getLayout(layoutId: Int, context: Context) : ViewGroup {
return LayoutInflater.from(context).inflate(layoutId, null, false) as ViewGroup
}
This is how you would get a Layout from the resources directory programmatically. Using it like so:
val myLayout = getLayout(R.layout.activity_main, context)
Changing the background
myLayout.setBackgroundColor(Color.RED)
Changing some text
myLayout.findViewById<TextView>(R.id.my_text_view).setText("Hello World")
I would like to access a textview called user_status that's inside a custom xml layout file in order to populate it with some data. I would like to access it from my main activity java class. How do I do this? I have tried some answers already on stack overflow but it's either not working or too outdated.
LayoutInflater inflater = MessageActivity.this.getLayoutInflater();
View inflatedView = inflater.inflate(R.layout.message_row, null);
TextView userMessage= (TextView) inflatedView.findViewById(R.id.user_status);
userMessage.setText("NEW MESSAGE DUMMMY!");
Use
TextView userMessage = inflatedView.findViewById(R.id.user_status);
userMessage.setText("NEW MESSAGE DUMMMY!");
To achieve this in your MainActivity, you should add view first then find TextView or you may include your custom layout file in your MainActivity's xml layout.
Add View can find here
include layout can find here
So i have 2 java files buyClothes.java and searchClothes.java.
At buyClothes by java the layout that is being used is : R.layout.bClothes and at searchClothes the layout that is being used is : R.layout.sClothes
In bClothes there is an Edittext which is being used in buyClothes, in searchClothes i would like to use that Edittext aswell. From what i understood from this website and information i then have to inflate it in searchClothes to be able to use it. I have done that this way..
View view;
LayoutInflater inflater1 = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater1.inflate(R.layout.bClothes, null);
EditText testEdit = (EditText)view.findViewById(R.id.edit1);
testEdit.setHint("testhint");
Now the application doesn't crash but i don't see the hint on the testEdit file, as if it is being ignored or nothing can be done with the view, or i have a slight feeling that te view might return null or something.
Do you have any hints on this matter?
You need to add that inflated view into your main layout or where ever you want.
Just take an example of Linear layout
LinearLayout layout = (LinearLayout)findViewById(R.id.edit_layout);
layout.addView(view);
This may work for you. I hope i got your question right.
I'm a new to android programming. I'm having issues with the layout in my activity. My menu appears like this:
and I've done all the layout work directly through the source code:
enterNameTxt.setText("Enter User Name");
enterNameTxt.setY(200);
enterNameTxt.setX(-600);
userNameTxt.setY(300);
userNameTxt.setX(100);
userNameTxt.setWidth(200);
enterSpeedTxt.setText("Enter Speed");
enterSpeedTxt.setX(-500);
enterSpeedTxt.setY(100);
userSpeedTxt.setX(-400);
userSpeedTxt.setY(700);
userSpeedTxt.setWidth(200);
configButton.setWidth(400);
configButton.setText("Back to Game");
configButton.setY(1000);
and as you can see the speed option doesn't even show up on the screen. And I keep playing with setX, setY, setWidth options but it keeps getting messy.
Is it wrong to do the layout directly through the source code?
I have a two activities but only a layout xml file for one of them. Am I supposed to create another xml file in res/layout section for the menu activity?
I just don't understand when I use the source code and when I should use the layout...
Thanks in advance
Is it wrong to do the layout directly through the source code?
No it's not. But you are strongly advised to use xml layout because it gives you a visual presentation of what you are trying before runtime.
I have a two activities but only a layout xml file for one of them. Am I supposed to create another xml file in res/layout section for the menu activity?
Yes. You have to create an xml file for every activity in your app.
I just don't understand when I use the source code and when I should use the layout...
You should use xml as much as you can. You should only use Java code to set layout attributes when it has to change at runtime. E.g populating a ListView with text from an database or a web service.
Use this code in you java file
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
LinearLayout ll = new LinearLayout(this);
LinearLayout.LayoutParams layout_params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
LinearLayout.LayoutParams box_params = new LinearLayout.LayoutParams(LayoutParams.MATCH_PARENT, 40);
LinearLayout.LayoutParams btn_params = new LinearLayout.LayoutParams(120, 40);
ll.setOrientation(LinearLayout.VERTICAL);
EditText et1 = new EditText(MainActivity.this);
et1.setHint("Enter User name");
EditText et2 = new EditText(MainActivity.this);
et2.setHint("Enter speed");
Button btn= new Button(MainActivity.this);
btn.setText("Back to Game");
btn.setGravity(Gravity.CENTER_HORIZONTAL);
ll.addView(et1, box_params);
ll.addView(et2, box_params);
ll.addView(btn, btn_params);
ll.setGravity(Gravity.CENTER);
rl.addView(ll,layout_params );
}
}
You will get required output but it is prefer to use xml file until you don't need dynamic changes in your UI. It is easy to maintain code and design screens using xml files and if there is no need of large dynamic changes you should use xml files. Through Xml files you can check you code on different resolution through graphical representation of code. You can create a xml file in layout folder and can link it to your activity. It is easy, time saving and provide you more accurate designs... :) Please check screen shot for dynamic creation of your required design.
Happy Coding !!!
I have to build a dynamic form in my activity depending on the data retrieved via HTTP that is popullated from XML.
This could be one or more RadioGroups each with exactly three RadioButtons. After RadioGroups I need to place two EditText and one CheckBox control with submit button afterwards.
I have prepared a LinearLayout with vertical orientation and unique ID to be addressed from code and I expect that I can create a form control within a Java code without defining in android XML layout and adding to this LinearLayout.
I was googling for a few hours but could not find any example how to do this.
Could anyone please provide some example how to create e.g. one RadioGruop with 1-2 RadioButtons and add it to the LinearLayout (that is prepared in XML layout)?
Many thanks for any advice!!!
These widgets can be create like every other widgets:
final Context context; /* get Context from somewhere */
final LinearLayout layout = (LinearLayout)findViewById(R.id.your_layout);
final RadioGroup group = new RadioGroup(context);
final RadioButton button1 = new RadioButton(context);
button1.setId(button1_id); // this id can be generated as you like.
group.addView(button1,
new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT));
final RadioButton button2 = new RadioButton(context);
button1.setId(button2_id); // this id can be generated as you like.
button2.setChecked(true);
group.addView(button2,
new RadioGroup.LayoutParams(
RadioGroup.LayoutParams.WRAP_CONTENT,
RadioGroup.LayoutParams.WRAP_CONTENT));
layout.addView(group,
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
I haven't tested this code, so it may contain some errors. But I hope you'll get the idea.