Linear layout clickable error - android-layout

In my Andoid app. I have a clickable linear layout that I've generated programmaticly, and I want it to turn green when it is pressed to indicate that it is clickable, like a button would. How would I go about doing this?
This is my code i implemented in my Header layout.
<LinearLayout
android:id="#+id/back_lay"
android:layout_width="46dp"
android:layout_height="46dp"
android:layout_marginLeft="10dp"
android:background="#android:color/transparent"
android:gravity="center_vertical|center_horizontal" >
<Button
android:id="#+id/ib_back_music"
android:layout_width="30dp"
android:layout_height="32dp"
android:layout_marginLeft="5dp"
android:background="#drawable/back_btn_sel"
android:clickable="true"
android:gravity="center_vertical|center_horizontal|center" />
</LinearLayout>

private LinearLayout lLinearLayout;
//OnCreate
lLinearLayout = (LinearLayout) findViewById(R.id.back_lay);
lLinearLayout.setOnClickListener(new
{
#Override
public void onClick(View v)
{
lLinearLayout.setBackgroundColor(Color.BLACK);
}
});

Related

How to onclick on pop up fragment ? Android studio

I have a popup.xml that have sign in and sign up button. This pop up will be shown when i click on certain function. Now the problem is i cant OnClick on the sign in and sign up button. I want to redirect to login activity when i click on sign in on the pop up fragment. I tried to create a class to the popup.xml and OnClick on sign in button but it seem like not working at all. It doesnt even triggered the OnClick function because Log.d("dwfsd","wefwef"); doesnt show at all.
Pop Up Image
My loginpopup.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="350dp"
android:layout_height="300dp"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="center"
android:background="#drawable/circlebackground"
tools:context=".loginpopup">
<ImageView
android:id="#+id/loginLogo"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:src="#drawable/foxsplash" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/loginLogo"
android:paddingTop="30dp"
android:gravity="center">
<Button
android:id="#+id/signIn"
android:layout_width="150dp"
android:layout_height="55dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="56dp"
android:textColor="#color/white"
android:textSize="24sp"
android:padding="3dp"
android:focusable="true"
android:textAllCaps="false"
android:background="#drawable/rounded_rectangle"
android:text="Sign In"/>
<Button
android:id="#+id/signUp"
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="56dp"
android:textColor="#color/white"
android:textSize="24sp"
android:padding="3dp"
android:focusable="true"
android:textAllCaps="false"
android:background="#drawable/rounded_rectangle"
android:text="Sign Up"
android:layout_toEndOf="#+id/signIn"/>
</RelativeLayout>
</RelativeLayout>
loginpopup.java:
public class loginpopup extends AppCompatActivity {
private Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpopup);
button =(Button) findViewById(R.id.signIn);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("dwfsd","wefwef");
openActivity();
}
});
}
private void openActivity() {
Intent intent = new Intent(this, login.class);
startActivity(intent);
}
}

Align the text to center in ListView

I've created a stopwatch app when the user clicks the Lap button just gives the current time the timer is at. My problem is text in ListView displays at the left corner. I'm finding it impossible to center the text in my listview. Here I attached my XML and Java code.
activity_main.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"
tools:context=".MainActivity">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_margin="100dp"
android:text="format"
android:textSize="40dp"
android:textStyle="bold" />
<Button
android:id="#+id/start"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="110dp"
android:text="Start" />
<Button
android:id="#+id/reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_toRightOf="#id/start"
android:text="Reset" />
<ListView
android:id="#+id/listview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/start"
android:layout_marginLeft="50dp"
android:layout_marginTop="9dp"
android:layout_marginRight="50dp"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll" />
</RelativeLayout>
MainActivity.java:
public class MainActivity extends AppCompatActivity
{
TextView time;
Button reset, start;
ListView listView;
long startTime, TimeBuff, millisecondsTime, UpdateTime = 0L;
int seconds, milliseconds, minutes;
Handler handler;
List<String> ListElementsArrayList;
ArrayAdapter<String> adapter;
String[] ListElements = new String[]{};///creating arrays of string type
#Override
protected void onCreate(final Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
time = (TextView) findViewById(R.id.textView);
start = (Button) findViewById(R.id.start);
reset = (Button) findViewById(R.id.reset);
listView = (ListView) findViewById(R.id.listview1);
handler = new Handler();
//Getting the list of array
ListElementsArrayList = new ArrayList<String>(Arrays.asList(ListElements));
adapter = new ArrayAdapter<String>(MainActivity.this,
android.R.layout.simple_list_item_1,
ListElementsArrayList);
listView.setAdapter(adapter);
}
}
To do it You have to create a layout in Your res/layout folder. With name e.g. list_item_layout.xml. In this layout add this:
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:textSize="20sp" />
In this file, You can make custom TextView which You want to display (change font, size etc.). To center text use android:gravity="center"
Next, when You create an ArrayAdapter, use this layout:
adapter = new ArrayAdapter<String>(
MainActivity.this,
R.layout.list_item_layout, // here You use created layout
ListElementsArrayList
);
Result:

Radio Button and TextField Android-studio

How can I make a radio button group and a TextField, so that whenever I click on TextField the radio button automatically changes from one to another?
Please give me a block of code for this.
If I understood your question correctly, you want the radioButton checked status change once the textView click. You can achieve by this way.
public class MainActivity extends Activity {
private RadioGroup radioSexGroup;
private RadioButton radioSexButton,male,female;
private TextView btnDisplay;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
radioSexGroup=(RadioGroup)findViewById(R.id.radioGroup);
btnDisplay=(TextView)findViewById(R.id.textView3);
male = (RadioButton)findViewById(R.id.radioButton);
female=(RadioButton)findViewById(R.id.radioButton2);
btnDisplay.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int selectedId=radioSexGroup.getCheckedRadioButtonId();
radioSexButton=(RadioButton)findViewById(selectedId);
if(radioSexButton.getText().toString().equals("Male"))
{
female.setChecked(true);
}
else
{
male.setChecked(true);
}
}
});
}
}
activity_main
<?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="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">
<RadioGroup
android:layout_width="fill_parent"
android:layout_height="90dp"
android:layout_marginTop="58dp"
android:weightSum="1"
android:id="#+id/radioGroup"
android:layout_alignRight="#+id/textView3"
android:layout_alignEnd="#+id/textView3">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="55dp"
android:text="Male"
android:id="#+id/radioButton"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Female"
android:id="#+id/radioButton2"
android:layout_gravity="center_horizontal"
android:checked="false"
android:textSize="25dp"
android:layout_weight="0.13" />
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me "
android:id="#+id/textView3"
android:textSize="35dp"
android:layout_below="#+id/radioGroup"
android:layout_centerHorizontal="true"
android:layout_marginTop="70dp"></TextView>
</RelativeLayout>
https://www.tutorialspoint.com/android/android_radiogroup_control.htm
check this link , to use radio group with textviews

getBaseline of TextView returns -1

I am trying to align two TextView one is inside HorizontalScrollView, and both are children of LinerLayout .
When I am trying to call getBaseLine of TextView inside onWindowAttached of RecyclerView Adapter it always returns -1.
Yes, the parent view of TextView, LinerLayout has android:baselineAligned="true"
Edit : Adding Code
Layout
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingBottom="6dp"
android:paddingEnd="10dp"
android:baselineAligned="true"
android:paddingStart="10dp"
android:paddingTop="10dp">
<TextView
android:id="#+id/outerTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical" />
<HorizontalScrollView
android:id="#+id/scrollView"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/inlineTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
/>
</HorizontalScrollView>
</LinearLayout>
Code in RecyclerView Adapter
#Override
public void onViewAttachedToWindow(RecyclerView.ViewHolder holder)
{
super.onViewAttachedToWindow(holder);
if(holder instanceof MyHolder)
{
dostuff((MyHolder)holder);
}
}
private void dostuff(QACommentViewHolder holder)
{
int baseline = holder.outerTextView.getBaseline();
}
View is not measured when onViewAttachedToWindow is called, hence it is returning -1.
Better approach would be.
#Override
public void onBindViewHolder(RecyclerView.ViewHolder holder, int position)
{
.
.
.
viewHolder.itemView.post(new Runnable()
{
//put your code here

How to set height & width of Edittext so that when i type something in edittext beside button should become visible

I have EditText & Button in LinearLayout. I have made Button invisible now & my EditText is fill parent .
However when I want to enter something in EditTextthe width of edit text should wrap & Button beside should become visible. Any code snippet would be really helpful.
Don't make the EditText to fill_parent. You can try to use :
<EditText
android:layout_width = "0dp"
android:layout_height = "wrap_content"
android:layout_weight = "1"
/>
<YourButton/>
and i assume your LinearLayout is horizontal orientated.
Try this :
layout :
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<EditText
android:id="#+id/editText"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
<Button
android:id="#+id/btnClickMe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click Me"
android:visibility="gone" />
</LinearLayout>
Code :
EditText editText=(EditText)findViewById(R.id.editText);
final Button btnClickMe=(Button) findViewById(R.id.btnClickMe);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
btnClickMe.setVisibility(View.VISIBLE);
}
});
Button has setVisibility method. To make visible use the following:
Button button.setVisibility(View.VISIBLE);
try this
<LinearLayout android:orientation="horizontal">
<EditText android:layout_weight="1" android:layout_height="wrap_content"
anroid:layout_width="0dp" />
<Button android:layout_width="50dp" android:layout_height="wrap_content"
android:visibility="gone" />
</LinearLayout>
I assume you already have the code for showing the button when you type something in the EditText
Try this:
Register OnFocusChangedListener to your EditText.
Override onFocusChange() and do the following if hasFocus== true:
Change the LayoutParameters of your EditText to be wrap_content in both Width and Height.
Set your Button to be Visisble.

Resources