Android Studio 3.6 - how use layouts in Design Palette? - android-studio

I am new to Android development and Android studio. I'm using Android Studio 3.6.1 . I'm having trouble understanding the "Layouts" options in the Design Palette. First, I don't see a "RelativeLayout" option. I realize many feel this is superceded by the ConstraintLayout, but it seems to me it should be a choice. Second, I don't see how to even use these layout options. If I drag one to an existing design, nothing happens. I thought maybe it would replace the root layout, but doesn't seem to. It doesn't create a child layout (if such a thing is possible). My code is just a simple "MainActivity" class with "setContentView(R.layout.activity_main);" in the onCreate() method. I have been tinkering with the activity_main.xml file to learn UI concepts. I can type in manual changes to change the layout to RelativeLayout, but it wasn't obvious what the classpath of the RelativeLayout class was, it is not in the same package as the ConstraintLayout class. I'm trying to use the power of the Android Studio IDE to discover options and build code, but I'm not finding how to use it for Layout options. I tried emptying the "activity_main.xml" file and then dragging a Layout from the palette, but nothing happens. I can delete the "activity_main.xml" file and create a new one, but when it prompts for a layout, there is no discovery to help choose one, it seems you have to know the package.
How can these Layouts in the Design Palette actually be used in the IDE? Can RelativeLayout be added to the list? Are there other missing Layouts?
package com.example.test;
import android.os.Bundle;
import com.google.android.material.floatingactionbutton.FloatingActionButton;
import com.google.android.material.snackbar.Snackbar;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import android.view.View;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
<?xml version="1.0" encoding="utf-8"?>
<android.widget.RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</android.widget.RelativeLayout>

Something changed and I can now right-click a Layout and "add to design" or drag it to the design. I can also right-click the Layout in the component tree and convert to another layout, including RelativeLayout. I had added a Calendar component to my design for the heck of it and built and run in the emulator. After that, I deleted the Calendar and found I was able to now drag the layout components or convert the root component. So I'm comfortable with use of Layouts in the Palette. I am still unclear how to discover Layouts when creating a new layout XML file when it asks for "root tag". But I realize you don't need a full path, just the class name it seems, apparently the IDE finds the class. The glue behind the scenes I'll need to learn more about.

Related

Android Studio with Kotlin How Does Control Pass From MainActivity to Another Class

I'm using android studio 4.1.1 and kotlin.
I'm trying to learn programming Android apps using Android Studio and Kotlin. I have a question that a couple of tutorials and searching threads have not answered for me. How does control pass from the MainActivity.kt class to another class, i.e: FirstFragment.kt?
I'm doing fine with other things I'm learning in tutorials, but I cannot figure this out.
Example:
I create a new Basic Activity app in Android Studio with Kotlin. In the java directory, I see these classes: FirstFragment.kt, MainActivity.kt and SecondFragment.kt.
In the res/layout/ directory, I see: activity_main.xml, content_main.xml, fragment_first.xml and fragment_second.xml.
Question 1) When the app loads in an emulator, I see the button and textView from the fragment_first.xml. How does this happen? I cannot see in the code where it says, "load fragment_first.xml".
MainActivity.kt has setContentView(R.layout.activity_main) which loads that layout. activity_main.xml has <include layout="#layout/content_main" />. But from there, I do not know where to find the code that loads the fragment_first.xml layout.
I believe that when fragment_first.xml loads, control passes from MainActivity.kt to FirstFragment.kt, yes? Question 2). Is this because of the onCreate function?
Thanks for helping me to understand this better.
There are multiple ways to load a fragment. Traditionally, you use the FragmentManager to create a Fragment transaction that tells the Activity to place a Fragment into the view hierarchy and start managing its lifecycle.
More recently, they have added the Navigation component, which can be used in place of directly using the FragmentManager.
In the current new project Basic Activity, the layout activity_main.xml includes the layout content_main. In content_main.xml, you'll see that it includes a fragment element that is set to the Fragment (using the name parameter) NavHostFragment, which is a special Fragment that the Navigation component uses to host children fragments that are specified in a navigation XML file. This fragment element also specifies the navigation xml file using the navGraph property. It specifies nav_graph.xml, which you'll find in the res/navigation directory.
If you open nav_graph.xml, you'll see that it has FirstFragment set as the home fragment, so that is what pulls it up as the first fragment that you see.
Basically, the control flow is:
Activity's onCreate() sets content view to activity_main, which includes content_main.
content_main includes a NavHostFragment that is automatically inserted in the view hierarchy so the Activity starts running it.
The NavHostFragment is set in the XML to use nav_graph, which tells it to load FirstFragment first.

Android Studio ActivityMain.kt not finding IDs of TextView, Buttons etc from activity-main.xml

I'm trying to learn programming. Using Android Studio I have created an
TextView and and Button in activity_main.xml and now I'm trining to use this two in MainActivity.kt but Android Studio cannot find the IDs of my Textview nor of my button. Am I doing something wrong?
Thanks for your help and suggestions.
OS ManjaroLinux
Android Studio v. 4.1.1 installed by Flatpack
using Kotlin as programming language
Images of my problem
Button declaration activity_main.xml
TextView declaration activity_main.xml
MainActivity.kt error
Refer ids of components using R.id.*.
For example, if the id is defined by :
android:id="#+id/buttonTxt"
Then refer it by :
val button = findViewById<Button>(R.id.buttonTxt)
The same applies for any resources like strings (R.string.*), colors, dimens etc.

How to get rid of the warning "cannot resolve symbole 'id/andr'" for ExpandableListView"

An app uses ExpandableListActivity, and the usage of ExpandableListView is standard based on the document:
<ExpandableListView
android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:divider="#android:color/transparent"
android:dividerHeight="5dip"
android:layout_marginTop="10dip">
</ExpandableListView>
Android Studio 3.2 has the following warning:
How can I get rid of the warning?
This is probably a typo. Change it to:
"#android:id/empty"
and
"#android:id/list"
See also https://developer.android.com/reference/android/app/ListActivity
ListActivity has a default layout that consists of a single,
full-screen list in the center of the screen. However, if you desire,
you can customize the screen layout by setting your own view layout
with setContentView() in onCreate(). To do this, your own view MUST
contain a ListView object with the id "#android:id/list" (or R.id.list
if it's in code)
I don't know what is ( id="#id/android:list")
give it an normal id ( #+id/androidlist )
and handle it in your code
but if you learn how to work with ExpandableListView
I prefer this link tutorial for you ExpandableListView Link
GL sir

how to get the bottom of a layout in android?

I'm using a relative layout in Android, and when adding a view to the bottom of the screen the view is placed lower than the actual bottom of the screen, where the user cant actualy see it.
view.setY(container.getBottom()-view.getHeight());
container.addView(view);
container is a RelativeLayout. i have also tried to use container.getHeight() which gave me the same result, and all the other solutions i found where using the xml file, which doesnt work for me since i need to add the view dynamically in a random position above the bottom of the screen, meaning
view.setY(container.getHeight()-Common.random.nextFloat()*100-view.getHieght());
this is the XML
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/layout"
tools:context="il.co.ovalley.dashvsponypolice.app.MainActivity">
</RelativeLayout>
and the View im trying to add:
public class GameView extends ImageView{
public GameView(RelativeLayout container) {
super(container.getContext());
m_container=container;
container.addView(this);
}
}
public class Cop extends GameView {
public Cop(RelativeLayout container) {
super(container);
m_isShooting=false;
m_LoadingTime=10;
m_isLoading=false;
m_xSpeed=1.5f;
setY(container.getHeight()-Common.random.nextFloat()*100-getHeight()/*-getPaddingBottom()*/);
float x=(Common.random.nextFloat()*m_container.getWidth());
setX(x);
drwableState=0;
changeDirection();
}
I have tested that on different versions of android and screen sizes and got the same result. so far, the only solution I found was subtracting an arbitrary int from the container's height which seems ok on one device and hope for the best for the others, I'm sure there is a better solution.
thnx
Why don't you just place the view at the bottom of the screen with ALIGN_PARENT_BOTTOM and then apply a random padding?
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
view.setPadding(0, 0, 0, randomValue); // left, top, right, bottom
container.addView(view, params);
All this calculating with the view.getHeight() is not good. Doing it like this works without you having to manage any of that yourself.

Android "tools" namespace in layout xml documentation

Per the question here,
What's "tools:context" in Android layout files?
The 'tools' namespace reference (xmlns:tools="http://schemas.android.com/tools") has begun to appear in my layouts recently, and I want to know more. The original post only described the 'tools:context' attribute, but I have also noticed usage of the "tools:listitem" attribute appearing when I have designated a preview layout item for a listview, i.e.
<ListView
android:id="#+id/lvCustomer"
tools:listitem="#layout/customer_list_item" >
</ListView>
Are there more elements?
What brought me to this 'tools' namespace is that I want to be able to have 'preview-only' text (i.e. in a TextView or EditText) when using the layout designer in eclipse.
Currently, I assign the 'text' or 'hint' property for previewing text when arranging my layouts... but then I always have to remember to clear the preview value from within the code.
Ideally, instead of
<string name="preview_customer_name">Billy Bob's Roadhouse Pub</string>
...
<TextView
android:id="#+id/tvCustomerName"
android:text="#string/preview_customer_name"
</TextView>
have a something like:
<TextView
android:id="#+id/tvCustomerName"
tools:previewText="#string/preview_customer_name"
</TextView>
Thanks-
We've just added support for designtime attributes like this in Android Studio 0.2.11. See http://tools.android.com/tips/layout-designtime-attributes for more.
Think of them as design time helpers only.They do not get processed in actual view rendering at run time.
For example you want to set background of some view in your layout design when working on android studio so that you can make clear distinction where that particular view is.So you would normally do that with
android:background="#color/<some-color>"
Now risk is that sometimes we forget to remove that color and it gets shipped in apk.
instead you can do as follows:
tools:background="#color/<some-color>"
These changes will be local to android studio and will never get transferred to apk.
And also check out http://tools.android.com/tech-docs/tools-attributes for more options.
You will find tool attribute when you set object in graphical layout.
Listview (in graphical mode) -> right Click -> Preview List Content -> Choose Layout...
produces:
tools:listitem="#layout/customer_list_item"
See in layout XML below. There are 2 namespace in use "xmlns:android" and "xmlns:tools".
Tools namespace is used when the developer wants to define placeholder content that is only used in preview or in design time. Tools namespace is removed when we compiled the app.
So in the code below, I want to show the placeholder image (image_empty) that will only be visible at design time, and image1 will the actual image that will be shown when the application launch

Resources