Square button android - 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?

Related

How to set error border to RelativeLayout? EditText box is placed in RelativeLayout [duplicate]

This question already has answers here:
Adding Shape to LinearLayout Android
(3 answers)
Closed 5 years ago.
The above image shows that only the edit text box's border changes to red. I need the relative layout to change it's border to red when the edit text field is empty on button click. The "white" portion on the screen is RelativeLayout.
Main activity code:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate ( savedInstanceState );
setContentView ( R.layout.phone );
final EditText number = (EditText) findViewById ( R.id.mobilenumber );
final String msisdn=number.getText().toString();
Button button = (Button) findViewById(R.id.login_button);
button.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
if(TextUtils.isEmpty(msisdn)) {
number.setBackgroundResource(R.drawable.edit_text_error_background);
number.setCompoundDrawablesWithIntrinsicBounds(0, 0, R.drawable.like_selected, 0);
return;
}
}
});
}
You change your editText background color as follows.
button.setOnClickListener( new View.OnClickListener() {
#Override
public void onClick(View view) {
if(TextUtils.isEmpty(msisdn)) {
number.setBackground(R.drawable.your_error_background);
}
else{
number.setBackground(R.drawable.your_normal_bakground);
}
}
});
Use setBackground() method instead of setBackgroundResource() method because its by default accept drawable resource
I hope its work for you. Thank You

Change between layouts / many xml files or all layouts in same xml file?

Writing a simple program that has 2 windows that switch from one to the other at a button press. At first I managed to do this with two layout files in the same XML and just hide one and show the other, but it was near impossible to make a UI this way since the preview window would just bug out and not show what i added to the bottom layer ( pic related: ) http://i.imgur.com/7RsP9t7.png
So I thought instead, I would make 2 XMLS and have 1 layout in each and now it was easy to make a UI but the code on the other hand does not work.
java.lang.NullPointerException # btn2.setOnClickListener(new View.OnClickListener() which doesnt seem to unlogical, guess u cant listen to a button that isnt active or?
public class MyActivity extends Activity {
Button btn1, btn2;
LinearLayout layHome, layAddNumer;
RelativeLayout layAddNumber ;
EditText ph0ne;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
btn1= (Button) findViewById(R.id.buttonGoTonumber);
btn2= (Button) findViewById(R.id.buttonAddNumber);
layHome = (LinearLayout) findViewById(R.id.layHomeddddddd);
layAddNumber = (RelativeLayout) findViewById(R.id.layyPhone);
ph0ne = (EditText) findViewById(R.id.phoneNumberText);
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.phonelayout);
//layHome.setVisibility(View.GONE);
//layAddNumber.setVisibility(View.VISIBLE);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
setContentView(R.layout.activity_my);
}
});
xml: This site does not let me add XML files. But btn1, LinearLayout layHome is in activity:my while Button btn2, RelativeLayout layAddNumber and ph0ne is in my second XML file

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

How to have a dialog with a design from xml?

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));

Android: Enable click event on empty activity area

i set my activity theme to translucent so as to see through to the underneath activity window.
I want to know if it's possible to enable click event when user tap on empty area on this translucent activity?
Thanks,
dara kok
It is possible to add click event to your activity. You need to do as below:
You could have done setContentView(R.layout.main); in onCreate() of your activity.
In main.xml, give some id to the root layout. For eg.,
Lets consider you have root as LinearLayout with id set as below,
Then in onCreate() of your activity, you will have to do the following:
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
}
});
Overriding this would work:
http://developer.android.com/reference/android/app/Activity.html#onTouchEvent(android.view.MotionEvent)
However i think it's your translucent activity that will get the taps, not the one visible under it.
You can add OnClickListener on parent view of your layout.
For example, add android:id="#+id/some_id" to your parent LinearLayout in main.xml.
Then add this code after setContentView in onCreate method:
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.some_id);
FrameLayout frameLayout = (FrameLayout) linearLayout.getParent(); // Get parent FrameLayout
frameLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
onBackPressed(); // Close activity, for example
}
});
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// empty block for prevent frameLayout click event, if you need
}
});

Categories

Resources