Why doesn’t the ‘text’ open custom dialog show?
I felt like an idiot. I started with here Copied this project. The zip file will not be imported into Android Studio 1.0.2. Suggested migrating it to Gradle, but I don’t know how. (I later found the link to do this, I couldn’t implement it, it’s at the bottom of this mess.) )
So I created a new project and cut and pasted 3 xml
and 1 java
file. I finally got compiled.
The following dialog should have been displayed, but when I ran it, it didn’t show the text of text
, which is “Android custom dialog for example“. It only shows icons; As specified in custom.xml
, there is no text at all on the right. I spent a few hours (I obviously didn’t have a good grasp of Android java or xml or connections – I was working on it – but I saw the TextView
named text
I would like to see in java and xml) trying to solve this problem. I hope you all help me now.
The custom.xml
file lists below what I tried (in vain).
EDIT – here’s what I see :
This is AndroidManifest.xml
:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.dslomer64.android">
<application android:allowBackup="true"
android:label="@string/app_name"
android:icon="@drawable/ic_launcher"
android:theme="@style/AppTheme">
<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>
This is the MainActivity .java
:
package com.dslomer64.android;
import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity {
final Context context = this;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonShowCustomDialog);
add button listener
button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
This is main.xml
.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<Button
android:id="@+id/buttonShowCustomDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show Custom Dialog" />
</LinearLayout>
This is custom.xml
:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="@+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="@+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="@+id/image"/>/>
<Button
android:id="@+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="@+id/image"
/>
</RelativeLayout>
I removed a />
and you see two in custom.xml
in TextView
.
I added View
to dialogButton.setOnClickListener
as follows:
dialogButton.setOnClickListener(new View.OnClickListener() {
I commented out the entire dialogButton.setOnClickListener.
I deleted toRightOf... image
line.
I removed all objects from custom.xml
except the TextView
named text
and removed the code for the connection from the MainActivity .java
I debugged it and text contains the text
it should contain, but it doesn’t show.
All to no avail.
This is the app's gradle.build
:
apply plugin: 'com.android.application'
android {
compileSdkVersion 21
buildToolsVersion "21.1.2"
defaultConfig {
applicationId "com.dslomer64.android"
minSdkVersion 15
targetSdkVersion 21
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0'
}
I
know this is trivial for experienced Android programmers, but I just can’t find it. And I found nothing insignificant about the Android GUI.
I hope no one will feel obligated to create a project from all these files. I hope the lost connection is obvious to experienced Android programmers.
When I tried to migrate to Gradle, I used this > located at ‘ build.gradle
file. But it doesn’t like this line :
classpath 'com.android.tools.build:gradle:0.5.+'
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:0.5.+'
}
}
apply plugin: 'android'
dependencies {
compile fileTree(dir: 'libs', include: '*.jar')
}
android {
compileSdkVersion 18
buildToolsVersion "18.0.1"
sourceSets {
main {
manifest.srcFile 'AndroidManifest.xml'
java.srcDirs = ['src']
resources.srcDirs = ['src']
aidl.srcDirs = ['src']
renderscript.srcDirs = ['src']
res.srcDirs = ['res']
assets.srcDirs = ['assets']
}
Move the tests to tests/java, tests/res, etc...
instrumentTest.setRoot('tests')
Move the build types to build-types/<type>
For instance, build-types/debug/java, build-types/debug/AndroidManifest.xml, ...
This moves them out of them default location under src/<type>/... which would
conflict with src/ being used by the main source set.
Adding new build types or product flavors should be accompanied
by a similar customization.
debug.setRoot('build-types/debug')
release.setRoot('build-types/release')
}
}
My gradle version is 2.2.1, but it doesn’t like that either :
classpath 'com.android.tools.build:gradle:2.2.1'
Solution
I see the text on your picture. The background of the dialog
is white, and you specify textColor
as the #FFF
, which is why you can’t see it. Change the color of the background or font of the Dialog
.