Android setContentView with Textview - java

I'm completely new to Android application programming, and I was reading through Google's tutorial: http://developer.android.com/training/basics/firstapp/starting-activity.html.
On this page, under the "Display the Message" section, they create TextView object, and use setContentView with the textView object as the argument, to display some text. I was wondering, if I'm understanding correctly, instead of creating the TextView object within the code, can you define it in XML instead? If you define it in XML, would that require you to create a new XML file besides main_activity.xml? Thanks.

You can declare all your layouts and views inside a xml. For the given example, the code would look like the following
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set your parent view
setContentView(R.layout.main_layout);
// Get the message from the intent
Intent intent = getIntent();
String message = intent.getStringExtra(MainActivity.EXTRA_MESSAGE);
// Get the reference to the TextView and update it's content
TextView textView = (TextView)findViewById(R.id.my_text_view);
textView.setText(message);
}
And your main_layout.xml would look like
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:id="#+id/my_text_view"/>

You can arbitrarily create view files (XML files) and specify the primary type of the view and the children it contains. You could create the TextView element within the main_activity.xml view and find it by the relative Id.
That being said in the article in question if you want to have a separate view for just the TextView element then you would likely need another XML file to define that view if you do not want to specify it programmatically.
In a standard application you will likely have a predefined view (XML file) that you will set as the content view and reference elements from within it (as well as possibly add new elements).
It is very flexible, in short to answer your question no, you do not need to generate a new XML view file, you could simply add a TextView to an existing view file or specify it at runtime.

Related

How and Where to define this ImageView in xml?

In this Android Navigation Icon - Profile picture instead of hamburger icon stackoverfolw post
ImageView profileImage = (ImageView)
binding.navView.getHeaderView(0).findViewById(R.id.iv_profile_image);
How an Where to define R.id.iv_profile_image in which xml file ?
I couldn't find the XML code in the example you linked. However, I noticed the following line in the Activity's onCreate() method. This would indicate that the XML file being used is called activity_maps.xml. Most likely this is where the ImageView was placed.
binding = DataBindingUtil.setContentView(this, R.layout.activity_maps);
Assuming this assumption is correct, the ImageView can be declared something like this. The answer to "where" within the XML depends as XML is a declarative language.
<ImageView android:id="#+id/iv_profile_image"
android:layout_width="60dp"
android:layout_height="60dp"
...
android:contentDescription="#string/to_replace" />

How to choose a view group in my android app

I am developing an app and have successfully added a floating action button using the library shown here. The floating button displays well but when i navigate to another fragment through the navigation drawer the button still displays instead i want the button to only display in the activity i created it. I checked for those that had similar issues online and i saw comments...saying that i have to set the View by modifying this line of code found within the method show below.
ViewGroup root = (ViewGroup) activity.findViewById(android.R.id.content);
root.addView(button, params);
Please can tell me how to achieve this, thanks in advance.
Library Method
public FloatingActionButton create() {
final FloatingActionButton button = new FloatingActionButton(activity);
button.setFloatingActionButtonColor(this.color);
button.setFloatingActionButtonDrawable(this.drawable);
params.gravity = this.gravity;
ViewGroup root = (ViewGroup) activity.findViewById(android.R.id.content);
root.addView(button, params);
return button;
}
if you can access the FAB view using findViewById you can simply do:
button.setVisibility(VISIBILITY.GONE);
to hide it, that way you can set visibility back to visible when you go back to that activity.
or if you can access view called 'root' (or can access button via getActivity().findViewById for Fragments)
root.removeView(button);
Set the FloatingActionButton to be invisible by default in the XML file:
<ImageButton android:id="#+id/button" android:visibility="invisible"/>
and make it visible by using the following code in the required activity class:
button.setVisibility(View.VISIBLE);

Create a Custom ImageButton Class with FrameLayout and TextView

I am a newbie, so, any help is appreciated. I read a lot of posts here at StackOverflow and also I searched for my doubt in Google, but it's hard to find a good answer.
Here is what I am trying to do:
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1">
<ImageButton
android:background="#layout/roundcorners"
android:id="#+id/hug"
android:scaleType="centerInside"
android:cropToPadding="false"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="10dip"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:src="#drawable/hug">
</ImageButton>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:textColor="#000000"
android:textSize="15dip"
android:textStyle="bold"
android:paddingBottom="5dip"
android:clickable="true"
android:text="Hug">
</TextView>
</FrameLayout>
Above you guys can see the XML version of what I need.
The point is... I will have many of these FrameLayouts at run time. Every information to fill out the buttons will come from a database.
So, I need to create a Java Class where I can use a loop through all the registers from my database and instantiate a new FrameLayout Class (this class must have an ImageButton and a TextView as you can see from above XML) and just pass parameters, like this:
for (int i = 0; i < mArray.length; i++) {
button = new MyNewImageButton(name, src, text);
}
The above is just to simplify. What I mean is that I will pass parameters from my database when creating an Instance of this class that I am planning to create. Of course, every single button created will be added to the layout.
So... my question is: I know how to do this using XML, but I am REALLY having a hard time to create a class to do this.
Any thoughts? Any help is appreciated.
P.S.: Sorry if I made any mistake in my English, ok? I am a Brazilian. Someday my English will be flawless! Sorry if this question was already answered.
sorry to answer my own question to make another question. I tried to use the comments but there's a limitation in the number of characters, so, I am really sorry.
Hey guys and #blessenm. Well... I tried to use inflater and I came up with the following code:
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// *******************************************
setContentView(R.layout.main);
//this is my main screen
//it's a linearlayout vertical orientation
ViewGroup parent = (ViewGroup) findViewById(R.id.tela_principal);
//these two new LinearLayouts will be one above the other,
//just like two lines
LinearLayout l1 = new LinearLayout(this);
LinearLayout l2 = new LinearLayout(this);
//inside of each linearlayout I set the orientation to horizontal
//so, everytime a picture is inflated from xml, it will fill in one
//linearlayout
l1.setOrientation(LinearLayout.HORIZONTAL);
l2.setOrientation(LinearLayout.HORIZONTAL);
//setting linearlayout parameters, so they fill the whole screen
l1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
l2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
//the first two inflated xml imagebuttons I add to LinearView1
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l1, true);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l1, true);
//the next two inflated xml imagebuttons I add to LinearView2
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l2, true);
view = LayoutInflater.from(getBaseContext()).inflate(R.layout.figurabotao,
l2, true);
//after the above, we should have a grid 2X2
//after linearlayouts are filled, I add them to the main screen
parent.addView(l1, new LinearLayout.LayoutParams(0, 0, 1));
parent.addView(l2, new LinearLayout.LayoutParams(0, 0, 1));
However this is not working. In the errorlog I get the following message:
"Unhandled event loop exception".
Any ideas about what I am doing wrong?
Thanks!
If you are just trying to create a view from the xml and add it to the layout. Just use the LayoutInflater.
Inside the activity use something like
FrameLayout frame = (FrameLayout)getLayoutInfalter.inflate(
R.id.YOUR_VIEW_XML,null);
layout.addView(frame);
If you are trying to create a class extend the frame layout or the the view. Create a constructor which takes your parameters and assign's the required values.
EDIT:
To Acess Elements Inside
If you have set id's to those element, you can access them by
TextView text = (TextView)frame.findViewById(R.id.yourtextview);
Or you can use the child index like
TextView text = (TextView)frame.getChildAt(0);
It sounds like you are looking for a way to create a view class that will be an ImageButton and a TextView wrapped with a FrameLayout.
In this case, you could look into creating your own View class. Probably a View class that extends FrameLayout. See this dev article for more information about how to create a custom view. http://developer.android.com/guide/topics/ui/custom-components.html
Specifically the "Compound Controls" section: http://developer.android.com/guide/topics/ui/custom-components.html#compound

Is it possible to set content view of a single view group within the code?

I want to know whether it is possible to use an xml layout file to define the content of a view dynamically from within the code. When we start an activity we pass it the xml layout to use with the method call setContentView(R.layout.main); but is it possible to use an xml layout file to define a dynamically created ViewGroup such as LinearLayout?
I have a layout xml which shows a score table for a game. Each score that is displayed on this screen needs to be dynamically added via code. I know that it is possible within the code to create a ViewGroup for this score and populate it with all the things I need to make a single score, and then do this every time for each score, and then add them all to the existing UI structure, already defined in the xml layout. What I would like to know is if it is possible to use another xml file to do this?
For example, a layout xml file:
<LinearLayout android:id="#+id/top">
<LinearLayout android:id="#+id/column_heading"/>
</LinearLayout>
In another xml layout file is something like:
<LinearLayout android:id="#+id/row">
<TextView/>
<TextView/>
<TextView/>
<TextView/>
</LinearLayout>
Within the code I would like to do something like the following:
LinearLayout top = (LinearLayout)findViewById(R.id.top);
for (int i = 0; i < num_of_rows; i++) {
LinearLayout row = new LinearLayout(this);
row.setContentView(R.layout.row); //where R.layout.row is the second layout above
// ... dynamically change values as needed
top.addView(row);
}
However .setContentView(...) is not a valid method of LinearLayout. Is there another way to do this? I know I could do it all by code, but that's rather messy and this way would seem to be very tidy and rational..
You should use LayoutInflater for this. Here is a short example
LinearLayout top = (LinearLayout)findViewById(R.id.top);
for (int i = 0; i < num_of_rows; i++) {
LayoutInflater inflater = LayoutInflater.from(this);
LinearLayout row = (LinearLayout)inflater.inflate(R.layout.row, null);
// ... dynamically change values as needed
top.addView(row);
}
LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.row, null);
You can use LayoutInflater's inflate method to inflate an arbitrary layout from resources. If you provide a root view parameter to this method, the inflated layout will be contained within it. This way you can inflate the XML view into your row.

Dynamically adding textbox in android

I am working on an app and need to add a textbox to the View when a button is selected. How can I do that or add any object dynamically. What class do I use or what method do I need to call? Thanks.
You just need to call the addView method to the target view. It's a method inherited from ViewGroup, see [here][2].
[2]:http://developer.android.com/reference/android/view/ViewGroup.html#addView(android.view.View, android.view.ViewGroup.LayoutParams)
One possibility is to define it in your layout XML, and set:
android:visibility="gone"
Then in your code you can do:
TextView myTextBox = (TextView) findViewById(R.id.myTextBoxId);
myTextBox.setVisibility(View.VISIBLE);
and to hide it again:
myTextBox.setVisibility(View.GONE);
Here is the documentation

Categories

Resources