This is my first post and I hope I won't make any mistakes writing it. I am in the process of making my own application, and I got stuck at this point.
I have a GridView with a custom layout, which has two views on top of each other (an ImageButton and an overlay, so to speak). I was trying to implement something that sounds like this: when I click on the Button, some views on the Activity would change:
//listener code goes here
title.setText("Title");
description.setText("Description);
preview.setAlpha(1.0f);
lock.setVisibility(View.GONE);
After a lot of failed attempts of trying to implement the OnItemClickListener(), I took the conclusion that I need to implement a OnClickListener() on the button in the BaseAdapter. However, I don't know how I can possibly do that.
I've tried a lot of things, like implementing a method on the Activity class to change them and then call it. I got a non-static error. I've also tried implementing an AsyncTask that checks for changes in a static variable in the Activity every 100ms. (That worked only once and then stopped) It works fine now. It was something like this:
public class UpdateChecker extends AsyncTask <Void, Void, Void>
{
#Override
protected Void doInBackground(Void... voids)
{
while(true)
{
if(selected)
{
Start();
selected = false;
}
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
}
The OnClickListener was like:
viewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view)
{
CustomizeActivity.currId = TargetManager.getTarget(position).ID;
CustomizeActivity.selected = true;
CustomizeActivity.currpos = position;
}
});
(The values set are static)
XML:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_above="#+id/setButton"
android:layout_centerHorizontal="true"
android:background="#ff191919"
android:id="#+id/relativeLayout">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/frameLayout"
android:layout_toEndOf="#+id/frameLayout"
android:layout_alignTop="#+id/frameLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Title"
android:id="#+id/titleView"
android:layout_toEndOf="#+id/preView"
android:textSize="30dp"
android:textColor="#ffeaeaea"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a sample long description. I am testing."
android:id="#+id/descriptionView"
android:layout_toEndOf="#+id/preView"
android:textColor="#ffcdcdcd"
android:textSize="20dp"
android:layout_below="#+id/titleView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="5dp" />
</RelativeLayout>
<FrameLayout
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/frameLayout"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:id="#+id/preView"
android:src="#drawable/target_hellokitty"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/imageView"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/preView"
android:layout_alignLeft="#+id/preView"
android:layout_alignRight="#+id/preView"
android:layout_alignBottom="#+id/preView"
android:src="#android:drawable/ic_lock_lock" />
</FrameLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/relativeLayout"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TabHost
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/tabHost"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="wrap_content"></TabWidget>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/tab1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gridView"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:columnWidth="100dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/tab2"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
<LinearLayout
android:id="#+id/tab3"
android:layout_width="fill_parent"
android:layout_height="fill_parent"></LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
</RelativeLayout>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Set Button"
android:id="#+id/setButton"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#ff00a4e1"
android:textColor="#ffffffff" />
Do you think implementing an ASyncTask as a listener with an infinite loop and a check is a good idea?
Related
Here is my MainActivity:
public class MainActivity extends AppCompatActivity {
private LinearLayout mStatusBarAllContents;
private FrameLayout mStatusBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStatusBar = findViewById(R.id.status_bar);
mStatusBarAllContents = findViewById(R.id.status_bar_all_contents);
mStatusBar.setOnClickListener(v -> {
Log.d("TAG", "status");
});
mStatusBarAllContents.setOnClickListener(v -> {
Log.d("TAG", "statusall");
});
}
My Layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/status_bar"
android:layout_width="match_parent"
android:layout_height="60px"
android:background="#drawable/sysheader_bg"
android:descendantFocusability="afterDescendants"
android:orientation="vertical">
<ImageView
android:id="#+id/notification_lights_out"
android:layout_width="22dip"
android:layout_height="match_parent"
android:paddingStart="6dp"
android:paddingBottom="2dip"
android:scaleType="center"
android:visibility="gone"
tools:ignore="RtlCompat" />
<LinearLayout
android:id="#+id/status_bar_all_contents"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<FrameLayout
android:layout_width="174px"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/clock_relativelayout"
android:layout_width="174px"
android:layout_height="match_parent"
android:clickable="true"
android:visibility="visible">
<TextView
android:id="#+id/clock_hour"
android:layout_width="60px"
android:layout_height="54px"
android:gravity="center"
android:includeFontPadding="false"
android:text="05"
android:textColor="#color/white"
android:textSize="52px"
android:textStyle="bold" />
<ImageView
android:id="#+id/clock_coron"
android:layout_width="12px"
android:layout_height="48px"
android:layout_marginTop="9px"
android:layout_toRightOf="#+id/clock_hour"
android:scaleType="fitCenter"
android:src="#drawable/num_12x48_colon" />
<TextView
android:layout_width="60px"
android:layout_height="54px"
android:layout_toRightOf="#+id/clock_coron"
android:gravity="center"
android:includeFontPadding="false"
android:text="30"
android:textColor="#color/white"
android:textSize="52px"
android:textStyle="bold">
</TextView>
</RelativeLayout>
</FrameLayout>
<LinearLayout
android:id="#+id/status_bar_info_layout"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="1"
android:clickable="true"
android:visibility="visible">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/audio_area"
android:layout_width="786px"
android:layout_height="match_parent"
android:background="#drawable/da_header_sections_background_click_over_info"
android:clickable="true"
android:orientation="horizontal"
android:splitMotionEvents="false">
<FrameLayout
android:id="#+id/audio_info_area"
android:layout_width="708px"
android:layout_height="match_parent"
android:clickable="false">
<TextView
android:id="#+id/audio_info"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="18px"
android:layout_marginLeft="18px"
android:ellipsize="end"
android:gravity="left"
android:singleLine="true"
android:text="ALOLO"
android:textSize="20sp"
android:visibility="visible" />
<LinearLayout
android:id="#+id/audio_off"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingStart="24px"
android:paddingLeft="24px"
android:paddingEnd="56px"
android:paddingRight="56px"
android:visibility="gone">
<TextView
android:id="#+id/audio_off_text"
android:layout_width="648px"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginStart="6px"
android:layout_marginLeft="6px"
android:ellipsize="end"
android:gravity="left"
android:singleLine="true"
android:text="PowerOFF"
android:textColor="#color/white"
android:textSize="30px">
</TextView>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</FrameLayout>
</LinearLayout>
</LinearLayout>
I set onclick on FrameLayout and LinearLayout, i wanna show log is status but when i touch it, nothing happen.
And then, i set android:layout_width="1dp", android:layout_height="1dp" of id: status_bar_all_contents, it show log: status, but status_bar_all_contents will gone, so i dont want that.
So how to fix when i touch it and show log status?
Ps: Sorry for my english is not good.
Layouts typically pass-through clicks to their children.
You can try this solution here disabling all children onclicks and enabling the FrameLaoyuts onclicks. But I don't think that will work with the LinearLayout on top of that.
Android frame layout click listener not working
My advice wrap the entire view in a ConstraintLayout, and position 2 basic <View>s
One <View> that is constrained to match the FrameLayout
One <View> that is constrained to match the LinearLayout
These <View>s will act as overlays.
<View
android:id="#+id/status_bar_overlay"
app:layout_constraintEnd_toEndOf="#id/status_bar"
app:layout_constraintStart_toStartOf="#id/status_bar"
app:layout_constraintTop_toTopOf="#id/status_bar"
app:layout_constraintBottom_toBottomOf="#id/status_bar" />
<View
android:id="#+id/status_bar_all_contents_overlay"
app:layout_constraintEnd_toEndOf="#id/status_bar_all_contents"
app:layout_constraintStart_toStartOf="#id/status_bar_all_contents"
app:layout_constraintTop_toTopOf="#id/status_bar_all_contents"
app:layout_constraintBottom_toBottomOf="#id/status_bar_all_contents" />
Set the two View's onClickListener in the Activity and there you go.
I have created a chatroom where user can discuss on several Topic. I'm using a Scrollview inside RelativeLayout which contains chatConversation
The Porblem is when user opens app then he has to scroll to the Bottom HImself to chat or view the new chat discussion.
How can I force scrollview to start at Bottom when activity is started..
Below is my layout file
<?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:theme="#style/AppTheme.NoActionBar"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v7.widget.Toolbar
android:id="#+id/main_chat"
app:titleTextAppearance="#style/Toolbar.TitleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#F0791F"
android:theme="#style/ThemeOverlay.AppCompat.Light"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navigationIcon="#drawable/ic_people_outline_black_24dp"/>
<Button
android:id="#+id/btn_send"
android:layout_width="42dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:drawableLeft="#drawable/ic_send_black_24dp" />
<EditText
android:id="#+id/msg_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/btn_send"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/btn_send"
android:layout_toStartOf="#+id/btn_send"
android:background="#null"
android:hint="Write a message" />
<ScrollView
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:transcriptMode="alwaysScroll"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_above="#+id/btn_send"
android:layout_below="#+id/main_chat"
android:layout_alignRight="#+id/btn_send"
android:layout_alignEnd="#+id/btn_send">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="2dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="2dp"
android:layout_weight="2"
android:autoLink="all" />
</ScrollView>
<View
android:layout_above="#+id/msg_input"
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#android:color/darker_gray"/>
</RelativeLayout>
I already tried using
final ScrollView scrollLayout = ((ScrollView) findViewById(R.id.scrollView));
scrollLayout.post(new Runnable() {
#Override
public void run() {
scrollLayout.fullScroll(ScrollView.FOCUS_DOWN);
}
});
But it doesn't work either.
Please Help
How are you displaying the messages? With a While Loop?
Last line of the while loop should be...
mScrollView.fullScroll(ScrollView.FOCUS_DOWN);
mScrollView or whatever name you gave to the ScrollView
I think you're appending the messages in a single TextView (correct me if I'm wrong).
Check if you are invoking fullScroll() after you are finished appending all the text in your TextView.
for (String oneMessage : messageList){
myTextView.append(oneMessage);
myTextView.append("\n");
....
....
}
//Scroll to bottom
scrollLayout.post(new Runnable() {
#Override
public void run() {
boolean success = scrollLayout.fullScroll(ScrollView.FOCUS_DOWN);
Log.d("Auto Scroll", success ? "Done" : "Failed");
}
});
Let me know if this fixed your issue!
I have two LinearLayout like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#drawable/border"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="2"
android:onClick="viewTipsDetail">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:src="#mipmap/img_breakfast" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Đừng bỏ bữa sáng"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:singleLine="false"
android:text="Các nghiên cứu cho thấy bữa sáng là một những điê"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:background="#drawable/border"
android:orientation="horizontal"
android:padding="10dp"
android:weightSum="2"
android:onClick="viewTipsDetail">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.5"
android:src="#mipmap/img_waterglass" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Uống đủ nước"
android:textSize="18sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity=""
android:singleLine="false"
android:text="Bổ sung đủ nước cho cơ thể, ngay cả các bạn làm việc trong môi trường máy lạnh hoặc ngoài trời. Việc mất nước có thể khiến bạn căng thẳng, khó tập trung vào công việc"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
I'm going to plan implement an onClickEvent with same method for both of them. In this occasion, the event will call viewTipsDetail() method. So how can I get the image src, the text in specific LinearLayout with only one method? And is there any possible way to auto-generate a LinearLayout like this with given detail?
This is sample, I'm going to use more LinearLayout than this, so what I need in general is a possible method for getting detail in specific LinearLayout.
<LinearLayout
id="#+id/layout1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
onClick="onLayoutClick"
android:orientation="vertical">
<LinearLayout
id="#+id/layout2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
onClick="onLayoutClick"
android:orientation="vertical">
In code:
#Override
public void onLayoutClick(View view) {
// click click
// view.getTag()
}
Or just use list view for achieve that...
When performing view switching in ViewAnimator, I first thought I can easily achieve animation effect, by using
viewAnimator.setDisplayedChild(1);
or
viewAnimator.showNext();
The view is switched successfully. However, there are no animation being shown.
I expect there will be slide in/ slide out effect as I had setup the animation during initialization.
Animation slideInLeftFast = AnimationUtils.loadAnimation(this.getContext(), R.anim.slide_in_left_fast);
Animation slideOutRightSlow = AnimationUtils.loadAnimation(this.getContext(), R.anim.slide_out_right_slow);
this.viewAnimator.setInAnimation(slideInLeftFast);
this.viewAnimator.setOutAnimation(slideOutRightSlow);
slideInLeftFast.setAnimationListener(new Animation.AnimationListener() {
#Override
public void onAnimationEnd(Animation arg0) {
android.util.Log.i("CHEOK", "animation end");
}
#Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub
android.util.Log.i("CHEOK", "animation repeat");
}
#Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub
android.util.Log.i("CHEOK", "animation start");
}
});
My complete layout file is as following. There are 2 different views in the ViewAnimator
trading_sign_in_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<ViewAnimator xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/view_animator"
android:animateFirstView="false"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:id="#+id/sign_in_relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:layout_above="#+id/sign_in_bottom_nav_bar">
<android.support.design.widget.TextInputLayout
android:id="#+id/username_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:id="#+id/username_edit_text"
android:inputType="textVisiblePassword|textNoSuggestions"
android:imeOptions="actionNext|flagNoExtractUi" />
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
app:passwordToggleEnabled="true"
android:id="#+id/password_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Password"
android:id="#+id/password_edit_text"
android:inputType="textPassword"
android:imeOptions="actionNext|flagNoExtractUi" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<Button
style="?android:attr/buttonBarButtonStyle"
android:id="#+id/forgot_password_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:enabled="true"
android:textAllCaps="false"
android:text="Forgot password"
android:textSize="16sp"
android:layout_above="#+id/sign_in_bottom_nav_bar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="8dp"
android:paddingLeft="32dp"
android:paddingRight="32dp" />
<LinearLayout
android:orientation="horizontal"
android:id="#+id/sign_in_bottom_nav_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
style="?android:attr/buttonBarButtonStyle"
android:background="?attr/selectableItemBackground"
android:id="#+id/sign_in_button"
android:layout_width="0dp"
android:width="0dp"
android:layout_weight="1.0"
android:layout_height="48dp"
android:gravity="center"
android:layout_gravity="center"
android:enabled="true"
android:textAllCaps="true"
android:text="Log in" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/forgot_password_relative_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="0dp"
android:layout_above="#+id/forgot_password_bottom_nav_bar">
<TextView
android:id="#+id/primary_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="16sp" />
<android.support.design.widget.TextInputLayout
android:id="#+id/forgot_password_username_text_input_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<android.support.design.widget.TextInputEditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Username"
android:id="#+id/forgot_password_username_edit_text"
android:inputType="textVisiblePassword|textNoSuggestions"
android:imeOptions="actionNext|flagNoExtractUi" />
</android.support.design.widget.TextInputLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:id="#+id/forgot_password_bottom_nav_bar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
style="?android:attr/buttonBarButtonStyle"
android:background="?attr/selectableItemBackground"
android:id="#+id/forgot_password_forgot_password_button"
android:layout_width="0dp"
android:width="0dp"
android:layout_weight="1.0"
android:layout_height="48dp"
android:gravity="center"
android:layout_gravity="center"
android:enabled="true"
android:textAllCaps="true"
android:text="Forgot password" />
</LinearLayout>
</RelativeLayout>
</ViewAnimator>
May I know what's wrong with my code. Has I missed out something? The complete minimal code can be found at https://github.com/yccheok/MyProject
If you just specify initially displayed child after initialization of viewAnimator, the animation will kick in as expected.
viewAnimator = (ViewAnimator) v.findViewById(R.id.view_animator);
viewAnimator.setDisplayedChild(0);
In my activity I am displaying splash(splash.xml) screen for 5 sec and then I am changing content view to my actual layout of main activity(activity_main). But when I am trying to use the components in the my main activity layout(activity_main) I am getting null pointer exception.
Main Activity is
public class MainActivity extends ActionBarActivity {
ProgressBar progressBar2;
EditText userName,password;
static String url;
JSONObject reader;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
new CountDownTimer(5000,1000){
#Override
public void onTick(long millisUntilFinished){}
#Override
public void onFinish(){
//set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
}
}.start();
progressBar2=(ProgressBar)findViewById(R.id.progressBar2);
progressBar2.setVisibility(View.GONE);
userName=(EditText)findViewById(R.id.userName);
password=(EditText)findViewById(R.id.password);
}
}
when I use any of this components I am getting null pointer exception.
My splash Screen layout file splash.xml
<RelativeLayout
android:layout_height="match_parent"
android:background="#drawable/ic_launcher_web">
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="111dp"
/>
</RelativeLayout>
and the main layout(activity_main.xml is)
<LinearLayout
android:layout_height="match_parent" android:paddingLeft="#dimen/activity_horizontal_margin"
android:orientation="vertical"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".MainActivity"
android:weightSum="1">
<EditText
android:layout_width="match_parent"
android:hint="Username"
android:layout_height="wrap_content"
android:id="#+id/userName"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/password"
android:hint="Password"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Remember me"
android:id="#+id/checkBox"
android:checked="false"
/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:id="#+id/button"
android:onClick="login"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signup"
android:id="#+id/button3"
android:onClick="signUp"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/progressBar2"/>
</LinearLayout>
</LinearLayout>
Because your
#Override
public void onFinish(){
//set the new Content of your activity
MainActivity.this.setContentView(R.layout.activity_main);
}
will be executed after 5 seconds.
Set your layout
setContentView(R.layout.activity_main);
before progressBar2=(ProgressBar)findViewById(R.id.progressBar2);