I have a relative layout. In that relative layout i have a textview at left end and check box at right end. Now
what i want is, when i clicked the entire layout( above relative layout) the check box need to check. any please
help me.
<RelativeLayout
android:id="#+id/layoutavailable"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/layouttaxexempt"
android:layout_marginTop="25dp"
>
<TextView
android:id="#+id/txt_avl"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:hint="IsAlwaysAvailable"
android:textSize="20sp"
android:textColorHint="#color/white"
android:layout_marginTop="5dp"
android:layout_marginLeft="13dp"
/>
<CheckBox
android:id="#+id/c_avl"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_marginTop="5dp"/>
</RelativeLayout>
Code:
public void onClick(View v) {
if (checkbox.isChecked()){
checkbox.setChecked(false);
}
else {
checkbox.setChecked(true);
}
}
Set the listener to the layout
CheckBox cBox = (CheckBox) findViewById(R.id.c_avl);
findViewById(R.id.layoutavailable).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
cBox.setChecked(!cBox.isChecked());
}
});
Use CheckedTextView instead of normal TextView.
And make CheckedTextView width and height to match_parent"
Just try this:
public class MainActivity extends Activity {
RelativeLayout layout;
CheckBox cb;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
cb=(CheckBox)findViewById(R.id.checkBox1);
layout = (RelativeLayout) findViewById(R.id.layoutavailable);
layout.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View arg0, MotionEvent arg1) {
cb.setChecked(true);
return false;
}
});
}
you can do by following way
yourRelativeLayout.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
youCheckbox.setChecked(!(test.isChecked()));
}
});
First make your Relative Layout clickable by writing the given code in your Relative Layout Tag body, and also assign the id to your layout.:-
android:clickable="true"
android:id="#+id/rl"
Then in your Activity Java file, write the code in your on create instance:-
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.test2);
Relative Layout rl = (LinearLayout)findViewById(R.id.rl);
final CheckBox c_avl = (CheckBox)findViewById(R.id.checkBox1);
rl.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
c_avl.setChecked(true);
}
});
}
This will certainly solve your problem, indeed.
You can also achieve this by using the toggle() method of the CheckBox class. Please note that when using the toggle method, there won't be any animation. The way that I fixed that issue was by overriding the toggle method, and defining my own toggle.
private RelativeLayout layoutAvailable = findViewById(R.id.layoutavailable);
private CheckBox cAvl = findViewById(R.id.c_avl);
layoutavailable.setOnClickListener(new View.OnClickListener(){
public void onClick(View view){
cAvl.toggle();
}
})
Just pass the state of the parent RelativeLayout to it's child CheckBox
set RelativeLayout clickable, and add an attribute android:duplicateParentState to CheckBox
<RelativeLayout
android:id="#+id/layoutavailable"
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_below="#+id/layouttaxexempt"
android:layout_marginTop="25dp"
android:clickable="true"
>
<TextView
android:id="#+id/txt_avl"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:hint="IsAlwaysAvailable"
android:textSize="20sp"
android:textColorHint="#color/white"
android:layout_marginTop="5dp"
android:layout_marginLeft="13dp"
/>
<CheckBox
android:id="#+id/c_avl"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:duplicateParentState='true'
android:layout_marginTop="5dp"/>
</RelativeLayout>
Related
i am a beginner in application development and i like to ask a question that i tried my best to get an answer in google but i failed.
So,
in my application (in java) i am using too text fields :
<com.google.android.material.textfield.TextInputLayout
android:layout_marginBottom="20dp"
android:id="#+id/start_date_Layout"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginHorizontal="20dp"
app:helperText="Start Date"
app:endIconMode="custom"
app:endIconDrawable="#drawable/ic_date"
>
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/start_date"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="dd/mm/yy"
/>
</com.google.android.material.textfield.TextInputLayout>
I added a listener to my end icon like that :
this.startDateLayout = v.findViewById(R.id.start_date_Layout);
this.startDateLayout.setEndIconOnClickListener(this);
My problem is when i try to get the view that the user clicked :
#Override
public void onClick(View v) {
if (v.getParent() == this.startDateLayout){
Toast.makeText(this.context, "click", Toast.LENGTH_LONG).show();
}
}
the toast never appear and the if condition is never true, my question is how can i get the view on witch the user clicked. thanks for reading i hope i was clear as much as possible.
For me only doing following works:
final TextInputLayout textInputLayout = findViewById(R.id.start_date_Layout);
textInputLayout.setEndIconOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
}
});
As you're explicitly setting listener on End icon only you shouldn't be worried about verifying viewId.
Just use:
startDateLayout = v.findViewById(R.id.start_date_Layout);
startDateLayout.setEndIconOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// DO something....
}
});
Simple kotlin:
b.dateLabel.setEndIconOnClickListener {
Toast.makeText(requireContext(), "date", Toast.LENGTH_SHORT).show()
}
First what I can notice in the above code, you're trying to listen to click events on your View
A simple way to do so is change your onClick() method to below
public void onClick(View v) {
if (v.getId() == R.id.start_date_Layout){
Toast.makeText(getApplicationContext(), "click", Toast.LENGTH_LONG).show();
}
}
I believe I could help
If you are using EditText, then try setting an onFocusChangeListner to the editText instead of onClickListner.
startDateLayout.setOnFocusChangeListener(new OnFocusChangeListener() {
#Override
public void onFocusChange(View v, boolean hasFocus) {
if(hasFocus) {
// Show your code when has focus
} else {
// when it does not have focus
}
}});
The question is simple, You can you a FragmeLayout or LinearLayout, EditText and an ImageView for achieving the solution, it will be similar to the TextInputEditText but you will have much more control over View Hierarchy, let me show you how you can achieve it, for the sake of simplicity as you are a beginner I'll use linear Layout
Create A FrameLayout or LinearLayout and then create an EditText and an ImageView like
XML:
<LinearLayout
android:layout_margin="10dp"
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#DCDFDF"
android:orientation="horizontal"
tools:ignore="MissingConstraints">
<ImageView
android:layout_gravity="center_vertical"
android:id="#+id/imageViewTitle"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_margin="15dp"
android:src="#drawable/android"
tools:ignore="ContentDescription" />
<EditText
android:layout_gravity="center_vertical"
android:id="#+id/editTextTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:background="#DCDFDF"
android:gravity="top"
android:hint="Start Date"
android:paddingTop="10dp"
android:textAlignment="gravity"
android:importantForAutofill="no" />
</LinearLayout>
Java
public class MainActivity extends AppCompatActivity {
EditText editTextTitle;
ImageView imageViewTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
editTextTitle = findViewById(R.id.editTextTitle);
imageViewTitle = findViewById(R.id.imageViewTitle);
imageViewTitle.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//Do your work here
Toast.makeText(MainActivity.this, "ImageView Pressed", Toast.LENGTH_SHORT).show();
}
});
}
}
When I click on the Sign up txt on my application it crashes I've tried almost everything i could find including the XML onClick
XML clickable
onClick
public class LoginActivity extends AppCompatActivity
{
TextView sign_up_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
sign_up_text = (TextView) findViewById(R.id.sign_up);
sign_up_text.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startActivity(new Intent(LoginActivity.this, RegisterActivity.class));
}
});
}
}
xml:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/sign_up_text"
android:textSize="18dp"
android:gravity="center"
android:layout_alignParentBottom="true"
android:id="#+id/sign_up"
android:clickable="true"
android:onClick="onClick"
/>
Android app crashes with no failure
You have android:onClick="onClick" attribute in your xml. And you have also define sign_up_text.setOnClickListener in java.
You can do only one thing at a time.
1.
If you want to use sign_up_text.setOnClickListener , you simply need to remove android:onClick="onClick" from xml file.(Reference)
2.
If you want to use android:onClick="onClick", you should define new method in your activity like:(Reference)
public void onClick(View view) {
}
First solution is to remove android:onClick="onClick" from your XML, according to my perception the reason of crash is the function name called on onClick of your button click define in XML is "onClick" which is also override function of setOnClickListner
#Override
public void onClick(View v){
//todo your code
}
Second Solution is if you use onClick in XMl change function name other that override function onClick like:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/sign_up_text"
android:textSize="18dp"
android:gravity="center"
android:layout_alignParentBottom="true"
android:id="#+id/sign_up"
android:clickable="true"
android:onClick="myFunction"
/>
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
}
private myFunction(View view){
}
I have problem.
I made method which creates Dialog with my own layout. And I have no idea how to pass values (Strings) from my EdiText and asing to any variable in my Activity.
In comments you can see how I was trying to solve this.
Java method
public void makeDialog(){
// custom dialog
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.dialog_ip);
dialog.setTitle("IP connection");
// Todo passing value from dialog to activity
// final EditText ipValueConnection = (EditText)findViewById(R.id.ipValueConnection);
// ipValueConnection.setOnClickListener(this);
// EditText portValueConnection = (EditText)findViewById(R.id.portValueConnection);
// Toast.makeText(context, ipValueConnection.getText().toString(), Toast.LENGTH_LONG).show();
Button dialogButtonLogin = (Button) dialog.findViewById(R.id.dialogButtonLogin);
// if button is clicked, close the custom dialog
dialogButtonLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
tryToConnect();
dialog.dismiss();
}
});
// set the custom dialog components - text, image and button
// TextView text = (TextView) dialog.findViewById(R.id.IP);
dialog.show();
}
XML layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:src="#drawable/antena"
android:layout_width="220dp"
android:layout_height="120dp"
android:scaleType="centerInside"
android:background="#FFFFBB33"
android:contentDescription="#string/app_name"
android:adjustViewBounds="true"
/>
<EditText
android:id="#+id/ipValueConnection"
android:inputType="textEmailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:hint="IP" />
<EditText
android:id="#+id/portValueConnection"
android:inputType="textPassword"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif"
android:hint="PORT"/>
<Button
android:id="#+id/dialogButtonLogin"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Login"
android:layout_marginTop="5dp"
/>
</LinearLayout>
Create a interface
public interface OnClickInterface {
public void onClick();
}
call it instantiate it in your activity onCreate()
OnClickInterface onClickInterface = new OnClickInterface() {
#Override
public void onClick() {
//Call Method from here
requiredMethod();
}
};
//And in your dialog classs or method
public void makeDialog(OnClickInterface onClickInterface){
//Your code
dialogButtonLogin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
onClickInterface.onClick();
dialog.dismiss();
}
});
}
The error you are getting means that a reference to the editText cannot be found in the current layout file. You have find the EditText in the custom dialog view instead of the activity view.
So instead of:
final EditText ipValueConnection =(EditText)findViewById(R.id.ipValueConnection);
use:
final EditText ipValueConnection =(EditText)dialog.findViewById(R.id.ipValueConnection);
If i have understood your question correctly,
your edit text is in dialog_ip so you need to use
final EditText ipValueConnection = (EditText) dialog.findViewById(R.id.ipValueConnection);
Then you can get text from edit text as
String text= ipValueConnection.getText().toString;
And use that variable in your activity.
I am trying to make a checkbox in android studio/java that if checked will result in a subset of other checkboxes checkable and checked.
The developer site says:
abstract void setChecked(boolean checked)
Change the checked state of the view
But this hasn't worked for me.
Current code:
Java:
package crc.cybereye;
public class CreateNew extends Activity {
LinearLayout background;
Button btnBack;
CheckBox checkbox_structural_info;
CheckBox checkbox_years_of;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
background = (LinearLayout) findViewById(R.id.background);
setContentView(R.layout.activity_create_new);
btnBack = (Button) findViewById(R.id.btnBack);
checkbox_years_of = (CheckBox) findViewById(R.id.checkbox_years_of);
checkbox_floors_above = (CheckBox) findViewById(R.id.checkbox_floors_above);
checkbox_structural_info = (CheckBox) findViewById(R.id.checkbox_structural_info);
// setUpIntroCheckBoxes(); // Add change listeners to check boxes
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(view.getContext(), NewFormActivity.class);
startActivityForResult(intent, 0);
}
});
}
More Java:
public void onCheckboxClicked(View view) {
// Is the view now checked?
boolean checked = ((CheckBox) view).isChecked();
// Check which checkbox was clicked
switch(view.getId()) {
case R.id.checkbox_structural_info:
if (checked){
checkbox_years_of checked.setChecked(true) //ERROR HERE
}
break;
case R.id.checkbox_years_of:
if (checked){
}
break;
XML:
<TableRow android:layout_marginTop="10dp">
<TextView
android:text="#string/structural_info"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
/>
<CheckBox android:id="#+id/checkbox_structural_info"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"/>
</TableRow>
<TableRow>
<TextView
android:text="#string/years_of"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
/>
<CheckBox android:id="#+id/checkbox_years_of"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onCheckboxClicked"/>
<TableRow>
There is an error saying that it cannot resolve method setChecked(boolean).
My code currently is just trying to get the structural_info checkbox to check the years_of checkbox if it is checked, but I also want to make years_or only checkable if structural_info is checked, I haven't got there yet because I was still stuck on this issue.
Thanks so much for any help in advance.
As requested by the creator of the post, i'm adding the comment as an answer:
You're calling setChecked to checked wich is a boolean variable.
You shoud write:
checkbox_years_of.setChecked(true)
Instead of
checkbox_years_of checked.setChecked(true)
I have a fragment with an XML layout file. in it I have an 2 clickable ImageViews.
for each ImageView I set an onClick method for example: android:onClick="commentFragmentRemoveOnClick".
In the FragmentActivity (The Activity no the Fragment) I defined it this way:
public void commentFragmentRemoveOnClick(View v)
{
}
No this Fragment is of type CommentFragment and it has a public void getFragmentTag()method
to get it's tag that I save in earlier time. I need to get an instance of the fragment in which the image was clicked to get it's tag.
I tried:
((CommentFragment)v).getParentFragment().getFragmentTag();
and:
((CommentFragment)v).getParent().getFragmentTag();
but eclipse gives me error on both of them, how is this done properly?
To make it more clear this is my CommentFragment:
public class CommentFragment extends Fragment {
private final static String TAG = CommentFragment.class.getSimpleName();
private String fragmentTag;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.comment_fragment_layout,
container, false);
Bundle bundle = getArguments();
String text = bundle.getString("comment");
String fullUser = bundle.getString("user");
String user = fullUser.substring(0, fullUser.indexOf("#"));
String at = bundle.getString("at");
TextView tvCmment = (TextView) rootView.findViewById(R.id.tvComment);
TextView tvUser = (TextView) rootView.findViewById(R.id.tvUser);
TextView tvAt = (TextView) rootView.findViewById(R.id.tvDate);
tvCmment.setText(text);
tvUser.setText(user);
tvAt.setText(at);
return rootView;
}
public void setText(String item)
{
TextView view = (TextView) getView().findViewById(R.id.tvComment);
view.setText(item);
}
public void setFragmentTag(String tag)
{
this.fragmentTag = tag;
}
public String getFragmentTag()
{
return this.fragmentTag;
}
}
and the layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llCommentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#drawable/try2">
<TextView
android:id="#+id/tvUser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tvComment"
android:layout_alignParentTop="true"
android:background="#color/my_gray"
android:text="demo"
android:textStyle="bold"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="#color/my_even_darker_gray" />
<TextView
android:id="#+id/tvComment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvDate"
android:padding="5dp"
android:text="This task is described in more details if you click on it."
android:textColor="#color/my_even_darker_gray" />
<TextView
android:id="#+id/tvAt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:paddingRight="5dp"
android:textColor="#color/my_even_darker_gray"
android:layout_toRightOf="#+id/tvUser"
android:background="#color/my_gray"
android:text="at" />
<TextView
android:id="#+id/tvDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/tvAt"
android:layout_alignBottom="#+id/tvAt"
android:layout_toRightOf="#+id/tvAt"
android:background="#color/my_gray"
android:text="12/02"
android:textColor="#color/my_even_darker_gray" />
<ImageView
android:id="#+id/iEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/tvComment"
android:layout_marginRight="4dp"
android:clickable="true"
android:contentDescription="#drawable/add_comment_button"
android:onClick="commentFragmentEditOnClick"
android:src="#drawable/add_comment_button" />
<ImageView
android:id="#+id/iRemove"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/iEdit"
android:layout_toRightOf="#+id/iEdit"
android:layout_marginRight="4dp"
android:clickable="true"
android:contentDescription="#drawable/add_comment_button"
android:onClick="commentFragmentRemoveOnClick"
android:src="#drawable/add_comment_button" />
</RelativeLayout>
I would love for a little assistance.
Thanks.
I have a general advice for you that would solve your problem and help you in the future -
don't use android:onClick in the xml file, use setOnClickListener in the code itself - need to avoid mixing your views with other parts of the app as much as possible.
Try to keep the Fragment independent of its activity.
If the image is part of the fragment, why does the listener is part of the FragmentActivity?
Use setOnClickListener in the fragment itself, and you might be able to use this Framgent in other pats of the app without being depended on the Activity.
It would also solve your problem of identifying the fragment in which the image was clicked.
v is not an instance of Fragment, that's why Eclipse does not like your code. If you want the instance of a fragment you have to use the FragmentManager and one of its findFragmentByXXX methods.
To get the instance of the fragment that the ImageView was clicked in I did the following:
in the Fragment I set two onClickListeners for both of the images like this:
iEdit = (ImageView)rootView.findViewById(R.id.iEdit);
iEdit.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Log.d(TAG, "pressed edit button");
((PicturesAndCommentsActivity) getActivity()).commentFragmentEditOnClick(fragmentTag);
}
});
iRemove = (ImageView)rootView.findViewById(R.id.iRemove);
iRemove.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v)
{
Log.d(TAG, "pressed remove button");
((PicturesAndCommentsActivity) getActivity()).commentFragmentRemoveOnClick(fragmentTag);
}
});
and in the fragment activity I defined those two methods like this:
public void commentFragmentRemoveOnClick (String tag)
{
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.remove(fragmentManager.findFragmentByTag(tag)).commit();
}
for removing the fragment, and Now I'm working on editing the fragment.