Why is `text` not showing when opening customdialog?
I feel like an idiot. I copied this project from here . The zip file will not be imported into Android Studio 1.0.2. The tip was to port it to Gradle, but I don't know how. (I later found a link to this. I couldn't implement it. It's at the bottom of this mess.)
So, I created a new project and cut and pasted files 3 xml
and 1 java
. I finally got the compilation.
The dialog below will presumably be shown, but when I run it it doesn't show the text text
, which is an example of a custom Android dialog. "It just shows an icon; the text shouldn't be to the right of it as stated in custom.xml
. I spent hours (I CLEAR NOT good at Java java or xml or connection - I'm working on it) but I can see what I expect to see in java and xml for TextView
named text
) trying to fix this Now I hope you all lend me a hand.
What I have tried (in vain) is given below the file custom.xml
.
EDIT - HERE WHAT I WILL 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 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 deleted one />
where you see two in custom.xml
in TextView
.
I added View
in dialogButton.setOnClickListener
as shown below:
dialogButton.setOnClickListener(new View.OnClickListener() {
I have commented out the whole dialogButton.setOnClickListener
.
I removed the line saying toRightOf...image
.
I removed all objects from custom.xml
except TextView
with the name text
and removed the connected code from MainActivity.java
.
I debugged it and it text
contains text it should be but it is not displayed.
All to no avail.
Here gradle.build
for app
:
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 will be something trivial for a seasoned Android programmer, but I just can't seem to find it. And I don't see anything trivial about the Android GUI.
I hope that no one will be obliged to create a project from all these files. I hope the missing connection will be obvious to an experienced Android programmer.
In my attempt to migrate to Gradle, I used this file build.gradle
found at ` http://tools.android.com/tech-docs/new-build-system/migrating-from-intellij-projects ', but he didn'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')
}
}
The gradle version I have is 2.2.1, but she didn't like it:
classpath 'com.android.tools.build:gradle:2.2.1'
source to share
The "fix" actually depends on Android Studio: I've changed the dropdown menu for Select theme
from AppTheme
to Base.Theme.appCompat
. So the code actually works as shown in the link shown at the beginning. (But it kind of stinks that it took so much effort to figure out the problem!)
HOWEVER, removing the unreasonable #FFF text color is a much better idea. (As I said, I just copied the code.)
Morality? If you are going to set the text color, set the dialog color!
So I did. And I canceled the dropping theme before AppTheme
to show that it didn't really cause the problem.
Here's the gist of what DID works:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
...
android:background="#ff4b14ff">
<TextView
android:id="@+id/text"
...
android:layout_toRightOf="@+id/image"
android:layout_toEndOf="@+id/image"
android:textColor="#FFF"/>
I added ...toEndOf...
as Android Studio recommended doing this for compatibility.
source to share