Set the position of the custom dialog box in android? - android

We have create a custom dialog box.
How do we set the custom dialog box at the Middle of the center position.
Sample code is dialog.getWindow().setGravity(Gravity.CENTER);
Now custom dialog box is displayed at the Top Of the center position.
Thanks in advance.

TestAActivity.java
package com.TestA;
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 TestAActivity extends Activity {
private Button btn;
/** Called when the activity is first created. */
// TwitPicResponse tpResponse = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
show_Dialog();
}
});
}
protected void show_Dialog() {
// TODO Auto-generated method stub
//Context mContext = getApplicationContext();
Dialog dialog = new Dialog(TestAActivity.this);
dialog.setContentView(R.layout.test);
dialog.setTitle("Custom Dialog");
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
dialog.show();
}
}
enter code here
test.xml
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/layout_root"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
>
<ImageView android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="10dp"
/>
<TextView android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:textColor="#FFF"
/>
</LinearLayout>
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" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello" />
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>

see below links it is default in CENTER.
http://developer.android.com/guide/topics/ui/dialogs.html#CustomDialog

Related

How to use two or multiple xml file in android

i am developing egreeting card application.i have two xml file in first xml file it contain buttons and list view.when i click on button list will display this list occupy the space of activity.So we cant put any widget in xml file.When click on list item i want to shows ImageView at same place of list .For to achieve this i created another xml and put ImageView inside that.now when i click on ListItem it new xml shows but not in same activiy.how could solve this.i attached full source code here. here is my xml files
main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/backcolor"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/linearback" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="40dp"
android:layout_height="30dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:background="#drawable/list_items" />
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="50dp"
android:layout_height="40dp"
android:layout_marginLeft="190dp"
android:layout_marginTop="6dp"
android:background="#drawable/search" />
</LinearLayout>
<!--
<LinearLayout
android:id="#+id/mylayouttwo"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView android:id="#+id/imgview1"
android:layout_width="match_parent"
android:layout_height="300dp"></ImageView>
</LinearLayout>
-->
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
images.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#color/backcolor">
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="234dp"
android:layout_marginTop="60dp"
android:src="#drawable/ic_launcher" />
</LinearLayout>
package com.triumph.greetings;
import android.R.color;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ViewSwitcher;
public class Launch extends Activity implements OnClickListener {
boolean isListVisible=true;
ImageButton img1,img2;
ListView lv;
String[] festivales= new String[]{"Christmas","New year","BirthDay","Easter","Wedding","Valentine","Love","Nature"};
ImageView img;
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
img1=(ImageButton)findViewById(R.id.imageButton1);
img2=(ImageButton)findViewById(R.id.imageButton2);
img1.setOnClickListener(this);
img2.setOnClickListener(this);
lv=(ListView)findViewById(R.id.listView1);
ArrayAdapter<String> adapter=new ArrayAdapter<String>(Launch.this,android.R.layout.simple_list_item_1,festivales);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> arg0, View v, int postion,
long arg3)
{
// TODO Auto-generated method stub
lv.setVisibility(View.INVISIBLE);
isListVisible=false;
Toast.makeText( Launch.this, ((TextView) v).getText(),Toast.LENGTH_LONG).show();
//state=true;
displayimages();
}
private void displayimages()
{
// TODO Auto-generated method stub
// fetch the images from server and shows in GridView
// call linear layout
setContentView(R.layout.images);
img=(ImageView)findViewById(R.id.imageView1);
img.setBackgroundColor(color.background_light);
}
});
}
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
switch(v.getId())
{
case R.id.imageButton1:
if(isListVisible)
{
lv.setVisibility(View.INVISIBLE);
isListVisible=false;
}
else
{
lv.setVisibility(View.VISIBLE);
isListVisible=true;
}
break;
//Toast.makeText(getBaseContext(), "Hi", Toast.LENGTH_LONG).show();break;
case R.id.imageButton2:
break;
}
}
}

How to connect xml with a class on android

I have created a new .xml file named "new_game.xml" and a class named "New_Game.java"
These are not Main activity
new_game.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"/>
<Button android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click"
android:id="#+id/button"
android:layout_marginTop="27dp"
android:layout_alignRight="#+id/editText"/>
<EditText android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="#+id/editText"
android:layout_marginTop="16dp"
android:layout_below="#+id/button"
android:layout_marginLeft="11dp"/>
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:paddingTop="50dp"
android:paddingBottom="50dp"
android:paddingLeft="50dp"
android:paddingRight="50dp"
android:background="#drawable/ic_launcher"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/button"/>
</RelativeLayout>
New_Game.java:
package com.example.logosquiz;
import android.app.Activity;
import android.app.ListActivity;
import android.app.TabActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class New_Game extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_game);
Button button1 = (Button)findViewById(R.id.button);
final EditText editText1 = (EditText)findViewById(R.id.editText);
ImageView imageView1 = (ImageView)findViewById(R.id.imageView);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
editText1.setText("Hello Android");
}
});
}
Please explain it very well
I put on AndroidManifest.xml this
<activity android:name=".New_Game"> /activity>
and this on the event onClick
Intent myIntent = new Intent(v.getContext(), New_Game.class);
startActivityForResult(myIntent, 0);
Your XML is wrong. Remove the last / from the first RelativeLayout in your 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">
instead of
<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"/>
Although Eclipse should have warned you about this.
your code is correct but
button android:layout_marginTop="27dp"
edittext android:layout_marginTop="16dp"
edit text is placed on the button so when you click you are clicking on edittext not button change edittext marginTop to 30dp and check
public class New_Game extends Activity {
EditText editText1;
#override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.new_game);
Button button1 = (Button)findViewById(R.id.button);
editText1 = (EditText)findViewById(R.id.editText);
ImageView imageView1 = (ImageView)findViewById(R.id.imageView);
button1.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
editText1.setText("Hello Android");
}
});
}

Image in TextView With clickable

I am trying to put image in TextView. I had done it using image span but my problem is i cannot put onClickListener in every image (in same TextView, there are multipal image in same TextView). Please suggest me how can i do that.
make a custom.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/thumbnail_view"
android:src="#drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView android:id="#+id/message_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/thumbnail_view"
android:textSize="18sp"
android:text="MyText" />
</RelativeLayout>
then in main.xml , include this custom.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_horizontal">
<include
android:id="#+id/customView"
layout="#layout/custom"/>
</LinearLayout>
This is my mainActivity.class
package com.example.test;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends Activity implements OnClickListener {
private String TAG = MainActivity.class.getSimpleName();
ImageView img;
ImageView img1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView txt = (TextView)findViewById(R.id.message_view);
img = (ImageView) findViewById(R.id.thumbnail_view);
img1 = (ImageView) findViewById(R.id.thumbnail_view1);
img.setOnClickListener(this);
img1.setOnClickListener(this);
}
#Override
public void onClick(View v) {
if(v== img){
// do something for img
}
else if (v== img1){
//do something for img1
}
}
}
make a custom view instead. That would be much easier.

How to show same image multiple time in row?

I am developing an application for children. Basic maths for children to learn with fun.
In this app I am using apple image for displaying on the screen. for addition when they give 1st input and second input it should show apple images as specified in 1st and 2nd input.
Is any one know solution for this or any one having source code thn help me
this is the way you can do it ,quite simple though :)
your xml should look like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/input1" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/input2" />
<Button android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="Result"/>
<HorizontalScrollView
android:id="#+id/scroll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout android:id="#+id/linear1"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:id="#+id/scroll2"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear2"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
<HorizontalScrollView
android:id="#+id/scroll3"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear3"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</LinearLayout>
</HorizontalScrollView>
</LinearLayout>
and you Activity looks like this
package com.example.stackanswer;
import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.HorizontalScrollView;
import android.widget.ImageView;
import android.widget.LinearLayout;
public class AppleActivity extends Activity {
private LinearLayout linear1;
private LinearLayout linear2;
private LinearLayout linear3;
private HorizontalScrollView scrollbar1;
private HorizontalScrollView scrollbar2;
private HorizontalScrollView scrollbar3;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_apple);
final EditText input1 = (EditText)findViewById(R.id.input1);
final EditText input2 = (EditText)findViewById(R.id.input2);
scrollbar1 = (HorizontalScrollView)findViewById(R.id.scroll1);
scrollbar2 = (HorizontalScrollView)findViewById(R.id.scroll2);
scrollbar3 = (HorizontalScrollView)findViewById(R.id.scroll3);
linear1 = (LinearLayout)findViewById(R.id.linear1);
linear2 = (LinearLayout)findViewById(R.id.linear2);
linear3 = (LinearLayout)findViewById(R.id.linear3);
Button output = (Button)findViewById(R.id.button);
output.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String input1A = input1.getText().toString();
String input1B = input2.getText().toString();
int value1 = Integer.parseInt(input1A);
int value2 = Integer.parseInt(input1B);
Log.i("1",""+value1);
Log.i("2",""+value2);
linear1.removeAllViews();
linear2.removeAllViews();
linear3.removeAllViews();
linear1.setScrollContainer(true);
ImageView image ;
for(int i=0;i<value1;i++){
image = new ImageView(getApplicationContext());
image.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT) );
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear1.addView(image);
scrollbar1.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
for(int i=0;i<value2;i++){
image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear2.addView(image);
scrollbar2.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
int sum = value1+value2;
for(int i=0;i<sum;i++){
image = new ImageView(getApplicationContext());
image.setImageResource(R.drawable.ic_launcher);
Log.i("i", i+"");
linear3.addView(image);
scrollbar3.fullScroll(HorizontalScrollView.FOCUS_RIGHT);
}
}
});
}
}
Complete source code here

Button click event issure, layout,and parsing issue?

There are three problems I am facing:
When I click(+)button, the edit boxes are going underneath the button, whereas i want them to be displayed above.
currently displaying:
Want like this:
<edit text1> <edit text2>
<edit text3> <edit text4>
<edit text5> <edit text6>
<edit text7> <edit text8>
<button> <button2>
As you can see, I tried parsing the edit text value which i got from the xml, into the docalc() function, and displaying the value in textbox. but its not working out. showing me nothing.
can I parse values in dostuff, if yes how will i inter-relate those in docalc?
Thanks in advance...
Java Code:
import android.app.Activity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;
public class PlusbuttonActivity extends Activity
implements OnClickListener {
TextView tt;
TextView j;
EditText amount1;
EditText amount2;
double x=0;
double y=0;
double a=0;
double z=0;
double b=0;
Button btnButton;
/** Called when the activity is first created. */
private LinearLayout root;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// modified
amount1=(EditText)findViewById(R.id.edittext1);
amount2=(EditText)findViewById(R.id.edittext2);
// modified
View btnButton =(Button) findViewById(R.id.button_next);
Button mButton = (Button) findViewById(R.id.button);
mButton.setGravity(Gravity.CENTER);
tt=(TextView)findViewById(R.id.tt);
j=(TextView)findViewById(R.id.j);
root = (LinearLayout) findViewById(R.id.linearLayout);
mButton.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.button:
View view = doStuff();
addViewToRoot(view);
break;
case R.id.button_next:
View view1 = doCalc();
addViewToRoot(view1);
break;
}
}
private View doCalc() {
// TODO Auto-generated method stub
x=Double.parseDouble(amount1.getText().toString());
y=Double.parseDouble(amount2.getText().toString());
z=(x*703);
tt.setText(Double.toString(z));
return tt;
}
private View doStuff() {
EditText t = new EditText(PlusbuttonActivity.this);
t.setGravity(Gravity.LEFT);
t.setWidth(250);
EditText a = new EditText(PlusbuttonActivity.this);
a.setGravity(Gravity.RIGHT);
a.setWidth(250);
LinearLayout l = new LinearLayout(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
// t.setBackgroundColor(0xffCECECE);
a.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
l.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
l.addView(t);
l.addView(a);
return l;
}
private void addViewToRoot(View v){
root.addView(v);
}
}
xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/linearLayout">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_weight="1"
android:text="Units"
android:id="#+id/Units"
/>
<TextView android:layout_height="wrap_content"
android:gravity="right"
android:layout_width="wrap_content"
android:layout_weight="1"
android:text="Grades"
android:id="#+id/j"></TextView>
</LinearLayout>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="250px"
android:layout_height="wrap_content"
android:id="#+id/edittext1">
</EditText>
<EditText
android:layout_height="wrap_content"
android:id="#+id/edittext2" android:layout_width="150dp">
</EditText>
</LinearLayout>
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:layout_width="250px"
android:layout_height="wrap_content"
android:id="#+id/edittext3">
</EditText>
<EditText
android:layout_height="wrap_content"
android:id="#+id/edittext4" android:layout_width="150dp">
</EditText>
</LinearLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<Button android:layout_height="wrap_content"
android:gravity="center" android:id="#+id/button" android:text="+" android:layout_width="wrap_content"></Button>
<Button
android:id="#+id/button_next"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:text="CALCULATE"
>
</Button>
</RelativeLayout>
<TextView android:text="TextView"
android:id="#+id/tt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"></TextView>
</LinearLayout>
First, set your LinearLayout's id to something other than main. Like root. Main is the name of the xml file containing your layout, not the id of the LinearLayout item.
Second, Instead of using this in the constructor for the EditText, use PlusbuttonActivity.this. So change the line you use to create the EditText to this:
EditText t = new EditText(PlusbuttonActivity.this);
You have to do this because when you're in the onClick method your technically in the OnClickListener class, and this refers to the OnClickListener object. By using PlusbuttonActivity.this you're clarifying that you mean the PlusbuttonActivity object that you're currently in, not the OnClickListener.
Third, to address the other issue, you can't reference root from you anonymous OnClickListener class. Instead of calling root.addView(t), you can extract that out to a method. Putting it all together we get this:
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
public class PlusbuttonActivity extends Activity {
/** Called when the activity is first created. */
LinearLayout root;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button mButton = (Button) findViewById(R.id.button1);
root = (LinearLayout) findViewById(R.id.root);
mButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
EditText t = new EditText(PlusbuttonActivity.this);
t.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
addViewToRoot(t);
}
});
}
private void addViewToRoot(View v){
root.addView(v);
}
}
Change the id of LinearLayout in main.xml, use different name.
Save & Clean project.
Fix like #Kurtis Nusbaum mentioned above!

Categories

Resources