Setting a ProgressBar to VISIBLE on click in android - android

I am trying to make my ProgressBar turn VISIBLE when the LogIn button is clicked. Here's my XML for the Activity:
<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:gravity="center_vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<EditText
android:id="#+id/txtView_UserName"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:hint="#string/login_username_hint">
<requestFocus />
</EditText>
<EditText
android:id="#+id/txtView_Password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/txtView_UserName"
android:layout_centerHorizontal="true"
android:layout_marginTop="5dp"
android:hint="#string/login_password_hint"
android:inputType="textPassword" />
<Button
android:id="#+id/button_LogIn"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtView_UserName"
android:layout_below="#+id/txtView_Password"
android:layout_marginTop="10dp"
android:onClick="logIn"
android:text="#string/login_button" />
<CheckBox
android:id="#+id/checkbox_RememberPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/txtView_Password"
android:layout_below="#+id/button_Login"
android:layout_marginTop="15dp"
android:text="#string/remember_my_password" />
<CheckBox
android:id="#+id/checkbox_LogMeInAutomatically"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/checkbox_RememberPassword"
android:layout_below="#+id/checkbox_RememberPassword"
android:layout_marginTop="-5dp"
android:text="#string/log_me_in_automatically" />
<ProgressBar
android:id="#+id/progressBar_LogIn"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/checkbox_LogMeInAutomatically"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:visibility="invisible" />
<TextView
android:id="#+id/txtView_Version"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="18dp"
android:text="#string/version_number" />
</RelativeLayout>
And here is where I am trying to change it to VISIBLE in the code.
import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ProgressBar;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
//Get Views and Buttons
EditText txt_Username = (EditText) findViewById(R.id.txtView_UserName);
EditText txt_Password = (EditText) findViewById(R.id.txtView_Password);
Button btn_LogIn = (Button) findViewById(R.id.button_LogIn);
CheckBox chkbox_RememberMyPassword = (CheckBox) findViewById(R.id.checkbox_RememberPassword);
CheckBox chkbox_LogMeInAutomatically = (CheckBox) findViewById(R.id.checkbox_LogMeInAutomatically);
ProgressBar progressbar_LogIn = (ProgressBar) findViewById(R.id.progressBar_LogIn);
public void logIn() {
progressbar_LogIn.setVisibility(0);
}
}
The program is crashing without rendering my Activity's Views. Any ideas?

I have a feeling its because you have an onClick for login, but login doesn't take a view parameter. Try setting the onclick for the button programmatically, instead of using the xml.
Edit: Try changing
public void logIn() {
progressbar_LogIn.setVisibility(0);
}
to
public void logIn(View v) {
progressbar_LogIn.setVisibility(0);
}
or to set it programatically
btn_LogIn.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View arg0) {
//CODE GOES HERE
}
});
Further Edit:
Also noticed that theres no sign of an onCreateView so make sure that all that view code is inside of, or atleast inside of onCreate:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
}

Related

Fragments and Layouts

When trying to run our app, we consistently have errors with conflicting fragments not letting us run the activity. Here's a screencast of the problem I am facing
As you can see, the app itself contains a multitude of problems, but my primary focus is getting the clicker to respond on the click. Here is the class page for the clicker activity:
package com.example.android.Chapsnat;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class ContactsFragment extends AppCompatActivity {
Button btnClick;
Button btnReset;
TextView txtCount;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_contacts);
btnClick = (Button)findViewById(R.id.buttonClick);
btnReset = (Button)findViewById(R.id.buttonReset);
txtCount = (TextView)findViewById(R.id.textViewCount);
btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
String countValue = txtCount.getText().toString();
int intCountValue = Integer.parseInt(countValue);
intCountValue++;
txtCount.setText(String.valueOf(intCountValue));
} });
btnReset.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
txtCount.setText(String.valueOf(0));
}
});
}
}
And this is the xml page it is being built in, which is also a fragment:
<?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:id="#+id/fragment_contacts"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.android.Chapsnat.ContactsFragment">
<TextView
android:id="#+id/textViewCount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="66dp"
android:text="0"
android:textColor="#android:color/holo_purple"
android:textSize="40sp" />
<Button
android:text="Click"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/buttonClick"
android:layout_below="#+id/textViewCount"
android:layout_centerHorizontal="true"
android:layout_marginTop="79dp" />
<Button
android:text="Reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="171dp"
android:id="#+id/buttonReset"
android:layout_below="#+id/textViewCount"
android:layout_alignStart="#+id/buttonClick" />
</RelativeLayout>
Is there any way you can help? If so please let me know.

Add checkboxes on click

I have an image file on which if clicked anywhere I would like X'd checkboxes to show up. (Right now its set up as check marks). The code that I have is just for connecting page 1 to page 2 with a button click. Here it is
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
public static Button button1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnclickButtonListener();
}
public void OnclickButtonListener() {
button1 = (Button) findViewById(R.id.nextpage);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText userTextEntry = (EditText)findViewById(R.id.BRinfo);
String userdata = userTextEntry.getText().toString();
int userNumber = Integer.parseInt(userdata);
Intent intent = new Intent(MainActivity.this,Page2.class);
intent.putExtra("para name",userNumber);
startActivity(intent);
}
});
}}
Part of the XML code is
<?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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="vt.tacticalcombat.MainActivity"
android:clickable="true">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView2"
android:src="#mipmap/combat2"
android:layout_alignBottom="#+id/editText17"
android:layout_alignLeft="#+id/last4"
android:layout_alignStart="#+id/last4"
android:layout_alignRight="#+id/tableLayout"
android:layout_alignEnd="#+id/tableLayout"
android:layout_alignTop="#+id/imageView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="NEXT"
android:id="#+id/nextpage"
android:layout_alignBottom="#+id/tableLayout"
android:layout_alignRight="#+id/otherinfo"
android:layout_alignEnd="#+id/otherinfo"
tools:ignore="HardcodedText,RtlHardcoded"
android:nestedScrollingEnabled="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/checkBox28"
android:checked="false"
android:visibility="visible"
android:layout_alignBottom="#+id/rarmtypeinfo"
android:layout_toRightOf="#+id/mvc"
android:clickable="true"
android:layout_toEndOf="#+id/mvc" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_alignBottom="#+id/checkBox28"
android:layout_alignRight="#+id/injury"
android:layout_alignEnd="#+id/injury"
android:layout_alignTop="#+id/larmtype"
android:layout_alignLeft="#+id/checkBox28"
android:layout_alignStart="#+id/checkBox28" />
</RelativeLayout>
I tried using this link! but it did not work. How can I do this? Any help is appreciated

how to display value of textbox on same screen in android application just like whatsaap

This is my main file where i am calling the click event on button to pass the value.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setTitle("aakash");
SetContentView(R.id.lst);
Button b = (Button) findViewById(R.id.sendbtn);
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(android.view.View v) {
startActivity(new Intent(View.this,View.class));
}
});
}
**xml**
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#ffffff"
>
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:text="aakash"
android:background="#c0c0c0"
/>
<ScrollView
android:id="#+id/lst"
android:background="#fefefe"
android:layout_below="#+id/username"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_holder"
>
</ScrollView>
<EditText
android:id="#+id/chattxt"
android:layout_width="230dp"
android:layout_height="45dp"
android:hint="type here"
android:inputType="textMultiLine" />
<Button
android:id="#+id/sendbtn"
android:layout_width="75dp"
android:layout_height="45dp"
android:text="Send" />
</LinearLayout>
I want the value of textbox to display on the same screen(scrollview)box and also i want to pass a httppost request to pass a value..plz help me out
Thnks in advance
I think, you can use a ListView insted of scroll view. and add list items from the chat text value dynamically.
call notifydatasetchanged() every time when you add a new item to it.
<TextView
android:id="#+id/username"
android:layout_width="match_parent"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_alignParentTop="true"
android:textColor="#000000"
android:text="aakash"
android:background="#c0c0c0"
/>
<ListView
android:id="#+id/listView_chats"
android:layout_width="match_parent"
android:layout_height="100dp"
>
</ListView>
<EditText
android:id="#+id/chattxt"
android:layout_width="230dp"
android:layout_height="45dp"
android:hint="type here"
android:inputType="textMultiLine" />
<Button
android:id="#+id/sendbtn"
android:layout_width="75dp"
android:layout_height="45dp"
android:text="Send" />
</LinearLayout>
Write a custom adapter for the listView and add the code to manage the list data.
i got the answer to my question
import android.R;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class ButtonActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_button);
}
public void setText(){
Button myButton = (Button)findViewById(R.id.button1);
final TextView myText = (TextView)findViewById(R.id.text1);
final EditText myInput = (EditText)findViewById(R.id.edit);
myButton.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
myText.setText(myInput.getText());
}
});
}
}

How to convert textview as link in android?

I have created a login page which consists of a username password login button and a signup button which should be a link. I want to convert this signup(text view) to a link that can be navigated to the next page. How would I do that? For example if I click on don't have an account? sigup,it should be navigated to welcome.java..
Please help....
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"
android:background="#drawable/bg_gradient"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="40dp"
android:text="#string/welcome"
android:textColor="#color/white"
android:textSize="45dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_form_rounded"
android:orientation="vertical" >
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#null"
android:hint="#string/email"
android:padding="5dp"
android:singleLine="true" />
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#null"
android:hint="#string/password"
android:inputType="textPassword"
android:padding="5dp" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:background="#drawable/bg_button_rounded"
android:text="#string/login"
android:textColor="#color/white" />
</LinearLayout>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginBottom="25dp"
android:gravity="center_horizontal"
android:text="#string/signup"
android:autoLink="web"
android:textColor="#color/white" />
</RelativeLayout>
mainactivity.java
package com.example.internationalization12;
import android.app.Activity;
import android.os.Bundle;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//getActionBar().hide();
}
}
Try this :
package com.example.internationalization12;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
public class MainActivity extends Activity {
TextView txtSignUp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
txtSignUp=(TextView)findViewById(R.id.txt_sign_up);
txtSignUp.setText(Html.fromHtml("<u>Dont have account?SignUp</u>")
txtSignUp.setOnClickListener(signUpListener);
//getActionBar().hide();
}
public OnClickListener signUpListener=new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
startActivity(new Intent(MainActivity.this,Welcome.class));
}
};
}
You have to setOnTouchListener to the TextView. Here I give you an example:
public class MainActivity extends Activity implements View.OnTouchListener {
private TextView tv1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tv1 = (TextView) findViewById(R.id.tv1);
tv1.setOnTouchListener(this);
}
#Override
public boolean onTouch(View v, MotionEvent event) {
// HERE YOU HAVE TO CHECK WHICH ID IS SELECTED, IF YOU HAVE MORE THAN ONE,
// AND WHICH EVENT HAS THE USER DONE.
// IN YOUR CASE.. OPEN WEBSITE LIKE THIS:
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(browserIntent);
return true;
}
}
you can try Linkify here
textview.setText("www.androiddeveloper.com");
Linkify.addLinks(textview, Linkify.WEB_URLS);

how to assign a string value associated with an image to a textwidget when image is clicked

package com.me.trial;
import android.app.Activity;
import android.os.Bundle;
import android.view.View.OnClickListener;
import android.widget.ImageView;
public class TrialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setClickable(true);
OnClickListener l;
}
private void hasBeenClicked(<method invoked when the user has clicked>){
}
}
<?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" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/relativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="56dp"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/textView1"
android:layout_alignRight="#+id/textView1"
android:layout_below="#+id/textView1"
android:layout_marginTop="64dp" >
<requestFocus />
</EditText>
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:scaleType="fitEnd"
android:tag="Employee name"
android:src="#drawable/img18" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
I need the application to pass some string value associated to it to be passed into the text as soon as the user clicks on the image.How do i use the click Listeners in android to achieve the following task.
It is not necessary to fill in the code snippet that i have given.Any new ideas regarding the design are most welcome.
U can set text as Tag to the Image so that u can get that text onClick event. Tag can be set either in XML or dynamically.
public class TrialActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ImageView img = (ImageView)findViewById(R.id.imageView1);
img.setTag("YOur text");
img.setClickable(true);
img.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
String urText= arg0.getTag().toString();
EditText edt = (EditText)findViewById(R.id.editText1);
edt.setText(urText);
}
});
}
}
This code demonstrates how to achieve your requirement dynamically.

Categories

Resources