Change title size of alertdialog in android? - android

about text in the message there is not problem. I can't change (personalize) color, font, style of Title of alertdialog?
What's the way?
thanks!

About font size you can use this:
SpannableStringBuilder ssBuilser = new SpannableStringBuilder("Sample");
StyleSpan span = new StyleSpan(Typeface.ITALIC);
ScaleXSpan span1 = new ScaleXSpan(1);
ssBuilser.setSpan(span, 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
ssBuilser.setSpan(span1, 0, 5, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle(ssBuilser);
builder.show();
or
AlertDialog dialog = new AlertDialog.Builder(this).setMessage("Hello world").show();
TextView textView = (TextView) dialog.findViewById(android.R.id.message);
textView.setTextSize(40);

Check out this link: Creating dialogs
This should help.
Or this Alert Dialog Builder

There is another way to set a new custom view to a dialog title. We can define every custom view such as TextView and add it some custom properties and set it to the dialog title:
AlertDialog.Builder builder = new AlertDialog.Builder(OrderItemsActivity.this);
TextView title_of_dialog = new TextView(getApplicationContext());
title_of_dialog.setHeight(50);
title_of_dialog.setBackgroundColor(Color.RED);
title_of_dialog.setText("Custom title");
title_of_dialog.setTextSize(TypedValue.COMPLEX_UNIT_SP, 15);
title_of_dialog.setTextColor(Color.WHITE);
title_of_dialog.setGravity(Gravity.CENTER);
builder.setCustomTitle(title_of_dialog);
builder.create().show();
Here, I define a dynamic TextView and set some properties to it. Finally, I set it to dialog title using setCustomTitle().

Here is an full example how i would do it. See the last line of code.
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(cntx);
alertDialogBuilder
.setMessage(Html.fromHtml(cntx.getString(R.string.UltimateDemoText)))
.setCancelable(false)
.setPositiveButton(cntx.getString(R.string.Ok), new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
((TextView)alertDialog.findViewById(android.R.id.title)).setTextSize(TypedValue.COMPLEX_UNIT_SP, 9);

Let's take example of this customised Dialog. Below are the files used to make it :
res/layout/dialog.xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"> <EditText
android:id="#+id/et_pc_forDialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tv_pc_dialogTitle"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_marginTop="20dp"
android:ems="10"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:hint="Enter Something to search .."
android:singleLine="true"
android:textColor="#FFFFFF" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/tv_pc_dialogTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginLeft="5dp"
android:text="SEARCH"
android:textColor="#FFFFFF"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/bt_pc_goButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/et_pc_forDialog"
android:layout_centerHorizontal="true"
android:layout_marginTop="14dp"
android:text="GO"
android:textColor="#FFFFFF"
android:textSize="25sp" /> </RelativeLayout>
res/layout/activity_main.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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="177dp"
android:text="Open Dialog" />
</RelativeLayout>
MainActivity.java :
package com.bhavit.stackoverflow;
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.app.Dialog;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
import android.app.Dialog;
import android.graphics.Typeface;
import android.view.Menu;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button openDialog = (Button) findViewById(R.id.button1);
openDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Dialog dialog = new Dialog(MainActivity.this); // here write the name of your activity in place of "YourActivity"
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.dialog);
TextView tv = (TextView)dialog.findViewById(R.id.tv_pc_dialogTitle);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/GOTHIC.TTF"); // here GOTHIC.TTF is the font-file I have pasted in the asset/fonts folder in my project
tv.setTypeface(tf); // Set the typeface like this
Button bt = (Button) dialog.findViewById(R.id.bt_pc_goButton);
bt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Write here, what do you want to do on button click
}
});
dialog.show();
}
});
}
}
This is how you can make a customised dialog, and customise the components of that dialog. Here I have set a custom font to the title of the Dialog.

Related

Android studio APP has stopped

I am new in android learning. Using Android Studio. My app name is Design1. I am learning events so created a event page with two buttons for testing two events, one is toast and another is Alert dialog box.The Toast is working fine but when I click ALERT button to show the alert dialog then a message comes ie "Design1 has stopped".
If I remove the showAlert event from activity_events.xml and events.java files then my works perfect.
events.java
package com.example.borntoflirt.design1;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.Camera;
import android.hardware.camera2.CameraManager;
import android.os.Build;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;
public class events extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_events);
}
public void showToast(View v) {
Context context = getApplicationContext(); // OR getBaseContext()
CharSequence text = "Hi Toast";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
}
public void showAlert(View v) {
Context context1 = getApplicationContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context1);
builder.setMessage("Write your message here.");
builder.setCancelable(true);
builder.setPositiveButton(
"Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder.setNegativeButton(
"No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
activity_events.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_red_light"
android:isScrollContainer="false"
android:orientation="vertical"
android:paddingLeft="15dp"
android:paddingRight="15dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="2dp"
android:layout_weight=".30"
android:background="#drawable/rounded_button"
android:text="Toast"
android:textColor="#000"
android:onClick="showToast"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight=".30"
android:text="Alert"
android:textColor="#000"
android:background="#drawable/rounded_button"
android:layout_margin="2dp"
android:onClick="showAlert"/>
</LinearLayout>
</LinearLayout>
You need to use Activity context. If you use application context, the IllegalStateExpetion will be thrown.
...
new AlertDialog.Buidler(this) // this is a Activity, which is a Context's subclass
....
Use Activity's Context :
AlertDialog.Builder builder = new AlertDialog.Builder(event.this);
Instead Of
Context context1 = getApplicationContext();
AlertDialog.Builder builder = new AlertDialog.Builder(context1);
Try to use getActivity() instead of getApplicationContext().
It has been 7 months but none of the answers worked on me. So this is solution that i found. Try MainActivity.this instead of getApplicationContext() or this.

Create infinite number of EditText on Clicking the button in custom dialog

I have to create a edit text one by one below inside custom dialog while clicking the button.
So far I have tried and created one edit text on clicking the button in custom dialog.
MainActivity.java:
Below I am shown the code what I had tried so far:
Edited:
import android.support.v7.app.ActionBarActivity;
import android.app.Dialog;
import android.os.Bundle;
import android.view.Gravity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
private Button button;
private LinearLayout ll;
EditText et;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) findViewById(R.id.addBtn);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(MainActivity.this);
//setting custom layout to dialog
dialog.setContentView(R.layout.custom_dialog_layout);
dialog.setTitle("Add List");
//adding button click event
Button createEditText = (Button) dialog.findViewById(R.id.button);
createEditText.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// dialog.dismiss();
et= new EditText(MainActivity.this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
dialog.addContentView(et,params);
((LinearLayout) dialog.findViewById(R.id.container)).addView(et);
}
});
dialog.show();
}
});
}
}
custom_dialog_layout.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" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_below="#+id/image"
android:layout_centerHorizontal="true"
android:layout_marginTop="100dp"
android:text="Next" />
</LinearLayout>
</RelativeLayout>
I don't know how to create infinite number of edittext one by one below.Anyone can help me with this.Thank you.
The way I've done it before is to have some containing layout (linearlayout, usually) and add my views to that container. Rough code:
LinearLayout container = (LinearLayout) findViewById(R.id.container);
and in your onclick listener
EditText edit = new EditText(MainActivity.this);
container.addView(edit);
Edit
Okay, after managing to absolutely miss the point of your question in my initial post, I went and checked out what was going on. What's happening (I think) is that it IS actually adding more edittexts, but they're all on top of each other so it looks like that it's not. So here's what you have to do, which plays into what I wrote above:
In your custom dialog view, add a linearlayout container
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/container"></LinearLayout>
and in your onclick, you add your edit texts to the linearlayout, NOT the dialog view
et= new EditText(TestActivity.this);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
((LinearLayout) dialog.findViewById(R.id.container)).addView(et);
and that should work.

debug error : confirm perspective switch

when i click on button add debug error
and opens confirm perspective switch dialog box
shows error in dis line " EditText add = (EditText) d1.findViewById(R.id.add); "
what is the mistake in my code??
xml page
<?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="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Question1" >
</TextView>
<EditText
android:id="#+id/question"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" >
</EditText>
<EditText
android:id="#+id/answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="" >
</EditText>
<Button
android:id="#+id/registerques"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="registerques" >
</Button>
</LinearLayout>
java class
showing error in editext line
package quesansw.the1;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.EditText;
public class Memo extends Activity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final Dialog d1 = new Dialog(this);
Window window = d1.getWindow();
window.setFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND,
WindowManager.LayoutParams.FLAG_BLUR_BEHIND);
d1.setTitle("Register Questions");
d1.setContentView(R.layout.memo);
d1.show();
Button view1 = (Button) d1.findViewById(R.id.view);
Button add = (Button) d1.findViewById(R.id.add);
add.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
EditText add = (EditText) d1.findViewById(R.id.add);
EditText view = (EditText) d1.findViewById(R.id.view);
System.out.println(add.toString());
System.out.println(view.toString());
}
});
view1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(getBaseContext(), View.class);
startActivity(intent);
}
});
}
}
Probably da error in the code is that you never defined an id "add" - you have #+id/TextView01 in the xml "page" (you mean file - and you must tell us which file) but no #+id/add

Multiple TextViews Added in Loop onClick Unexpected Behavior

I have two RelativeLayouts that I am programmatically adding TextViews to from an ArrayList with the intent that, based on the TextView clicked, I can index back into the original ArrayList. For my trivial code example, I'm arbitrarily splitting the items into the two RelativeLayouts.
The code works as expected when the first TextView is clicked. The dialog shows up displaying the correct word and index. However, after closing the dialog, if a different TextView is clicked, the word and index (testWord and testID in my code example) are not updated and only the first TextView's information is displayed. It seems that onClick is only being called on the first click.
Here is an example Java class (I apologize for any formatting errors, it's my first time posting here):
package com.test.test;
import java.util.ArrayList;
import java.util.Scanner;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.graphics.Color;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class AndTestActivity extends Activity {
private final int DIALOG_CASE_ITEM_SELECT=0,CURR_ID=128,P_ID=256,P_HR=512,C_HR=1024;
private final String TEST="This is a test. Only a test.";
private int numPast,testID;
private String testWord;
private ArrayList<String> test;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Scanner s=new Scanner(TEST);
test=new ArrayList<String>();
while(s.hasNext()){
test.add(s.next());
}
int currID=CURR_ID,pID=P_ID,pHrID=P_HR,cHrID=C_HR,tempLen=3;
numPast=0;
RelativeLayout rlP=(RelativeLayout)findViewById(R.id.caseItemListPastLayout);
RelativeLayout rlC=(RelativeLayout)findViewById(R.id.caseItemListCurrLayout);
for(int x=0;x<test.size();x++){
if(x>tempLen){
addToRL(test.get(x),true,rlC,currID,cHrID);
currID++;
cHrID++;
}else{
numPast++;
addToRL(test.get(x),false,rlP,pID,pHrID);
pID++;
pHrID++;
}
}
}
private void addToRL(String sb, boolean current,RelativeLayout rl,int id,int hrID){
TextView citv=new TextView(this);
citv.setText((CharSequence)sb);
citv.setMovementMethod(new ScrollingMovementMethod());
citv.setTextSize(15);
citv.setTextColor(Color.BLACK);
citv.setGravity(Gravity.CENTER_HORIZONTAL);
if(current){
citv.setBackgroundColor(Color.RED);
}else{
citv.setBackgroundColor(Color.WHITE);
}
citv.setPadding(20, 10, 20, 10);
citv.setWidth((int)TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 360, getResources().getDisplayMetrics()));
citv.setId(id);
RelativeLayout.LayoutParams cilp=new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
if(current&&id!=CURR_ID)cilp.addRule( RelativeLayout.BELOW, hrID-1);
else{
if(id!=P_ID)cilp.addRule( RelativeLayout.BELOW, hrID-1);
}
citv.setLayoutParams(cilp);
citv.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
TextView tv0=(TextView)v;
int tvID=tv0.getId()-P_ID;
if(tvID<0){
tvID=(tv0.getId()-CURR_ID)+numPast;
}
testID=tvID;
testWord=test.get(testID);
showDialog(DIALOG_CASE_ITEM_SELECT);
}
});
rl.addView(citv);
View hr=new View(this);
hr.setBackgroundColor(getResources().getColor(R.color.background));
RelativeLayout.LayoutParams hrlp=new RelativeLayout.LayoutParams(LayoutParams.FILL_PARENT, 5);
hrlp.addRule(RelativeLayout.BELOW,id);
hr.setLayoutParams(hrlp);
hr.setId(hrID);
rl.addView(hr);
}
protected Dialog onCreateDialog(int id) {
Dialog dialog;
AlertDialog.Builder builder;
switch(id){
case DIALOG_CASE_ITEM_SELECT:
builder = new AlertDialog.Builder(this);
builder.setMessage("word: "+testWord+" index: "+testID)
.setCancelable(true)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
dialog = builder.create();
break;
default:
dialog = null;
}
return dialog;
}
}
Here is the associated XML for the layout (nothing special, but I thought I'd include it so there's a fully working example):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:layout_height="wrap_content" android:layout_width="wrap_content" android:orientation="vertical"
android:background="#color/background"
android:screenOrientation="portrait">
<ScrollView
android:id="#+id/caseItemCurrScroll"
android:fillViewport="true"
android:padding="10dp"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<RelativeLayout android:id="#+id/caseItemListCurrLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</RelativeLayout>
</ScrollView>
<View
android:id="#+id/caseItemScrollSpacer"
android:layout_width="fill_parent"
android:layout_below="#id/caseItemCurrScroll"
android:layout_height="5dp"
android:background="#color/background"/>
<ScrollView
android:id="#+id/caseItemPastScroll"
android:fillViewport="true"
android:padding="10dp"
android:layout_below="#id/caseItemScrollSpacer"
android:layout_height="wrap_content"
android:layout_width="fill_parent">
<RelativeLayout android:id="#+id/caseItemListPastLayout"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
</RelativeLayout>
</ScrollView>
</RelativeLayout>
That should be a fully "working" example, with the addition of
<color name="background">#3E3E3E</color>
in the strings.xml
onCreateDialog() is only called once, while onPrepareDialog() is called each time the Dialog opens.
Override onPrepareDialog() and call ((AlertDialog) dialog).setMessage() here.

dialog code wrong somewere?

Can someone help with this code. what am looking for is for the button on my dialog to go back a pagewhen the users press the button.so i dont have to press the phone back button thank you.
java file
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.view.View.OnClickListener;
public class DlogActivity extends Activity {
/** Called when the activity is first created. */
Dialog dialog;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
dialog = new Dialog(this);
dialog.setContentView(R.layout.main2);
dialog.setTitle("This is my custom dialog box");
dialog.setCancelable(true);
Button b=(Button)findViewById(R.id.button1);
#SuppressWarnings("unused")Button button01 =(Button)findViewById(R.id.btn2);
Intent intent = new Intent(DlogActivity.this, DlogActivity.class);
startActivity(intent);
finish();
b.setOnClickListener(new OnClickListener() {
public void onBackPressed() {
dialog.cancel();
// Simply Dismiss the dialog to make it close and return to back..
/*What you are using is not a valid construct */
}
public void onClick(View v) {
dialog.show();
}
});
}
}
xml.code
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="Button" />
<ImageView
android:layout_width="236dp"
android:layout_height="220dp"
android:layout_marginRight="100dp" android:background="#drawable/carsee"/>
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</RelativeLayout>
</LinearLayout>
seems like the problem is at below code,
dialog.setContentView(R.layout.main2);
you cannot give layout like this, instead you need to inflate layout to your dialog.

Categories

Resources