I have a Linearlayouts, a ScrollView and a button, I am not able to figure out why when I programmatically add a Checkbox to the Linearlayout, at the end of the view, the method from the scrollview
scrollview.scrollto(0, ScrollView.FOCUS_DOWN);
will not focus the last item added.
here the 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"
android:weightSum="100"
tools:context="com.eidotab.myapplication.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="vertical"
android:padding="10dp"
android:layout_weight="30">
<ScrollView
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linear"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/boton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="TEST"/>
</LinearLayout>
and here the java:
public class MainActivity extends AppCompatActivity {
ScrollView scrollView;
LinearLayout linearLayout;
Button button;
int num = -1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
scrollView = (ScrollView) findViewById(R.id.scroll);
linearLayout = (LinearLayout) findViewById(R.id.linear);
button = (Button) findViewById(R.id.boton);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num++;
CheckBox checkBox = new CheckBox(getApplicationContext());
checkBox.setText("TEST " + num);
checkBox.setTextColor(Color.BLACK);
linearLayout.addView(checkBox);
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
}
Im also addind the .rar so you just have to download it and try it.. I have spent several hours trying to make it work.. also tried to get the specific child from the LinearLayout with the
y = Linearlayout.getchildat(N).getBottom();
and works similar to the FOCUS_DOWN... I can never see the very last item.. only the item before the last.
Thanks in advance for the help.
Here is the program
https://www.dropbox.com/home/Public?preview=MyApplication.rar
Well after some research, seems like there is some kind of problem when trying to perform scrolls programmatically and the solution I have found that work best is to add a runable inside the onClick method in my code so I added
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
and the end result was
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
num++;
CheckBox checkBox = new CheckBox(getApplicationContext());
checkBox.setText("TEST " + num);
checkBox.setTextColor(Color.BLACK);
linearLayout.addView(checkBox);
//scrollView.fullScroll(ScrollView.FOCUS_DOWN);
scrollView.post(new Runnable() {
#Override
public void run() {
scrollView.fullScroll(ScrollView.FOCUS_DOWN);
}
});
}
});
it is now working flawlessly but that said, I donĀ“t think it is the ideal solution.
Related
so i was creating a pdf viewer and use a buttons to scroll down or up. so i tried using scrollTo but it's not working but it is working when i make a textview with a long text. is scrollTo only working in a text and not in pdf? since i'm not getting any error i don't know whats wrong the code. can someone suggest how can i make it work?
heres the xml:
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="match_parent"
android:layout_height="140dp"
android:layout_marginVertical="140dp"
android:fillViewport="true"
android:padding="16dp">
<com.github.barteksc.pdfviewer.PDFView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/pdfviewer"
android:background="#color/cardview_dark_background"/>
<TextView
android:id="#+id/lorem_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum"
android:textAppearance="?attr/textAppearanceHeadline6"
android:textColor="#000000" />
</ScrollView>
my activity:
public class navigaze_scroll extends AppCompatActivity {
PDFView testpdf;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigaze_scroll);
final ScrollView scrollView = findViewById(R.id.scroll_view);
Button scrollUp = findViewById(R.id.up_button);
Button scrollDown = findViewById(R.id.down_button);
testpdf = (PDFView) findViewById(R.id.pdfviewer);
testpdf.fromAsset("pdf1.pdf")
.defaultPage(0)
.enableAnnotationRendering(true)
.scrollHandle(new DefaultScrollHandle(this))
.spacing(2)
//.pageFitPolicy(FitPolicy.WIDTH)
.load();
scrollUp.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
scrollView.scrollTo(0, scrollView.getScrollY() -24);
}
});
scrollDown.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
scrollView.scrollTo(0, scrollView.getScrollY() + 24);
}
});
}
}
edit: i forgot to change the scrollView to testpdf
This question may have been asked before but I seem to not make much progress.
Basically I want to add an onclicklistener to my HorizontalScrollView that expands the image when pressed.
Here's the current code in the XML file:
<HorizontalScrollView
android:id="#+id/firstscrollview"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="left"
android:orientation="horizontal"
android:layout_marginTop="50dp">
<LinearLayout
android:id="#+id/firstlinear"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center_horizontal"
android:orientation="horizontal" >
<ImageView
android:id="#+id/Cercie_button"
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:src="#drawable/image"/>
I've been trying out different things but I can't seem to get it to work, if anyone knows or can find a good example it would be very much appreciated!
Try this .
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_second);
ImageView Cercie_button = findViewById(R.id.Cercie_button);
Cercie_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do something here
}
});
}
if i am not getting it wrong then what so difficult in it you have to just do this in your on create method
ImageView btn = findViewById(R.id.Cercie_button);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//write what you want to do
}
});
I have a activity in that I need to change the layout.
In the first layout I have four buttons to display and in the second I need a GridView to display images.
I need to show the second layout in an AsyncTask onPostExecute method.
For now, I'm trying to set two setContentViews, but I get the following exception: ClassCastException
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_focusarea);
videoBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadFiles().execute();
}
});
animateBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadFiles().execute();
}
});
pdfBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadPDFFiles().execute();
}
});
}
And in my postExecute i try like this
protected void onPostExecute(String file_url) {
pDialog.dismiss();
runOnUiThread(new Runnable() {
public void run() {
setContentView(R.layout.gallery);
girGridView=(GridView) findViewById(R.id.gridView1_bir);
girGridView.setAdapter(new ImageAdapter(this));
girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
Toast.makeText(getApplicationContext(), GridViewConfig.getResim_list().get(position), Toast.LENGTH_SHORT).show();
}
});
}
});
Instead of using two layouts use a single layout as bellow
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/MyLayoutOne"
android:layout_width="fill_parent"
android:visibility="gone"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hi This is my first layout" />
<!-- Your first layout contents add here-->
</LinearLayout>
<LinearLayout
android:id="#+id/MyLayoutTwo"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="Hi This is my Second layout" />
<!-- Your second layout contents add here -->
</LinearLayout>
</LinearLayout>
Add your first layout contents inside MyLayoutOne and second layout contents inside MyLayoutTwo
And use following codes inside your activity,
public class MainActivity extends Activity {
LinearLayout MyLayoutOne;
LinearLayout MyLayoutTwo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyLayoutOne=(LinearLayout)findViewById(R.id.MyLayoutOne);
MyLayoutTwo=(LinearLayout)findViewById(R.id.MyLayoutTwo);
// this will make first layout visible
MyLayoutOne.setVisibility(View.VISIBLE);
// this will make second layout hidden from your layout
MyLayoutTwo.setVisibility(View.GONE);
//=========================================
//in your post create add this codes
//=========================================
// this will make first layout hidden
MyLayoutOne.setVisibility(View.GONE);
// this will make second layout visible in your layout
MyLayoutTwo.setVisibility(View.VISIBLE);
//=========================================
}
}
This is a simplest method you must study fragments for better UI management. You can use viewflipper also.
So Study Fragments and Viewflipper..
Instead of that you can have a layout that contains both a wrapper for your four buttons and other for the GridView while this last one is with visibility set to 'gone'.
When the AsycTask finished, you hide the buttons layout and show the GridView layout.
Why don't you set one content view with two layouts or two fragments? A layout can be like this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/text1">
</Button>
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/text2">
</Button>
<Button
android:id="#+id/button3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/text3">
</Button>
<Button
android:id="#+id/button4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="#string/text4">
</Button>
<GridView
android:layout_width="match_parent"
android:layout_height="wrap_content"
...
</GridView>
</LinearLayout>
Use one setContentView() and define separates Linear/Relative layout one for buttons and second for gridView.And hide/show the Views according to your need.
onPostExecute of AsyncTask runs on UI thread so you need not specify runonUiThread explicitly.Instead of using 2 setcontent View it is better to have 2 views in your layout file and make it visible invisible as required.
Friend change the id of wrapper1 to child as below,
<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"
tools:context=".Focusarea" >
<LinearLayout
android:id="#+id/wrapper1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:visibility="gone" >
<GridView
android:id="#+id/gridView1_bir"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center" >
</GridView>
</LinearLayout>
<RelativeLayout
android:id="#+id/wrapper2"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/vid_btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
and initialize your linear layouts outside oncreate as below,
LinearLayout wrapper1;
RelativeLayout wrapper2;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
wrapper1 = (LinearLayout)findViewById(R.id.wrapper1);
wrapper2=(RelativeLayout)findViewById(R.id.wrapper2);
// this will make first layout visible
wrapper2.setVisibility(View.VISIBLE);
// this will make second layout hidden from your layout
wrapper1.setVisibility(View.GONE);
ImageView videoBtn = (ImageView) findViewById(R.id.vid_btn);
ImageView animateBtn = (ImageView) findViewById(R.id.anit_btn);
ImageView pdfBtn = (ImageView) findViewById(R.id.pdf_btn);
videoBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadFiles().execute();
}
});
animateBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadFiles().execute();
}
});
pdfBtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
new LoadPDFFiles().execute();
}
});
}
protected void onPostExecute(String file_url) {
// dismiss the dialog after getting VIDEOS
pDialog.dismiss();
vid=new ArrayList<String>(new ArrayList<String>(vid));
videoUrl=parsing.parse(videoUrl);
System.out.println("VIDEO URL" +videoUrl);
runOnUiThread(new Runnable() {
public void run() {
//--here you wont need to initialize again--
// this will make first layout visible
wrapper1.setVisibility(View.VISIBLE);
// this will make second layout hidden from your layout
wrapper2.setVisibility(View.GONE);
girGridView=(GridView) findViewById(R.id.gridView1_bir);
//ListView gibi buna da adapter set ediliyor.
girGridView.setAdapter(new ImageAdapter(this));
girGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View view, int position,long arg3) {
Toast.makeText(getApplicationContext(), GridViewConfig.getResim_list().get(position), Toast.LENGTH_SHORT).show();
}
});
}
});
I need to disable the click-event for a button in Android. Just as a sample, I have tried doing the following. I have taken a TextView named it (entered a text) as Name. The condition checks if, TextView is empty button and clickable should be set to false. However this does not happen when the Toast is printed. Can somemone tell me the reason. Also if the text field is not empty I want to reset the clickable event for button as true.
Java File:
public class ButtonclickabkeActivity extends Activity {
TextView tv;
Button btn;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tv = (TextView) findViewById(R.id.textView1);
tv.setText("Name");
btn = (Button) findViewById(R.id.button1);
if (tv.getText().toString().length() != 0) {
btn.setClickable(false);
Toast.makeText(getApplicationContext(), "" + tv.getText().toString().length(), Toast.LENGTH_LONG).show();
} else {
btn.setClickable(true);
}
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(), "Button clicked", Toast.LENGTH_LONG).show();
}
});
}
}
XML file:
<?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">
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/hello"/>
<TextView
android:text="TextView"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
Use
btn.setEnable(false);
instead of
btn.setClickable(false);
User 370305 is correct. .setEnable is what your looking for. Or you could use android:clickable in the layout XML file.
In my case
getActivity().runOnUiThread(new Runnable() {
#Override
public void run() {
view.setEnabled(false);
}
});
I am new to android and just trying to modify a basic example.
main.xml is like
<?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:weightSum="1">
<Button android:layout_width="228dp" android:layout_height="wrap_content" android:id="#+id/addButton" android:text="#string/addBtn"></Button>
</LinearLayout>
And want to add TextField on Click of the button. How to add Child to view?
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn=(Button)findViewById(R.id.addButton);
btn.setOnClickListener(this);
}
#Override
public void onClick(View v) {
EditText editText = new EditText(v.getContext());
//Append the editText to View
}
How to append it to current view/layout?
Something like this would help you i think.
ParentView.post(new Runnable() {
public void run() {
ParentView.addView(newView,0);
LinearLayout.LayoutParams rlparams = (LinearLayout.LayoutParams)rldeal.getLayoutParams();
rlparams.width = LinearLayout.LayoutParams.WRAP_CONTENT;
rlparams.height = LinearLayout.LayoutParams.WRAP_CONTENT;
newView.setLayoutParams(rlparams);
}
});
Try to use include and merge:
http://mfarhan133.wordpress.com/2010/10/01/reusing-layout-include-merge-tag-for-androd/