Android Studio XML closing tag indentation - android

I have a TextView defined as
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Semita"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
/>
But I don't like the closing tag on the same indentation level as the attributes. When I hit backspace I'd like it to look like:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Semita"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
/>
which is what I'd like, I instead get:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Semita"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"/>
where the /> is on the same level as the attributes. How can I change this behavior?

Place the cursor before the /> and press Shift + Tab if you are using Mac.
Pressing Tab adds 4 spaces and pressing Shift + Tab moves 4 spaces back.

There is auto-indentation if you have already your closing tags, simply press "Ctrl+Alt+I". This will auto-indent whatever block you are highlighting.

Go to Settings -> Editor -> Code Style -> XML and select Android tab
In Layout Files section check "Insert line break after last attribute"

While Bob's answer should solve your problem. You should consider following this format:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/nav_header_vertical_spacing"
android:text="Semita"
android:textAppearance="#style/TextAppearance.AppCompat.Body1">
</TextView>

see
Settings -> Editor -> Code Style -> XML
for xml formatting & indentation
but you should use node starting and full ending tag for this
there is no option to move short closing node tag to new line with current ide options instead use
// node
<tag attr="val" // starting tag
name="alalala">
i'm body // tag body
</tag> // closing tag
or post your issue here with feature request
https://youtrack.jetbrains.com/issues

Related

Android Studio Kotlin variables

ok, brand new to android studio and kotlin (not programming in general)
installed Android Studio, set up a couple of hardware profiles (and enabled dev mode).
Here is my issue:
Simple app, 'every time you click the button, the number above doubles.'
in the first_fragment.xml file, I rename the ID to 'textDisplayedValue'
in the MainActivity.kt file, I create 2 variables originalValue and newValue.
When I try to set the originalValue to the value in textDisplayedValue, I get an 'Unresolved reference' error
Code
Log
In addition, when I launch the debugger, the 'ok' button is disabled. I assume that is due to the code errors so correct me if I am wrong.
Thanks in advance for the help.
Layout File
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FirstFragment">
<TextView
android:id="#+id/textDisplayedValue"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
app:layout_constraintBottom_toTopOf="#id/button_first"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button_first"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/next"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/textDisplayedValue" />
</androidx.constraintlayout.widget.ConstraintLayout>
binding.fab.setOnClickListener { view ->
val originalValue = 1
val newValue = originalValue * 2
textDisplayedValue = newValue.toString()
Snackbar.make(view, "Value $originalValue changed to $newValue", Snackbar.LENGTH_LONG).show()
}
Try this
Comment the not working code
From the toolbar select
A. Build -> clean project
B. After that Build -> rebuild project
Uncomment the not working code and do this
val originalValue = binding.textDisplayedValue. text() .toString().toLong()
val newValue = originalValue * 2

Android custom fonts (nunito) not set properly in TextView

I am using custom fonts in my android TextView.
Typeface tf = Typeface.createFromAsset(getAssets(),
"nunitomedium.ttf");
txt2= (TextView) findViewById(R.id.txt2);
txt2.setTypeface(tf);
But padding between lines are not properly show.
<TextView
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_10sdp"
android:layout_marginRight="#dimen/_10sdp"
android:layout_marginTop="#dimen/_15sdp"
android:text="You can send emergency blast to friends and family whenever you feel uncomfortable. Once activated, you will be able to send an emergency blast to your safety network within seconds with the pressing of one button."
android:textColor="#color/colorBlack"
android:textSize="#dimen/_15sdp" />
Here is my output :
I also tried with following code :
android:includeFontPadding="false"
but still same issue. Not sure but I also tried to use justifyTextView from Here but same issue. Does anybody faced same issue before?
Thanks in advance.
EDIT :
If i use android:lineSpacingExtra 1st line have more space then other lines. Here is my code and output.
<TextView
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_10sdp"
android:layout_marginRight="#dimen/_10sdp"
android:layout_marginTop="#dimen/_15sdp"
android:lineSpacingExtra="#dimen/_5sdp" <<<<<<<
android:text="You can send emergency blast to friends and family whenever you feel uncomfortable. Once activated, you will be able to send an emergency blast to your safety network within seconds with the pressing of one button."
android:textColor="#color/colorBlack"
android:textSize="#dimen/_15sdp" />
Late to the party on this - we had exactly the same issue, was fixed completely by replacing the .ttf font file with the latest from Google Fonts - there's a bad early version of this font doing the rounds. Don't forget to remove any android:lineSpacingMultiplier fudges you've put in place int he meanwhile.
Add linespacingMultiplier in your XML
<TextView
android:id="#+id/textview_font"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_10sdp"
android:layout_marginRight="#dimen/_10sdp"
android:layout_marginTop="#dimen/_15sdp"
android:text="You can send emergency blast to friends and family whenever you feel uncomfortable. Once activated, you will be able to send an emergency blast to your safety network within seconds with the pressing of one button."
android:textColor="#android:color/black"
android:lineSpacingMultiplier="3"
android:textSize="#dimen/_15sdp" />
Or
Add these line in your code:-
mTextViewFont=(TextView)findViewById(R.id.textview_font);
Typeface tf = Typeface.createFromAsset(getAssets(),
"nunitomedium.ttf");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
mTextViewFont.setLineSpacing((float) 5.5,(float) 1.2);
}
mTextViewFont.setTypeface(tf);
There was issue in the font file that is because of the site from where you downloaded it. I download the file from
dafontfree.net/download-nunito-f92263.htm
and then replace your file from asset folder.
and this is the output of it.

Android studio throws "Too much output to process error" on debug

Android studio throws "Too much output to process error" on debug.
I just started working on android development and trying to debug using my nexus 5 as a connected device.
Being a novice, I thought it was a memory issue and even closed all the background applications. In my single activity I am just displaying a background image and a button.
Please have a look at the layout xml...
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/backgroundimage"
android:weightSum="5"
android:orientation="vertical">
<LinearLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="1"
android:background="#C9BDBD"
android:padding="5dp">
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
android:background="#6C7B8B"
android:textColor="#000000"
android:text="Become An Android Developer"
android:onClick="Congratulations!!! You are now initiated.."/>
</LinearLayout>
your Button's android:onClick should be a method in the Java code.
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="20dp"
android:background="#6C7B8B"
android:textColor="#000000"
android:text="Become An Android Developer"
android:onClick="Congratulations!!! You are now initiated.."/>
something like
android:onClick="doSomthing"/>
and in the Java code:
public void doSomthing(View v){
...
...
...
}
As far as "too much output..." issue is concerned, you can mostly ignore it. Still it would make sense if you remove some of the really unnecessary Log lines from your code, especially from methods that are called repeatedly and frequently.
Another thing, i believe you want to show a text "Congratulations!!! You are now initiated.." when user clicks on button.
So change your xml's android:onClick to android:onClick= "myButtonClicked"
In the activity, implement this function
public void myButtonClicked(){
}
Finally, show the text "Congratulations!!! You are now initiated.." either via dialog (e.g. alertDialog) or in some textView
You may have selected "No Filters" in android monitor which causes "Too much output to process error", Change it to "Show only selected Application" then all other Log line are filtered and will not show in Logcat.

button press in Android app creates casting error

I am new to android development, and have been trying to use the beginner's tutorial as my starting point for developing a simple app. There is one screen, with an image, a row of four buttons, a textbox for the user to enter a pin, and a textview to display results.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/linear_layout_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:contentDescription="#string/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/skytrek"
/>
<LinearLayout
android:id="#+id/linear_layout_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_today"
android:onClick="today_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_tomorrow"
android:onClick="tomorrow_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_this_week"
android:onClick="this_week_click" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="14sp"
android:text="#string/button_next_week"
android:onClick="next_week_click" />
</LinearLayout>
<EditText android:id="#+id/editText1"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:hint="#string/edit_message" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/init_message"/>
</LinearLayout>
I have code to deliver the results I want based on the button pressed - all fine. But then I wanted the user to enter a PIN as well as pressing a button, so I added the EditText control. This threw the following error:
E/AndroidRuntime(28578): Caused by: java.lang.ClassCastException: android.widget.TextView cannot be cast to android.widget.EditText
My Java class:
private String content = null;
private TextView textView1;
private EditText editText1;
public void today_click(View view) {
getPage("today");
}
public void tomorrow_click(View view) {
getPage("tomorrow");
}
public void this_week_click(View view) {
getPage("thisweek");
}
public void next_week_click(View view) {
getPage("nextweek");
}
public void getPage(String strParam) {
editText1 = (EditText) findViewById(R.id.editText1);
String message = editText1.getText().toString();
if (message.equals("4567")) {
content = "PIN recognised";
} else {
content = "PIN not recognised";
}
textView1 = (TextView) findViewById(R.id.textView1);
textView1.setText(content);
}
I thought I had done something silly, using the name of a TextView instead of an EditText control, but I can't find it if I have.
The error is being thrown at the line
getPage("thisweek");
I didn't understand how this line involved views of any sort, but of course the function heading
this_week_click(View view)
does, and when I changed the order of the TextView and the EditText in the XML file (so that the TextView comes first), the error disappeared. It is as if the "view" being passed is not the button, but the nearest widget to the button. I have read
existence of parameter (View view)
but it only seems to confirm my (mis)understanding that a button should be passed as the view parameter. I have also tried cleaning the project, and building a completely new project. What on earth is causing the casting error?
If you're using Eclipse, go to the menu voice "Project" and select "Clean"
Sometimes Eclipse has some problem with ids, by cleaning the project you regenerate them..
Everytime if you make any changes in xml or any interchange of position of views in xml or change of id's,you need to clean build your project.If not you will get this exception.
Hello I have tested your code, your code is fine. Please clean your project from
select your project then click on Project and then Clean and also check the Build Automatically . Your auto generated class R is not generated properly.
Try cleaning your project and run it again..
Eclipse -> Project menu ->Clean
It is because eclipse gets confused when we play around in the xml file ;)

MigLayout for design android Forms

I am on a project. My instructor has told me to use MigLayout to design the forms in android. I am finding it difficult to find proper examples and while Using Eclipse for development its showing errors that android perfix is required to use the attributes.
please help me with some examples..
code that i pasted from the source site,mig4android.
<com.saynomoo.mig4android.MigLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
layout_constraints="wrap 4, debug 1"
column_constraints="[]15[75px]25[min]25[]"
row_constraints="[]15"
>
<TextView android:text="label1" component_constraints="skip"/>
<TextView android:text="label2" />
<TextView android:text="label3" component_constraints="wrap"/>
<TextView android:text="label4" />
<TextView android:text="label5" component_constraints="wmin 30"/>
<TextView android:text="label6" component_constraints="wmin 30"/>
<TextView android:text="label7" component_constraints="wmin 30"/>
</com.saynomoo.mig4android.MigLayout>
The component_constraints gave the error
This error means you forget to add android word before any attribute in xml file.
For example-
id="#+id/my_location"
You need to write android before id like below.
android:id="#+id/my_location"
Your code is perfect. But "skip" in component_constraints appears to be ignored. I had to manually add place holder to the layout to "fill in" empty spots. I tried "skip 1" and that also had no impact.
For more information see following link.
Skip as a constraint is not working

Categories

Resources