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;
}
}
}
Related
I have a popup menu inside every item on a listview. When you click the imageview to the left (settings img with 3 dots) a popup menu should showup. however, I'm getting error
Could not find a method showPopupMenu(View) in the activity class android.app.Application for onClick handler on view class android.widget.ImageView with id 'settings_img'
does anyone know what this error is. it is obvious that can't find the method, but is it because it can't find the class/view? if so how can I fix it? thanks
main_activity
package com.example.george.hostscanmenus;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List hostList;
ArrayAdapter<String> hostAdapter;
PopupMenu popup;
String subnet = "192.168.10.";
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hostList = new ArrayList<String>();
//populating host addresses
for(int i = 0; i < 255; i++) {
hostList.add(subnet+i +"-aa:bb:00:cc:33:ee");
}
//inflating adapter
listView = (ListView) findViewById(R.id.scan_list);
hostAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.host_item, R.id.ip_address, hostList);
listView.setAdapter(hostAdapter);
}
/* code for popup menu in listview */
public void showPopupMenu(View v) {
popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.change_icon:
Toast.makeText(getApplicationContext(), menuItem.getTitle(), Toast.LENGTH_SHORT).show();
return true;
case R.id.notifications:
Toast.makeText(getApplicationContext(), menuItem.getTitle(), Toast.LENGTH_SHORT).show();
return true;
default:
return MainActivity.super.onContextItemSelected(menuItem);
}
}
});
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.host_menu, popup.getMenu());
popup.show();
}
}
array adapter
package com.example.george.hostscanmenus;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by george on 8/11/17.
*/
public class HostAdapter extends ArrayAdapter<String> {
public HostAdapter(Context context, int num, ArrayList<String> allHost) {
super(context, 0, allHost);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
String host = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
}
// Lookup view for data population
ImageView nodeHostImgView = (ImageView) convertView.findViewById(R.id.host_icon);
TextView nodeIpTxtView = (TextView) convertView.findViewById(R.id.ip_address);
TextView nodeMacTxtView = (TextView) convertView.findViewById(R.id.mac_address);
ImageView nodeArrowImgView = (ImageView) convertView.findViewById(R.id.port_scan_arrow);
// Populate the data into the template view using the data object
nodeHostImgView.setImageResource(R.drawable.ic_computer_white_24dp);
nodeIpTxtView.setText(host.substring(0,host.indexOf("-")));
nodeMacTxtView.setText(host.substring(host.indexOf("-")));
nodeArrowImgView.setImageResource(R.drawable.ic_keyboard_arrow_right_white_24dp);
// Return the completed view to render on screen
return convertView;
}
}
main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/scan_list_linear"
tools:context=".MainActivity">
<!-- labels for ip / mac addres list -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_label">
<TextView
android:id="#+id/host_port_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Host"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="#color/colorAccent"/>
<TextView
android:id="#+id/ip_open_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.80"
android:textSize="20dp"
tools:text="IP / MAC"
android:paddingLeft="10dp"
android:textColor="#color/colorAccent"/>
<TextView
android:id="#+id/scan_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Scan"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="#color/colorAccent"/>
</LinearLayout>
<!--Listview to display scan output-->
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scan_list"/>
</LinearLayout>
list item xml
<?xml version="1.0" encoding="utf-8"?>
<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="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/text_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/settings_img"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.10"
android:padding="3dp"
android:src="#drawable/ic_action_more_vert"
android:onClick="showPopupMenu"/>
<ImageView
android:id="#+id/host_icon"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:padding="5dp"
android:src="#drawable/ic_computer_white_24dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:orientation="vertical"
android:paddingLeft="0dp"
>
<TextView
android:id="#+id/ip_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
tools:text="192.168.10.100"
/>
<TextView
android:id="#+id/mac_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
tools:text="aa:bb:cc:00:11:22"
/>
</LinearLayout>
<ImageView
android:id="#+id/port_scan_arrow"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:padding="5dp"
android:src="#drawable/ic_keyboard_arrow_right_white_24dp"/>
</LinearLayout>
</RelativeLayout>
menu xml
<!-- listview menu for host scan options eg:
change hostname, icon, notification etc -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/change_icon"
android:title="change icon"
/>
<item android:id="#+id/notifications"
android:title="set notification"
/>
</menu>
You should define your settings_img on your adapter getView and add a click listener for each element.
example:
ImageView settingsImgView = (ImageView) convertView.findViewById(R.id.settings_img);
settingsImgView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
context.showPopupMenu() //or popupmenu logic in here if you want
}
});
Usually, when it comes to the adapter, I pass the activity itself instead of the context. Then I pass the activity's context to super() and keep the activity in the field variable. That way I'm able to use all public methods of the parent activity.
This question already has answers here:
Custom toast on Android: a simple example
(18 answers)
Closed 7 years ago.
I am trying to run a toast app. where one button shows a normal toast message and the second button shows a custom toast with an image (ic_launcher) and some text.
The normal toast works fine without the second button code inserted. For the custom toast.
1.custom_layout and 2. custom_toast_layout_id are not recognized and the application shows an error.
Here is the xml layout:
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity">
<TextView android:text="#string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv1"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/ivImage"
android:layout_marginRight="5dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press"
android:id="#+id/button"
android:layout_below="#+id/tv1"
android:layout_centerHorizontal="true"
android:layout_marginTop="44dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Press Again"
android:id="#+id/button2"
android:layout_marginTop="56dp"
android:layout_below="#+id/button2"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:id="#+id/tvToast"
android:textColor="#000"/>
</RelativeLayout>
This is my activity:
package com.example.rahulshukla.myapplication;
import android.app.Activity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import org.w3c.dom.Text;
public class MainActivity extends Activity {
TextView helloText;
Button press, press2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initialize();
press.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(),"This is a Toast", Toast.LENGTH_LONG).show();
}
});
press2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast, (ViewGroup) findViewById(R.id.custom_toast_layout_id));
ImageView image = (ImageView)findViewById(R.id.ivImage);
image.setImageResource(R.drawable.ic_launcher);
TextView toastText = (TextView)findViewById(R.id.tvToast);
toastText.setText("Button is clicked");
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL,0,0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
});
}
private void initialize() {
helloText = (TextView)findViewById(R.id.tv1);
press = (Button)findViewById(R.id.button);
press2 = (Button)findViewById(R.id.button2);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
}
Add id here . set id android:id="#+id/custom_toast_layout_id" & set this layout name custom_toast.xml.Then clean -Rebuild-Restart
<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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:id="#+id/custom_toast_layout_id">
I have an app in which i want to assign a bitmap named original to an image view whose id is img1 in the layout by taking the position of the image view in the layout(here it is child at position 0). When i click the save button,i want to assign bitmap named original to image view whose id is img1 in the layout.I know we can assign by using ourImageView.setImageBitmap(bitmaptobeassigned). But in my app,my situation is to assign the bitmap original by means of View object child only.Please help me. My main class is
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class Test extends Activity
{
Button save;
Bitmap original;
FrameLayout frame;
ImageView background;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
save=(Button)findViewById(R.id.button3);
frame=(FrameLayout)findViewById(R.id.frame);
background=(ImageView)findViewById(R.id.img1);
background.setOnTouchListener(new myTouchListener());
original=BitmapFactory.decodeResource(getResources(), R.drawable.parrot);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
View child=frame.getChildAt(0);
//Here i obtained image view as a child.But I do not know how to assign bitmap to it.
//please help me how to write code here.
}
});
}
}
and my xml file is
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/vg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/frame"
>
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="matrix"
/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save" />
</LinearLayout>
I have found my answer.my main class is
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.FrameLayout;
import android.widget.ImageView;
public class Test extends Activity
{
Button save;
Bitmap original;
FrameLayout frame;
ImageView background;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
save=(Button)findViewById(R.id.button3);
frame=(FrameLayout)findViewById(R.id.frame);
background=(ImageView)findViewById(R.id.img1);
background.setOnTouchListener(new myTouchListener());
original=BitmapFactory.decodeResource(getResources(), R.drawable.parrot);
//background.setImageBitmap(original);
save.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView child=(ImageView) frame.getChildAt(0);
child.setImageBitmap(original);
}
});
}
}
and my xml is
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/vg"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/frame"
>
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:scaleType="matrix"
/>
</FrameLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom" >
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="save" />
</LinearLayout>
package com.example.kym;
import android.content.SharedPreferences;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.support.v7.app.ActionBarActivity;
import android.widget.ImageView;
import android.widget.TextView;
public class Accumulation extends ActionBarActivity
{
TextView tv1,tv2;
#Override
protected void onCreate(Bundle savedInstanceState)
{
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
tv1=(TextView)findViewById(R.id.acc1);
Typeface font=null;
tv2=(TextView)findViewById(R.id.acc2);
try
{
font = Typeface.createFromAsset(getAssets(),"fonts/Devanagari.ttf");
getData= PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String s=getData.getString("lang", "E");
if(s.contentEquals("E"))
setContentView(R.layout.accumulation);
else if(s.contentEquals("H"))
{
setContentView(R.layout.accumulationh);
tv1.setTypeface(font);
tv1.setText("कर दिया");
}
else if(s.contentEquals("B"))
{
setContentView(R.layout.accumulationb);
}
getSupportActionBar().setTitle("Accumulation Distribution");
Drawable d=getResources().getDrawable(R.drawable.blue);
getSupportActionBar().setBackgroundDrawable(d);
}
catch(NullPointerException e)
{
e.printStackTrace();
}
}
}
<?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="vertical"
android:padding="10dp"
android:background="#drawable/blue"
>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="insideInset">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/accimg1"
android:layout_width="wrap_content"
android:textColor="#color/black"
android:layout_height="wrap_content"
android:text=""/>
<ImageView
android:id="#+id/acc1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
<TextView
android:id="#+id/acc2"
android:textColor="#color/black"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
Why is there a ClassCastException?
I am trying to use use a custom TypeFace on textView1 but its giving the same error everytime.
TextView1 is empty & i want to use a typeface to implement a different language. But everytime it is showing blank even when I applied tv1.setText.
You can't call findViewById() until after you've inflated the containing view by calling setContentView() on it or by inflating it manually.
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