How can I view "Hello World!" TextView in Android Studio? - android

I have just started learning development on Android, and I am not able to view "Hellow World!" TextView in the Design Tab.
MainActivity.java:
package com.dummy.demoapp;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dummy.demoapp">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
The TextView appears in the Component Tree, but the preview of the activity is totally blank:
I tried zooming in, but the TextView is just not there.
[EDIT]: The red exclamation (!) gives me the following message:
And refreshing doesn't solve the problem. Any help?

Go to style.xml and change this:
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
to
<style name="AppTheme" parent="Base.Theme.AppCompat.Light.DarkActionBar">

Android Studio sometimes does not appear upto you've success build,
Build->Build&Run, then select your virtual device if there is a helloworld it's okay, it's not your code problem it's related with IDE.

I think it's you default AppTheme with color white , try to add color to TextView
android:textColor="#android:color/black"

Click on the blue icon at the top left corner and then click on force layout. and You will see the Hello World!

Related

The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant)

i am new to Android and i might have a silly / dumb Question. I have got an Activity where i want to dynamically create Multiple Input Fields. The amount of Input Fields is defined by the User.
Because the Input is Styled and Consists of 2 Elements and didnt want to Create these Elements every Time because of Elements have got multiple Parameters that are the same every time. Thats why i created a XML File just for this two Elements which i would like to use in my Programm to Create the Inputs.
View_input.xml
<?xml version="1.0" encoding="utf-8"?>
<merge 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"
android:orientation="vertical"
tools:parentTag="androidx.constraintlayout.widget.ConstraintLayout">
<com.google.android.material.textfield.TextInputLayout
android:id="#+id/textInputLayoutTableName"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:layout_marginStart="20dp"
android:layout_marginEnd="10dp"
android:hint="#string/create_table_name"
android:inputType="text"
app:boxStrokeColor="#color/colorAccent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:placeholderText="#string/create_table_table_name_placeholder"
app:startIconTintMode="src_in">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/textInputName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="text"
android:text="" />
</com.google.android.material.textfield.TextInputLayout>
</merge>
My Target XML (activity_create_table_column_names.xml) looks like this:
<?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=".ui.createtable.CreateTableColumnNamesActivity">
<LinearLayout
android:id="#+id/columnNameInputContainer"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="start"
android:orientation="vertical"
android:padding="20dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/create_table_column_names"
android:textStyle="bold" />
<!-- HERE -->
</LinearLayout>
Where i wrote <!-- HERE -->i want all my Input-Fields to be.
I started with just displaying one Input:
private fun initLayout() {
val container = findViewById<LinearLayout>(R.id.columnNameInputContainer)
val inflater = applicationContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
val view = inflater.inflate(R.layout.view_input, LinearLayout(this))
container.addView(view)
}
But i got the Following Exception:
Caused by: java.lang.IllegalArgumentException: The style on this component requires your app theme to be Theme.MaterialComponents (or a descendant).
at com.google.android.material.internal.ThemeEnforcement.checkTheme(ThemeEnforcement.java:243)
at com.google.android.material.internal.ThemeEnforcement.checkMaterialTheme(ThemeEnforcement.java:217)
at com.google.android.material.internal.ThemeEnforcement.checkCompatibleTheme(ThemeEnforcement.java:145)
at com.google.android.material.internal.ThemeEnforcement.obtainTintedStyledAttributes(ThemeEnforcement.java:115)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:458)
at com.google.android.material.textfield.TextInputLayout.<init>(TextInputLayout.java:417)
My Styles.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight">
...
</style>
</resources>
What am i doing wrong? Is this even the correct way i should programm something like this?
Edit:
Android.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="de.hsos.ma.adhocdb">
<uses-permission android:name="android.permission.INTERNET" />
<application
android:allowBackup="true"
android:appComponentFactory="#string/app_name"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme"
tools:replace="android:appComponentFactory">
<activity
android:name=".ui.createtable.CreateTableColumnNamesActivity"
android:parentActivityName=".ui.homescreen.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".ui.createtable.CreateTableActivity"
android:parentActivityName=".ui.homescreen.MainActivity" />
<activity
android:name=".ui.homescreen.TestTableActivity"
android:parentActivityName=".ui.homescreen.MainActivity">
<!--
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
-->
</activity>
<activity
android:name=".ui.TableShow.TableShowActivity"
android:parentActivityName=".ui.homescreen.MainActivity" />
<activity android:name=".ui.tablelist.LayoutTableListItem" />
<activity android:name=".ui.homescreen.MainActivity">
</activity>
</application>
</manifest>
found the solution ,get your inflator from activity not application just modify your initLayout() like this
// here is the fix
val inflater = getSystemService(Context.LAYOUT_INFLATER_SERVICE) as LayoutInflater
just set the theme on the chip itself using android:theme="#style/Theme.MaterialComponents.Bridge:
<com.google.android.material.chip.Chip
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:theme="#style/Theme.MaterialComponents.Bridge"****
style="#style/CustomFiltersChipChoice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:chipStrokeColor="#F0F"
app:chipStrokeWidth="1dp"
app:textEndPadding="10dp"
app:textStartPadding="10dp"
tools:chipText="my chip"
tools:text="Fortune" />
If you don't want your view to change and with on tool bar try this,
<style name="AppTheme" parent="Theme.MaterialComponents.DayNight.NoActionBar.Bridge"/>

Android Studio cannot initiate the constraint layout

[enter image description here]
At the top right, next to the zoom buttons a blue warning appears which basically says :
Failed to initiate one or more classes
The following classes could not be instantiated:
android.support.constraint.ConstraintLayout
with exception details:
Exception Details java.lang.NoClassDefFoundError: android/support/constraint/R$styleable android.support.constraint.ConstraintLayout.init(ConstraintLayout.java:599) android.support.constraint.ConstraintLayout.(ConstraintLayout.java:576) java.lang.reflect.Constructor.newInstance(Constructor.java:423) android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730) android.view.LayoutInflater.inflate(LayoutInflater.java:492)
android.view.LayoutInflater.inflate(LayoutInflater.java:394)
And also in the text format of activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
On the tools:context=".MainActivity" line .MainActivity appears in red color saying "Unresolved class MainActivity"
This is my AndroidManifest.xml file :
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.kaan.asd">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
I'm new to Android Studio and I've been trying to solve the problem for quite some time now, I have tried clearing caches, clean/rebuild project but neither solved my issue.
I am using the Android Studio 3.3.1 version with gradle version of 4.10.1 on Windows.
I'd appriciate it if anyone could help me fix it.
You have to add the repository of constraint layout in the android directory: app/build.gradle like below:
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
Then sync your project and enjoy this perfect layout.

Theme.AppCompat.Light.DarkActionBar results in no ActionBar showing

I created a project with a blank activity (and then wrote code to put in a basic fragment - empty linear layout) in Android Studio but there is no ActionBar showing. Why?
Device: Emulator (Nexus 5 size), API 19
The main activity extends FragmentActivity.
The Android Studio generated styles.xml:
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
The activity layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fragmentContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
The fragment:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.mycompany.test" >
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".TestActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Instead of extending FragmentActivity, I need the main class to extend AppCompatActivity.
As explained here:
http://developer.android.com/training/basics/fragments/creating.html
If you're using the v7 appcompat library, your activity should instead extend ActionBarActivity, which is a subclass of FragmentActivity (for more information, read Adding the Action Bar).
EDIT
ActionBarActivity is now deprecated. Use AppCompatActivity instead.

How can I get a multiline custom title on android?

I am working on a xamarin project (has no relevance here as far as I can tell - the android layout properties are the same)
I am using Window.SetFeatureInt (WindowFeatures.CustomTitle, ...) to set the title (2 "columns". I wish to make two rows of text though for my title. Is this possible? I have not had much success. I have an upper left, upper right and lower left and lower right TextViews to set.
My previous attempts have ended up with the TextViews overwriting each other.
Here I am designing a XML file with an image, text for title & button. This is the XML file. U can use it however you want, for a complete application or perticular activity.
In case of activity add this activity's theme tag. For multi line just use text view in side the below linear layout, set it as multline true.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="35dip"
android:gravity="center_vertical"
android:background="#android:color/darker_gray">
<ImageView
android:id="#+id/header"
android:background="#drawable/icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Custom Window Text"
android:textColor="#android:color/black"
android:textStyle="bold"/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
Create a theme in Custom_theme.xml,
<resources>
<style name="CustomWindowTitleBarBG">
<item name="android:background">#323331</item>
</style>
<style name="TitleBarTheme" parent="android:Theme">
<item name="android:windowTitleSize">35dip</item>
<item name="android:windowTitleBackgroundStyle"> #style/CustomWindowTitleBarBG</item>
</style>
Modify the AndroidManifest.xml file as follows for applying custom theme
<application
android:icon="#drawable/icon"
android:label="#string/app_name"
android:theme="#style/TitleBarTheme" >
<activity
android:name=".CustomWindowTitle"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
Now in main java file add “requestWindowFeature(Window.FEATURE_CUSTOM_TITLE)” &
“getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title)”
This is the code snippet for CustomWindowTitle.java
package com.example.android;
import android.app.Activity;
import android.os.Bundle;
import android.view.Window;
public class CustomWindowTitle extends Activity
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_CUSTOM_TITLE);
setContentView(R.layout.main);
getWindow().setFeatureInt(Window.FEATURE_CUSTOM_TITLE, R.layout.window_title);
}
}

The emulator isn't showing the XML contents

I have been reading Google's android tutorial and I got a problem... In the tutorial they explain the XML elements (EditText and Button) and in the end they say how to tun the program to see the button + the text field.
The problem is, that the emulator doesn't show them.. just a black screen.
I even tried adding this line to the onCreate function -
System.out.println("Hello World!");
but still the emulator is showing only black screen..
Here's the .java main file - http://pastebin.com/G5J9YjNe
And the XML files (I mentioned what code is what file) - http://pastebin.com/VnRwAfMW
What should I do?
Thanks a lot for everyone who will help!
System.out.println("Hello World!");
does not print any thing on screen. it can seen on Consol. i tried below code and working fine for me.
// Main Activity
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.kuchbhi);
System.out.println("Hello World!");
}
Layout /.xml
<LinearLayout 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:orientation="horizontal" >
<EditText android:id="#+id/text_message"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="enter text" />
<Button android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Send" />
</LinearLayout>
Manifest file ..
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.test"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="8" />
<application
android:icon="#drawable/ic_launcher"
android:label="#string/app_name" >
<activity
android:name="MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
try this code and let me know what u see.
I tried your source code and it is working fine for me. see screen

Categories

Resources