SeekBar disappear when change fragment? - android

I have 3 fragment and 2 framelayout in 1 XML file in here , the first 2 fragment is a text and it display normally , but when i replaced the third fragment which have seekbar then it went disappear !
But if i display the third fragment first then the seekbar display normal and the TextView went gone ! This is my code :
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button)findViewById(R.id.btn1);
btn2=(Button)findViewById(R.id.btn2);
btn3=(Button)findViewById(R.id.btn3);
fragment1= new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
replaceFragment(fragment1,R.id.fragment1,"FRG1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
replaceFragment(fragment2,R.id.fragment2,"FRG2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
replaceFragment(fragment3,R.id.fragment2,"FRG3");
}
});
}
public void replaceFragment(Fragment fragment , int containerResId, String tag){
android.support.v4.app.FragmentTransaction trans = getSupportFragmentManager()
.beginTransaction();
trans.replace(containerResId , fragment , tag);
trans.commit();
getSupportFragmentManager().executePendingTransactions();
}
fragment 1 :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment1,container,false);
return view;
}
Fragment 2 and 3 is same as fragment1 .
This is main.xml :
<LinearLayout 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:orientation="vertical"
tools:context="com.example.macbook.fragment.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment1"/>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment2"/>
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment3"/>
</LinearLayout>
<FrameLayout
android:id="#+id/fragment1"
android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</FrameLayout>
<FrameLayout
android:id="#+id/fragment2"
android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="wrap_content"></FrameLayout>
</LinearLayout>
Fragment3.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">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:visibility="visible" />
</RelativeLayout>
and this happen when i display fragment 3 which have seekbar
if i display fragment 3 first then any TextView can't display

1) Just update main.xml for weight sum
<LinearLayout 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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment1"/>
<Button
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment2"/>
<Button
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="fragment3"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="10"
android:orientation="vertical">
<FrameLayout
android:id="#+id/fragment1"
android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
<FrameLayout
android:id="#+id/fragment2"
android:layout_weight="5"
android:layout_width="match_parent"
android:layout_height="0dp">
</FrameLayout>
</LinearLayout>
</LinearLayout>
2) MainActivity.class
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Fragment1 fragment1;
Fragment2 fragment2;
Fragment3 fragment3;
Button btn1, btn2, btn3;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn1 = (Button) findViewById(R.id.btn1);
btn2 = (Button) findViewById(R.id.btn2);
btn3 = (Button) findViewById(R.id.btn3);
fragment1 = new Fragment1();
fragment2 = new Fragment2();
fragment3 = new Fragment3();
btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
replaceFragment(fragment1, R.id.fragment1, "FRG1");
}
});
btn2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
replaceFragment(fragment2, R.id.fragment2, "FRG2");
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
replaceFragment(fragment3, R.id.fragment2, "FRG3");
}
});
}
public void replaceFragment(Fragment fragment, int containerResId, String tag) {
android.support.v4.app.FragmentTransaction trans = getSupportFragmentManager()
.beginTransaction();
trans.replace(containerResId, fragment, tag);
trans.commit();
getSupportFragmentManager().executePendingTransactions();
}
}
3) Fragment1 same for fragment 2 and 3
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Fragment1 extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.fragment1,container,false);
return view;
}
4) Fragment1.xml same for fragment2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hi"
android:textSize="20dp"
android:layout_margin="20dp"/>
</LinearLayout>
5) fragment3.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">
<SeekBar
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:visibility="visible" />
</RelativeLayout>

Related

Fragment won't show up with onButtonClick

The fragment shows up by default but I wanted it to be hidden/invisible until I click this button. The problem that I am experiencing is that when I click this button, the fragment won't show up. I'm a complete beginner when it comes to fragments.
My codes in my Main Activity:
package com.example.julian.tutorial_fragment;
import android.support.v4.app.FragmentManager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements TopSectionFragment.TopSectionListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View frag = findViewById(R.id.fragment3);
frag.setVisibility(View.GONE);
}
//This gets called by the TopFragment when the user clicks the button
#Override
public void createMeme(String top, String bottom) {
BottomPictureFragment bottomFragment = (BottomPictureFragment)getSupportFragmentManager().findFragmentById(R.id.fragment2);
bottomFragment.setMemeText(top, bottom);
}
//this is the onButtonClick
public void sample(View view) {
android.support.v4.app.FragmentManager fragmentManager = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
Fragment_Search hello = new Fragment_Search();
fragmentTransaction.add(R.id.fragment3, hello, "HELLO");
fragmentTransaction.commit();
}
}
My codes in Fragment_Search:
package com.example.julian.tutorial_fragment;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Julian on 8/17/2017.
*/
public class Fragment_Search extends Fragment {
public interface Fragment_SearchInterface {
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.sample_layout, container, false);
return view;
}
}
My activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.julian.tutorial_fragment.MainActivity">
<RelativeLayout
android:layout_width="377dp"
android:layout_height="498dp"
tools:layout_editor_absoluteY="8dp"
tools:layout_editor_absoluteX="8dp">
<fragment
android:id="#+id/fragment2"
android:name="com.example.julian.tutorial_fragment.BottomPictureFragment"
android:layout_width="250dp"
android:layout_height="250dp"
tools:layout="#layout/bottom_picture_fragment"
tools:layout_editor_absoluteX="67dp"
tools:layout_editor_absoluteY="223dp"
android:layout_marginBottom="30dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
<fragment
android:id="#+id/fragment"
android:name="com.example.julian.tutorial_fragment.TopSectionFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:layout="#layout/top_section_fragment"
tools:layout_editor_absoluteX="42dp"
tools:layout_editor_absoluteY="16dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/fragment2"
android:layout_alignStart="#+id/fragment"
android:text="Button"
android:onClick="sample"/>
<fragment
android:id="#+id/fragment3"
android:name="com.example.julian.tutorial_fragment.Fragment_Search"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_below="#+id/button2"
android:layout_marginTop="92dp"
android:layout_toStartOf="#+id/button2" />
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
change your sample function and add this
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.your_layout, new Fragment_Search())
.addToBackStack(null)
.commit();

I want to hide Action Bar while scrolling the webview

Basically i have BottomNavigationView activity in that there are 3 fragments (tab1,tab2,tab3) which contain 3 webviews, what i actually wanna to do is while scrolling down the webview my actionbar should hide, likegetSupportActionBar().hide(); i know already there are answers in the stack overflow for the same question, but i don't know what to do in my case, because i am a beginner in android development
here is my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hackerinside.jaisonjoseph.polysocial.MainActivity">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/holo_blue_dark">
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
/>
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:layout_alignParentBottom="true"
app:menu="#menu/navigation" />
This is my first fragment (fragment_tab1)
<LinearLayout 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:orientation="vertical"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.hackerinside.jaisonjoseph.polysocial.tab1">
<FrameLayout
android:id="#+id/frame1"
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#android:color/transparent">
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleHorizontal"
android:layout_width="fill_parent"
android:layout_height="3dp"
android:background="#android:color/transparent"
android:foregroundGravity="top"
android:progressDrawable="#drawable/custom_progress"
android:progress="20"/>
</FrameLayout>
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</WebView>
This is my java code for tab1
package com.hackerinside.jaisonjoseph.polysocial;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.FrameLayout;
import android.widget.ProgressBar;
/**
* A simple {#link Fragment} subclass.
*/
public class tab1 extends Fragment {
public ProgressBar bar;
public FrameLayout frameLayout;
public tab1() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tab1, null);
frameLayout=(FrameLayout) rootView.findViewById(R.id.frame1);
bar=(ProgressBar)rootView.findViewById(R.id.progressBar1);
bar.setMax(100);
final WebView view=(WebView) rootView.findViewById(R.id.webview);
view.loadUrl("http://facebook.com");
view.getSettings().setJavaScriptEnabled(true);
view.setWebViewClient(new MyWebViewClient());
view.setWebChromeClient(new WebChromeClient(){
public void onProgressChanged(WebView view1,int progress){
frameLayout.setVisibility(View.VISIBLE);
bar.setProgress(progress);
if (progress==100){
frameLayout.setVisibility(View.GONE);
}
super.onProgressChanged(view1,progress);
}
});
view.getSettings().setBuiltInZoomControls(true);
view.getSettings().setDisplayZoomControls(false);
bar.setProgress(0);
return rootView;
}
}
Here is a sample idea of implementation.
boolean isShowing;
//oncraete code is here
WebView mScrollView = (WebView) findViewById(R.id.scrollView);
mScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
float mfloat = mScrollView.getScrollY();
if (mfloat >= tToolbar.getHeight() && isShowing) {
toolbarAnimateHide();
} else if (mfloat == 0 && !isShowing) {
toolbarAnimateShow(0);
}
}
});
private void toolbarAnimateShow(final int verticalOffset) {
isShowing = true;
//tToolbar is your view(any view)
// add getSupportActionBar().hide() this code here to hide actionbar
// remove below code or comment
tToolbar.animate()
.translationY(0)
.setInterpolator(new LinearInterpolator())
.setDuration(180)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationStart(Animator animation) {
toolbarSetElevation(verticalOffset == 0 ? 0 : TOOLBAR_ELEVATION);
}
});
}
private void toolbarAnimateHide() {
isShowing = false;
// add getSupportActionBar().show() this code here to showactionbar
// remove below code or comment
//tToolbar is your view(any view)
tToolbar.animate()
.translationY(-tToolbar.getHeight())
.setInterpolator(new LinearInterpolator())
.setDuration(180)
.setListener(new AnimatorListenerAdapter() {
#Override
public void onAnimationEnd(Animator animation) {
toolbarSetElevation(0);
}
});
}

Using getFragmentManager on a ListActivity

I just want to know if this is possible since first. I have created a custom listView based on the tutorial I read from Sai Geetha. Well it works perfectly on my app except that it needs to extend ListActivity instead of FragmentActivity. Now I'm having a hard time configuring and adding a dialog for this since I need to apply a fragment dialog and I can't use the getFragmentManager() since I'm not working with the FragmentActivity. Is there's another way I can do to work on this without sacrificing the ListActivity? Thanks!
Here's my code so far
XML:
conversation_list_view
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#id/android:list"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginBottom="5dp"
android:layout_marginTop="20dp"/>
</LinearLayout>
group_screen
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="48dp"
android:background="#drawable/action_bar_separator"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Group Name"
android:id="#+id/txt_group_name"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:textColor="#color/dark_gray"
android:shadowColor="#color/dark_shadow"
android:shadowRadius="1"
android:shadowDy="1"/>
<Button
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/btn_back"
android:background="#drawable/btn_navigate_back"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"/>
<Button
android:layout_width="32dp"
android:layout_height="32dp"
android:id="#+id/btn_information"
android:background="#drawable/btn_information"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"/>
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Conversations"
android:id="#+id/textView2"
android:textColor="#color/holo_light_blue"
android:layout_marginLeft="15dp"
android:layout_marginTop="10dp"/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="340dp"
>
<fragment
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:name="com.mark.exercise.ListViewFragment"
android:id="#+id/fragment"/>
</LinearLayout>
<Button
android:layout_width="match_parent"
android:layout_height="42dp"
android:text="Ask something"
android:id="#+id/btn_ask_question"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:textSize="15dp"/>
</LinearLayout>
</LinearLayout>
Java
package com.mark.exercise;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
/**
* Created by pc on 9/24/13.
*/
public class GroupActivity extends FragmentActivity {
Button information, back, new_topic;
ListView conversations;
TextView group_name;
String name, group_description, group_administrator,image_id;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.group_screen);
Intent intent = getIntent();
name = intent.getStringExtra("group_name");
group_description = intent.getStringExtra("group_description");
group_administrator = intent.getStringExtra("group_administrator");
image_id = intent.getStringExtra("image_id");
information = (Button)findViewById(R.id.btn_information);
back = (Button)findViewById(R.id.btn_back);
new_topic = (Button)findViewById(R.id.btn_ask_question);
group_name = (TextView)findViewById(R.id.txt_group_name);
group_name.setText(name);
information.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(GroupActivity.this, GroupInformationActivity.class);
intent.putExtra("group_name",name);
intent.putExtra("group_description",group_description);
intent.putExtra("group_administrator",group_administrator);
intent.putExtra("image_id",image_id);
startActivity(intent);
GroupActivity.this.overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
}
});
new_topic.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
showCreateNewTopicDialog();
}
});
back.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
onBackPressed();
}
});
}
private void showCreateNewTopicDialog() {
FragmentManager fm = getSupportFragmentManager();
DialogFragmentCreateGroup createGroup = new DialogFragmentCreateGroup();
createGroup.show(fm, "create_group");
}
#Override
public void onBackPressed(){
super.onBackPressed();
overridePendingTransition(R.anim.in_from_right,R.anim.out_to_left);
}
}
List Fragment
package com.mark.exercise;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Random;
/**
* Created by pc on 9/27/13.
*/
public class ListViewFragment extends ListFragment {
final ArrayList<HashMap<String,String>> list = new ArrayList<HashMap<String,String>>();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.conversations_list_view,
container, false);
setListView set_list = new setListView();
set_list.start();
return view;
}
public void onListItemClick(ListView l, View v, int position, long id) {
//super.onListItemClick(l, v, position, id);
Intent intent = new Intent(getActivity(), ConversationActivity.class);
startActivity(intent);
getActivity().overridePendingTransition(R.anim.in_from_left, R.anim.out_to_right);
}
private class setListView extends Thread {
public void run() {
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
setConversations();
}
});
}
}
private void setConversations(){
list.clear();
SimpleAdapter adapter = new SimpleAdapter(
getActivity(),
list,
R.layout.custom_list_main_conversations,
new String[] {"message","date", "reply_count", "stars_count"},
new int[] {R.id.txt_conversation_message,R.id.txt_topic_date, R.id.txt_no_of_reply, R.id.txt_no_of_stars}
);
for(int ctr=0;ctr<=5;ctr++){
Random randomGenerator = new Random();
HashMap<String,String> item_list = new HashMap<String,String>();
item_list.put("message", "This is the conversation number "+(ctr+1)+" and this topic is just a dummy data.");
item_list.put("date", "0"+(ctr+1)+"/0"+(ctr+2)+"/2013 "+(ctr+1)+":00:am");
item_list.put("reply_count", String.valueOf(ctr+randomGenerator.nextInt(10)));
item_list.put("stars_count", String.valueOf(ctr+randomGenerator.nextInt(10)));
list.add(item_list);
}
android.app.ListFragment lf = new android.app.ListFragment();
lf.setListAdapter(adapter);
}
}
You can use ListFragment inside FragmentActivity instead of using a ListActivity.

2 button OnClick event on Popup Window in Android

I have 2 buttons in my xml file, onclicklistener open new popup.why this eror? can help me??
My Java code is as follows:
Public class Bab1_b1a extends Activity {
final Context context = this;
private Button hans, logemann;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_bab1_b1a);
hans = (Button) findViewById(R.id.hans);
logemann = (Button) findViewById(R.id.logemann);
hans.setOnClickListener(myhandler1);
logemann.setOnClickListener(myhandler2);
}
View.OnClickListener myhandler1 = new View.OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.activity_popup_hans);
dialog.setTitle("Hans Kelsen");
TextView text1 = (TextView) dialog.findViewById(R.id.textView1);
text1.setText("Negara ialah");
ImageView image = (ImageView) dialog.findViewById(R.id.imageView1);
image.setImageResource(R.drawable.hans_kelsen);
Button dialogButton = (Button) dialog.findViewById(R.id.back_hans);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
};
View.OnClickListener myhandler2 = new View.OnClickListener() {
public void onClick(View v) {
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.popup_logemann);
dialog.setTitle("Logemann");`enter code here`
TextView text1 = (TextView) dialog.findViewById(R.id.textView1);
text1.setText("Negara ialah.");
ImageView image = (ImageView) dialog.findViewById(R.id.imageView1);
image.setImageResource(R.drawable.logemann);
Button dialogButton = (Button) dialog.findViewById(R.id.back_logemann);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
};
};
xml :
<LinearLayout 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/back"
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=".Bab1_b1a" >
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/nav_back"
android:contentDescription="#string/skx" />
<LinearLayout
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:layout_weight="1"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="#string/h_neg"
android:textSize="13sp" />
<Button
android:id="#+id/hans"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hans_kelsen"
android:textSize="15sp" />
<Button
android:id="#+id/logemann"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/logemann" />
</LinearLayout>
<ImageButton
android:id="#+id/imageButton2"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/nav_forw"
android:contentDescription="#string/skx" />
</LinearLayout>
Can anyone help me know the reason for this problem and how do I solve it. I am a newbie to Android.
Thanks
try this
import android.os.Bundle;
import android.app.Activity;
import android.app.Dialog;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button bt_1 = (Button) findViewById(R.id.bt1);
bt_1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.activity_popup);
dialog.setTitle("Hans Kelsen");
//Her add your textView and ImageView if you want
Button dialogButton = (Button) dialog.findViewById(R.id.bt1_popUP);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
// Same thing for bt_2
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Your Main Activity 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: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" >
<Button
android:id="#+id/bt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BT 1" />
<Button
android:id="#+id/bt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/bt1"
android:layout_marginTop="40dp"
android:text="BT 2" />
</RelativeLayout>
Your POP-UP 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="200dp"
android:background="#FFF123"
android:gravity="center" >
<Button
android:id="#+id/bt1_popUP"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Close" />
</RelativeLayout>
enjoy :)

I get a forcestop occasionally when I press buttons other than the first

I have 3 buttons in total. And it seemed that, if I don't press the first button, and instead second or third button right after I startup, my application will forcestop.
My code for part1 : http://pastebin.com/udkEdF3E
package project.ernest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Part1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button a = (Button) findViewById(R.id.Disclaimer);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent b = new Intent(getBaseContext(), Part2.class);
Part1.this.startActivity(b);
final Button c = (Button) findViewById(R.id.Orientation);
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent d = new Intent(getBaseContext(), Part3.class);
Part1.this.startActivity(d);
final Button e = (Button) findViewById(R.id.Course);
e.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent f = new Intent(getBaseContext(), Part4.class);
Part1.this.startActivity(f);
}
});
};
});
};
});
};
}
my code for main.xml : http://pastebin.com/SxfxfJ4a
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/myColor" android:weightSum="1">
<LinearLayout android:orientation="vertical" android:id="#+id/linearLayout1" android:weightSum="1" android:layout_width="wrap_content" android:layout_height="404dp">
<TextView android:text="#string/HospName" android:id="#+id/textView1" android:textSize="20mm" android:layout_width="324dp" android:layout_height="142dp"></TextView>
<Button android:text="#string/Disclaimer" android:layout_weight="0.37" android:textSize="5mm" android:layout_height="75dp" android:layout_width="fill_parent" android:id="#+id/Disclaimer" android:drawingCacheQuality="high" android:onClick="#string/DisclaimerHandler"></Button>
<Button android:text="#string/Orientation" android:textSize="5mm" android:layout_height="75dp" android:layout_weight="0.37" android:layout_width="fill_parent" android:id="#+id/Orientation" android:onClick="#string/OrientationHandler"></Button>
<Button android:text="#string/Course" android:textSize="5mm" android:layout_height="75dp" android:layout_weight="0.37" android:layout_width="fill_parent" android:id="#+id/Course" android:onClick="#string/CourseHandler"></Button>
</LinearLayout>
<LinearLayout android:layout_width="match_parent" android:orientation="horizontal" android:layout_weight="0.19" android:layout_height="wrap_content" android:id="#+id/linearLayout2" android:weightSum="1">
<Button android:layout_weight="0.23" android:text="#string/Search" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/SearchHandler" android:id="#+id/Search"></Button>
<Button android:layout_weight="0.23" android:text="#string/Contact" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/ContactHandler" android:id="#+id/Contact"></Button>
<Button android:onClick="#string/LinkHandler" android:layout_height="75dp" android:layout_width="75dp" android:layout_weight="0.23" android:textSize="1.9mm" android:text="#string/Link" android:id="#+id/Link"></Button>
<Button android:layout_weight="0.23" android:text="#string/Copyright" android:textSize="1.9mm" android:layout_height="75dp" android:layout_width="75dp" android:onClick="#string/CopyrightHandler" android:id="#+id/Copyright"></Button>
</LinearLayout>
</LinearLayout>
The error I get is either:
java.lang.IllegalStateException: Could not find a method CourseHandler(View) in activity class project.ernest.Part1 or...
java.lang.IllegalStateException: Could not find a method OrientationHandler(View) in activity class project.ernest.Part1 or...
Please help me out!
You've placed initialization of buttons 2 and 3 inside the first button's onClickListener(), means they won't be initialized before you click the first button. Fix your code.
this is the format.
public class Part1 extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
final Button a = (Button) findViewById(R.id.Disclaimer);
a.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent b = new Intent(getBaseContext(), Part2.class);
Part1.this.startActivity(b);
}
});
final Button c = (Button) findViewById(R.id.Orientation);
c.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent d = new Intent(getBaseContext(), Part3.class);
Part1.this.startActivity(d);
}
});
final Button e = (Button) findViewById(R.id.Course);
e.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent f = new Intent(getBaseContext(), Part4.class);
Part1.this.startActivity(f);
}
});
}
}

Categories

Resources