How to have a dialog with a design from xml? - android

What I want to do is to show a Dialog with a couple of options that I designed in xml.
Here is how the code looks:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_main);
Dialog_Choices();
}
public void Dialog_Choices()
{
Dialog dialog = new Dialog(Main.this);
dialog.setContentView(R.layout.test_dialog);
Button_FinishOne=(Button)findViewById(R.id.Ramadan_Button_FinishOne);
Button_FinishTwo=(Button)findViewById(R.id.Ramadan_Button_FinishTwo);
Button_FinishThree=(Button)findViewById(R.id.Ramadan_Button_FinishThree);
dialog.setTitle("اختار عدد مرات");
dialog.setCancelable(false);
Button_FinishOne.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(Main.this, "One", Toast.LENGTH_SHORT).show();
}
});
dialog.show();
}
The problem is that Button_FinishOne is null when it reaches SetOnClickListener..
If I change the SetContentView in the OnCreate to (R.layout.test_dialog) , it works.
R.layout.test_dialog has 3 buttons in a relativeView
R.layout.test_main has a listview.
How can I have a xml layout for the main Activity, and another xml for the dialog?
Thanks.

You need to be using dialog.findViewById(R.id.Ramadan_Button_FinishOne) instead of just findViewById

change
Button_FinishOne=(Button)findViewById(R.id.Ramadan_Button_FinishOne);
Button_FinishTwo=(Button)findViewById(R.id.Ramadan_Button_FinishTwo);
Button_FinishThree=(Button)findViewById(R.id.Ramadan_Button_FinishThree);
to
Button_FinishOne=(Button)(dialog.findViewById(R.id.Ramadan_Button_FinishOne));
Button_FinishTwo=(Button)(dialog.findViewById(R.id.Ramadan_Button_FinishTwo));
Button_FinishThree=(Button)(dialog.findViewById(R.id.Ramadan_Button_FinishThree));

Related

Android can not remove button

I am trying to remove a button when the button itself is tapped, I am trying the following:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
tagsView.removeView(button);
}
};
}
This code is working, but when I add the following line of code:
editText.setText(button.getText());
The code stops working and the button does not get removed. I add it like so:
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
tagsView.removeView(button);
}
};
}
What is the problem here?
use this in your OnClick method
button.setVisibility(view.GONE);
Your code will look like this
View.OnClickListener getOnClickDoSomething(final Button button) {
return new View.OnClickListener() {
public void onClick(View v) {
editText.setText(button.getText());
button.setVisibility(view.GONE);
}
};
}
Or Try this
Button mybtn = (Button)findViewById(R.id.mybtn_id);
mybtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
mybtn.setVisibility(view.GONE); // or (view.INVISIBLE)
}
});
Depending on what you're trying to achieve, something like deejay proposed would work just fine. If want the button to hide, call button.setVisibility(View.INVISIBLE). However, if you are trying to dismiss it completely from the view hierarchy, call button.setVisibility(View.GONE).
just set button visibility to false
Obviously button.setVisibility(View.GONE) comes to mind but if it doesn't work you should look one level above for the source of the bug. Maybe you don't set OnClickListener you created to the button and hence nothing happens?

Setting visibility of ListView

I want to show my list view when the user clicks on a button and hide it again when they click on a button. This is the onClick listener for the button in question:
connectBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(open){
mDbAdapter.close();
connectBtn.setText("Open Database");
open = false;
hideUI();
}else{
mDbAdapter = new ContactsDbAdapter(v.getContext());
mDbAdapter.open();
connectBtn.setText("Close Database");
open = true;
showUI();
//retrieve data
fillData();
}
}
});
This is the showUI() method:
protected void showUI() {
fName.setVisibility(View.VISIBLE);
lName.setVisibility(View.VISIBLE);
fNameBox.setVisibility(View.VISIBLE);
lNameBox.setVisibility(View.VISIBLE);
createBtn.setVisibility(View.VISIBLE);
this.setVisible(true);
createBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mDbAdapter.createContact(fNameBox.getText().toString(), lNameBox.getText().toString());
fillData();
}
});
}
and the hideUI() method:
protected void hideUI() {
fName.setVisibility(View.INVISIBLE);
lName.setVisibility(View.INVISIBLE);
fNameBox.setVisibility(View.INVISIBLE);
fNameBox.clearComposingText();
lNameBox.setVisibility(View.INVISIBLE);
lNameBox.clearComposingText();
createBtn.setVisibility(View.INVISIBLE);
this.setVisible(false);
}
It works fine when I set the visibility to true. However when I set it to false I get a black screen but no crash or error. Any idea?
NOTE: this.setVisible(false);. My class extends ListActivity.
setVisibility(View.INVISIBLE);
Just makes you view invisible but the space taken by view will be their itself
use setVisibility(View.GONE); so that the size of view will be lapsed
Use this and let me know if it is helpful
ListActivity is hold list view
if u do this.setVissiblity(false);
it hide the list view and its contents so u r seeing background color in ur case it is black.
Good way is take Listview in xml and get id make vissible nad invissible of that view u feel very confortable with this apprch
http://www.vogella.com/articles/AndroidListView/article.html read this u will get clear idea. and make changes accordingly

adding click listener to titlebar image

Merry Christmas and Happy Holidays everyone!
I'm trying to setup a listener on the image icon that appears on the left side of the default title bar, but so far not having any luck.
Here's my Activity's onCreate:
#Override public void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_LEFT_ICON);
super.onCreate(savedInstanceState);
findViewById(Window.FEATURE_LEFT_ICON).setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
System.out.println("It works!");
}
});
}
Any suggestions? I'm hoping to not see the answer "it's not possible" :)
There doesn't seem to be an id for the left icon, however for the classic title bar, there is an id available: android.R.id.title Here is a sample Activity using this id. The requestWindowFeature(Window.FEATURE_LEFT_ICON); should force the classic title bar regardless of theme.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_LEFT_ICON);
setContentView(R.layout.activity_main);
getWindow().setFeatureDrawableResource(Window.FEATURE_LEFT_ICON,R.drawable.ic_launcher);
View v = findViewById (android.R.id.title);
v.setClickable(true);
v.setOnClickListener(new OnClickListener() {
#Override public void onClick(View v) {
Toast.makeText(MainActivity.this, "Works!", Toast.LENGTH_SHORT).show();
}
});
}
}
Basically, what this does, is it finds the id of the title bar (android.R.id.title) then assigns an onClickListener to it.
This will not work with ActionBars, only classic window title bars.
You should use the ActionBar. Use ActionBarSherlock to have it also for Android versions less than 3.0. To make the Icon clickable, see the ActionBar API Docs (see link below). It's very easy, you just activate the behaviour and then it works like a menu-item with a special item-id.
http://developer.android.com/guide/topics/ui/actionbar.html#Home

Square button android

I want to make a button square with the width the same as the heigth.
I try bt.setWidth(bt.getHeight()) but it doesn't work.
If I hardcode the width (bt.setWidth(90)) it works but I don't know the height so I can hardcoded it.
Here is some code. When I click on a button, it opens a dialog and this dialog must contain the square button.
public class MyClass extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.myLayout);
}
public void doClick(View view) {
final Dialog dialog = new Dialog(view.getContext());
dialog.setContentView(R.layout.dialogLayout);
dialog.setTitle("Title");
Button bt = (Button) dialog.findViewById(R.id.myButton);
bt.setWidth(bt.getHeight());
bt.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
...
}
});
dialog.show();
}
}
How could I do it?
The direct answer is that until the dialog is displayed, it has not measured it's layout and the button height will be zero. You could extend the Dialog class and override the onMeasure() method or attach a global layout listener to the layout and set the button size in onLayoutComplete().
However, your approach might be wrong. Why can't you do this in the dialog's layout XML?

Image Change On Button Click In Android

I'm new to android and am making a simple app. I'm trying to change the image (in an imageview) on a button click.
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
frown = (ImageView)findViewById(R.id.imageView1);
}
public void action(View view)
{
Toast.makeText(getApplicationContext(), buttontest, Toast.LENGTH_SHORT).show();
frown.setImageResource(R.drawable.chimpy);
}
"action" is being called via XML with the "android:onClick"[insert method here]" for my button
The button works fine and I get my toast, but the image stays the same.
Try changing the drawable to something standard e.g. android.R.drawable.btn_default.
Does it chnage Now?
I am sure you are having some issues with R.drawable.chimpy.
You must use .png image and you can go with the following code snippet:
frown.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
frown.setBackgroundDrawable(R.id.chimpy);
}
});
If it is not working, just tell me...!

Categories

Resources